Author: eelco
Date: Thu Apr 28 13:39:42 2011
New Revision: 27032
URL: https://svn.nixos.org/websvn/nix/?rev=27032&sc=1

Log:
* Start of the next-gen, cloud-enabled nixos-deploy-network :-)

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

Added: cloud/trunk/examples/apache.nix
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ cloud/trunk/examples/apache.nix     Thu Apr 28 13:39:42 2011        (r27032)
@@ -0,0 +1,55 @@
+let
+
+  backend = 
+    { config, pkgs, ... }:
+
+    {
+      services.openssh.enable = true;
+
+      services.httpd.enable = true;
+      services.httpd.adminAddr = "f...@example.org";
+      services.httpd.documentRoot = "${pkgs.valgrind}/share/doc/valgrind/html";
+    };
+
+in
+
+{
+
+  proxy =
+    { config, pkgs, nodes, ... }:
+
+    {
+      services.httpd.enable = true;
+      services.httpd.adminAddr = "e.dols...@tudelft.nl";
+      services.httpd.extraModules = ["proxy_balancer"];
+
+      services.httpd.extraConfig =
+        ''
+          ExtendedStatus on
+
+          <Location /server-status>
+            Order deny,allow
+            Allow from all
+            SetHandler server-status
+          </Location>
+
+          <Proxy balancer://cluster>
+            Allow from all
+            BalancerMember http://${nodes.backend1.config.networking.hostName} 
retry=0
+            BalancerMember http://${nodes.backend2.config.networking.hostName} 
retry=0
+          </Proxy>
+
+          ProxyStatus       full
+          ProxyPass         /server-status !
+          ProxyPass         /       balancer://cluster/
+          ProxyPassReverse  /       balancer://cluster/
+
+          # For testing; don't want to wait forever for dead backend servers.
+          ProxyTimeout      5
+        '';
+    };
+
+  backend1 = backend;
+  backend2 = backend;
+  
+}

Added: cloud/trunk/src/eval-machine-info.nix
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ cloud/trunk/src/eval-machine-info.nix       Thu Apr 28 13:39:42 2011        
(r27032)
@@ -0,0 +1,22 @@
+{ nixpkgs ? builtins.getEnv "NIXPKGS_ALL"
+, nixos ? builtins.getEnv "NIXOS"
+, system ? builtins.currentSystem
+, networkExpr
+}:
+
+with import "${nixos}/lib/testing.nix" { inherit nixpkgs system; };
+with pkgs;
+
+rec {
+  x = complete { nodes = import networkExpr; testScript = ""; };
+  
+  machineInfo = builtins.attrNames (x.nodes);
+  
+  machines = runCommand "vms" {}
+    ''
+      mkdir -p $out
+      ${toString (lib.attrValues (lib.mapAttrs (n: v: ''
+        ln -s ${v.config.system.build.vm} $out/${n}
+      '') x.nodes))}
+    '';
+}

Added: cloud/trunk/src/nixos-deploy-network.pl
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ cloud/trunk/src/nixos-deploy-network.pl     Thu Apr 28 13:39:42 2011        
(r27032)
@@ -0,0 +1,117 @@
+#! /var/run/current-system/sw/bin/perl -w
+
+use XML::LibXML;
+
+my $networkExpr;
+my @machines = ();
+my $outPath;
+
+
+sub main {
+    # Parse the command line.
+    processArgs();
+    
+    # Evaluate the user's network specification to determine machine
+    # names and the desired deployment characteristics.
+    evalMachineInfo();
+
+    # Read the state file to obtain info about previously started VMs.
+    readState();
+
+    # Create missing VMs.
+    startMachines();
+
+    # Evaluate and build each machine configuration locally.
+    buildConfigs();
+
+    # Copy the closures of each machine configuration to the
+    # corresponding target machine.
+    copyClosures();
+
+    # Activate the new configuration on each machine, and do a
+    # rollback if any fails.
+    activateConfigs();
+}
+
+
+sub processArgs {
+    $networkExpr = $ARGV[0];
+    die unless defined $networkExpr;
+}
+
+
+sub evalMachineInfo {
+    my $machineInfoXML =
+        `nix-instantiate --eval-only --xml --strict ./eval-machine-info.nix 
--argstr networkExpr $networkExpr -A machineInfo`;
+    die "evaluation of $networkExpr failed" unless $? == 0;
+    
+    #print $machineInfoXML, "\n";
+
+    my $dom = XML::LibXML->load_xml(string => $machineInfoXML);
+    foreach my $m ($dom->findnodes('/expr/list/string')) {
+        my $name = $m->findvalue('./@value');
+        #print STDERR "got machine ‘$name’\n";
+        push @machines, { name => $name };
+    }
+}
+
+
+sub readState {
+}
+
+
+sub startMachines {
+    foreach my $machine (@machines) {
+        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;
+
+        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;
+
+            $ipv6 = `ssh root\@stan.nixos.org query-vm $machine->{name} 2> 
/dev/null`;
+            die "unable to query VM state: $?" unless $? == 0;
+        }
+
+        chomp $ipv6;
+
+        print STDERR "IPv6 address is $ipv6\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;
+
+        $machine->{ipv6} = $ipv6;
+    }
+}
+
+
+sub buildConfigs {
+    print STDERR "building all machine configurations...\n";
+    $outPath = `nix-build ./eval-machine-info.nix --argstr networkExpr 
$networkExpr -A machines`;
+    die "unable to build all machine configurations" unless $? == 0;
+    chomp $outPath;
+}
+
+
+sub copyClosures {
+    # !!! Should copy closures in parallel.
+    foreach my $machine (@machines) {
+        print STDERR "copying closure to machine ‘$machine->{name}’...\n";
+        my $systemPath = readlink "$outPath/$machine->{name}/system" or die;
+        system "nix-copy-closure --gzip --to root\@$machine->{ipv6} 
$systemPath";
+        die "unable to copy closure to machine ‘$machine->{name}’" unless $? 
== 0;
+    }
+}
+
+
+sub activateConfigs {
+    # TODO
+}
+
+
+main;
_______________________________________________
nix-commits mailing list
nix-comm...@cs.uu.nl
http://mail.cs.uu.nl/mailman/listinfo/nix-commits

Reply via email to