knows about the new plcapi interface about ipaddresses and routes
[tests.git] / system / Substrate.py
index b2748a8..4e67a85 100644 (file)
@@ -415,15 +415,31 @@ class PlcVsInstance (PlcInstance):
 
 class PlcLxcInstance (PlcInstance):
     # does lxc have a context id of any kind ?
-    def __init__ (self, plcbox, lxcname):
+    def __init__ (self, plcbox, lxcname, pid):
         PlcInstance.__init__(self, plcbox)
         self.lxcname = lxcname
+       self.pid = pid
 
-    def kill (self):
-        print "TODO lxc PlcLxcInstance.kill ..."
+    def vplcname (self):
+        return self.lxcname.split('-')[-1]
+    def buildname (self):
+        return self.lxcname.rsplit('-',2)[0]
 
     def line (self):
-        return "TODO lxc PlcLxcInstance.line with lxcname=%s"%(self.lxcname)
+        msg="== %s =="%(self.vplcname())
+        msg += " [=%s]"%self.lxcname
+        if self.pid==-1:  msg+=" not (yet?) running"
+        else:              msg+=" (pid=%s)"%self.pid
+        if self.timestamp: msg += " @ %s"%self.pretty_timestamp()
+        else:              msg += " *unknown timestamp*"
+        return msg
+
+    def kill (self):
+        command="rsync lxc-driver.sh  %s:/root"%self.plc_box.hostname
+       commands.getstatusoutput(command)
+       msg="lxc container stopping %s on %s"%(self.lxcname,self.plc_box.hostname)
+       self.plc_box.run_ssh(['/root/lxc-driver.sh','-c','stop_lxc','-n',self.lxcname],msg)
+        self.plc_box.forget(self)
 
 ##########
 class PlcBox (Box):
@@ -487,7 +503,7 @@ class PlcVsBox (PlcBox):
         return None
 
     def soft_reboot (self, options):
