1 import os, sys, time, base64
5 from TestUser import TestUser
6 from TestBox import TestBox
10 def __init__ (self,test_plc,test_site,node_spec):
11 self.test_plc=test_plc
12 self.test_site=test_site
13 self.node_spec=node_spec
16 return self.node_spec['node_fields']['hostname']
19 def is_qemu_model (model):
20 return model.find("qemu") >= 0
22 return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
25 def is_real_model (model):
26 return not TestNode.is_qemu_model(model)
28 return TestNode.is_real_model (self.node_spec['node_fields']['model'])
31 return self.test_plc.options.buildname
35 return "qemu-%s"%self.name()
37 return "real-%s"%self.name()
39 # this returns a hostname
45 return self.node_spec['host_box']
47 utils.header("WARNING : qemu nodes need a host box")
50 # this returns a TestBox instance - cached in .test_box_value
53 return self.test_box_value
55 self.test_box_value = TestBox (self.host_box(),self.buildname())
56 return self.test_box_value
58 def create_node (self):
59 ownername = self.node_spec['owner']
60 user_spec = self.test_site.locate_user(ownername)
61 test_user = TestUser(self.test_plc,self.test_site,user_spec)
62 userauth = test_user.auth()
63 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
64 rootauth=self.test_plc.auth_root()
65 server = self.test_plc.apiserver
66 server.AddNode(userauth,
67 self.test_site.site_spec['site_fields']['login_base'],
68 self.node_spec['node_fields'])
69 # create as reinstall to avoid user confirmation
70 server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
71 # populate network interfaces - primary
72 server.AddNodeNetwork(userauth,self.name(),
73 self.node_spec['network_fields'])
74 # populate network interfaces - others
75 if self.node_spec.has_key('extra_interfaces'):
76 for interface in self.node_spec['extra_interfaces']:
77 server.AddNodeNetwork(userauth,self.name(),
78 interface['network_fields'])
79 if interface.has_key('settings'):
80 for (attribute,value) in interface['settings'].iteritems():
82 nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
83 nnid=nn['nodenetwork_id']
84 # locate or create node network attribute type
86 nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
88 nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
91 server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
93 def delete_node (self):
94 # uses the right auth as far as poss.
96 ownername = self.node_spec['owner']
97 user_spec = self.test_site.locate_user(ownername)
98 test_user = TestUser(self.test_plc,self.test_site,user_spec)
99 auth = test_user.auth()
101 auth=self.test_plc.auth_root()
102 self.test_plc.apiserver.DeleteNode(auth,self.name())
104 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
106 utils.system("rm -rf %s"%self.nodedir())
107 utils.system("mkdir %s"%self.nodedir())
108 if not self.is_qemu():
110 return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
113 utils.header("Calling GetBootMedium for %s"%self.name())
117 encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(), self.name(), 'node-iso', '', options)
119 raise Exception, 'GetBootmedium failed'
121 filename="%s/%s.iso"%(self.nodedir(),self.name())
122 utils.header('Storing boot medium into %s'%filename)
123 if self.test_plc.options.dry_run:
124 print "Dry_run: skipped writing of iso image"
127 file(filename,'w').write(base64.b64decode(encoded))
130 def configure_qemu(self):
131 if not self.is_qemu():
133 mac=self.node_spec['network_fields']['mac']
134 hostname=self.node_spec['node_fields']['hostname']
135 auth=self.test_plc.auth_root()
136 target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
137 conf_filename="%s/qemu.conf"%(self.nodedir())
138 if self.test_plc.options.dry_run:
139 print "dry_run: skipped actual storage of qemu.conf"
141 utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
142 file=open(conf_filename,'w')
143 file.write('MACADDR=%s\n'%mac)
144 file.write('NODE_ISO=%s.iso\n'%self.name())
145 file.write('HOSTNAME=%s\n'%hostname)
146 file.write('TARGET_ARCH=%s\n'%target_arch)
149 # if relevant, push the qemu area onto the host box
150 if self.test_box().is_local():
152 utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
153 return self.test_box().copy(self.nodedir(),recursive=True)==0
155 def start_node (self,options):
156 model=self.node_spec['node_fields']['model']
157 #starting the Qemu nodes before
159 self.start_qemu(options)
161 utils.header("TestNode.start_node : %s model %s taken as real node"%(self.name(),model))
163 def start_qemu (self, options):
164 test_box = self.test_box()
165 utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
167 test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/qemu.log"%(self.nodedir(),self.nodedir()))
168 # kick it off in background, as it would otherwise hang
169 test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/qemu.log"%(self.nodedir(),self.nodedir()),True)
171 def list_qemu (self):
172 utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
173 command="qemu-%s/qemu-kill-node -l %s"%(self.name(),self.name())
174 self.test_box().run_in_buildname(command)
177 def kill_qemu (self):
178 #Prepare the log file before killing the nodes
179 test_box = self.test_box()
180 # kill the right processes
181 utils.header("Stopping qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
182 command="qemu-%s/qemu-kill-node %s"%(self.name(),self.name())
183 self.test_box().run_in_buildname(command)
186 def gather_qemu_logs (self):
187 if not self.is_qemu():
189 remote_log="%s/qemu.log"%self.nodedir()
190 local_log="logs/%s-qemu.log"%self.name()
191 self.test_box().test_ssh.fetch(remote_log,local_log)