# Copyright 2005 Princeton University
-#$Id: vserver.py,v 1.63 2007/07/24 17:22:37 dhozac Exp $
+#$Id: vserver.py,v 1.69 2007/08/01 18:33:28 dhozac Exp $
import errno
import fcntl
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:
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:
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)
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" %
# execute each init script in turn
# XXX - we don't support all scripts that vserver script does
self.__do_chcontext(state_file)
- for cmd in self.INITSCRIPTS + [None]:
+ 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:])
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)