Author: eelco
Date: Fri Apr 29 14:29:25 2011
New Revision: 27050
URL: https://svn.nixos.org/websvn/nix/?rev=27050&sc=1

Log:
* Start using the deployment.* attributes from network specifications.

Modified:
   cloud/trunk/examples/apache.nix
   cloud/trunk/src/eval-machine-info.nix
   cloud/trunk/src/nixos-deploy-network.pl

Modified: cloud/trunk/examples/apache.nix
==============================================================================
--- cloud/trunk/examples/apache.nix     Fri Apr 29 13:24:24 2011        (r27049)
+++ cloud/trunk/examples/apache.nix     Fri Apr 29 14:29:25 2011        (r27050)
@@ -7,6 +7,9 @@
       services.httpd.enable = true;
       services.httpd.adminAddr = "[email protected]";
       services.httpd.documentRoot = "${pkgs.valgrind}/share/doc/valgrind/html";
+
+      deployment.targetEnv = "adhoc";
+      deployment.adhoc.controller = "[email protected]";
     };
 
 in
@@ -45,6 +48,9 @@
           # For testing; don't want to wait forever for dead backend servers.
           ProxyTimeout      5
         '';
+        
+      deployment.targetEnv = "adhoc";
+      deployment.adhoc.controller = "[email protected]";
     };
 
   backend1 = backend;

Modified: cloud/trunk/src/eval-machine-info.nix
==============================================================================
--- cloud/trunk/src/eval-machine-info.nix       Fri Apr 29 13:24:24 2011        
(r27049)
+++ cloud/trunk/src/eval-machine-info.nix       Fri Apr 29 14:29:25 2011        
(r27050)
@@ -29,9 +29,10 @@
               # Provide a default hostname and deployment target equal
               # to the attribute name of the machine in the model.
               { key = "set-default-hostname";
-                networking.hostName = pkgs.lib.mkOverride 900 
configurationName;
-                deployment.targetHost = pkgs.lib.mkOverride 900 
configurationName;
-                networking.firewall.enable = pkgs.lib.mkOverride 900 false; # 
hack, think about this later
+                networking.hostName = mkOverride 900 configurationName;
+                deployment.targetHost = mkOverride 900 configurationName;
+                networking.firewall.enable = mkOverride 900 false; # hack, 
think about this later
+                environment.checkConfigurationOptions = false; # should only 
do this in phase 1
               }
             ];
           extraArgs = { inherit nodes; };
@@ -39,12 +40,19 @@
       }
     ) (attrNames network));
 
-  machineInfo = builtins.attrNames nodes;
-  
+  # Phase 1: evaluate only the deployment attributes.
+  machineInfo =
+    flip mapAttrs nodes (n: v:
+      { inherit (v.config.deployment) targetEnv targetHost;
+        adhoc = optionalAttrs (v.config.deployment.targetEnv == "adhoc") 
v.config.deployment.adhoc;
+      }
+    );
+
+  # Phase 2: build complete machine configurations.  
   machines = runCommand "vms" {}
     ''
       mkdir -p $out
