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.server
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.server.DeleteNode(auth,self.name())
104 # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
105 def prepare_area(self):
106 utils.system("rm -rf %s"%self.areaname())
107 utils.system("mkdir %s"%self.areaname())
108 #create the tar log file
109 utils.system("rm -rf nodeslogs && mkdir nodeslogs")
111 utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.areaname())
113 def create_boot_cd(self):
114 utils.header("Calling GetBootMedium for %s"%self.name())
118 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), self.name(), 'node-iso', '', options)
120 raise Exception, 'GetBootmedium failed'
122 filename="%s/%s.iso"%(self.areaname(),self.name())
123 utils.header('Storing boot medium into %s'%filename)
124 file(filename,'w').write(base64.b64decode(encoded))
126 def configure_qemu(self):
127 if not self.is_qemu():
129 mac=self.node_spec['network_fields']['mac']
130 conf_filename="%s/start-qemu.conf"%(self.areaname())
131 utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
132 file=open(conf_filename,'w')
133 file.write('MACADDR=%s\n'%mac)
134 file.write('NODE_ISO=%s.iso\n'%self.name())
137 # if relevant, push the qemu area onto the host box
138 if ( not self.test_box().is_local()):
139 utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
140 self.test_box().clean_dir(self.buildname())
141 self.test_box().mkdir(self.buildname())
142 self.test_box().mkdir("nodeslogs")
143 self.test_box().copy(self.areaname(),recursive=True)
146 def start_node (self,options):
147 model=self.node_spec['node_fields']['model']
148 #starting the Qemu nodes before
150 self.start_qemu(options)
152 utils.header("TestNode.start_node : %s model %s taken as real node"%(self.name(),model))
154 def start_qemu (self, options):
155 test_box = self.test_box()
156 utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
158 test_box.run_in_buildname("qemu-%s/env-qemu start >> nodeslogs/%s.log"%(self.name(),self.name()))
159 test_box.run_in_buildname("qemu-%s/start-qemu-node 2>&1 >> nodeslogs/%s.log &"%(self.name(),self.name()))
161 def kill_qemu (self):
162 #Prepare the log file before killing the nodes
163 test_box = self.test_box()
164 if(not test_box.tar_logs()):
165 utils.header("Failed to get the nodes log files")
166 # kill the right processes
167 utils.header("Stopping qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
168 command="qemu_kill.sh %s"%self.name()
169 self.test_box().run_in_buildname(command)