rename initscript_body into initscript_code
[nodemanager.git] / sliver_vs.py
index a86b3f6..9914433 100644 (file)
@@ -1,4 +1,4 @@
-# 
+#
 
 """VServer slivers.
 
@@ -21,6 +21,7 @@ don't have to guess if there is a running process or not.
 import errno
 import traceback
 import os, os.path
+import sys
 import time
 from threading import BoundedSemaphore
 import subprocess
@@ -124,6 +125,9 @@ class Sliver_VS(accounts.Account, vserver.VServer):
         logger.log_call(['/bin/bash','-x','/usr/sbin/vuserdel', name, ])
 
     def configure(self, rec):
+        # in case we update nodemanager..
+        self.install_and_enable_vinit()
+
         new_rspec = rec['_rspec']
         if new_rspec != self.rspec:
             self.rspec = new_rspec
@@ -147,8 +151,8 @@ class Sliver_VS(accounts.Account, vserver.VServer):
         rc3_link="/vservers/%s/etc/rc.d/rc3.d/S99vinit"%self.name
         rc3_target="../init.d/vinit"
         # install in sliver
-        body=file(vinit_source).read()
-        if tools.replace_file_with_string(vinit_script,body,chmod=0755):
+        code=file(vinit_source).read()
+        if tools.replace_file_with_string(vinit_script,code,chmod=0755):
             logger.log("vsliver_vs: %s: installed generic vinit rc script"%self.name)
         # create symlink for runlevel 3
         if not os.path.islink(rc3_link):
@@ -158,17 +162,28 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             except:
                 logger.log_exc("vsliver_vs: %s: failed to create runlevel3 symlink %s"%rc3_link)
 
+    def rerun_slice_vinit(self):
+        command = "/usr/sbin/vserver %s exec /etc/rc.d/init.d/vinit restart" % (self.name)
+        logger.log("vsliver_vs: %s: Rerunning slice initscript: %s" % (self.name, command))
+        subprocess.call(command + "&", stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=True)
+
     # this one checks for the existence of the slice initscript
     # install or remove the slice inistscript, as instructed by the initscript tag
     def refresh_slice_vinit(self):
-        body=self.initscript
+        code=self.initscript
         sliver_initscript="/vservers/%s/etc/rc.d/init.d/vinit.slice"%self.name
-        if tools.replace_file_with_string(sliver_initscript,body,remove_if_empty=True,chmod=0755):
-            if body:
+        if tools.replace_file_with_string(sliver_initscript,code,remove_if_empty=True,chmod=0755):
+            if code:
                 logger.log("vsliver_vs: %s: Installed new initscript in %s"%(self.name,sliver_initscript))
+                if self.is_running():
+                    # Only need to rerun the initscript if the vserver is
+                    # already running. If the vserver isn't running, then the
+                    # initscript will automatically be started by
+                    # /etc/rc.d/vinit when the vserver is started.
+                    self.rerun_slice_vinit()
             else:
                 logger.log("vsliver_vs: %s: Removed obsolete initscript %s"%(self.name,sliver_initscript))
-    
+
     # bind mount root side dir to sliver side
     # needs to be done before sliver starts
     def expose_ssh_dir (self):
@@ -176,7 +191,7 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             root_ssh="/home/%s/.ssh"%self.name
             sliver_ssh="/vservers/%s/home/%s/.ssh"%(self.name,self.name)
             # any of both might not exist yet
-            for path in [root_ssh,sliver_ssh]: 
+            for path in [root_ssh,sliver_ssh]:
                 if not os.path.exists (path):
                     os.mkdir(path)
                 if not os.path.isdir (path):
@@ -197,7 +212,9 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             time.sleep(delay)
             # the generic /etc/init.d/vinit script is permanently refreshed, and enabled
             self.install_and_enable_vinit()
-            self.expose_ssh_dir()
+            # expose .ssh for omf_friendly slivers
+            if 'omf_control' in self.rspec['tags']:
+                self.expose_ssh_dir()
             # if a change has occured in the slice initscript, reflect this in /etc/init.d/vinit.slice
             self.refresh_slice_vinit()
             child_pid = os.fork()