Convert values to strings before writing to config file
[util-vserver.git] / python / vserver.py
index 1a55493..7774ceb 100644 (file)
@@ -59,10 +59,6 @@ class VServer:
         self.remove_caps = ~vserverimpl.CAP_SAFE;
         self.ctx = int(self.config["S_CONTEXT"])
 
-    def __str__(self):
-
-        return self.name
-
     config_var_re = re.compile(r"^ *([A-Z_]+)=(.*)\n?$", re.MULTILINE)
 
     def __read_config_file(self, filename):
@@ -88,7 +84,7 @@ class VServer:
             (key, val) = m.groups()
             newval = todo.pop(key, None)
             if newval != None:
-                data = data[:m.start(2)] + newval + data[m.end(2):]
+                data = data[:m.start(2)] + str(newval) + data[m.end(2):]
                 changed = True
         for (newkey, newval) in todo.items():
             data += "%s=%s\n" % (newkey, newval)
@@ -104,12 +100,19 @@ class VServer:
         f.close()
 
         # 'copy' original file, rename new to original
-        os.link(filename, filename + ".old")
+        backup = filename + ".old"
+        try:
+            os.unlink(backup)
+        except OSError, ex:
+            if ex.errno != errno.ENOENT:
+                raise
+        os.link(filename, backup)
         os.rename(newfile, filename)
 
     def __do_chroot(self):
 
-        return os.chroot(self.dir)
+        os.chroot(self.dir)
+        os.chdir("/")
 
     def set_disklimit(self, block_limit):
 
@@ -130,7 +133,7 @@ class VServer:
                               block_usage,
                               block_limit,
                               inode_usage,
-                              -1,  # inode limit
+                              vserverimpl.DLIMIT_INF,  # inode limit
                               2)   # %age reserved for root
 
     def get_disklimit(self):
@@ -329,6 +332,9 @@ class VServer:
 
     def start(self, wait, runlevel = 3):
 
+        # XXX - temporary hack
+        self.set_disklimit(int(self.config.get("DISKLIMIT", 5000000)))
+
         child_pid = os.fork()
         if child_pid == 0:
             # child process
@@ -346,6 +352,7 @@ class VServer:
                 self.__do_chroot()
                 log = open("/var/log/boot.log", "w", 0)
                 os.dup2(1, 2)
+                # XXX - close all other fds
 
                 print >>log, ("%s: starting the virtual server %s" %
                               (time.asctime(time.gmtime()), self.name))
@@ -356,6 +363,7 @@ class VServer:
                 # execute each init script in turn
                 # XXX - we don't support all scripts that vserver script does
                 cmd_pid = 0
+                first_child = True
                 for cmd in self.INITSCRIPTS + [None]:
                     # wait for previous command to terminate, unless it
                     # is the last one and the caller has specified to wait
@@ -398,9 +406,16 @@ class VServer:
 
     def update_resources(self, resources):
 
+        self.config.update(resources)
+
         # write new values to configuration file
         self.__update_config_file(self.config_file, resources)
 
+        # disklimit can be applied without a process in context
+        disklimit = resources.get("DISKLIMIT", 0)
+        if disklimit:
+            self.set_disklimit(disklimit)
+
         #
         # Figure out if any processes are active in context, apply new
         # values if there are.