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 def is_local(host_box):
32 if (host_box == "localhost"): return True
36 return self.test_plc.options.buildname
40 utils.header("WARNING : real nodes dont have a host box")
44 return self.node_spec['host_box']
46 utils.header("WARNING : qemu nodes need a host box")
49 def create_node (self):
50 ownername = self.node_spec['owner']
51 user_spec = self.test_site.locate_user(ownername)
52 test_user = TestUser(self.test_plc,self.test_site,user_spec)
53 userauth = test_user.auth()
54 utils.header("node %s created by user %s"%(self.name(),test_user.name()))
55 rootauth=self.test_plc.auth_root()
56 server = self.test_plc.server
57 server.AddNode(userauth,
58 self.test_site.site_spec['site_fields']['login_base'],
59 self.node_spec['node_fields'])
60 # create as reinstall to avoid user confirmation
61 server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
62 # populate network interfaces - primary
63 server.AddNodeNetwork(userauth,self.name(),
64 self.node_spec['network_fields'])
65 # populate network interfaces - others
66 if self.node_spec.has_key('extra_interfaces'):
67 for interface in self.node_spec['extra_interfaces']:
68 server.AddNodeNetwork(userauth,self.name(),
69 interface['network_fields'])
70 if interface.has_key('attributes'):
71 for (attribute,value) in interface['attributes'].iteritems():
73 nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
74 nnid=nn['nodenetwork_id']
75 # locate or create node network attribute type
77 nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
79 nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
82 server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
84 def delete_node (self):
85 # uses the right auth as far as poss.
87 ownername = self.node_spec['owner']
88 user_spec = self.test_site.locate_user(ownername)
89 test_user = TestUser(self.test_plc,self.test_site,user_spec)
90 auth = test_user.auth()
92 auth=self.test_plc.auth_root()
93 self.test_plc.server.DeleteNode(auth,self.name())
95 def get_node_status(self,hostname):
98 node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
99 utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
100 if (node_status[0]['boot_state'] == 'boot'):
101 utils.header('%s has reached boot state'%hostname)
103 elif (node_status[0]['boot_state'] == 'dbg' ):
104 utils.header('%s has reached debug state'%hostname)
107 def conffile(self,image,hostname,path):
108 model=self.node_spec['node_fields']['model']
110 host_box=self.host_box()
111 mac=self.node_spec['network_fields']['mac']
112 dest_dir=self.buildname()+"/qemu-%s"%(hostname)
113 utils.header('Storing the mac address for node %s'%hostname)
114 file=open(path+'/qemu-'+hostname+'/MAC','a')
115 file.write('%s\n'%mac)
118 utils.header ('Transferring configuration files for node %s into %s '%(hostname,host_box))
119 if ( not self.is_local(host_box)):
120 cleandir_command="ssh root@%s rm -rf %s"%(host_box, dest_dir)
121 createdir_command = "ssh root@%s mkdir -p %s"%(host_box, dest_dir)
122 createlog_command = "ssh root@%s touch %s/%s.log "%(host_box, dest_dir,hostname)
123 self.test_plc.run_in_host(cleandir_command)
124 self.test_plc.run_in_host(createdir_command)
125 self.test_plc.run_in_host(createlog_command)
126 scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
127 self.test_plc.run_in_host(scp_command)
129 def create_boot_cd(self,path):
130 model=self.node_spec['node_fields']['model']
131 node_spec=self.node_spec
132 hostname=node_spec['node_fields']['hostname']
133 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '', ['serial'])
135 raise Exception, 'boot.iso not found'
137 if model.find("qemu") >= 0:
138 clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
139 mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
140 self.test_plc.run_in_host(clean_dir)
141 self.test_plc.run_in_host(mkdir_command)
142 copy_command="cp -r %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
143 self.test_plc.run_in_host(copy_command)
144 utils.header('Creating boot medium for node %s'%hostname)
145 file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
147 nodepath="%s/real-%s"%(path,hostname)
148 self.test_plc.run_in_host("rm -rf %s"%nodepath)
149 self.test_plc.run_in_host("mkdir %s"%nodepath)
150 file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
152 file.write(base64.b64decode(encoded))
154 utils.header('boot cd created for %s'%hostname)
155 self.conffile('boot_file.iso',hostname, path)
157 def start_node (self,options):
158 model=self.node_spec['node_fields']['model']
159 #starting the Qemu nodes before
160 if model.find("qemu") >= 0:
161 self.start_qemu(options)
163 utils.header("TestNode.start_node : ignoring model %s"%model)
165 def start_qemu (self, options):
166 utils.header("Starting Qemu node")
167 host_box=self.host_box()
168 hostname=self.node_spec['node_fields']['hostname']
170 dest_dir=self.buildname()+"/qemu-%s"%(hostname)
171 utils.header('Starting qemu for node %s and Redirect logs to /%s/%s.log '
172 %(hostname, dest_dir, hostname))
173 if (not self.is_local(host_box)):
174 self.test_plc.run_in_host("ssh root@%s ~/%s/env-qemu start >> ~/%s/%s.log "
175 %(host_box, dest_dir, dest_dir, hostname ))
176 self.test_plc.run_in_host("ssh root@%s ~/%s/start-qemu-node %s >> ~/%s/%s.log & "
177 %( host_box, dest_dir, dest_dir, dest_dir, hostname))
179 self.test_plc.run_in_host(" ~/%s/env-qemu start >> ~/%s/%s.log "
180 %(dest_dir, dest_dir, hostname ))
181 self.test_plc.run_in_host(" ~/%s/start-qemu-node %s >> ~/%s/%s.log & "
182 %(dest_dir, dest_dir, dest_dir, hostname))
184 def kill_qemu (self):
185 hostname = self.name()
186 # kill the right processes
187 command="./qemu_kill.sh %s"%hostname
188 utils.header("Stopping qemu for host %s on box %s"%(hostname,self.host_box()))
189 TestBox(self.host_box()).run(command)