1 import os, sys, time, base64
5 from TestUser import TestUser
9 def __init__ (self,test_plc,test_site,node_spec):
10 self.test_plc=test_plc
11 self.test_site=test_site
12 self.node_spec=node_spec
15 return self.node_spec['node_fields']['hostname']
18 model=self.node_spec['node_fields']['model']
19 return model.find("vmware") >= 0
22 model=self.node_spec['node_fields']['model']
23 return model.find("qemu") >= 0
27 return self.node_spec['host_box']
31 def create_node (self):
32 ownername = self.node_spec['owner']
33 user_spec = self.test_site.locate_user(ownername)
34 test_user = TestUser(self.test_plc,self.test_site,user_spec)
35 userauth = test_user.auth()
36 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
37 rootauth=self.test_plc.auth_root()
38 server = self.test_plc.server
39 server.AddNode(userauth,
40 self.test_site.site_spec['site_fields']['login_base'],
41 self.node_spec['node_fields'])
42 # create as reinstall to avoid user confirmation
43 server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
44 # populate network interfaces - primary
45 server.AddNodeNetwork(userauth,self.name(),
46 self.node_spec['network_fields'])
47 # populate network interfaces - others
48 if self.node_spec.has_key('extra_interfaces'):
49 for interface in self.node_spec['extra_interfaces']:
50 server.AddNodeNetwork(userauth,self.name(),
51 interface['network_fields'])
52 if interface.has_key('attributes'):
53 for (attribute,value) in interface['attributes'].iteritems():
55 nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
56 nnid=nn['nodenetwork_id']
57 # locate or create node network attribute type
59 nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
61 nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
64 server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
66 def delete_node (self):
67 # uses the right auth as far as poss.
69 ownername = self.node_spec['owner']
70 user_spec = self.test_site.locate_user(ownername)
71 test_user = TestUser(self.test_plc,self.test_site,user_spec)
72 auth = test_user.auth()
74 auth=self.test_plc.auth_root()
75 self.test_plc.server.DeleteNode(auth,self.name())
77 def get_node_status(self,hostname):
80 node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
81 utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
82 if (node_status[0]['boot_state'] == 'boot'):
83 utils.header('%s has reached boot state'%hostname)
85 elif (node_status[0]['boot_state'] == 'dbg' ):
86 utils.header('%s has reached debug state'%hostname)
89 def conffile(self,image,hostname,path):
90 model=self.node_spec['node_fields']['model']
92 host_box=self.host_box()
93 template='%s/template-vmplayer/node.vmx'%(path)
94 actual='%s/vmplayer-%s/node.vmx'%(path,hostname)
95 sed_command="sed -e s,@BOOTCD@,%s,g %s > %s"%(image,template,actual)
96 utils.header('Creating %s from %s'%(actual,template))
97 utils.system(sed_command)
99 host_box=self.host_box()
100 mac=self.node_spec['network_fields']['mac']
101 dest_dir="qemu-%s"%(hostname)
102 utils.header('Storing the mac address for node %s'%hostname)
103 file=open(path+'/qemu-'+hostname+'/MAC','a')
104 file.write('%s\n'%mac)
107 utils.header ('Transferring configuration files for node %s into %s '%(hostname,host_box))
108 cleandir_command="ssh root@%s rm -rf %s"%(host_box, dest_dir)
109 createdir_command = "ssh root@%s mkdir -p %s"%(host_box, dest_dir)
110 utils.system(cleandir_command)
111 utils.system(createdir_command)
112 scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
113 utils.system(scp_command)
115 def create_boot_cd(self,path):
116 model=self.node_spec['node_fields']['model']
117 node_spec=self.node_spec
118 hostname=node_spec['node_fields']['hostname']
119 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
121 raise Exception, 'boot.iso not found'
123 if model.find("vmware") >= 0:
124 utils.header('Initializing vmplayer area for node %s'%hostname)
125 clean_dir="rm -rf %s/vmplayer-%s"%(path,hostname)
126 mkdir_command="mkdir -p %s/vmplayer-%s"%(path,hostname)
127 tar_command="tar -C %s/template-vmplayer -cf - . | tar -C %s/vmplayer-%s -xf -"%(path,path,hostname)
128 utils.system(clean_dir)
129 utils.system(mkdir_command)
130 utils.system(tar_command);
131 utils.header('Creating boot medium for node %s'%hostname)
132 file=open(path+'/vmplayer-'+hostname+'/boot_file.iso','w')
133 elif model.find("qemu") >= 0:
134 clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
135 mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
136 utils.system(clean_dir)
137 utils.system(mkdir_command)
138 copy_command="cp -r %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
139 utils.system(copy_command)
140 utils.header('Creating boot medium for node %s'%hostname)
141 file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
143 nodepath="%s/real-%s"%(path,hostname)
144 utils.system("rm -rf %s"%nodepath)
145 utils.system("mkdir %s"%nodepath)
146 file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
148 file.write(base64.b64decode(encoded))
150 utils.header('boot cd created for %s'%hostname)
151 self.conffile('boot_file.iso',hostname, path)
153 def start_node (self,options):
154 model=self.node_spec['node_fields']['model']
155 if model.find("vmware") >= 0:
156 self.start_vmware(options)
157 elif model.find("qemu") >= 0:
158 self.start_qemu(options)
160 utils.header("TestNode.start_node : ignoring model %s"%model)
162 def start_vmware (self,options):
163 hostname=self.node_spec['node_fields']['hostname']
165 display=options.display
166 utils.header('Starting vmplayer for node %s on %s'%(hostname,display))
167 utils.system('cd %s/vmplayer-%s ; DISPLAY=%s vmplayer node.vmx < /dev/null >/dev/null 2>/dev/null &'%(path,hostname,display))
169 def start_qemu (self, options):
170 host_box=self.host_box()
171 hostname=self.node_spec['node_fields']['hostname']
173 display=options.display
174 dest_dir="qemu-%s"%(hostname)
175 utils.header('Starting qemu for node %s '%(hostname))
176 utils.system("ssh root@%s ~/%s/env-qemu start "%(host_box, dest_dir ))
177 utils.system("ssh root@%s DISPLAY=%s ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
180 if not self.is_qemu():
182 hostname=self.node_spec['node_fields']['hostname']
183 host_box=self.host_box()
184 utils.system('ssh root@%s killall qemu'%host_box)
185 utils.header('Stoping qemu emulation of %s on the host machine %s and Restoring the initial network'
186 %(hostname,host_box))
187 utils.system("ssh root@%s ~/qemu-%s/env-qemu stop "%(host_box, hostname ))