Make sure the configuration directory exists and that it's usable
[util-vserver.git] / python / vserver.py
index 50f48a1..93a22a2 100644 (file)
@@ -1,6 +1,6 @@
 # Copyright 2005 Princeton University
 
-#$Id: vserver.py,v 1.63 2007/07/24 17:22:37 dhozac Exp $
+#$Id: vserver.py,v 1.68 2007/07/31 22:04:24 dhozac Exp $
 
 import errno
 import fcntl
@@ -61,6 +61,9 @@ class VServerConfig:
     def __init__(self, name, directory):
         self.name = name
         self.dir = directory
+        if not (os.path.isdir(self.dir) and
+                os.access(self.dir, os.R_OK | os.W_OK | os.X_OK):
+            raise NoSuchVServer, "%s does not exist" % self.dir
 
     def get(self, option, default = None):
         try:
@@ -89,8 +92,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:
@@ -189,7 +201,9 @@ class VServer:
     def set_ipaddresses_config(self, addresses):
         i = 0
         for a in addresses.split(","):
-            self.config.set("interfaces/%d/ip" % i, a)
+            self.config.update("interfaces/%d/ip" % i, a)
+            i += 1
+        while self.config.unset("interfaces/%d/ip" % i):
             i += 1
         self.set_ipaddresses(addresses)
 
@@ -394,11 +408,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" %
@@ -417,7 +434,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)