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(self.name(),':',state)
223 def qemu_local_config(self):
224 "all nodes: compute qemu config qemu.conf and store it locally"
225 if not self.is_qemu():
227 mac = self.node_spec['interface_fields']['mac']
228 hostname = self.node_spec['node_fields']['hostname']
229 ip = self.node_spec['interface_fields']['ip']
230 auth = self.test_plc.auth_root()
231 target_arch = self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
232 conf_filename = "{}/qemu.conf".format(self.nodedir())
234 print("dry_run: skipped actual storage of qemu.conf")
236 utils.header('Storing qemu config for {} in {}'.format(self.name(), conf_filename))
237 with open(conf_filename,'w') as file:
238 file.write('MACADDR={}\n'.format(mac))
239 file.write('NODE_ISO={}.iso\n'.format(self.name()))
240 file.write('HOSTNAME={}\n'.format(hostname))
241 file.write('IP={}\n'.format(ip))
242 file.write('TARGET_ARCH={}\n'.format(target_arch))
245 def qemu_clean(self):
246 utils.header("Cleaning up qemu for host {} on box {}"\
247 .format(self.name(),self.test_box().hostname()))
248 dry_run = self.dry_run()
249 self.test_box().rmdir(self.nodedir(), dry_run=dry_run)
252 def qemu_export(self):
253 "all nodes: push local node-dep directory on the qemu box"
254 # if relevant, push the qemu area onto the host box
255 if self.test_box().is_local():
257 dry_run = self.dry_run()
258 utils.header("Cleaning any former sequel of {} on {}"\
259 .format(self.name(), self.host_box()))
260 utils.header("Transferring configuration files for node {} onto {}"\
261 .format(self.name(), self.host_box()))
262 return self.test_box().copy(self.nodedir(), recursive=True, dry_run=dry_run) == 0
264 def qemu_start(self):
265 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
266 model = self.node_spec['node_fields']['model']
267 #starting the Qemu nodes before
271 utils.header("TestNode.qemu_start : {} model {} taken as real node"\
272 .format(self.name(), model))
275 def qemu_timestamp(self):
276 "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
277 test_box = self.test_box()
278 test_box.run_in_buildname("mkdir -p {}".format(self.nodedir()), dry_run=self.dry_run())
279 now = int(time.time())
280 return test_box.run_in_buildname("echo {:d} > {}/timestamp"\
281 .format(now, self.nodedir()), dry_run=self.dry_run()) == 0
283 def qemu_nodefamily(self):
284 auth = self.test_plc.auth_root()
285 hostname = self.node_spec['node_fields']['hostname']
286 nodeflavour = self.test_plc.apiserver.GetNodeFlavour(auth, hostname)
289 nodedir = self.nodedir()
290 nodefamily = nodeflavour['nodefamily']
291 self.test_box().run_in_buildname("echo {nodefamily} > {nodedir}/nodefamily".format(**locals()))
294 def start_qemu(self):
295 test_box = self.test_box()
296 utils.header("Starting qemu node {} on {}".format(self.name(), test_box.hostname()))
298 test_box.run_in_buildname("{}/qemu-bridge-init start >> {}/log.txt"\
299 .format(self.nodedir(), self.nodedir()),
300 dry_run=self.dry_run())
301 # kick it off in background, as it would otherwise hang
302 test_box.run_in_buildname("{}/qemu-start-node 2>&1 >> {}/log.txt"\
303 .format(self.nodedir(), self.nodedir()))
306 utils.header("Listing qemu for host {} on box {}"\
307 .format(self.name(), self.test_box().hostname()))
308 command = "{}/qemu-kill-node -l {}".format(self.nodedir(), self.name())
309 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
313 #Prepare the log file before killing the nodes
314 test_box = self.test_box()
315 # kill the right processes
316 utils.header("Stopping qemu for node {} on box {}"\
317 .format(self.name(), self.test_box().hostname()))
318 command = "{}/qemu-kill-node {}".format(self.nodedir(),self.name())
319 self.test_box().run_in_buildname(command, dry_run=self.dry_run())
322 def gather_qemu_logs(self):
323 if not self.is_qemu():
325 remote_log = "{}/log.txt".format(self.nodedir())
326 local_log = "logs/node.qemu.{}.txt".format(self.name())
327 self.test_box().test_ssh.fetch(remote_log,local_log,dry_run=self.dry_run())
329 def keys_clear_known_hosts(self):
330 "remove test nodes entries from the local known_hosts file"
331 TestSsh(self.name()).clear_known_hosts()
334 def create_test_ssh(self):
335 # get the plc's keys for entering the node
336 vservername = self.test_plc.vservername
337 ### # assuming we've run testplc.fetch_keys()
338 ### key = "keys/{vservername}.rsa".format(**locals())
339 # fetch_keys doesn't grab the root key anymore
340 key = "keys/key_admin.rsa"
341 return TestSsh(self.name(), buildname=self.buildname(), key=key)
343 def check_hooks(self):
344 extensions = [ 'py','pl','sh' ]
346 scripts = utils.locate_hooks_scripts('node '+self.name(), path,extensions)
348 for script in scripts:
349 if not self.check_hooks_script(script):
353 def check_hooks_script(self,local_script):
354 # push the script on the node's root context
355 script_name = os.path.basename(local_script)
356 utils.header("NODE hook {} ({})".format(script_name, self.name()))
357 test_ssh = self.create_test_ssh()
358 test_ssh.copy_home(local_script)
359 if test_ssh.run("./"+script_name) != 0:
360 utils.header("WARNING: node hooks check script {} FAILED (ignored)"\
361 .format(script_name))
365 utils.header("SUCCESS: node hook {} OK".format(script_name))
368 def has_libvirt(self):
369 test_ssh = self.create_test_ssh()
370 return test_ssh.run("rpm -q --quiet libvirt-client") == 0
372 def _check_system_slice(self, slicename, dry_run=False):
373 sitename = self.test_plc.plc_spec['settings']['PLC_SLICE_PREFIX']
374 vservername = "{}_{}".format(sitename, slicename)
375 test_ssh = self.create_test_ssh()
376 if self.has_libvirt():
377 utils.header("Checking system slice {} using virsh".format(slicename))
378 return test_ssh.run("virsh --connect lxc:// list | grep -q ' {} '".format(vservername),
379 dry_run = dry_run) == 0
382 utils.output_of(test_ssh.actual_command("cat /vservers/{}/etc/slicefamily 2> /dev/null")\
383 .format(vservername))
384 # get last line only as ssh pollutes the output
385 slicefamily = output.split("\n")[-1]
386 utils.header("Found slicefamily '{}'for slice {}".format(slicefamily,slicename))
389 utils.header("Checking system slice {} using vserver-stat".format(slicename))
390 return test_ssh.run("vserver-stat | grep {}".format(vservername), dry_run=dry_run) == 0