new steps to change a node's fcdistro in a bonding environment
[tests.git] / system / TestNode.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import sys
5 import os, os.path
6 import time
7 import base64
8
9 import utils
10 from TestUser import TestUser
11 from TestBoxQemu import TestBoxQemu
12 from TestSsh import TestSsh
13 from Completer import CompleterTask
14
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)
29         if self.expected:
30             return retcod == 0
31         else:
32             return retcod != 0
33     def failure_epilogue(self):
34         print("Cannot reach {} in {} mode".format(self.hostname, self.boot_state))
35
36 class TestNode:
37
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
42         
43     def name(self):
44         return self.node_spec['node_fields']['hostname']
45     
46     def dry_run(self):
47         return self.test_plc.options.dry_run
48
49     @staticmethod
50     def is_qemu_model(model):
51         return model.find("qemu") >= 0
52     def is_qemu(self):
53         return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
54
55     @staticmethod
56     def is_real_model(model):
57         return not TestNode.is_qemu_model(model)
58     def is_real(self):
59         return TestNode.is_real_model(self.node_spec['node_fields']['model'])
60
61     def buildname(self):
62         return self.test_plc.options.buildname
63         
64     def nodedir(self):
65         if self.is_qemu():
66             return "qemu-{}".format(self.name())
67         else:
68             return "real-{}".format(self.name())
69
70     # this returns a hostname
71     def host_box(self):
72         if self.is_real():
73             return 'localhost'
74         else:
75             try:
76                 return self.node_spec['host_box']
77             except:
78                 utils.header("WARNING : qemu nodes need a host box")
79                 return 'localhost'
80
81     # this returns a TestBoxQemu instance - cached in .test_box_value
82     def test_box(self):
83         try:
84             return self.test_box_value
85         except:
86             self.test_box_value = TestBoxQemu(self.host_box(),self.buildname())
87             return self.test_box_value
88
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' })
102
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)
106             
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'])
112         else:
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'])
121             pass
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
133                         try:
134                             interface_tagtype = server.GetTagTypes(userauth, {'name' : attribute})[0]
135                         except:
136                             interface_tagtype = server.AddTagType(rootauth,{'category' : 'test',
137                                                                             'tagname' : attribute})
138                         # attach value
139                         server.AddInterfaceTag(userauth, interface_id, attribute, value)
140
141     def delete_node(self):
142         # uses the right auth as far as poss.
143         try:
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()
148         except:
149             auth = self.test_plc.auth_root()
150         self.test_plc.apiserver.DeleteNode(auth, self.name())
151
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():
158             return True
159         return utils.system("rsync -v -a --exclude .svn template-qemu/ {}/"\
160                             .format(self.nodedir())) == 0
161
162     def bootcd(self):
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)
169         if encoded == '':
170             raise Exception('GetBootmedium failed')
171
172         filename = "{}/{}.iso".format(self.nodedir(), self.name())
173         utils.header('Storing boot medium into {}'.format(filename))
174
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)
181         if self.dry_run():
182             print("Dry_run: skipped writing of iso image")
183             return True
184         else:
185             # with python3 we need to call decodestring here
186             with open(filename,'wb') as storage:
187                 storage.write(base64.decodestring(bencoded))
188             return True
189
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'})
194         return True
195     
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'})
200         return True
201     
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'})
206         return True
207     
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'})
212         return True
213
214     def nodestate_show(self):
215         "all nodes: show PLCAPI boot_state"
216         if self.dry_run():
217             print("Dry_run: skipped getting current node state")
218             return True
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))
221         return True
222     
223     def nodefcdistro_f14(self):
224         return self.nodefcdistro_set('f14')
225     def nodefcdistro_f18(self):
226         return self.nodefcdistro_set('f18')
227     def nodefcdistro_f20(self):
228         return self.nodefcdistro_set('f20')
229     def nodefcdistro_f21(self):
230         return self.nodefcdistro_set('f21')
231     def nodefcdistro_set(self, distro):
232         "set the fcdistro tag to distro, passed in arg"
233         self.test_plc.apiserver.SetNodeFcdistro(self.test_plc.auth_root(),
234                                                self.name(), distro)
235         return True
236
237     def nodefcdistro_show(self):
238         "display the fcdistro tag - or flavour actually - of node"
239         if self.dry_run():
240             print("Dry_run: would fetch node flavour")
241             return True
242         flavour = self.test_plc.apiserver.GetNodeFlavour(self.test_plc.auth_root(),
243                                                          self.name())
244         print("Flavour for {} : {}".format(self.name(), flavour))
245         return True
246
247     def qemu_local_config(self):
248         "all nodes: compute qemu config qemu.conf and store it locally"
249         if not self.is_qemu():
250             return
251         mac = self.node_spec['interface_fields']['mac']
252         hostname = self.node_spec['node_fields']['hostname']
253         ip = self.node_spec['interface_fields']['ip']
254         auth = self.test_plc.auth_root()
255         target_arch = self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
256         conf_filename = "{}/qemu.conf".format(self.nodedir())
257         if self.dry_run():
258             print("dry_run: skipped actual storage of qemu.conf")
259             return True
260         utils.header('Storing qemu config for {} in {}'.format(self.name(), conf_filename))
261         with open(conf_filename,'w') as file:
262             file.write('MACADDR={}\n'.format(mac))
263             file.write('NODE_ISO={}.iso\n'.format(self.name()))
264             file.write('HOSTNAME={}\n'.format(hostname))
265             file.write('IP={}\n'.format(ip))
266             file.write('TARGET_ARCH={}\n'.format(target_arch))
267         return True
268
269     def qemu_clean(self):
270         utils.header("Cleaning up qemu for host {} on box {}"\
271                      .format(self.name(),self.test_box().hostname()))
272         dry_run = self.dry_run()
273         self.test_box().rmdir(self.nodedir(), dry_run=dry_run)
274         return True
275
276     def qemu_export(self):
277         "all nodes: push local node-dep directory on the qemu box"
278         # if relevant, push the qemu area onto the host box
279         if self.test_box().is_local():
280             return True
281         dry_run = self.dry_run()
282         utils.header("Cleaning any former sequel of {} on {}"\
283                      .format(self.name(), self.host_box()))
284         utils.header("Transferring configuration files for node {} onto {}"\
285                      .format(self.name(), self.host_box()))
286         return self.test_box().copy(self.nodedir(), recursive=True, dry_run=dry_run) == 0
287             
288     def qemu_start(self):
289         "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
290         model = self.node_spec['node_fields']['model']
291         #starting the Qemu nodes before 
292         if self.is_qemu():
293             self.start_qemu()
294         else:
295             utils.header("TestNode.qemu_start : {} model {} taken as real node"\
296                          .format(self.name(), model))
297         return True
298
299     def qemu_timestamp(self):
300         "all nodes: start the qemu instance (also runs qemu-bridge-init start)"
301         test_box = self.test_box()
302         test_box.run_in_buildname("mkdir -p {}".format(self.nodedir()), dry_run=self.dry_run())
303         now = int(time.time())
304         return test_box.run_in_buildname("echo {:d} > {}/timestamp"\
305                                          .format(now, self.nodedir()), dry_run=self.dry_run()) == 0
306
307     def qemu_nodefamily(self):
308         auth = self.test_plc.auth_root()
309         hostname = self.node_spec['node_fields']['hostname']
310         nodeflavour = self.test_plc.apiserver.GetNodeFlavour(auth, hostname)
311         if self.dry_run():
312             return True
313         nodedir = self.nodedir()
314         nodefamily = nodeflavour['nodefamily']
315         self.test_box().run_in_buildname("echo {nodefamily} > {nodedir}/nodefamily".format(**locals()))
316         return True
317
318     def start_qemu(self):
319         test_box = self.test_box()
320         utils.header("Starting qemu node {} on {}".format(self.name(), test_box.hostname()))
321
322         test_box.run_in_buildname("{}/qemu-bridge-init start >> {}/log.txt"\
323                                   .format(self.nodedir(), self.nodedir()),
324                                   dry_run=self.dry_run())
325         # kick it off in background, as it would otherwise hang
326         test_box.run_in_buildname("{}/qemu-start-node 2>&1 >> {}/log.txt"\
327                                   .format(self.nodedir(), self.nodedir()))
328
329     def list_qemu(self):
330         utils.header("Listing qemu for host {} on box {}"\
331                      .format(self.name(), self.test_box().hostname()))
332         command = "{}/qemu-kill-node -l {}".format(self.nodedir(), self.name())
333         self.test_box().run_in_buildname(command, dry_run=self.dry_run())
334         return True
335
336     def kill_qemu(self):
337         #Prepare the log file before killing the nodes
338         test_box = self.test_box()
339         # kill the right processes 
340         utils.header("Stopping qemu for node {} on box {}"\
341                      .format(self.name(), self.test_box().hostname()))
342         command = "{}/qemu-kill-node {}".format(self.nodedir(),self.name())
343         self.test_box().run_in_buildname(command, dry_run=self.dry_run())
344         return True
345
346     def gather_qemu_logs(self):
347         if not self.is_qemu():
348             return True
349         remote_log = "{}/log.txt".format(self.nodedir())
350         local_log = "logs/node.qemu.{}.txt".format(self.name())
351         self.test_box().test_ssh.fetch(remote_log,local_log,dry_run=self.dry_run())
352
353     def keys_clear_known_hosts(self):
354         "remove test nodes entries from the local known_hosts file"
355         TestSsh(self.name()).clear_known_hosts()
356         return True
357
358     def create_test_ssh(self):
359         # get the plc's keys for entering the node
360         vservername = self.test_plc.vservername
361 ###        # assuming we've run testplc.fetch_keys()
362 ###        key = "keys/{vservername}.rsa".format(**locals())
363         # fetch_keys doesn't grab the root key anymore
364         key = "keys/key_admin.rsa"
365         return TestSsh(self.name(), buildname=self.buildname(), key=key)
366
367     def check_hooks(self):
368         extensions = [ 'py','pl','sh' ]
369         path = 'hooks/node'
370         scripts = utils.locate_hooks_scripts('node '+self.name(), path,extensions)
371         overall = True
372         for script in scripts:
373             if not self.check_hooks_script(script):
374                 overall = False
375         return overall
376
377     def check_hooks_script(self,local_script):
378         # push the script on the node's root context
379         script_name = os.path.basename(local_script)
380         utils.header("NODE hook {} ({})".format(script_name, self.name()))
381         test_ssh = self.create_test_ssh()
382         test_ssh.copy_home(local_script)
383         if test_ssh.run("./"+script_name) != 0:
384             utils.header("WARNING: node hooks check script {} FAILED (ignored)"\
385                          .format(script_name))
386             #return False
387             return True
388         else:
389             utils.header("SUCCESS: node hook {} OK".format(script_name))
390             return True
391
392     def has_libvirt(self):
393         test_ssh = self.create_test_ssh()
394         return test_ssh.run("rpm -q --quiet libvirt-client") == 0
395
396     def _check_system_slice(self, slicename, dry_run=False):
397         sitename = self.test_plc.plc_spec['settings']['PLC_SLICE_PREFIX']
398         vservername = "{}_{}".format(sitename, slicename)
399         test_ssh = self.create_test_ssh()
400         if self.has_libvirt():
401             utils.header("Checking system slice {} using virsh".format(slicename))
402             return test_ssh.run("virsh --connect lxc:// list | grep -q ' {} '".format(vservername),
403                                 dry_run = dry_run) == 0
404         else:
405             retcod, output = \
406                     utils.output_of(test_ssh.actual_command("cat /vservers/{}/etc/slicefamily 2> /dev/null")\
407                                     .format(vservername))
408             # get last line only as ssh pollutes the output
409             slicefamily = output.split("\n")[-1]
410             utils.header("Found slicefamily '{}'for slice {}".format(slicefamily,slicename))
411             if retcod != 0: 
412                 return False
413             utils.header("Checking system slice {} using vserver-stat".format(slicename))
414             return test_ssh.run("vserver-stat | grep {}".format(vservername), dry_run=dry_run) == 0
415         
416