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