-        self.run_ssh(['service','util-vserver','stop'],"Stopping all running vservers",
+        self.run_ssh(['service','util-vserver','stop'],"Stopping all running vservers on %s"%(self.hostname,),
                      dry_run=options.dry_run)
 
     def sense (self, options):
@@ -543,22 +559,56 @@ class PlcVsBox (PlcBox):
 
 class PlcLxcBox (PlcBox):
 
+    def add_lxc (self,lxcname,pid):
+        for plc in self.plc_instances:
+            if plc.lxcname==lxcname:
+                header("WARNING, duplicate myplc %s running on %s"%\
+                           (lxcname,self.hostname),banner=False)
+                return
+        self.plc_instances.append(PlcLxcInstance(self,lxcname,pid))    
+
+
     # a line describing the box
     def line(self): 
         msg="%s [max=%d,%d free, LXC-based] (%s)"%(self.hostname, self.max_plcs,self.free_slots(),self.uname())
         return msg
-        
+    
+    def plc_instance_by_lxcname (self, lxcname):
+        for p in self.plc_instances:
+            if p.lxcname==lxcname: return p
+        return None
+    
     # essentially shutdown all running containers
     def soft_reboot (self, options):
-        print "TODO lxc PlcLxcBox.soft_reboot"
+        command="rsync lxc-driver.sh  %s:/root"%self.hostname
+        commands.getstatusoutput(command)
+       self.run_ssh(['/root/lxc-driver.sh','-c','stop_all'],"Stopping all running lxc containers on %s"%(self.hostname,),
+                     dry_run=options.dry_run)
+
 
     # sense is expected to fill self.plc_instances with PlcLxcInstance's 
     # to describe the currently running VM's
     # as well as to call  self.get_uname() once
     def sense (self, options):
-        print "px (todo:PlcLxcBox.sense)",
+        print "xp",
         self.get_uname()
-
+       command="rsync lxc-driver.sh  %s:/root"%self.hostname
+        commands.getstatusoutput(command)
+       command=['/root/lxc-driver.sh','-c','sense_all']
+        lxc_stat = self.backquote_ssh (command)
+       for lxc_line in lxc_stat.split("\n"):
+            if not lxc_line: continue
+            lxcname=lxc_line.split(";")[0]
+           pid=lxc_line.split(";")[1]
+           timestamp=lxc_line.split(";")[2]
+            self.add_lxc(lxcname,pid)
+            timestamp=int(timestamp)
+            p=self.plc_instance_by_lxcname(lxcname)
+            if not p:
+                print 'WARNING zombie plc',self.hostname,lxcname
+                print '... was expecting',lxcname,'in',[i.lxcname for i in self.plc_instances]
+                continue
+            p.set_timestamp(timestamp)
 
 ############################################################
 class QemuInstance: 
@@ -890,13 +940,22 @@ class Substrate:
         
         self.rescope (plcs_on_vs=plcs_on_vs, plcs_on_lxc=plcs_on_lxc)
 
-    def rescope(self, plcs_on_vs, plcs_on_lxc):
+    # which plc boxes are we interested in ?
+    def rescope (self, plcs_on_vs, plcs_on_lxc):
         self.plc_boxes=[]
         if plcs_on_vs: self.plc_boxes += self.plc_vs_boxes
         if plcs_on_lxc: self.plc_boxes += self.plc_lxc_boxes
         self.default_boxes = self.plc_boxes + self.qemu_boxes
         self.all_boxes = self.build_boxes + [ self.test_box ] + self.plc_boxes + self.qemu_boxes
 
+    def summary_line (self):
+        msg  = "["
+        msg += " %d vp"%len(self.plc_vs_boxes)
+        msg += " %d xp"%len(self.plc_lxc_boxes)
+        msg += " %d tried plc boxes"%len(self.plc_boxes)
+        msg += "]"
+        return msg
+
     def fqdn (self, hostname):
         if hostname.find('.')<0: return "%s.%s"%(hostname,self.domain())
         return hostname
@@ -995,8 +1054,9 @@ class Substrate:
                 except:
                     msg=""
                     if not plc_boxname: msg += " PLC boxes are full"
-                    if not vplc_hostname: msg += " vplc IP pool exhausted" 
-                    raise Exception,"Could not make space for a PLC instance:"+msg
+                    if not vplc_hostname: msg += " vplc IP pool exhausted"
+                    msg += " %s"%self.summary_line()
+                    raise Exception,"Cannot make space for a PLC instance:"+msg
                 freed_plc_boxname=plc_instance_to_kill.plc_box.hostname
                 freed_vplc_hostname=plc_instance_to_kill.vplcname()
                 message='killing oldest plc instance = %s on %s'%(plc_instance_to_kill.line(),
@@ -1090,7 +1150,8 @@ class Substrate:
                         msg=""
                         if not qemu_boxname: msg += " QEMU boxes are full"
                         if not vnode_hostname: msg += " vnode IP pool exhausted" 
-                        raise Exception,"Could not make space for a QEMU instance:"+msg
+                        msg += " %s"%self.summary_line()
+                        raise Exception,"Cannot make space for a QEMU instance:"+msg
                     freed_qemu_boxname=qemu_instance_to_kill.qemu_box.hostname
                     freed_vnode_hostname=short_hostname(qemu_instance_to_kill.nodename)
                     # kill it
@@ -1113,6 +1174,7 @@ class Substrate:
             nodemap={'host_box':qemu_boxname,
                      'node_fields:hostname':vnode_fqdn,
                      'interface_fields:ip':ip, 
+                     'ipaddress_fields:ip_addr':ip, 
                      'interface_fields:mac':mac,
                      }
             nodemap.update(self.network_settings())
@@ -1165,7 +1227,7 @@ class Substrate:
             box.reboot(self.options)
 
     ####################
-    # can be run as a utility to manage the local infrastructure
+    # can be run as a utility to probe/display/manage the local infrastructure
     def main (self):
         parser=OptionParser()
         parser.add_option ('-r',"--reboot",action='store_true',dest='reboot',default=False,
@@ -1178,8 +1240,6 @@ class Substrate:
                            help='add build boxes')
         parser.add_option ('-p',"--plc",action='store_true',dest='plcs',default=False,
                            help='add plc boxes')
-        parser.add_option ('-X', "--lxc",action='store_true',dest='plcs_use_lxc',
-                           help='use lxc-enabled plc boxes instead of vs-enabled ones')
         parser.add_option ('-q',"--qemu",action='store_true',dest='qemus',default=False,
                            help='add qemu boxes') 
         parser.add_option ('-a',"--all",action='store_true',dest='all',default=False,
@@ -1190,8 +1250,7 @@ class Substrate:
                            help='dry run mode')
         (self.options,args)=parser.parse_args()
 
-        if self.options.plcs_use_lxc:
-            self.rescope (plcs_on_vs=False, plcs_on_lxc=True)
+        self.rescope (plcs_on_vs=True, plcs_on_lxc=True)
 
         boxes=args
         if self.options.testbox: boxes += [self.test_box]