real nodes -> warning only - review (wrongly) multiple loop on slices
[tests.git] / system / TestNode.py
1 import os, sys, time, base64
2 import xmlrpclib
3
4 import utils
5 from TestUser import TestUser
6
7 class TestNode:
8
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
13
14     def name(self):
15         return self.node_spec['node_fields']['hostname']
16         
17     @staticmethod
18     def is_vmware_model(model):
19         return model.find("vmware") >= 0        
20     def is_vmware (self):
21         return TestNode.is_vmware_model(self.node_spec['node_fields']['model'])
22
23     @staticmethod
24     def is_qemu_model (model):
25         return model.find("qemu") >= 0
26     def is_qemu (self):
27         return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
28
29     @staticmethod
30     def is_real_model (model):
31         return (not TestNode.is_vmware_model(model)) \
32             and (not TestNode.is_qemu_model(model))
33     def is_real (self):
34         return TestNode.is_real_model (self.node_spec['node_fields']['model'])
35
36     def host_box (self):
37         try:
38             return self.node_spec['host_box']
39         except:
40             return 'localhost'
41
42     def create_node (self):
43         ownername = self.node_spec['owner']
44         user_spec = self.test_site.locate_user(ownername)
45         test_user = TestUser(self.test_plc,self.test_site,user_spec)
46         userauth = test_user.auth()
47         utils.header("node %s created by user %s"%(self.name(),test_user.name()))
48         rootauth=self.test_plc.auth_root()
49         server = self.test_plc.server
50         server.AddNode(userauth,
51                        self.test_site.site_spec['site_fields']['login_base'],
52                        self.node_spec['node_fields'])
53         # create as reinstall to avoid user confirmation
54         server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
55         # populate network interfaces - primary
56         server.AddNodeNetwork(userauth,self.name(),
57                                             self.node_spec['network_fields'])
58         # populate network interfaces - others
59         if self.node_spec.has_key('extra_interfaces'):
60             for interface in self.node_spec['extra_interfaces']:
61                 server.AddNodeNetwork(userauth,self.name(),
62                                                     interface['network_fields'])
63                 if interface.has_key('attributes'):
64                     for (attribute,value) in interface['attributes'].iteritems():
65                         # locate node network
66                         nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
67                         nnid=nn['nodenetwork_id']
68                         # locate or create node network attribute type
69                         try:
70                             nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
71                         except:
72                             nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
73                                                                           'name':attribute})
74                         # attach value
75                         server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
76
77     def delete_node (self):
78         # uses the right auth as far as poss.
79         try:
80             ownername = self.node_spec['owner']
81             user_spec = self.test_site.locate_user(ownername)
82             test_user = TestUser(self.test_plc,self.test_site,user_spec)
83             auth = test_user.auth()
84         except:
85             auth=self.test_plc.auth_root()
86         self.test_plc.server.DeleteNode(auth,self.name())
87
88     def get_node_status(self,hostname):
89         filter=['boot_state']
90         status=False
91         node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
92         utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
93         if (node_status[0]['boot_state'] == 'boot'):
94             utils.header('%s has reached boot state'%hostname)
95             status=True 
96         elif (node_status[0]['boot_state'] == 'dbg' ):
97             utils.header('%s has reached debug state'%hostname)
98         return status
99
100     def conffile(self,image,hostname,path):
101         model=self.node_spec['node_fields']['model']
102         if self.is_vmware():
103             host_box=self.host_box()
104             template='%s/template-vmplayer/node.vmx'%(path)
105             actual='%s/vmplayer-%s/node.vmx'%(path,hostname)
106             sed_command="sed -e s,@BOOTCD@,%s,g %s > %s"%(image,template,actual)
107             utils.header('Creating %s from %s'%(actual,template))
108             utils.system(sed_command)
109         elif self.is_qemu():
110             host_box=self.host_box()
111             mac=self.node_spec['network_fields']['mac']
112             dest_dir="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             cleandir_command="ssh root@%s rm -rf %s"%(host_box, dest_dir)
120             createdir_command = "ssh root@%s mkdir -p  %s"%(host_box, dest_dir)
121             utils.system(cleandir_command)
122             utils.system(createdir_command)
123             scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_box,dest_dir)
124             utils.system(scp_command)
125         
126     def create_boot_cd(self,path):
127         model=self.node_spec['node_fields']['model']
128         node_spec=self.node_spec
129         hostname=node_spec['node_fields']['hostname']
130         encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
131         if (encoded == ''):
132             raise Exception, 'boot.iso not found'
133             
134         if model.find("vmware") >= 0:
135             utils.header('Initializing vmplayer area for node %s'%hostname)
136             clean_dir="rm -rf %s/vmplayer-%s"%(path,hostname)
137             mkdir_command="mkdir -p %s/vmplayer-%s"%(path,hostname)
138             tar_command="tar -C %s/template-vmplayer -cf - . | tar -C %s/vmplayer-%s -xf -"%(path,path,hostname)
139             utils.system(clean_dir)
140             utils.system(mkdir_command)
141             utils.system(tar_command);
142             utils.header('Creating boot medium for node %s'%hostname)
143             file=open(path+'/vmplayer-'+hostname+'/boot_file.iso','w')
144         elif  model.find("qemu") >= 0:
145             clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
146             mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
147             utils.system(clean_dir)
148             utils.system(mkdir_command)
149             copy_command="cp -r  %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
150             utils.system(copy_command)
151             utils.header('Creating boot medium for node %s'%hostname)
152             file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
153         else:
154             nodepath="%s/real-%s"%(path,hostname)
155             utils.system("rm -rf %s"%nodepath)
156             utils.system("mkdir %s"%nodepath)
157             file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
158
159         file.write(base64.b64decode(encoded))
160         file.close()
161         utils.header('boot cd created for %s'%hostname)
162         self.conffile('boot_file.iso',hostname, path)
163
164     def start_node (self,options):
165         model=self.node_spec['node_fields']['model']
166         if model.find("vmware") >= 0:
167             self.start_vmware(options)
168         elif model.find("qemu") >= 0:
169             self.start_qemu(options)
170         else:
171             utils.header("TestNode.start_node : ignoring model %s"%model)
172
173     def start_vmware (self,options):
174         hostname=self.node_spec['node_fields']['hostname']
175         path=options.path
176         display=options.display
177         utils.header('Starting vmplayer for node %s on %s'%(hostname,display))
178         utils.system('cd %s/vmplayer-%s ; DISPLAY=%s vmplayer node.vmx < /dev/null >/dev/null 2>/dev/null &'%(path,hostname,display))
179         
180     def start_qemu (self, options):
181         host_box=self.host_box()
182         hostname=self.node_spec['node_fields']['hostname']
183         path=options.path
184         display=options.display
185         dest_dir="qemu-%s"%(hostname)
186         utils.header('Starting qemu for node %s '%(hostname))
187         utils.system("ssh root@%s ~/%s/env-qemu start "%(host_box, dest_dir ))
188         utils.system("ssh  root@%s DISPLAY=%s  ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
189         
190     def stop_qemu(self):
191         if not self.is_qemu():
192             return True
193         hostname=self.node_spec['node_fields']['hostname']
194         host_box=self.host_box()
195         utils.system('ssh root@%s  killall qemu'%host_box)
196         utils.header('Stoping qemu emulation of %s on the host machine %s and Restoring the initial network'
197                      %(hostname,host_box))
198         utils.system("ssh root@%s ~/qemu-%s/env-qemu stop "%(host_box, hostname ))
199         return True