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']
17 def create_node (self):
18 ownername = self.node_spec['owner']
19 user_spec = self.test_site.locate_user(ownername)
20 test_user = TestUser(self.test_plc,self.test_site,user_spec)
21 userauth = test_user.auth()
22 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
23 rootauth=self.test_plc.auth_root()
24 server = self.test_plc.server
25 server.AddNode(userauth,
26 self.test_site.site_spec['site_fields']['login_base'],
27 self.node_spec['node_fields'])
28 # create as reinstall to avoid user confirmation
29 server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
30 # populate network interfaces - primary
31 server.AddNodeNetwork(userauth,self.name(),
32 self.node_spec['network_fields'])
33 # populate network interfaces - others
34 if self.node_spec.has_key('extra_interfaces'):
35 for interface in self.node_spec['extra_interfaces']:
36 server.AddNodeNetwork(userauth,self.name(),
37 interface['network_fields'])
38 if interface.has_key('attributes'):
39 for (attribute,value) in interface['attributes'].iteritems():
41 nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
42 nnid=nn['nodenetwork_id']
43 # locate or create node network attribute type
45 nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
47 nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
50 server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
52 def delete_node (self):
53 # uses the right auth as far as poss.
55 ownername = self.node_spec['owner']
56 user_spec = self.test_site.locate_user(ownername)
57 test_user = TestUser(self.test_plc,self.test_site,user_spec)
58 auth = test_user.auth()
60 auth=self.test_plc.auth_root()
61 self.test_plc.server.DeleteNode(auth,self.name())
63 def get_node_status(self,hostname,host_box):
66 node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
67 utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
68 if (node_status[0]['boot_state'] == 'boot'):
69 utils.header('%s has reached boot state'%hostname)
71 elif (node_status[0]['boot_state'] == 'dbg' ):
72 utils.header('%s has reached debug state'%hostname)
75 def conffile(self,image,hostname,path):
76 model=self.node_spec['node_fields']['model']
77 if model.find("vmware") >= 0:
78 host_box=self.node_spec['node_fields']['host_box']
79 template='%s/template-vmplayer/node.vmx'%(path)
80 actual='%s/vmplayer-%s/node.vmx'%(path,hostname)
81 sed_command="sed -e s,@BOOTCD@,%s,g %s > %s"%(image,template,actual)
82 utils.header('Creating %s from %s'%(actual,template))
83 utils.system(sed_command)
84 elif model.find("qemu") >= 0:
85 host_box=self.node_spec['node_fields']['host_box']
86 mac=self.node_spec['network_fields']['mac']
87 dest_dir="qemu-%s"%(hostname)
88 utils.header('Storing the mac address for node %s'%hostname)
89 file=open(path+'/qemu-'+hostname+'/MAC','a')
90 file.write('%s\n'%mac)
93 utils.header ('Transfert of configuration files for node %s into %s '%(hostname,host_box))
94 cleandir_command="ssh root@%s rm -rf %s"%(host_box, dest_dir)
95 createdir_command = "ssh root@%s mkdir -p %s"%(host_box, dest_dir)
96 utils.system(cleandir_command)
97 utils.system(createdir_command)
98 scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
99 utils.system(scp_command)
101 def create_boot_cd(self,path):
102 model=self.node_spec['node_fields']['model']
103 node_spec=self.node_spec
104 hostname=node_spec['node_fields']['hostname']
105 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
107 raise Exception, 'boot.iso not found'
109 if model.find("vmware") >= 0:
110 utils.header('Initializing vmplayer area for node %s'%hostname)
111 clean_dir="rm -rf %s/vmplayer-%s"%(path,hostname)
112 mkdir_command="mkdir -p %s/vmplayer-%s"%(path,hostname)
113 tar_command="tar -C %s/template-vmplayer -cf - . | tar -C %s/vmplayer-%s -xf -"%(path,path,hostname)
114 utils.system(clean_dir)
115 utils.system(mkdir_command)
116 utils.system(tar_command);
117 utils.header('Creating boot medium for node %s'%hostname)
118 file=open(path+'/vmplayer-'+hostname+'/boot_file.iso','w')
119 elif model.find("qemu") >= 0:
120 clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
121 mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
122 utils.system(clean_dir)
123 utils.system(mkdir_command)
124 copy_command="cp -r %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
125 utils.system(copy_command)
126 utils.header('Creating boot medium for node %s'%hostname)
127 file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
129 nodepath="%s/real-%s"%(path,hostname)
130 utils.system("rm -rf %s"%nodepath)
131 utils.system("mkdir %s"%nodepath)
132 file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
134 file.write(base64.b64decode(encoded))
136 utils.header('boot cd created for %s'%hostname)
137 self.conffile('boot_file.iso',hostname, path)
139 def start_node (self,options):
140 model=self.node_spec['node_fields']['model']
141 if model.find("vmware") >= 0:
142 self.start_vmware(options)
143 elif model.find("qemu") >= 0:
144 self.start_qemu(options)
146 utils.header("TestNode.start_node : ignoring model %s"%model)
148 def start_vmware (self,options):
149 hostname=self.node_spec['node_fields']['hostname']
151 display=options.display
152 utils.header('Starting vmplayer for node %s on %s'%(hostname,display))
153 utils.system('cd %s/vmplayer-%s ; DISPLAY=%s vmplayer node.vmx < /dev/null >/dev/null 2>/dev/null &'%(path,hostname,display))
155 def start_qemu (self, options):
156 host_box=self.node_spec['node_fields']['host_box']
157 hostname=self.node_spec['node_fields']['hostname']
159 display=options.display
160 dest_dir="qemu-%s"%(hostname)
161 utils.header('Starting qemu for node %s '%(hostname))
162 utils.system("ssh root@%s ~/%s/env-qemu start "%(host_box, dest_dir ))
163 utils.system("ssh root@%s DISPLAY=%s ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
165 def stop_qemu(self,host_box, hostname):
166 utils.header('Stoping qemu emulation of %s on the host machine %s and Restoring the initial network'
167 %(hostname,host_box))
168 utils.system("ssh root@%s ~/qemu-%s/env-qemu stop "%(host_box, hostname ))