run INITSCRIPTS in vservers
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 12 May 2010 16:59:35 +0000 (16:59 +0000)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 12 May 2010 16:59:35 +0000 (16:59 +0000)
python/vserver.py

index 55d78f0..0576bb7 100644 (file)
@@ -261,13 +261,13 @@ class VServer:
         os.chroot(self.dir)
         os.chdir("/")
 
         os.chroot(self.dir)
         os.chdir("/")
 
-    def chroot_call(self, fn, *args):
+    def chroot_call(self, fn, *args, **kwargs={}):
         cwd_fd = os.open(".", os.O_RDONLY)
         try:
             root_fd = os.open("/", os.O_RDONLY)
             try:
                 self.__do_chroot()
         cwd_fd = os.open(".", os.O_RDONLY)
         try:
             root_fd = os.open("/", os.O_RDONLY)
             try:
                 self.__do_chroot()
-                result = fn(*args)
+                result = fn(*args, **kwargs)
             finally:
                 os.fchdir(root_fd)
                 os.chroot(".")
             finally:
                 os.fchdir(root_fd)
                 os.chroot(".")
@@ -382,6 +382,23 @@ class VServer:
             try:
                 subprocess.call("/usr/sbin/vserver %s start" % self.name, 
                                 shell=True)
             try:
                 subprocess.call("/usr/sbin/vserver %s start" % self.name, 
                                 shell=True)
+
+                # execute initscripts
+                for cmd in self.INITSCRIPTS:
+                    try:
+                        # enter vserver context
+                        arg_subst = { 'runlevel': runlevel }
+                        cmd_args = [cmd[0]] + map(lambda x: x % arg_subst, cmd[1:])
+                        cmd_file = "/vservers/" + self.name + cmd[0]
+                        self.log(cmd_file)
+                        if os.path.isfile(cmd_file):
+                            self.log("executing '%s'" % " ".join(cmd_args))
+                            self.chroot_call(subprocess.call, " ".join(cmd_args), shell=True)
+                        else:
+                            self.log("WARNING: could not run %s on %s" % (cmd[0], self.name))
+                    except:
+                        self.log(traceback.format_exc())
+
             # we get here due to an exception in the top-level child process
             except Exception, ex:
                 self.log(traceback.format_exc())
             # we get here due to an exception in the top-level child process
             except Exception, ex:
                 self.log(traceback.format_exc())