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'])
32 utils.header("WARNING : real nodes dont have a host box")
36 return self.node_spec['host_box']
38 utils.header("WARNING : qemu nodes need a host box")
41 def create_node (self):
42 ownername = self.node_spec['owner']
43 user_spec = self.test_site.locate_user(ownername)
44 test_user = TestUser(self.test_plc,self.test_site,user_spec)
45 userauth = test_user.auth()
46 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
47 rootauth=self.test_plc.auth_root()
48 server = self.test_plc.server
49 server.AddNode(userauth,
50 self.test_site.site_spec['site_fields']['login_base'],
51 self.node_spec['node_fields'])
52 # create as reinstall to avoid user confirmation
53 server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
54 # populate network interfaces - primary
55 server.AddNodeNetwork(userauth,self.name(),
56 self.node_spec['network_fields'])
57 # populate network interfaces - others
58 if self.node_spec.has_key('extra_interfaces'):
59 for interface in self.node_spec['extra_interfaces']:
60 server.AddNodeNetwork(userauth,self.name(),
61 interface['network_fields'])
62 if interface.has_key('attributes'):
63 for (attribute,value) in interface['attributes'].iteritems():
65 nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
66 nnid=nn['nodenetwork_id']
67 # locate or create node network attribute type
69 nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
71 nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
74 server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
76 def delete_node (self):
77 # uses the right auth as far as poss.
79 ownername = self.node_spec['owner']
80 user_spec = self.test_site.locate_user(ownername)
81 test_user = TestUser(self.test_plc,self.test_site,user_spec)
82 auth = test_user.auth()
84 auth=self.test_plc.auth_root()
85 self.test_plc.server.DeleteNode(auth,self.name())
87 def get_node_status(self,hostname):
90 node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
91 utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
92 if (node_status[0]['boot_state'] == 'boot'):
93 utils.header('%s has reached boot state'%hostname)
95 elif (node_status[0]['boot_state'] == 'dbg' ):
96 utils.header('%s has reached debug state'%hostname)
99 def conffile(self,image,hostname,path):
100 model=self.node_spec['node_fields']['model']
102 host_box=self.host_box()
103 mac=self.node_spec['network_fields']['mac']
104 dest_dir="qemu-%s"%(hostname)
105 utils.header('Storing the mac address for node %s'%hostname)
106 file=open(path+'/qemu-'+hostname+'/MAC','a')
107 file.write('%s\n'%mac)
110 utils.header ('Transferring configuration files for node %s into %s '%(hostname,host_box))
111 cleandir_command="ssh root@%s rm -rf %s"%(host_box, dest_dir)
112 createdir_command = "ssh root@%s mkdir -p %s"%(host_box, dest_dir)
113 utils.system(cleandir_command)
114 utils.system(createdir_command)
115 scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
116 utils.system(scp_command)
118 def create_boot_cd(self,path):
119 model=self.node_spec['node_fields']['model']
120 node_spec=self.node_spec
121 hostname=node_spec['node_fields']['hostname']
122 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
124 raise Exception, 'boot.iso not found'
126 if model.find("qemu") >= 0:
127 clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
128 mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
129 utils.system(clean_dir)
130 utils.system(mkdir_command)
131 copy_command="cp -r %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
132 utils.system(copy_command)
133 utils.header('Creating boot medium for node %s'%hostname)
134 file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
136 nodepath="%s/real-%s"%(path,hostname)
137 utils.system("rm -rf %s"%nodepath)
138 utils.system("mkdir %s"%nodepath)
139 file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
141 file.write(base64.b64decode(encoded))
143 utils.header('boot cd created for %s'%hostname)
144 self.conffile('boot_file.iso',hostname, path)
146 def start_node (self,options):
147 model=self.node_spec['node_fields']['model']
148 #starting the Qemu nodes before
149 if model.find("qemu") >= 0:
150 self.start_qemu(options)
152 utils.header("TestNode.start_node : ignoring model %s"%model)
154 def get_host_in_hostbox(self,hostbox,test_site):
156 for node_spec in test_site.site_spec['nodes']:
157 if (node_spec['host_box'] == hostbox):
158 hosts.append((node_spec['node_fields']['hostname'],node_spec['node_fields']['model']))
161 def start_qemu (self, options):
162 utils.header("Starting Qemu nodes")
163 host_box=self.host_box()
164 hostname=self.node_spec['node_fields']['hostname']
166 display=options.display
167 dest_dir="qemu-%s"%(hostname)
168 utils.header('Starting qemu for node %s '%(hostname))
169 self.test_plc.run_in_host("ssh root@%s ~/%s/%s/env-qemu start"%(host_box, path, dest_dir ))
170 self.test_plc.run_in_host("ssh root@%s DISPLAY=%s ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
172 def kill_qemu (self):
173 hostname = self.name()
174 # kill the right processes
175 command="kill $(ps $(pgrep qemu) | grep %s | awk '{print $1;}')"%hostname
176 utils.header("Stopping qemu for host %s on box %s"%(hostname,self.host_box()))
177 TestBox(self.host_box()).run(command)