new node in wifilab, bios updated to A09
[tests.git] / system / TestNode.py
1 import os, sys, time, base64
2 import xmlrpclib
3
4 import utils
5 from TestUser import TestUser
6 from TestBox import TestBox
7
8 class TestNode:
9
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
14
15     def name(self):
16         return self.node_spec['node_fields']['hostname']
17     
18     @staticmethod
19     def is_qemu_model (model):
20         return model.find("qemu") >= 0
21     def is_qemu (self):
22         return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
23
24     @staticmethod
25     def is_real_model (model):
26         return not TestNode.is_qemu_model(model)
27     def is_real (self):
28         return TestNode.is_real_model (self.node_spec['node_fields']['model'])
29
30     @staticmethod
31     def is_local(host_box):
32         if (host_box == "localhost"): return True
33         else: return False
34
35     def buildname(self):
36         return self.test_plc.options.buildname
37         
38     def host_box (self):
39         if self.is_real ():
40             utils.header("WARNING : real nodes dont have a host box")
41             return None
42         else:
43             try:
44                 return self.node_spec['host_box']
45             except:
46                 utils.header("WARNING : qemu nodes need a host box")
47                 return 'localhost'
48
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('settings'):
71                     for (attribute,value) in interface['settings'].iteritems():
72                         # locate node network
73                         nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
74                         nnid=nn['nodenetwork_id']
75                         # locate or create node network attribute type
76                         try:
77                             nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
78                         except:
79                             nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
80                                                                               'name':attribute})
81                         # attach value
82                         server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
83
84     def delete_node (self):
85         # uses the right auth as far as poss.
86         try:
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()
91         except:
92             auth=self.test_plc.auth_root()
93         self.test_plc.server.DeleteNode(auth,self.name())
94
95     def get_node_status(self,hostname):
96         filter=['boot_state']
97         status=False
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)
102             status=True 
103         elif (node_status[0]['boot_state'] == 'dbg' ):
104             utils.header('%s has reached debug state'%hostname)
105         return status
106
107     def conffile(self,image,hostname,path):
108         model=self.node_spec['node_fields']['model']
109         if self.is_qemu():
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)
116             file.write(dest_dir)
117             file.close()
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)
128
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'])
134         if (encoded == ''):
135             raise Exception, 'boot.iso not found'
136
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')
146         else:
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')
151
152         file.write(base64.b64decode(encoded))
153         file.close()
154         utils.header('boot cd created for %s'%hostname)
155         self.conffile('boot_file.iso',hostname, path)
156     
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)
162         else:
163             utils.header("TestNode.start_node : ignoring model %s"%model)
164
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']
169         path=options.path
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             host_string="ssh root@",host_box
175         else:
176             host_string=""
177         
178         self.test_plc.run_in_host(" %s ~/%s/env-qemu start >> ~/%s/%s.log "
179                                   %(host_string,  dest_dir, dest_dir, hostname ))
180         self.test_plc.run_in_host(" %s ~/%s/start-qemu-node %s >> ~/%s/%s.log & "
181                                   %(host_string, dest_dir, dest_dir, dest_dir, hostname))
182
183     def kill_qemu (self):
184         hostname = self.name()
185         # kill the right processes 
186         command="./qemu_kill.sh %s"%hostname
187         utils.header("Stopping qemu for host %s on box %s"%(hostname,self.host_box()))
188         TestBox(self.host_box()).run(command)
189         return True