1 import sys, os, os.path, time, base64
5 from TestUser import TestUser
6 from TestBox import TestBox
7 from TestSsh import TestSsh
11 def __init__ (self,test_plc,test_site,node_spec):
12 self.test_plc=test_plc
13 self.test_site=test_site
14 self.node_spec=node_spec
17 return self.node_spec['node_fields']['hostname']
20 def is_qemu_model (model):
21 return model.find("qemu") >= 0
23 return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
26 def is_real_model (model):
27 return not TestNode.is_qemu_model(model)
29 return TestNode.is_real_model (self.node_spec['node_fields']['model'])
32 return self.test_plc.options.buildname
36 return "qemu-%s"%self.name()
38 return "real-%s"%self.name()
40 # this returns a hostname
46 return self.node_spec['host_box']
48 utils.header("WARNING : qemu nodes need a host box")
51 # this returns a TestBox instance - cached in .test_box_value
54 return self.test_box_value
56 self.test_box_value = TestBox (self.host_box(),self.buildname())
57 return self.test_box_value
59 def create_node (self):
60 ownername = self.node_spec['owner']
61 user_spec = self.test_site.locate_user(ownername)
62 test_user = TestUser(self.test_plc,self.test_site,user_spec)
63 userauth = test_user.auth()
64 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
65 rootauth=self.test_plc.auth_root()
66 server = self.test_plc.apiserver
67 server.AddNode(userauth,
68 self.test_site.site_spec['site_fields']['login_base'],
69 self.node_spec['node_fields'])
70 server.SetNodePlainBootstrapfs(userauth,
71 self.node_spec['node_fields']['hostname'],
73 # create as reinstall to avoid user confirmation
74 server.UpdateNode(userauth, self.name(), {'boot_state':'reinstall'})
75 # populate network interfaces - primary
76 server.AddInterface(userauth,self.name(),
77 self.node_spec['interface_fields'])
78 # populate network interfaces - others
79 if self.node_spec.has_key('extra_interfaces'):
80 for interface in self.node_spec['extra_interfaces']:
81 server.AddInterface(userauth,self.name(), interface['interface_fields'])
82 if interface.has_key('settings'):
83 for (attribute,value) in interface['settings'].iteritems():
85 interface = server.GetInterfaces(userauth,{'ip':interface['interface_fields']['ip']})[0]
86 interface_id=interface['interface_id']
87 # locate or create node network attribute type
89 interface_tagtype = server.GetTagTypes(userauth,{'name':attribute})[0]
91 interface_tagtype = server.AddTagType(rootauth,{'category':'test',
94 server.AddInterfaceTag(userauth,interface_id,attribute,value)
96 def delete_node (self):
97 # uses the right auth as far as poss.
99 ownername = self.node_spec['owner']
100 user_spec = self.test_site.locate_user(ownername)
101 test_user = TestUser(self.test_plc,self.test_site,user_spec)
102 auth = test_user.auth()
104 auth=self.test_plc.auth_root()
105 self.test_plc.apiserver.DeleteNode(auth,self.name())
107 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
109 utils.system("rm -rf %s"%self.nodedir())
110 utils.system("mkdir %s"%self.nodedir())
111 if not self.is_qemu():
113 return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
116 utils.header("Calling GetBootMedium for %s"%self.name())
119 options.append('serial')
120 options.append('no-hangcheck')
121 encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(),
122 self.name(), 'node-iso', '', options)
124 raise Exception, 'GetBootmedium failed'
126 filename="%s/%s.iso"%(self.nodedir(),self.name())
127 utils.header('Storing boot medium into %s'%filename)
128 if self.test_plc.options.dry_run:
129 print "Dry_run: skipped writing of iso image"
132 file(filename,'w').write(base64.b64decode(encoded))
135 def reinstall_node (self):
136 self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
137 self.name(),{'boot_state':'reinstall'})
140 def configure_qemu(self):
141 if not self.is_qemu():
143 mac=self.node_spec['interface_fields']['mac']
144 hostname=self.node_spec['node_fields']['hostname']
145 ip=self.node_spec['interface_fields']['ip']
146 auth=self.test_plc.auth_root()
147 target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
148 conf_filename="%s/qemu.conf"%(self.nodedir())
149 if self.test_plc.options.dry_run:
150 print "dry_run: skipped actual storage of qemu.conf"
152 utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
153 file=open(conf_filename,'w')
154 file.write('MACADDR=%s\n'%mac)
155 file.write('NODE_ISO=%s.iso\n'%self.name())
156 file.write('HOSTNAME=%s\n'%hostname)
157 file.write('IP=%s\n'%ip)
158 file.write('TARGET_ARCH=%s\n'%target_arch)
162 def export_qemu (self):
163 # if relevant, push the qemu area onto the host box
164 if self.test_box().is_local():
166 utils.header ("Cleaning any former sequel of %s on %s"%(self.name(),self.host_box()))
167 self.test_box().run_in_buildname("rm -rf %s"%self.nodedir())
168 utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
169 return self.test_box().copy(self.nodedir(),recursive=True)==0
171 def start_node (self):
172 model=self.node_spec['node_fields']['model']
173 #starting the Qemu nodes before
177 utils.header("TestNode.start_node : %s model %s taken as real node"%(self.name(),model))
180 def start_qemu (self):
181 options = self.test_plc.options
182 test_box = self.test_box()
183 utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
185 test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/log.txt"%(self.nodedir(),self.nodedir()))
186 # kick it off in background, as it would otherwise hang
187 test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/log.txt"%(self.nodedir(),self.nodedir()))
189 def list_qemu (self):
190 utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
191 command="%s/qemu-kill-node -l %s"%(self.nodedir(),self.name())
192 self.test_box().run_in_buildname(command)
195 def kill_qemu (self):
196 #Prepare the log file before killing the nodes
197 test_box = self.test_box()
198 # kill the right processes
199 utils.header("Stopping qemu for node %s on box %s"%(self.name(),self.test_box().hostname()))
200 command="%s/qemu-kill-node %s"%(self.nodedir(),self.name())
201 self.test_box().run_in_buildname(command)
204 def gather_qemu_logs (self):
205 if not self.is_qemu():
207 remote_log="%s/log.txt"%self.nodedir()
208 local_log="logs/node.qemu.%s.log"%self.name()
209 self.test_box().test_ssh.fetch(remote_log,local_log)
211 def clear_known_hosts (self):
212 TestSsh(self.name()).clear_known_hosts()
215 def create_test_ssh(self):
216 # get the plc's keys for entering the node
217 vservername=self.test_plc.vservername
218 # assuming we've run testplc.fetch_keys()
219 key = "keys/%(vservername)s.rsa"%locals()
220 return TestSsh(self.name(), buildname=self.buildname(), key=key)
222 def check_hooks (self):
223 extensions = [ 'py','pl','sh' ]
225 scripts=utils.locate_hooks_scripts ('node '+self.name(), path,extensions)
227 for script in scripts:
228 if not self.check_hooks_script (script):
232 def check_hooks_script (self,local_script):
233 # push the script on the node's root context
234 script_name=os.path.basename(local_script)
235 utils.header ("NODE hook %s (%s)"%(script_name,self.name()))
236 test_ssh=self.create_test_ssh()
237 test_ssh.copy_home(local_script)
238 if test_ssh.run("./"+script_name) != 0:
239 utils.header ("WARNING: node hooks check script %s FAILED (ignored)"%script_name)
243 utils.header ("SUCCESS: node hook %s OK"%script_name)