1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
4 import sys, os, os.path, time, base64
7 from TestUser import TestUser
8 from TestBoxQemu import TestBoxQemu
9 from TestSsh import TestSsh
10 from Completer import CompleterTask
12 class CompleterTaskNodeSsh (CompleterTask):
13 def __init__ (self, hostname, qemuname, local_key, command=None,boot_state="boot", expected=True, dry_run=False):
14 self.hostname=hostname
15 self.qemuname=qemuname
16 self.boot_state=boot_state
17 self.local_key=local_key
18 self.command=command if command is not None else "hostname;uname -a"
19 self.expected=expected
20 self.dry_run = dry_run
21 def run (self, silent):
22 command = TestSsh (self.hostname,key=self.local_key).actual_command(self.command)
23 retcod=utils.system (command, silent=silent, dry_run=self.dry_run)
24 if self.expected: return retcod==0
25 else: return retcod !=0
26 def failure_message (self):
27 return "Cannot reach %s @ %s in %s mode"%(self.hostname, self.qemuname, self.boot_state)
31 def __init__ (self,test_plc,test_site,node_spec):
32 self.test_plc=test_plc
33 self.test_site=test_site
34 self.node_spec=node_spec
37 return self.node_spec['node_fields']['hostname']
40 return self.test_plc.options.dry_run
42 def is_qemu_model (model):
43 return model.find("qemu") >= 0
45 return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
48 def is_real_model (model):
49 return not TestNode.is_qemu_model(model)
51 return TestNode.is_real_model (self.node_spec['node_fields']['model'])
54 return self.test_plc.options.buildname
58 return "qemu-%s"%self.name()
60 return "real-%s"%self.name()
62 # this returns a hostname
68 return self.node_spec['host_box']
70 utils.header("WARNING : qemu nodes need a host box")
73 # this returns a TestBoxQemu instance - cached in .test_box_value
76 return self.test_box_value
78 self.test_box_value = TestBoxQemu (self.host_box(),self.buildname())
79 return self.test_box_value
81 def create_node (self):
82 ownername = self.node_spec['owner']
83 user_spec = self.test_site.locate_user(ownername)
84 test_user = TestUser(self.test_plc,self.test_site,user_spec)
85 userauth = test_user.auth()
86 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
87 rootauth=self.test_plc.auth_root()
88 server = self.test_plc.apiserver
89 node_id=server.AddNode(userauth,
90 self.test_site.site_spec['site_fields']['login_base'],
91 self.node_spec['node_fields'])
92 server.SetNodePlainBootstrapfs(userauth,
93 self.node_spec['node_fields']['hostname'],
95 # create as reinstall to avoid user confirmation
96 server.UpdateNode(userauth, self.name(), {'boot_state':'reinstall'})
98 if not self.test_plc.has_addresses_api():
99 # print 'USING OLD INTERFACE'
100 # populate network interfaces - primary
101 server.AddInterface(userauth,self.name(),
102 self.node_spec['interface_fields'])
104 # print 'USING NEW INTERFACE with separate ip addresses'
105 # this is for setting the 'dns' stuff that now goes with the node
106 server.UpdateNode (userauth, self.name(), self.node_spec['node_fields_nint'])
107 interface_id = server.AddInterface (userauth, self.name(),self.node_spec['interface_fields_nint'])
108 server.AddIpAddress (userauth, interface_id, self.node_spec['ipaddress_fields'])
109 route_fields=self.node_spec['route_fields']
110 route_fields['interface_id']=interface_id
111 server.AddRoute (userauth, node_id, self.node_spec['route_fields'])
113 # populate network interfaces - others
114 if self.node_spec.has_key('extra_interfaces'):
115 for interface in self.node_spec['extra_interfaces']:
116 server.AddInterface(userauth,self.name(), interface['interface_fields'])
117 if interface.has_key('settings'):
118 for (attribute,value) in interface['settings'].iteritems():
119 # locate node network
120 interface = server.GetInterfaces(userauth,{'ip':interface['interface_fields']['ip']})[0]
121 interface_id=interface['interface_id']
122 # locate or create node network attribute type
124 interface_tagtype = server.GetTagTypes(userauth,{'name':attribute})[0]
126 interface_tagtype = server.AddTagType(rootauth,{'category':'test',
127 'tagname':attribute})
129 server.AddInterfaceTag(userauth,interface_id,attribute,value)
131 def delete_node (self):
132 # uses the right auth as far as poss.
134 ownername = self.node_spec['owner']
135 user_spec = self.test_site.locate_user(ownername)
136 test_user = TestUser(self.test_plc,self.test_site,user_spec)
137 auth = test_user.auth()
139 auth=self.test_plc.auth_root()
140 self.test_plc.apiserver.DeleteNode(auth,self.name())
142 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
143 def qemu_local_init(self):
144 "all nodes : init a clean local directory for holding node-dep stuff like iso image..."
145 utils.system("rm -rf %s"%self.nodedir())
146 utils.system("mkdir %s"%self.nodedir())
147 if not self.is_qemu():
149 return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
152 "all nodes: invoke GetBootMedium and store result locally"
153 utils.header("Calling GetBootMedium for %s"%self.name())
156 options.append('serial')
157 options.append('no-hangcheck')
158 encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(),
159 self.name(), 'node-iso', '', options)
161 raise Exception, 'GetBootmedium failed'
163 filename="%s/%s.iso"%(self.nodedir(),self.name())
164 utils.header('Storing boot medium into %s'%filename)
166 print "Dry_run: skipped writing of iso image"
169 file(filename,'w').write(base64.b64decode(encoded))
172 def nodestate_reinstall (self):
173 "all nodes: mark PLCAPI boot_state as reinstall"
174 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
175 self.name(),{'boot_state':'reinstall'})
178 def nodestate_safeboot (self):
179 "all nodes: mark PLCAPI boot_state as safeboot"
180 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
181 self.name(),{'boot_state':'safeboot'})
184 def nodestate_boot (self):
185 "all nodes: mark PLCAPI boot_state as boot"
186 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
187 self.name(),{'boot_state':'boot'})
190 def nodestate_show (self):
191 "all nodes: show PLCAPI boot_state"
193 print "Dry_run: skipped getting current node state"
195 state=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), self.name(), ['boot_state'])[0]['boot_state']
196 print self.name(),':',state
199 def qemu_local_config(self):
200 "all nodes: compute qemu config qemu.conf and store it locally"
201 if not self.is_qemu():
203 mac=self.node_spec['interface_fields']['mac']
204 hostname=self.node_spec['node_fields']['hostname']
205 ip=self.node_spec['interface_fields']['ip']
206 auth=self.test_plc.auth_root()
207 target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
208 conf_filename="%s/qemu.conf"%(self.nodedir())
210 print "dry_run: skipped actual storage of qemu.conf"
212 utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
213 file=open(conf_filename,'w')
214 file.write('MACADDR=%s\n'%mac)
215 file.write('NODE_ISO=%s.iso\n'%self.name())
216 file.write('HOSTNAME=%s\n'%hostname)
217 file.write('IP=%s\n'%ip)
218 file.write('TARGET_ARCH=%s\n'%target_arch)
222 def qemu_clean (self):
223 utils.header("Cleaning up qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
224 dry_run=self.dry_run()
225 self.test_box().rmdir(self.nodedir(), dry_run=dry_run)
228 def qemu_export (self):
229 "all nodes: push local node-dep directory on the qemu box"
230 # if relevant, push the qemu area onto the host box
231 if self.test_box().is_local():
233 dry_run=self.dry_run()
234 utils.header ("Cleaning any former sequel of %s on %s"%(self.name(),self.host_box()))
235 utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
236 return self.test_box().copy(self.nodedir(),recursive=True,dry_run=dry_run)==0
238 def qemu_start (self):
239 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
240 model=self.node_spec['node_fields']['model']
241 #starting the Qemu nodes before
245 utils.header("TestNode.qemu_start : %s model %s taken as real node"%(self.name(),model))
248 def qemu_timestamp (self):
249 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
250 test_box = self.test_box()
251 test_box.run_in_buildname("mkdir -p %s"%self.nodedir(), dry_run=self.dry_run())
253 return test_box.run_in_buildname("echo %d > %s/timestamp"%(now,self.nodedir()), dry_run=self.dry_run())==0
255 def start_qemu (self):
256 test_box = self.test_box()
257 utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
259 test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/log.txt"%(self.nodedir(),self.nodedir()),
260 dry_run=self.dry_run())
261 # kick it off in background, as it would otherwise hang
262 test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/log.txt"%(self.nodedir(),self.nodedir()))
264 def list_qemu (self):
265 utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
266 command="%s/qemu-kill-node -l %s"%(self.nodedir(),self.name())
267 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
270 def kill_qemu (self):
271 #Prepare the log file before killing the nodes
272 test_box = self.test_box()
273 # kill the right processes
274 utils.header("Stopping qemu for node %s on box %s"%(self.name(),self.test_box().hostname()))
275 command="%s/qemu-kill-node %s"%(self.nodedir(),self.name())
276 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
279 def gather_qemu_logs (self):
280 if not self.is_qemu():
282 remote_log="%s/log.txt"%self.nodedir()
283 local_log="logs/node.qemu.%s.txt"%self.name()
284 self.test_box().test_ssh.fetch(remote_log,local_log,dry_run=self.dry_run())
286 def keys_clear_known_hosts (self):
287 "remove test nodes entries from the local known_hosts file"
288 TestSsh(self.name()).clear_known_hosts()
291 def create_test_ssh(self):
292 # get the plc's keys for entering the node
293 vservername=self.test_plc.vservername
294 ### # assuming we've run testplc.fetch_keys()
295 ### key = "keys/%(vservername)s.rsa"%locals()
296 # fetch_keys doesn't grab the root key anymore
297 key = "keys/key_admin.rsa"
298 return TestSsh(self.name(), buildname=self.buildname(), key=key)
300 def check_hooks (self):
301 extensions = [ 'py','pl','sh' ]
303 scripts=utils.locate_hooks_scripts ('node '+self.name(), path,extensions)
305 for script in scripts:
306 if not self.check_hooks_script (script):
310 def check_hooks_script (self,local_script):
311 # push the script on the node's root context
312 script_name=os.path.basename(local_script)
313 utils.header ("NODE hook %s (%s)"%(script_name,self.name()))
314 test_ssh=self.create_test_ssh()
315 test_ssh.copy_home(local_script)
316 if test_ssh.run("./"+script_name) != 0:
317 utils.header ("WARNING: node hooks check script %s FAILED (ignored)"%script_name)
321 utils.header ("SUCCESS: node hook %s OK"%script_name)
324 def has_libvirt (self):
325 test_ssh=self.create_test_ssh()
326 return test_ssh.run ("rpm -q --quiet libvirt-client")==0
328 def _check_system_slice (self, slicename,dry_run=False):
329 sitename=self.test_plc.plc_spec['settings']['PLC_SLICE_PREFIX']
330 vservername="%s_%s"%(sitename,slicename)
331 test_ssh=self.create_test_ssh()
332 if self.has_libvirt():
333 utils.header("Checking system slice %s using virsh"%slicename)
334 return test_ssh.run("virsh --connect lxc:// list | grep -q ' %s '"%vservername,
337 (retcod,output)=utils.output_of(test_ssh.actual_command("cat /vservers/%s/etc/slicefamily 2> /dev/null")%vservername)
338 # get last line only as ssh pollutes the output
339 slicefamily=output.split("\n")[-1]
340 utils.header("Found slicefamily '%s'for slice %s"%(slicefamily,slicename))
343 utils.header("Checking system slice %s using vserver-stat"%slicename)
344 return test_ssh.run("vserver-stat | grep %s"%vservername,dry_run=dry_run)==0