1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
10 from TestUser import TestUser
11 from TestBoxQemu import TestBoxQemu
12 from TestSsh import TestSsh
13 from Completer import CompleterTask
15 class CompleterTaskNodeSsh(CompleterTask):
16 def __init__(self, hostname, qemuname, local_key, command=None,
17 boot_state="boot", expected=True, dry_run=False):
18 self.hostname = hostname
19 self.qemuname = qemuname
20 self.boot_state = boot_state
21 self.local_key = local_key
22 self.command = command if command is not None else "hostname;uname -a"
23 self.expected = expected
24 self.dry_run = dry_run
25 self.test_ssh = TestSsh(self.hostname, key=self.local_key)
26 def run(self, silent):
27 command = self.test_ssh.actual_command(self.command)
28 retcod = utils.system(command, silent=silent, dry_run=self.dry_run)
33 def failure_epilogue(self):
34 print("Cannot reach {} in {} mode".format(self.hostname, self.boot_state))
38 def __init__(self, test_plc, test_site, node_spec):
39 self.test_plc = test_plc
40 self.test_site = test_site
41 self.node_spec = node_spec
44 return self.node_spec['node_fields']['hostname']
47 return self.test_plc.options.dry_run
50 def is_qemu_model(model):
51 return model.find("qemu") >= 0
53 return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
56 def is_real_model(model):
57 return not TestNode.is_qemu_model(model)
59 return TestNode.is_real_model(self.node_spec['node_fields']['model'])
62 return self.test_plc.options.buildname
66 return "qemu-{}".format(self.name())
68 return "real-{}".format(self.name())
70 # this returns a hostname
76 return self.node_spec['host_box']
78 utils.header("WARNING : qemu nodes need a host box")
81 # this returns a TestBoxQemu instance - cached in .test_box_value
84 return self.test_box_value
86 self.test_box_value = TestBoxQemu(self.host_box(),self.buildname())
87 return self.test_box_value
89 def create_node(self):
90 ownername = self.node_spec['owner']
91 user_spec = self.test_site.locate_user(ownername)
92 test_user = TestUser(self.test_plc,self.test_site,user_spec)
93 userauth = test_user.auth()
94 utils.header("node {} created by user {}".format(self.name(), test_user.name()))
95 rootauth = self.test_plc.auth_root()
96 server = self.test_plc.apiserver
97 node_id = server.AddNode(userauth,
98 self.test_site.site_spec['site_fields']['login_base'],
99 self.node_spec['node_fields'])
100 # create as reinstall to avoid user confirmation
101 server.UpdateNode(userauth, self.name(), { 'boot_state' : 'reinstall' })
103 # you are supposed to make sure the tags exist
104 for tagname, tagvalue in self.node_spec['tags'].items():
105 server.AddNodeTag(userauth, node_id, tagname, tagvalue)
107 if not self.test_plc.has_addresses_api():
108 # print 'USING OLD INTERFACE'
109 # populate network interfaces - primary
110 server.AddInterface(userauth, self.name(),
111 self.node_spec['interface_fields'])
113 # print 'USING NEW INTERFACE with separate ip addresses'
114 # this is for setting the 'dns' stuff that now goes with the node
115 server.UpdateNode(userauth, self.name(), self.node_spec['node_fields_nint'])
116 interface_id = server.AddInterface(userauth, self.name(),self.node_spec['interface_fields_nint'])
117 server.AddIpAddress(userauth, interface_id, self.node_spec['ipaddress_fields'])
118 route_fields = self.node_spec['route_fields']
119 route_fields['interface_id'] = interface_id
120 server.AddRoute(userauth, node_id, self.node_spec['route_fields'])
122 # populate network interfaces - others
123 if 'extra_interfaces' in self.node_spec:
124 for interface in self.node_spec['extra_interfaces']:
125 server.AddInterface(userauth, self.name(), interface['interface_fields'])
126 if 'settings' in interface:
127 for attribute, value in interface['settings'].items():
128 # locate node network
129 interface = server.GetInterfaces( userauth,
130 {'ip' : interface['interface_fields']['ip']})[0]
131 interface_id = interface['interface_id']
132 # locate or create node network attribute type
134 interface_tagtype = server.GetTagTypes(userauth, {'name' : attribute})[0]
136 interface_tagtype = server.AddTagType(rootauth,{'category' : 'test',
137 'tagname' : attribute})
139 server.AddInterfaceTag(userauth, interface_id, attribute, value)
141 def delete_node(self):
142 # uses the right auth as far as poss.
144 ownername = self.node_spec['owner']
145 user_spec = self.test_site.locate_user(ownername)
146 test_user = TestUser(self.test_plc, self.test_site, user_spec)
147 auth = test_user.auth()
149 auth = self.test_plc.auth_root()
150 self.test_plc.apiserver.DeleteNode(auth, self.name())
152 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
153 def qemu_local_init(self):
154 "all nodes : init a clean local directory for holding node-dep stuff like iso image..."
155 utils.system("rm -rf {}".format(self.nodedir()))
156 utils.system("mkdir {}".format(self.nodedir()))
157 if not self.is_qemu():
159 return utils.system("rsync -v -a --exclude .svn template-qemu/ {}/"\
160 .format(self.nodedir())) == 0
163 "all nodes: invoke GetBootMedium and store result locally"
164 utils.header("Calling GetBootMedium for {}".format(self.name()))
165 # this would clearly belong in the config but, well ..
166 options = self.node_spec['bootmedium_options'] if 'bootmedium_options' in self.node_spec else []
167 encoded = self.test_plc.apiserver.GetBootMedium(
168 self.test_plc.auth_root(), self.name(), 'node-iso', '', options)
170 raise Exception('GetBootmedium failed')
172 filename = "{}/{}.iso".format(self.nodedir(), self.name())
173 utils.header('Storing boot medium into {}'.format(filename))
175 # xxx discovered with python3, but a long stading issue:
176 # encoded at this point is a str instead of a bytes
177 # Quick & dirty : we convert this explicitly to a bytearray
178 # Longer run : clearly it seems like the plcapi server side should
179 # tag its result with <base64></base64> rather than as a string
180 bencoded = str.encode(encoded)
182 print("Dry_run: skipped writing of iso image")
185 # with python3 we need to call decodestring here
186 with open(filename,'wb') as storage:
187 storage.write(base64.decodestring(bencoded))
190 def nodestate_reinstall(self):
191 "all nodes: mark PLCAPI boot_state as reinstall"
192 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
193 self.name(),{'boot_state':'reinstall'})
196 def nodestate_upgrade(self):
197 "all nodes: mark PLCAPI boot_state as upgrade"
198 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
199 self.name(),{'boot_state':'upgrade'})
202 def nodestate_safeboot(self):
203 "all nodes: mark PLCAPI boot_state as safeboot"
204 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
205 self.name(),{'boot_state':'safeboot'})
208 def nodestate_boot(self):
209 "all nodes: mark PLCAPI boot_state as boot"
210 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
211 self.name(),{'boot_state':'boot'})
214 def nodestate_show(self):
215 "all nodes: show PLCAPI boot_state"
217 print("Dry_run: skipped getting current node state")
219 state = self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), self.name(), ['boot_state'])[0]['boot_state']
220 print("boot_state for {} : {}".format(self.name(), state))
223 def nodedistro_f14(self):
224 return self.nodedistro_set('f14', 'onelab')
225 def nodedistro_f18(self):
226 return self.nodedistro_set('f18', 'lxc')
227 def nodedistro_f20(self):
228 return self.nodedistro_set('f20', 'lxc')
229 def nodedistro_f21(self):
230 return self.nodedistro_set('f21', 'lxc')
231 def nodedistro_f22(self):
232 return self.nodedistro_set('f22', 'lxc')
233 def nodedistro_set(self, fcdistro, pldistro):
234 "set the fcdistro tag to distro, passed in arg"
235 self.test_plc.apiserver.SetNodeFcdistro(self.test_plc.auth_root(),
236 self.name(), fcdistro)
237 self.test_plc.apiserver.SetNodePldistro(self.test_plc.auth_root(),
238 self.name(), pldistro)
240 def nodedistro_show(self):
241 "display the fcdistro tag - or flavour actually - of node"
243 print("Dry_run: would fetch node flavour")
245 flavour = self.test_plc.apiserver.GetNodeFlavour(self.test_plc.auth_root(),
247 print("Flavour for {} : {}".format(self.name(), flavour))
250 def qemu_local_config(self):
251 "all nodes: compute qemu config qemu.conf and store it locally"
252 if not self.is_qemu():
254 mac = self.node_spec['interface_fields']['mac']
255 hostname = self.node_spec['node_fields']['hostname']
256 ip = self.node_spec['interface_fields']['ip']
257 auth = self.test_plc.auth_root()
258 target_arch = self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
259 conf_filename = "{}/qemu.conf".format(self.nodedir())
261 print("dry_run: skipped actual storage of qemu.conf")
263 utils.header('Storing qemu config for {} in {}'.format(self.name(), conf_filename))
264 with open(conf_filename,'w') as file:
265 file.write('MACADDR={}\n'.format(mac))
266 file.write('NODE_ISO={}.iso\n'.format(self.name()))
267 file.write('HOSTNAME={}\n'.format(hostname))
268 file.write('IP={}\n'.format(ip))
269 file.write('TARGET_ARCH={}\n'.format(target_arch))
272 def qemu_clean(self):
273 utils.header("Cleaning up qemu for host {} on box {}"\
274 .format(self.name(),self.test_box().hostname()))
275 dry_run = self.dry_run()
276 self.test_box().rmdir(self.nodedir(), dry_run=dry_run)
279 def qemu_export(self):
280 "all nodes: push local node-dep directory on the qemu box"
281 # if relevant, push the qemu area onto the host box
282 if self.test_box().is_local():
284 dry_run = self.dry_run()
285 utils.header("Cleaning any former sequel of {} on {}"\
286 .format(self.name(), self.host_box()))
287 utils.header("Transferring configuration files for node {} onto {}"\
288 .format(self.name(), self.host_box()))
289 return self.test_box().copy(self.nodedir(), recursive=True, dry_run=dry_run) == 0
291 def qemu_cleanlog(self):
292 "rename log.txt into log.txt.bak in the qemu dir"
293 test_box = self.test_box()
294 test_box.run_in_buildname("cd {}; mv -f log.txt log.txt.bak".
295 format(self.nodedir()), dry_run=self.dry_run())
298 def qemu_start(self):
299 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
300 model = self.node_spec['node_fields']['model']
301 #starting the Qemu nodes before
305 utils.header("TestNode.qemu_start : {} model {} taken as real node"\
306 .format(self.name(), model))
309 def qemu_timestamp(self):
310 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
311 test_box = self.test_box()
312 test_box.run_in_buildname("mkdir -p {}".format(self.nodedir()), dry_run=self.dry_run())
313 now = int(time.time())
314 return test_box.run_in_buildname("echo {:d} > {}/timestamp"\
315 .format(now, self.nodedir()), dry_run=self.dry_run()) == 0
317 def qemu_nodefamily(self):
318 "write nodefamily stamp in qemu working dir"
319 auth = self.test_plc.auth_root()
320 hostname = self.node_spec['node_fields']['hostname']
321 nodeflavour = self.test_plc.apiserver.GetNodeFlavour(auth, hostname)
324 nodedir = self.nodedir()
325 nodefamily = nodeflavour['nodefamily']
326 self.test_box().run_in_buildname("echo {nodefamily} > {nodedir}/nodefamily".format(**locals()))
329 def start_qemu(self):
330 test_box = self.test_box()
331 utils.header("Starting qemu node {} on {}".format(self.name(), test_box.hostname()))
333 test_box.run_in_buildname("{}/qemu-bridge-init start >> {}/log.txt"\
334 .format(self.nodedir(), self.nodedir()),
335 dry_run=self.dry_run())
336 # kick it off in background, as it would otherwise hang
337 test_box.run_in_buildname("{}/qemu-start-node 2>&1 >> {}/log.txt"\
338 .format(self.nodedir(), self.nodedir()))
341 utils.header("Listing qemu for host {} on box {}"\
342 .format(self.name(), self.test_box().hostname()))
343 command = "{}/qemu-kill-node -l {}".format(self.nodedir(), self.name())
344 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
348 #Prepare the log file before killing the nodes
349 test_box = self.test_box()
350 # kill the right processes
351 utils.header("Stopping qemu for node {} on box {}"\
352 .format(self.name(), self.test_box().hostname()))
353 command = "{}/qemu-kill-node {}".format(self.nodedir(),self.name())
354 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
357 def gather_qemu_logs(self):
358 if not self.is_qemu():
360 remote_log = "{}/log.txt".format(self.nodedir())
361 local_log = "logs/node.qemu.{}.txt".format(self.name())
362 self.test_box().test_ssh.fetch(remote_log,local_log,dry_run=self.dry_run())
364 def keys_clear_known_hosts(self):
365 "remove test nodes entries from the local known_hosts file"
366 TestSsh(self.name()).clear_known_hosts()
369 def create_test_ssh(self):
370 # get the plc's keys for entering the node
371 vservername = self.test_plc.vservername
372 ### # assuming we've run testplc.fetch_keys()
373 ### key = "keys/{vservername}.rsa".format(**locals())
374 # fetch_keys doesn't grab the root key anymore
375 key = "keys/key_admin.rsa"
376 return TestSsh(self.name(), buildname=self.buildname(), key=key)
378 def check_hooks(self):
379 extensions = [ 'py','pl','sh' ]
381 scripts = utils.locate_hooks_scripts('node '+self.name(), path,extensions)
383 for script in scripts:
384 if not self.check_hooks_script(script):
388 def check_hooks_script(self,local_script):
389 # push the script on the node's root context
390 script_name = os.path.basename(local_script)
391 utils.header("NODE hook {} ({})".format(script_name, self.name()))
392 test_ssh = self.create_test_ssh()
393 test_ssh.copy_home(local_script)
394 if test_ssh.run("./"+script_name) != 0:
395 utils.header("WARNING: node hooks check script {} FAILED (ignored)"\
396 .format(script_name))
400 utils.header("SUCCESS: node hook {} OK".format(script_name))
403 def has_libvirt(self):
404 test_ssh = self.create_test_ssh()
405 return test_ssh.run("rpm -q --quiet libvirt-client") == 0
407 def _check_system_slice(self, slicename, dry_run=False):
408 sitename = self.test_plc.plc_spec['settings']['PLC_SLICE_PREFIX']
409 vservername = "{}_{}".format(sitename, slicename)
410 test_ssh = self.create_test_ssh()
411 if self.has_libvirt():
412 utils.header("Checking system slice {} using virsh".format(slicename))
413 return test_ssh.run("virsh --connect lxc:// list | grep -q ' {} '".format(vservername),
414 dry_run = dry_run) == 0
417 utils.output_of(test_ssh.actual_command("cat /vservers/{}/etc/slicefamily 2> /dev/null")\
418 .format(vservername))
419 # get last line only as ssh pollutes the output
420 slicefamily = output.split("\n")[-1]
421 utils.header("Found slicefamily '{}'for slice {}".format(slicefamily,slicename))
424 utils.header("Checking system slice {} using vserver-stat".format(slicename))
425 return test_ssh.run("vserver-stat | grep {}".format(vservername), dry_run=dry_run) == 0