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