-      ${toString (lib.attrValues (lib.mapAttrs (n: v: ''
+      ${toString (attrValues (mapAttrs (n: v: ''
         ln -s ${v.config.system.build.toplevel} $out/${n}
       '') nodes))}
     '';

Modified: cloud/trunk/src/nixos-deploy-network.pl
==============================================================================
--- cloud/trunk/src/nixos-deploy-network.pl     Fri Apr 29 13:24:24 2011        
(r27049)
+++ cloud/trunk/src/nixos-deploy-network.pl     Fri Apr 29 14:29:25 2011        
(r27050)
@@ -1,7 +1,10 @@
 #! /var/run/current-system/sw/bin/perl -w
 
+use utf8;
 use XML::LibXML;
 
+binmode(STDERR, ":utf8");
+
 my $networkExpr;
 my @machines = ();
 my $outPath;
@@ -48,10 +51,27 @@
     #print $machineInfoXML, "\n";
 
     my $dom = XML::LibXML->load_xml(string => $machineInfoXML);
-    foreach my $m ($dom->findnodes('/expr/list/string')) {
-        my $name = $m->findvalue('./@value');
+    foreach my $m ($dom->findnodes('/expr/attrs/attr')) {
+        my $name = $m->findvalue('./@name') || die;
         #print STDERR "got machine ‘$name’\n";
-        push @machines, { name => $name };
+        my $targetEnv = $m->findvalue('./attrs/attr[@name = 
"targetEnv"]/string/@value') || die;
+        my $info =
+            { name => $name
+            , targetEnv => $targetEnv
+            };
+        if ($targetEnv eq "none") {
+            $info->{targetHost} =
+                $m->findvalue('./attrs/attr[@name = 
"targetHost"]/string/@value') || die;
+        } elsif ($targetEnv eq "adhoc") {
+            $info->{adhoc} =
+                { controller => $m->findvalue('./attrs/attr[@name = 
"adhoc"]/attrs/attr[@name = "controller"]/string/@value') || die
+                , startVMCommand => $m->findvalue('./attrs/attr[@name = 
"adhoc"]/attrs/attr[@name = "startVMCommand"]/string/@value') || die
+                , queryVMCommand => $m->findvalue('./attrs/attr[@name = 
"adhoc"]/attrs/attr[@name = "queryVMCommand"]/string/@value') || die
+                };
+        } else {
+            die "machine ‘$name’ has an unknown target environment type 
‘$targetEnv’";
+        }
+        push @machines, $info;
     }
 }
 
@@ -62,32 +82,46 @@
 
 sub startMachines {
     foreach my $machine (@machines) {
-        print STDERR "checking whether VM ‘$machine->{name}’ exists...\n";
+        
+        if ($machine->{targetEnv} eq "none") {
+            # Nothing to do here.
+        }
+        
+        elsif ($machine->{targetEnv} eq "adhoc") {
+        
+            print STDERR "checking whether VM ‘$machine->{name}’ exists...\n";
 
-        my $ipv6 = `ssh root\@stan.nixos.org query-vm $machine->{name} 2> 
/dev/null`;
-        die "unable to query VM state: $?" unless $? == 0 || $? == 256;
+            my $ipv6 = `ssh $machine->{adhoc}->{controller} 
$machine->{adhoc}->{queryVMCommand} $machine->{name} 2> /dev/null`;
+            die "unable to query VM state: $?" unless $? == 0 || $? == 256;
 
-        if ($? == 256) {
-            print STDERR "starting missing VM ‘$machine->{name}’...\n";
-            system "ssh root\@stan.nixos.org create-vm $machine->{name}";
-            die "unable to start VM: $?" unless $? == 0;
+            if ($? == 256) {
+                print STDERR "starting missing VM ‘$machine->{name}’...\n";
+                system "ssh $machine->{adhoc}->{controller} 
$machine->{adhoc}->{createVMCommand} $machine->{name}";
+                die "unable to start VM: $?" unless $? == 0;
 
-            $ipv6 = `ssh root\@stan.nixos.org query-vm $machine->{name} 2> 
/dev/null`;
-            die "unable to query VM state: $?" unless $? == 0;
-        }
+                $ipv6 = `ssh $machine->{adhoc}->{controller} 
$machine->{adhoc}->{queryVMCommand} $machine->{name} 2> /dev/null`;
+                die "unable to query VM state: $?" unless $? == 0;
+            }
 
-        chomp $ipv6;
+            chomp $ipv6;
 
-        print STDERR "IPv6 address is $ipv6\n";
+            print STDERR "IPv6 address is $ipv6\n";
 
-        print STDERR "checking whether VM ‘$machine->{name}’ is reachable via 
SSH...\n";
+            print STDERR "checking whether VM ‘$machine->{name}’ is reachable 
via SSH...\n";
 
-        system "ssh -o StrictHostKeyChecking=no root\@$ipv6 true < /dev/null 
2> /dev/null";
-        die "cannot SSH to VM: $?" unless $? == 0;
+            system "ssh -o StrictHostKeyChecking=no root\@$ipv6 true < 
/dev/null 2> /dev/null";
+            die "cannot SSH to VM: $?" unless $? == 0;
 
-        $machine->{ipv6} = $ipv6;
+            $machine->{ipv6} = $ipv6;
+        }
     }
 
+    # Figure out how we're gonna SSH to each machine.  Prefer IPv6
+    # addresses over hostnames.
+    foreach my $machine (@machines) {
+        $machine->{sshName} = $machine->{ipv6} || $machine->{targetHost} || 
die "don't know how to reach ‘$machine->{name}’";
+    }
+    
     # So now that we know the hostnames / IP addresses of all
     # machines, generate a Nix expression containing the physical
     # network configuration that can be stacked on top of the
@@ -124,7 +158,7 @@
         print STDERR "copying closure to machine ‘$machine->{name}’...\n";
         my $toplevel = readlink "$outPath/$machine->{name}" or die;
         $machine->{toplevel} = $toplevel;
-        system "nix-copy-closure --gzip --to root\@$machine->{ipv6} $toplevel";
+        system "nix-copy-closure --gzip --to root\@$machine->{sshName} 
$toplevel";
         die "unable to copy closure to machine ‘$machine->{name}’" unless $? 
== 0;
     }
 }
@@ -133,7 +167,7 @@
 sub activateConfigs {
     foreach my $machine (@machines) {
         print STDERR "activating new configuration on machine 
‘$machine->{name}’...\n";
-        system "ssh -o StrictHostKeyChecking=no root\@$machine->{ipv6} nix-env 
-p /nix/var/nix/profiles/system --set $machine->{toplevel} \\; 
/nix/var/nix/profiles/system/bin/switch-to-configuration switch";
+        system "ssh -o StrictHostKeyChecking=no root\@$machine->{sshName} 
nix-env -p /nix/var/nix/profiles/system --set $machine->{toplevel} \\; 
/nix/var/nix/profiles/system/bin/switch-to-configuration switch";
         if ($? != 0) {
             # !!! do a rollback
             die "unable to activate new configuration on machine 
‘$machine->{name}’";
_______________________________________________
nix-commits mailing list
[email protected]
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to