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