1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA
4 import sys, os, os.path, time, base64
8 from TestUser import TestUser
9 from TestBoxQemu import TestBoxQemu
10 from TestSsh import TestSsh
14 def __init__ (self,test_plc,test_site,node_spec):
15 self.test_plc=test_plc
16 self.test_site=test_site
17 self.node_spec=node_spec
20 return self.node_spec['node_fields']['hostname']
23 def is_qemu_model (model):
24 return model.find("qemu") >= 0
26 return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
29 def is_real_model (model):
30 return not TestNode.is_qemu_model(model)
32 return TestNode.is_real_model (self.node_spec['node_fields']['model'])
35 return self.test_plc.options.buildname
39 return "qemu-%s"%self.name()
41 return "real-%s"%self.name()
43 # this returns a hostname
49 return self.node_spec['host_box']
51 utils.header("WARNING : qemu nodes need a host box")
54 # this returns a TestBoxQemu instance - cached in .test_box_value
57 return self.test_box_value
59 self.test_box_value = TestBoxQemu (self.host_box(),self.buildname())
60 return self.test_box_value
62 def create_node (self):
63 ownername = self.node_spec['owner']
64 user_spec = self.test_site.locate_user(ownername)
65 test_user = TestUser(self.test_plc,self.test_site,user_spec)
66 userauth = test_user.auth()
67 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
68 rootauth=self.test_plc.auth_root()
69 server = self.test_plc.apiserver
70 server.AddNode(userauth,
71 self.test_site.site_spec['site_fields']['login_base'],
72 self.node_spec['node_fields'])
73 server.SetNodePlainBootstrapfs(userauth,
74 self.node_spec['node_fields']['hostname'],
76 # create as reinstall to avoid user confirmation
77 server.UpdateNode(userauth, self.name(), {'boot_state':'reinstall'})
78 # populate network interfaces - primary
79 server.AddInterface(userauth,self.name(),
80 self.node_spec['interface_fields'])
81 # populate network interfaces - others
82 if self.node_spec.has_key('extra_interfaces'):
83 for interface in self.node_spec['extra_interfaces']:
84 server.AddInterface(userauth,self.name(), interface['interface_fields'])
85 if interface.has_key('settings'):
86 for (attribute,value) in interface['settings'].iteritems():
88 interface = server.GetInterfaces(userauth,{'ip':interface['interface_fields']['ip']})[0]
89 interface_id=interface['interface_id']
90 # locate or create node network attribute type
92 interface_tagtype = server.GetTagTypes(userauth,{'name':attribute})[0]
94 interface_tagtype = server.AddTagType(rootauth,{'category':'test',
97 server.AddInterfaceTag(userauth,interface_id,attribute,value)
99 def delete_node (self):
100 # uses the right auth as far as poss.
102 ownername = self.node_spec['owner']
103 user_spec = self.test_site.locate_user(ownername)
104 test_user = TestUser(self.test_plc,self.test_site,user_spec)
105 auth = test_user.auth()
107 auth=self.test_plc.auth_root()
108 self.test_plc.apiserver.DeleteNode(auth,self.name())
110 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
111 def qemu_local_init(self):
112 utils.system("rm -rf %s"%self.nodedir())
113 utils.system("mkdir %s"%self.nodedir())
114 if not self.is_qemu():
116 return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
119 utils.header("Calling GetBootMedium for %s"%self.name())
122 options.append('serial')
123 options.append('no-hangcheck')
124 encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(),
125 self.name(), 'node-iso', '', options)
127 raise Exception, 'GetBootmedium failed'
129 filename="%s/%s.iso"%(self.nodedir(),self.name())
130 utils.header('Storing boot medium into %s'%filename)
131 if self.test_plc.options.dry_run:
132 print "Dry_run: skipped writing of iso image"
135 file(filename,'w').write(base64.b64decode(encoded))
138 def nodestate_reinstall (self):
139 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
140 self.name(),{'boot_state':'reinstall'})
143 def nodestate_safeboot (self):
144 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
145 self.name(),{'boot_state':'safeboot'})
148 def nodestate_boot (self):
149 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
150 self.name(),{'boot_state':'boot'})
153 def nodestate_show (self):
154 if self.test_plc.options.dry_run:
155 print "Dry_run: skipped getting current node state"
157 state=self.test_plc.apiserver.GetNodes(self.test_plc.auth_root(), self.name(), ['boot_state'])[0]['boot_state']
158 print self.name(),':',state
161 def qemu_local_config(self):
162 if not self.is_qemu():
164 mac=self.node_spec['interface_fields']['mac']
165 hostname=self.node_spec['node_fields']['hostname']
166 ip=self.node_spec['interface_fields']['ip']
167 auth=self.test_plc.auth_root()
168 target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
169 conf_filename="%s/qemu.conf"%(self.nodedir())
170 if self.test_plc.options.dry_run:
171 print "dry_run: skipped actual storage of qemu.conf"
173 utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
174 file=open(conf_filename,'w')
175 file.write('MACADDR=%s\n'%mac)
176 file.write('NODE_ISO=%s.iso\n'%self.name())
177 file.write('HOSTNAME=%s\n'%hostname)
178 file.write('IP=%s\n'%ip)
179 file.write('TARGET_ARCH=%s\n'%target_arch)
183 def qemu_export (self):
184 # if relevant, push the qemu area onto the host box
185 if self.test_box().is_local():
187 utils.header ("Cleaning any former sequel of %s on %s"%(self.name(),self.host_box()))
188 self.test_box().run_in_buildname("rm -rf %s"%self.nodedir())
189 utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
190 return self.test_box().copy(self.nodedir(),recursive=True)==0
192 def qemu_start (self):
193 model=self.node_spec['node_fields']['model']
194 #starting the Qemu nodes before
198 utils.header("TestNode.qemu_start : %s model %s taken as real node"%(self.name(),model))
201 def timestamp_qemu (self):
202 test_box = self.test_box()
203 test_box.run_in_buildname("mkdir -p %s"%self.nodedir())
205 return test_box.run_in_buildname("echo %d > %s/timestamp"%(now,self.nodedir()))==0
207 def start_qemu (self):
208 test_box = self.test_box()
209 utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
211 test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/log.txt"%(self.nodedir(),self.nodedir()))
212 # kick it off in background, as it would otherwise hang
213 test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/log.txt"%(self.nodedir(),self.nodedir()))
215 def list_qemu (self):
216 utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
217 command="%s/qemu-kill-node -l %s"%(self.nodedir(),self.name())
218 self.test_box().run_in_buildname(command)
221 def kill_qemu (self):
222 #Prepare the log file before killing the nodes
223 test_box = self.test_box()
224 # kill the right processes
225 utils.header("Stopping qemu for node %s on box %s"%(self.name(),self.test_box().hostname()))
226 command="%s/qemu-kill-node %s"%(self.nodedir(),self.name())
227 self.test_box().run_in_buildname(command)
230 def gather_qemu_logs (self):
231 if not self.is_qemu():
233 remote_log="%s/log.txt"%self.nodedir()
234 local_log="logs/node.qemu.%s.txt"%self.name()
235 self.test_box().test_ssh.fetch(remote_log,local_log)
237 def keys_clear_known_hosts (self):
238 TestSsh(self.name()).clear_known_hosts()
241 def create_test_ssh(self):
242 # get the plc's keys for entering the node
243 vservername=self.test_plc.vservername
244 ### # assuming we've run testplc.fetch_keys()
245 ### key = "keys/%(vservername)s.rsa"%locals()
246 # fetch_keys doesn't grab the root key anymore
247 key = "keys/key1.rsa"
248 return TestSsh(self.name(), buildname=self.buildname(), key=key)
250 def check_hooks (self):
251 extensions = [ 'py','pl','sh' ]
253 scripts=utils.locate_hooks_scripts ('node '+self.name(), path,extensions)
255 for script in scripts:
256 if not self.check_hooks_script (script):
260 def check_hooks_script (self,local_script):
261 # push the script on the node's root context
262 script_name=os.path.basename(local_script)
263 utils.header ("NODE hook %s (%s)"%(script_name,self.name()))
264 test_ssh=self.create_test_ssh()
265 test_ssh.copy_home(local_script)
266 if test_ssh.run("./"+script_name) != 0:
267 utils.header ("WARNING: node hooks check script %s FAILED (ignored)"%script_name)
271 utils.header ("SUCCESS: node hook %s OK"%script_name)
274 def check_systemslice (self, slicename):
275 sitename=self.test_plc.plc_spec['PLC_SLICE_PREFIX']
276 vservername="%s_%s"%(sitename,slicename)
277 test_ssh=self.create_test_ssh()
278 (retcod,output)=utils.output_of(test_ssh.actual_command("cat /vservers/%s/etc/slicefamily")%vservername)
281 # get last line only as ssh pollutes the output
282 slicefamily=output.split("\n")[-1]
283 utils.header("system slice %s has slicefamily %s"%(slicename, slicefamily))
284 return test_ssh.run("vserver-stat | grep %s"%vservername)==0