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