X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sliver_vs.py;h=579e2abd51bdab5bcd349712634446cae5d7be55;hb=31d147a70d18ae73e60d427a05aaef6ef93ad3e0;hp=97aa37552c8dac3c26c94437363c8b3579de82f2;hpb=3e745a161b33bb6dfa2fcdec593866904d62a153;p=nodemanager.git diff --git a/sliver_vs.py b/sliver_vs.py index 97aa375..579e2ab 100644 --- a/sliver_vs.py +++ b/sliver_vs.py @@ -17,9 +17,9 @@ don't have to guess if there is a running process or not. """ import errno +import traceback import os, os.path import time -import commands import vserver @@ -68,22 +68,9 @@ class Sliver_VS(accounts.Account, vserver.VServer): self.slice_id = rec['slice_id'] self.disk_usage_initialized = False self.initscriptchanged = False + self.enabled = True self.configure(rec) - _root_context_arch=None - @staticmethod - def root_context_arch(): - if not Sliver_VS._root_context_arch: - Sliver_VS._root_context_arch=commands.getoutput("uname -i") - return Sliver_VS._root_context_arch - - @staticmethod - def personality (arch): - personality="linux32" - if arch.find("64")>=0: - personality="linux64" - return personality - @staticmethod def create(name, vref = None): logger.verbose('Sliver_VS:create - name=%s'%name) @@ -122,10 +109,8 @@ class Sliver_VS(accounts.Account, vserver.VServer): refname="-".join( (pldistro,fcdistro,arch) ) # check the template exists -- there's probably a better way.. - if os.path.isdir ("/vservers/.vref/%s"% vref): refname = vref - - if not os.path.isdir ("/vservers/.vref/%s"% refname): - logger.verbose("%s (%s) : vref %s not found, using default %s"%( + if not os.path.isdir ("/vservers/.vref/%s"%refname): + logger.log("%s (%s) : vref %s not found, using default %s"%( name,vref,refname,default)) refname=default # reset so arch is right @@ -135,24 +120,29 @@ class Sliver_VS(accounts.Account, vserver.VServer): except IOError: # have not found slicefamily - logger.verbose("%s (%s): legacy node - using fallback vrefname 'default'"%(name,vref)) + logger.log("%s (%s): legacy node - using fallback vrefname 'default'"%(name,vref)) # for legacy nodes refname="default" arch="i386" except: - import traceback logger.log("%s (%s) : unexpected error follows - using 'default'"%(name,vref)) logger.log(traceback.format_exc()) refname="default" arch="i386" + def personality (arch): + personality="linux32" + if arch.find("64")>=0: + personality="linux64" + return personality + logger.log_call('/usr/sbin/vuseradd', '-t', refname, name) # export slicename to the slice in /etc/slicename file('/vservers/%s/etc/slicename' % name, 'w').write(name) # set personality: only if needed (if arch's differ) - if Sliver_VS.root_context_arch() != arch: - file('/etc/vservers/%s/personality' % name, 'w').write(Sliver_VS.personality(arch)) - logger.log('%s: set personality to %s'%(name,Sliver_VS.personality(arch))) + if tools.root_context_arch() != arch: + file('/etc/vservers/%s/personality' % name, 'w').write(personality(arch)) + logger.log('%s: set personality to %s'%(name,personality(arch))) @staticmethod def destroy(name): logger.log_call('/usr/sbin/vuserdel', name) @@ -174,6 +164,8 @@ class Sliver_VS(accounts.Account, vserver.VServer): if self.rspec['enabled'] > 0: logger.log('%s: starting in %d seconds' % (self.name, delay)) time.sleep(delay) + # VServer.start calls fork() internally, + # so just close the nonstandard fds and fork once to avoid creating zombies child_pid = os.fork() if child_pid == 0: if self.initscriptchanged: @@ -185,23 +177,23 @@ class Sliver_VS(accounts.Account, vserver.VServer): os.close(fd) try: self.chroot_call(install_initscript) - self.initscriptchanged = False except: logger.log_exc(self.name) - # VServer.start calls fork() internally, - # so just close the nonstandard fds and fork once to avoid creating zombies tools.close_nonstandard_fds() vserver.VServer.start(self) os._exit(0) - else: os.waitpid(child_pid, 0) + else: + os.waitpid(child_pid, 0) + self.initscriptchanged = False else: logger.log('%s: not starting, is not enabled' % self.name) def stop(self): logger.log('%s: stopping' % self.name) vserver.VServer.stop(self) - def is_running(self): vserver.VServer.is_running(self) + def is_running(self): + return vserver.VServer.is_running(self) - def set_resources(self): + def set_resources(self,setup=False): disk_max = self.rspec['disk_max'] logger.log('%s: setting max disk usage to %d KiB' % (self.name, disk_max)) try: # if the sliver is over quota, .set_disk_limit will throw an exception @@ -238,6 +230,23 @@ class Sliver_VS(accounts.Account, vserver.VServer): cpu_pct = self.rspec['cpu_pct'] cpu_share = self.rspec['cpu_share'] + if setup: + for key in self.rspec.keys(): + if key.find('sysctl.') == 0: + sysctl=key.split('.') + try: + path="/proc/sys/%s" % ("/".join(sysctl[1:])) + logger.log("%s: opening %s"%(self.name,path)) + flags = os.O_WRONLY + fd = os.open(path, flags) + logger.log("%s: writing %s=%s"%(self.name,key,self.rspec[key])) + os.write(fd,self.rspec[key]) + os.close(fd) + except IOError, e: + logger.log("%s: could not set %s=%s"%(self.name,key,self.rspec[key])) + logger.log("%s: error = %s"%(self.name,e)) + + if self.rspec['enabled'] > 0: if cpu_pct > 0: logger.log('%s: setting cpu reservation to %d%%' % (self.name, cpu_pct)) @@ -259,6 +268,10 @@ class Sliver_VS(accounts.Account, vserver.VServer): if self.is_running(): logger.log("%s: Setting name to %s" % (self.name, self.slice_id),2) self.setname(self.slice_id) + + if self.enabled == False: + self.enabled = True + self.start() if False: # Does not work properly yet. if self.have_limits_changed(): @@ -274,4 +287,5 @@ class Sliver_VS(accounts.Account, vserver.VServer): else: # tell vsh to disable remote login by setting CPULIMIT to 0 logger.log('%s: disabling remote login' % self.name) self.set_sched_config(0, 0) + self.enabled = False self.stop()