+ # could use a TestKey class
+ def store_keys(self, options):
+ for key_spec in self.plc_spec['keys']:
+ TestKey(self,key_spec).store_key()
+ return True
+
+ def clean_keys(self, options):
+ utils.system("rm -rf %s/keys/"%self.path)
+
+ def sites (self,options):
+ return self.do_sites(options)
+
+ def clean_sites (self,options):
+ return self.do_sites(options,action="delete")
+
+ def do_sites (self,options,action="add"):
+ for site_spec in self.plc_spec['sites']:
+ test_site = TestSite (self,site_spec)
+ if (action != "add"):
+ utils.header("Deleting site %s in %s"%(test_site.name(),self.name()))
+ test_site.delete_site()
+ # deleted with the site
+ #test_site.delete_users()
+ continue
+ else:
+ utils.header("Creating site %s & users in %s"%(test_site.name(),self.name()))
+ test_site.create_site()
+ test_site.create_users()
+ return True
+
+ def nodes (self, options):
+ return self.do_nodes(options)
+ def clean_nodes (self, options):
+ return self.do_nodes(options,action="delete")
+
+ def do_nodes (self, options,action="add"):
+ for site_spec in self.plc_spec['sites']:
+ test_site = TestSite (self,site_spec)
+ if action != "add":
+ utils.header("Deleting nodes in site %s"%test_site.name())
+ for node_spec in site_spec['nodes']:
+ test_node=TestNode(self,test_site,node_spec)
+ utils.header("Deleting %s"%test_node.name())
+ test_node.delete_node()
+ else:
+ utils.header("Creating nodes for site %s in %s"%(test_site.name(),self.name()))
+ for node_spec in site_spec['nodes']:
+ utils.pprint('Creating node %s'%node_spec,node_spec)
+ test_node = TestNode (self,test_site,node_spec)
+ test_node.create_node ()
+ return True
+
+ # create nodegroups if needed, and populate
+ # no need for a clean_nodegroups if we are careful enough
+ def nodegroups (self, options):
+ # 1st pass to scan contents
+ groups_dict = {}
+ for site_spec in self.plc_spec['sites']:
+ test_site = TestSite (self,site_spec)
+ for node_spec in site_spec['nodes']:
+ test_node=TestNode (self,test_site,node_spec)
+ if node_spec.has_key('nodegroups'):
+ nodegroupnames=node_spec['nodegroups']
+ if isinstance(nodegroupnames,StringTypes):
+ nodegroupnames = [ nodegroupnames ]
+ for nodegroupname in nodegroupnames:
+ if not groups_dict.has_key(nodegroupname):
+ groups_dict[nodegroupname]=[]
+ groups_dict[nodegroupname].append(test_node.name())
+ auth=self.auth_root()
+ for (nodegroupname,group_nodes) in groups_dict.iteritems():
+ try:
+ self.server.GetNodeGroups(auth,{'name':nodegroupname})[0]
+ except:
+ self.server.AddNodeGroup(auth,{'name':nodegroupname})
+ for node in group_nodes:
+ self.server.AddNodeToNodeGroup(auth,node,nodegroupname)
+ return True
+
+ def all_hostnames (self) :
+ hostnames = []
+ for site_spec in self.plc_spec['sites']:
+ hostnames += [ node_spec['node_fields']['hostname'] \
+ for node_spec in site_spec['nodes'] ]
+ return hostnames
+
+ # gracetime : during the first <gracetime> minutes nothing gets printed
+ def do_nodes_booted (self, minutes, gracetime=2):
+ # compute timeout
+ timeout = datetime.datetime.now()+datetime.timedelta(minutes=minutes)
+ graceout = datetime.datetime.now()+datetime.timedelta(minutes=gracetime)
+ # the nodes that haven't checked yet - start with a full list and shrink over time
+ tocheck = self.all_hostnames()
+ utils.header("checking nodes %r"%tocheck)
+ # create a dict hostname -> status
+ status = dict ( [ (hostname,'undef') for hostname in tocheck ] )
+ while tocheck:
+ # get their status
+ tocheck_status=self.server.GetNodes(self.auth_root(), tocheck, ['hostname','boot_state' ] )
+ # update status
+ for array in tocheck_status:
+ hostname=array['hostname']
+ boot_state=array['boot_state']
+ if boot_state == 'boot':
+ utils.header ("%s has reached the 'boot' state"%hostname)
+ else:
+ # if it's a real node, never mind
+ (site_spec,node_spec)=self.locate_node(hostname)
+ if TestNode.is_real_model(node_spec['node_fields']['model']):
+ utils.header("WARNING - Real node %s in %s - ignored"%(hostname,boot_state))
+ # let's cheat
+ boot_state = 'boot'
+ if datetime.datetime.now() > graceout:
+ utils.header ("%s still in '%s' state"%(hostname,boot_state))
+ graceout=datetime.datetime.now()+datetime.timedelta(1)
+ status[hostname] = boot_state
+ # refresh tocheck
+ tocheck = [ hostname for (hostname,boot_state) in status.iteritems() if boot_state != 'boot' ]
+ if not tocheck:
+ return True
+ if datetime.datetime.now() > timeout:
+ for hostname in tocheck:
+ utils.header("FAILURE due to %s in '%s' state"%(hostname,status[hostname]))
+ return False
+ # otherwise, sleep for a while
+ time.sleep(15)
+ # only useful in empty plcs
+ return True
+
+ def nodes_booted(self,options):
+ return self.do_nodes_booted(minutes=5)