a bit more robust
[nodemanager.git] / sliver_vs.py
index bf99cec..9afbe68 100644 (file)
@@ -29,7 +29,7 @@ import subprocess
 # the util-vserver-pl module
 import vserver
 
-import accounts
+import account
 import logger
 import tools
 
@@ -45,7 +45,7 @@ for rlimit in vserver.RLIMITS.keys():
     DEFAULT_ALLOCATION["%s_soft"%rlim]=KEEP_LIMIT
     DEFAULT_ALLOCATION["%s_hard"%rlim]=KEEP_LIMIT
 
-class Sliver_VS(accounts.Account, vserver.VServer):
+class Sliver_VS(account.Account, vserver.VServer):
     """This class wraps vserver.VServer to make its interface closer to what we need."""
 
     SHELL = '/bin/vsh'
@@ -64,10 +64,9 @@ class Sliver_VS(accounts.Account, vserver.VServer):
                 logger.log_exc("sliver_vs:__init__ (first chance) %s",name=name)
                 logger.log('sliver_vs: %s: recreating bad vserver' % name)
                 self.destroy(name)
-            self.create(name, rec['vref'])
+            self.create(name, rec)
             logger.log("sliver_vs: %s: second chance..."%name)
             vserver.VServer.__init__(self, name,logfile='/var/log/nodemanager')
-
         self.keys = ''
         self.rspec = {}
         self.slice_id = rec['slice_id']
@@ -77,8 +76,9 @@ class Sliver_VS(accounts.Account, vserver.VServer):
         self.configure(rec)
 
     @staticmethod
-    def create(name, vref = None):
+    def create(name, rec = None):
         logger.verbose('sliver_vs: %s: create'%name)
+        vref = rec['vref']
         if vref is None:
             logger.log("sliver_vs: %s: ERROR - no vref attached, this is unexpected"%(name))
             # added by caglar
@@ -109,8 +109,18 @@ class Sliver_VS(accounts.Account, vserver.VServer):
                 personality="linux64"
             return personality
 
+        command=[]
+        # be verbose
+        command += ['/bin/bash','-x',]
+        command += ['/usr/sbin/vuseradd', ]
+        if 'attributes' in rec and 'isolate_loopback' in rec['attributes'] and rec['attributes']['isolate_loopback'] == '1':
+            command += [ "-i",]
+        # the vsliver imge to use
+        command += [ '-t', vref, ]
+        # slice name
+        command += [ name, ]            
 #        logger.log_call(['/usr/sbin/vuseradd', '-t', vref, name, ], timeout=15*60)
-        logger.log_call(['/bin/bash','-x','/usr/sbin/vuseradd', '-t', vref, name, ], timeout=15*60)
+        logger.log_call(command, timeout=15*60)
         # export slicename to the slice in /etc/slicename
         file('/vservers/%s/etc/slicename' % name, 'w').write(name)
         file('/vservers/%s/etc/slicefamily' % name, 'w').write(vref)
@@ -125,6 +135,9 @@ class Sliver_VS(accounts.Account, vserver.VServer):
         logger.log_call(['/bin/bash','-x','/usr/sbin/vuserdel', name, ])
 
     def configure(self, rec):
+        # in case we update nodemanager..
+        self.install_and_enable_vinit()
+
         new_rspec = rec['_rspec']
         if new_rspec != self.rspec:
             self.rspec = new_rspec
@@ -137,7 +150,7 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             #self.initscriptchanged = True
             self.refresh_slice_vinit()
 
-        accounts.Account.configure(self, rec)  # install ssh keys
+        account.Account.configure(self, rec)  # install ssh keys
 
     # unconditionnally install and enable the generic vinit script
     # mimicking chkconfig for enabling the generic vinit script
@@ -148,8 +161,8 @@ class Sliver_VS(accounts.Account, vserver.VServer):
         rc3_link="/vservers/%s/etc/rc.d/rc3.d/S99vinit"%self.name
         rc3_target="../init.d/vinit"
         # install in sliver
