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 createlog_command = "ssh root@%s touch %s/%s.log "%(host_box, dest_dir,hostname)
114 self.test_plc.run_in_host(cleandir_command)
115 self.test_plc.run_in_host(createdir_command)
116 self.test_plc.run_in_host(createlog_command)
117 scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
118 self.test_plc.run_in_host(scp_command)
120 def create_boot_cd(self,path):
121 model=self.node_spec['node_fields']['model']
122 node_spec=self.node_spec
123 hostname=node_spec['node_fields']['hostname']
124 encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
126 raise Exception, 'boot.iso not found'
128 if model.find("qemu") >= 0:
129 clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
130 mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
131 self.test_plc.run_in_host(clean_dir)
132 self.test_plc.run_in_host(mkdir_command)
133 copy_command="cp -r %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
134 self.test_plc.run_in_host(copy_command)
135 utils.header('Creating boot medium for node %s'%hostname)
136 file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
138 nodepath="%s/real-%s"%(path,hostname)
139 self.test_plc.run_in_host("rm -rf %s"%nodepath)
140 self.test_plc.run_in_host("mkdir %s"%nodepath)
141 file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
143 file.write(base64.b64decode(encoded))
145 utils.header('boot cd created for %s'%hostname)
146 self.conffile('boot_file.iso',hostname, path)
148 def start_node (self,options):
149 model=self.node_spec['node_fields']['model']
150 #starting the Qemu nodes before
151 if model.find("qemu") >= 0:
152 self.start_qemu(options)
154 utils.header("TestNode.start_node : ignoring model %s"%model)
156 def start_qemu (self, options):
157 utils.header("Starting Qemu node")
158 host_box=self.host_box()
159 hostname=self.node_spec['node_fields']['hostname']
161 display=options.display
162 dest_dir="qemu-%s"%(hostname)
163 utils.header('Starting qemu for node %s and Redirect logs to /%s/%s.log '
164 %(hostname, dest_dir, hostname))
165 self.test_plc.run_in_host("ssh root@%s ~/%s/%s/env-qemu start >> ~/%s/%s.log "
166 %(host_box, path, dest_dir, dest_dir, hostname ))
167 self.test_plc.run_in_host("ssh root@%s DISPLAY=%s ~/%s/start-qemu-node %s >> ~/%s/%s.log & "
168 %( host_box, display, dest_dir, dest_dir, dest_dir, hostname))
170 def kill_qemu (self):
171 hostname = self.name()
172 # kill the right processes
173 command="./qemu_kill.sh %s"%hostname
174 utils.header("Stopping qemu for host %s on box %s"%(hostname,self.host_box()))
175 TestBox(self.host_box()).run(command)