Author: eelco
Date: Sat Mar 17 19:12:33 2012
New Revision: 33214
URL: https://nixos.org/websvn/nix/?rev=33214&sc=1

Log:
* Provide two utility functions in Upstart jobs: "ensure JOBNAME"
  starts the given job and waits until it's running; "stop_check"
  checks that the current job hasn't been asked to stop.

Modified:
   nixos/trunk/modules/services/network-filesystems/nfsd.nix
   nixos/trunk/modules/services/networking/portmap.nix
   nixos/trunk/modules/system/upstart/upstart.nix
   nixos/trunk/modules/tasks/filesystems.nix
   nixos/trunk/modules/tasks/filesystems/nfs.nix

Modified: nixos/trunk/modules/services/network-filesystems/nfsd.nix
==============================================================================
--- nixos/trunk/modules/services/network-filesystems/nfsd.nix   Sat Mar 17 
18:01:42 2012        (r33213)
+++ nixos/trunk/modules/services/network-filesystems/nfsd.nix   Sat Mar 17 
19:12:33 2012        (r33214)
@@ -89,8 +89,8 @@
 
         preStart =
           ''
-            start portmap || true
-            start mountd || true
+            ensure portmap
+            ensure mountd
 
             # Create a state directory required by NFSv4.
             mkdir -p /var/lib/nfs/v4recovery
@@ -104,7 +104,7 @@
 
         postStart =
           ''
-            start statd || true
+            ensure statd
           '';
       };
 
@@ -115,7 +115,7 @@
 
         preStart =
           ''
-            start portmap || true
+            ensure portmap
 
             mkdir -p /var/lib/nfs
             touch /var/lib/nfs/rmtab

Modified: nixos/trunk/modules/services/networking/portmap.nix
==============================================================================
--- nixos/trunk/modules/services/networking/portmap.nix Sat Mar 17 18:01:42 
2012        (r33213)
+++ nixos/trunk/modules/services/networking/portmap.nix Sat Mar 17 19:12:33 
2012        (r33214)
@@ -88,7 +88,7 @@
             # Portmap forks into the background before it starts
             # listening, so wait until its ready.
             while ! nc -z localhost 111; do
-                if [[ "$(status)" =~ stop/ ]]; then exit; fi
+                stop_check                
                 sleep 1
             done
           '';

Modified: nixos/trunk/modules/system/upstart/upstart.nix
==============================================================================
--- nixos/trunk/modules/system/upstart/upstart.nix      Sat Mar 17 18:01:42 
2012        (r33213)
+++ nixos/trunk/modules/system/upstart/upstart.nix      Sat Mar 17 19:12:33 
2012        (r33214)
@@ -45,6 +45,7 @@
           ${optionalString (job.preStart != "") ''
             pre-start script
               exec >> ${log} 2>&1
+              source ${jobHelpers}
               ${job.preStart}
             end script
           ''}
@@ -55,6 +56,7 @@
               ''
                 script
                   exec >> ${log} 2>&1
+                  source ${jobHelpers}
                   ${job.script}
                 end script
               ''
@@ -71,6 +73,7 @@
           ${optionalString (job.postStart != "") ''
             post-start script
               exec >> ${log} 2>&1
+              source ${jobHelpers}
               ${job.postStart}
             end script
           ''}
@@ -83,6 +86,7 @@
             optionalString (job.preStop != "") (assert hasMain; ''
             pre-stop script
               exec >> ${log} 2>&1
+              source ${jobHelpers}
               ${job.preStop}
             end script
           '')}
@@ -90,6 +94,7 @@
           ${optionalString (job.postStop != "") ''
             post-stop script
               exec >> ${log} 2>&1
+              source ${jobHelpers}
               ${job.postStop}
             end script
           ''}
@@ -122,6 +127,46 @@
         '';
 
 
+  # Shell functions for use in Upstart jobs.
+  jobHelpers = pkgs.writeText "job-helpers.sh"
+    ''
+      # Ensure that an Upstart service is running.
+      ensure() {
+          local job="$1"
+          local status="$(status "$job")"
+
+          # If it's already running, we're happy.
+          [[ "$status" =~ start/running ]] && return 0
+
+          # If its current goal is to stop, start it.
+          [[ "$status" =~ stop/ ]] && { status="$(start "$job")" || true; }
+
+          # The "start" command is synchronous *if* the job is
+          # not already starting.  So if somebody else started
+          # the job in parallel, the "start" above may return
+          # while the job is still starting.  So wait until it
+          # is up or has failed.
+          while true; do
+              [[ "$status" =~ stop/ ]] && { echo "job $job failed to start"; 
return 1; }
+              [[ "$status" =~ start/running ]] && return 0
+              echo "waiting for job $job to start..."
+              sleep 1
+              status="$(status "$job")"
+          done
+      }
+
+      # Check whether the current job has been stopped.  Used in
+      # post-start jobs to determine if they should continue.
+      stop_check() {
+          if [[ "$(status)" =~ stop/ ]]; then
+              echo "job asked to stop!"
+              return 1
+          fi
+          return 0
+      }
+    '';
+        
+
   jobOptions = {
 
     name = mkOption {

Modified: nixos/trunk/modules/tasks/filesystems.nix
==============================================================================
--- nixos/trunk/modules/tasks/filesystems.nix   Sat Mar 17 18:01:42 2012        
(r33213)
+++ nixos/trunk/modules/tasks/filesystems.nix   Sat Mar 17 19:12:33 2012        
(r33214)
@@ -184,7 +184,7 @@
             # ${fstab}
             
             ${optionalString config.services.nfs.client.enable ''
-              start statd || true
+              ensure statd || true
             ''}
             
             exec > /dev/console 2>&1

Modified: nixos/trunk/modules/tasks/filesystems/nfs.nix
==============================================================================
--- nixos/trunk/modules/tasks/filesystems/nfs.nix       Sat Mar 17 18:01:42 
2012        (r33213)
+++ nixos/trunk/modules/tasks/filesystems/nfs.nix       Sat Mar 17 19:12:33 
2012        (r33214)
@@ -49,7 +49,7 @@
 
         preStart =
           ''
-            start portmap || true
+            ensure portmap
             mkdir -p /var/lib/nfs
             mkdir -p /var/lib/nfs/sm
             mkdir -p /var/lib/nfs/sm.bak
_______________________________________________
nix-commits mailing list
[email protected]
http://lists.science.uu.nl/mailman/listinfo/nix-commits

Reply via email to