-        body=file(vinit_source).read()
-        if tools.replace_file_with_string(vinit_script,body,chmod=0755):
+        code=file(vinit_source).read()
+        if tools.replace_file_with_string(vinit_script,code,chmod=0755):
             logger.log("vsliver_vs: %s: installed generic vinit rc script"%self.name)
         # create symlink for runlevel 3
         if not os.path.islink(rc3_link):
@@ -160,21 +173,20 @@ class Sliver_VS(accounts.Account, vserver.VServer):
                 logger.log_exc("vsliver_vs: %s: failed to create runlevel3 symlink %s"%rc3_link)
 
     def rerun_slice_vinit(self):
-        command = "/usr/sbin/vserver %s exec /etc/rc.d/init.d/vinit.slice restart %s" % (self.name, self.name)
-
+        command = "/usr/sbin/vserver %s exec /etc/rc.d/init.d/vinit restart" % (self.name)
         logger.log("vsliver_vs: %s: Rerunning slice initscript: %s" % (self.name, command))
         subprocess.call(command + "&", stdin=open('/dev/null', 'r'), stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT, shell=True)
 
     # this one checks for the existence of the slice initscript
     # install or remove the slice inistscript, as instructed by the initscript tag
     def refresh_slice_vinit(self):
-        body=self.initscript
+        code=self.initscript
         sliver_initscript="/vservers/%s/etc/rc.d/init.d/vinit.slice"%self.name
-        if tools.replace_file_with_string(sliver_initscript,body,remove_if_empty=True,chmod=0755):
-            if body:
+        if tools.replace_file_with_string(sliver_initscript,code,remove_if_empty=True,chmod=0755):
+            if code:
                 logger.log("vsliver_vs: %s: Installed new initscript in %s"%(self.name,sliver_initscript))
                 if self.is_running():
-                    # Only need to rerun the initscript if the vserver is
+                     # Only need to rerun the initscript if the vserver is
                     # already running. If the vserver isn't running, then the
                     # initscript will automatically be started by
                     # /etc/rc.d/vinit when the vserver is started.
@@ -182,26 +194,6 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             else:
                 logger.log("vsliver_vs: %s: Removed obsolete initscript %s"%(self.name,sliver_initscript))
 
-    # bind mount root side dir to sliver side
-    # needs to be done before sliver starts
-    def expose_ssh_dir (self):
-        try:
-            root_ssh="/home/%s/.ssh"%self.name
-            sliver_ssh="/vservers/%s/home/%s/.ssh"%(self.name,self.name)
-            # any of both might not exist yet
-            for path in [root_ssh,sliver_ssh]:
-                if not os.path.exists (path):
-                    os.mkdir(path)
-                if not os.path.isdir (path):
-                    raise Exception
-            mounts=file('/proc/mounts').read()
-            if mounts.find(sliver_ssh)<0:
-                # xxx perform mount
-                subprocess.call("mount --bind -o ro %s %s"%(root_ssh,sliver_ssh),shell=True)
-                logger.log("expose_ssh_dir: %s mounted into slice %s"%(root_ssh,self.name))
-        except:
-            logger.log_exc("expose_ssh_dir with slice %s failed"%self.name)
-
     def start(self, delay=0):
         if self.rspec['enabled'] <= 0:
             logger.log('sliver_vs: not starting %s, is not enabled'%self.name)
@@ -310,7 +302,10 @@ class Sliver_VS(accounts.Account, vserver.VServer):
             if self.rspec['ip_addresses'] != '0.0.0.0':
                 logger.log('sliver_vs: %s: setting IP address(es) to %s' % \
                 (self.name, self.rspec['ip_addresses']))
-            self.set_ipaddresses_config(self.rspec['ip_addresses'])
+            add_loopback = True
+            if 'isolate_loopback' in self.rspec['tags']:
+                add_loopback = self.rspec['tags']['isolate_loopback'] != "1"
+            self.set_ipaddresses_config(self.rspec['ip_addresses'], add_loopback)
 
             #logger.log("sliver_vs: %s: Setting name to %s" % (self.name, self.slice_id))
             #self.setname(self.slice_id)