Author: sandervanderburg
Date: Wed Jul 13 20:58:48 2011
New Revision: 27771
URL: https://svn.nixos.org/websvn/nix/?rev=27771&sc=1

Log:
Added MySQL replication support + 2 MySQL testcases (including replication)

Added:
   nixos/trunk/tests/mysql-replication.nix
   nixos/trunk/tests/mysql.nix
   nixos/trunk/tests/testdb.sql
Modified:
   nixos/trunk/modules/services/databases/mysql.nix
   nixos/trunk/tests/default.nix

Modified: nixos/trunk/modules/services/databases/mysql.nix
==============================================================================
--- nixos/trunk/modules/services/databases/mysql.nix    Wed Jul 13 18:54:27 
2011        (r27770)
+++ nixos/trunk/modules/services/databases/mysql.nix    Wed Jul 13 20:58:48 
2011        (r27771)
@@ -14,6 +14,20 @@
     "--user=${cfg.user} --datadir=${cfg.dataDir} " +
     "--log-error=${cfg.logError} --pid-file=${pidFile}";
 
+  myCnf = pkgs.writeText "my.cnf"
+  ''
+    [mysqld]
+    ${optionalString (cfg.replication.role == "master" || cfg.replication.role 
== "slave") "log-bin=mysql-bin"}
+    ${optionalString (cfg.replication.role == "master" || cfg.replication.role 
== "slave") "server-id = ${toString cfg.replication.serverId}"}
+    ${optionalString (cfg.replication.role == "slave")
+    ''
+      master-host = ${cfg.replication.masterHost}
+      master-user = ${cfg.replication.masterUser}
+      master-password = ${cfg.replication.masterPassword}
+      master-port = ${toString cfg.replication.masterPort}
+    ''}
+  '';
+
 in
 
 {
@@ -81,6 +95,35 @@
         default = null;
        description = "Path to a file containing the root password, modified on 
the first startup. Not specifying a root password will leave the root password 
empty."; 
       };
+      
+      replication = {
+        role = mkOption {
+         default = "none";
+         description = "Role of the MySQL server instance. Can be either: 
master, slave or none";
+       };
+       
+        serverId = mkOption {
+         default = 1;
+         description = "Id of the MySQL server instance. This number must be 
unique for each instance";
+       };
+       
+       masterHost = mkOption {
+         description = "Hostname of the MySQL master server";
+       };
+       
+       masterUser = mkOption {
+         description = "Username of the MySQL replication user";
+       };
+       
+       masterPassword = mkOption {
+         description = "Password of the MySQL replication user";
+       };
+       
+       masterPort = mkOption {
+         default = 3306;
+         description = "Port number on which the MySQL master server runs";
+       };
+      };
     };
     
   };
@@ -115,7 +158,7 @@
             chown -R ${cfg.user} ${cfg.pidDir}
           '';
 
-        exec = "${mysql}/libexec/mysqld ${mysqldOptions}";
+        exec = "${mysql}/libexec/mysqld --defaults-extra-file=${myCnf} 
${mysqldOptions}";
        
        postStart =
          ''

Modified: nixos/trunk/tests/default.nix
==============================================================================
--- nixos/trunk/tests/default.nix       Wed Jul 13 18:54:27 2011        (r27770)
+++ nixos/trunk/tests/default.nix       Wed Jul 13 20:58:48 2011        (r27771)
@@ -14,6 +14,8 @@
   kde4 = makeTest (import ./kde4.nix);
   login = makeTest (import ./login.nix);
   mpich = makeTest (import ./mpich.nix);
+  mysql = makeTest (import ./mysql.nix);
+  mysql_replication = makeTest (import ./mysql-replication.nix);
   nat = makeTest (import ./nat.nix);
   nfs = makeTest (import ./nfs.nix);
   openssh = makeTest (import ./openssh.nix);

Added: nixos/trunk/tests/mysql-replication.nix
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ nixos/trunk/tests/mysql-replication.nix     Wed Jul 13 20:58:48 2011        
(r27771)
@@ -0,0 +1,57 @@
+{ pkgs, ... }:
+
+let
+  replicateUser = "replicate";
+  replicatePassword = "secret";
+in
+{
+  nodes = {
+    master = 
+      { pkgs, config, ... }:
+      
+      {
+        services.mysql.enable = true;
+       services.mysql.replication.role = "master";
+       services.mysql.initialDatabases = [ { name = "testdb"; schema = 
./testdb.sql; } ];
+       services.mysql.initialScript = pkgs.writeText "initmysql"
+        ''
+         create user '${replicateUser}'@'%' identified by 
'${replicatePassword}';
+          grant replication slave on *.* to '${replicateUser}'@'%';
+        '';
+      };
+    
+    slave1 =
+      { pkgs, config, nodes, ... }:
+      
+      {
+        services.mysql.enable = true;
+       services.mysql.replication.role = "slave";
+       services.mysql.replication.serverId = 2;
+       services.mysql.replication.masterHost = 
"${nodes.master.config.networking.hostName}";
+       services.mysql.replication.masterUser = replicateUser;
+       services.mysql.replication.masterPassword = replicatePassword;  
+      };
+      
+    slave2 =
+      { pkgs, config, nodes, ... }:
+      
+      {
+        services.mysql.enable = true;
+       services.mysql.replication.role = "slave";
+       services.mysql.replication.serverId = 3;
+       services.mysql.replication.masterHost = 
"${nodes.master.config.networking.hostName}";
+       services.mysql.replication.masterUser = replicateUser;
+       services.mysql.replication.masterPassword = replicatePassword;
+      };
+  };
+  
+  testScript = ''
+    startAll;
+    
+    $master->waitForJob("mysql");
+    $master->waitForJob("mysql");
+    $slave2->waitForJob("mysql");
+    $slave2->sleep(100); # Hopefully this is long enough!!
+    $slave2->mustSucceed("echo 'use testdb; select * from tests' | mysql -u 
root -N | grep 4");
+  '';
+}

Added: nixos/trunk/tests/mysql.nix
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ nixos/trunk/tests/mysql.nix Wed Jul 13 20:58:48 2011        (r27771)
@@ -0,0 +1,22 @@
+{ pkgs, ... }:
+
+{
+  nodes = {
+    master = 
+      { pkgs, config, ... }:
+      
+      {
+        services.mysql.enable = true;
+       services.mysql.replication.role = "master";
+       services.mysql.initialDatabases = [ { name = "testdb"; schema = 
./testdb.sql; } ];
+      };    
+  };
+  
+  testScript = ''
+    startAll;
+    
+    $master->waitForJob("mysql");
+    $master->sleep(10); # Hopefully this is long enough!!
+    $master->mustSucceed("echo 'use testdb; select * from tests' | mysql -u 
root -N | grep 4");
+  '';
+}

Added: nixos/trunk/tests/testdb.sql
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ nixos/trunk/tests/testdb.sql        Wed Jul 13 20:58:48 2011        (r27771)
@@ -0,0 +1,10 @@
+create table tests
+( Id   INTEGER      NOT NULL,
+  Name VARCHAR(255) NOT NULL,
+  primary key(Id)
+);
+
+insert into tests values (1, 'a');
+insert into tests values (2, 'b');
+insert into tests values (3, 'c');
+insert into tests values (4, 'd');
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to