spawnvp only takes three arguments
[util-vserver.git] / python / vserver.py
index cfd133d..cfd347c 100644 (file)
@@ -1,6 +1,6 @@
 # Copyright 2005 Princeton University
 
-#$Id: vserver.py,v 1.61 2007/07/18 14:50:49 dhozac Exp $
+#$Id: vserver.py,v 1.67 2007/07/31 18:14:02 dhozac Exp $
 
 import errno
 import fcntl
@@ -89,8 +89,17 @@ class VServerConfig:
                 f.write("%s\n" % value)
             f.close()
             os.umask(old_umask)
-        except KeyError, e:
-            raise KeyError, "Don't know how to handle %s, sorry" % option
+        except:
+            raise
+
+    def unset(self, option):
+        try:
+            filename = os.path.join(self.dir, option)
+            os.unlink(filename)
+            os.removedirs(os.path.dirname(filename))
+            return True
+        except:
+            return False
 
 
 class VServer:
@@ -181,6 +190,35 @@ class VServer:
     def get_capabilities_config(self):
         return self.config.get('bcapabilities', '')
 
+    def set_ipaddresses(self, addresses):
+        vserverimpl.netremove(self.ctx, "all")
+        for a in addresses.split(","):
+            vserverimpl.netadd(self.ctx, a)
+
+    def set_ipaddresses_config(self, addresses):
+        i = 0
+        for a in addresses.split(","):
+            self.config.update("interfaces/%d/ip" % i, a)
+            i += 1
+        while self.config.unset("interfaces/%d/ip" % i):
+            i += 1
+        self.set_ipaddresses(addresses)
+
+    def get_ipaddresses_config(self):
+        i = 0
+        ret = []
+        while True:
+            r = self.config.get("interfaces/%d/ip" % i, '')
+            if r == '':
+                break
+            ret += [r]
+            i += 1
+        return ",".join(ret)
+
+    def get_ipaddresses(self):
+        # No clean way to do this right now.
+        return None
+
     def __do_chroot(self):
 
         os.chroot(self.dir)
@@ -324,8 +362,9 @@ class VServer:
                          ([], filter_fn))[0]
         garbage += filter(os.path.isfile, map((LOCKDIR + "/").__add__,
                                               os.listdir(LOCKDIR)))
-        for f in garbage:
-            os.unlink(f)
+        if False:
+            for f in garbage:
+                os.unlink(f)
 
         # set the initial runlevel
         f = open(RUNDIR + "/utmp", "w")
@@ -366,11 +405,14 @@ class VServer:
                 state_file = open("/var/run/vservers/%s" % self.name, "w")
 
                 # use /dev/null for stdin, /var/log/boot.log for stdout/err
-                os.close(0)
-                os.close(1)
-                os.open("/dev/null", os.O_RDONLY)
+                fd = os.open("/dev/null", os.O_RDONLY)
+                if fd != 0:
+                    os.dup2(fd, 0)
+                    os.close(fd)
                 self.__do_chroot()
                 log = open("/var/log/boot.log", "w", 0)
+                if log.fileno() != 1:
+                    os.dup2(log.fileno(), 1)
                 os.dup2(1, 2)
 
                 print >>log, ("%s: starting the virtual server %s" %
@@ -389,7 +431,7 @@ class VServer:
                          cmd_args = [cmd[0]] + map(lambda x: x % arg_subst,
                                                    cmd[1:])
                          print >>log, "executing '%s'" % " ".join(cmd_args)
-                         os.spawnvp(os.P_WAIT,cmd[0],*cmd_args)
+                         os.spawnvp(os.P_WAIT,cmd[0],cmd_args)
                      except:
                          traceback.print_exc()
                          os._exit(1)