*Now the TestFrame is Qemu emulation capable
[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 conffile(self,image,hostname,path):
64         model=self.node_spec['node_fields']['model']
65         host_machine=self.node_spec['node_fields']['host_machine']    
66         if model.find("vmware") >= 0:
67             template='%s/template-vmplayer/node.vmx'%(path)
68             actual='%s/vmplayer-%s/node.vmx'%(path,hostname)
69             sed_command="sed -e s,@BOOTCD@,%s,g %s > %s"%(image,template,actual)
70             utils.header('Creating %s from %s'%(actual,template))
71             utils.system(sed_command)
72         elif  model.find("qemu") >= 0:
73             mac=self.node_spec['network_fields']['mac']
74             dest_dir="qemu-%s"%(hostname)
75             utils.header('Storing the mac address for node %s'%hostname)
76             file=open(path+'/qemu-'+hostname+'/MAC','a')
77             file.write('%s\n'%mac)
78             file.write(dest_dir)
79             file.close()
80             utils.header ('Transfert of configuration files for node %s into %s '%(hostname,host_machine))
81             cleandir_command="ssh root@%s rm -rf %s"%(host_machine, dest_dir)
82             createdir_command = "ssh root@%s mkdir -p  %s"%(host_machine, dest_dir)
83             utils.system(cleandir_command)
84             utils.system(createdir_command)
85             scp_command = "scp -r %s/qemu-%s/* root@%s:/root/%s"%(path,hostname,host_machine,dest_dir)
86             utils.system(scp_command)
87         
88     def create_boot_cd(self,path):
89         model=self.node_spec['node_fields']['model']
90         node_spec=self.node_spec
91         hostname=node_spec['node_fields']['hostname']
92         encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
93         if (encoded == ''):
94             raise Exception, 'boot.iso not found'
95             
96         if model.find("vmware") >= 0:
97             utils.header('Initializing vmplayer area for node %s'%hostname)
98             clean_dir="rm -rf %s/vmplayer-%s"%(path,hostname)
99             mkdir_command="mkdir -p %s/vmplayer-%s"%(path,hostname)
100             tar_command="tar -C %s/template-vmplayer -cf - . | tar -C %s/vmplayer-%s -xf -"%(path,path,hostname)
101             utils.system(clean_dir)
102             utils.system(mkdir_command)
103             utils.system(tar_command);
104             utils.header('Creating boot medium for node %s'%hostname)
105             file=open(path+'/vmplayer-'+hostname+'/boot_file.iso','w')
106         elif  model.find("qemu") >= 0:
107             clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
108             mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
109             utils.system(clean_dir)
110             utils.system(mkdir_command)
111             copy_command="cp -r  %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
112             utils.system(copy_command)
113             utils.header('Creating boot medium for node %s'%hostname)
114             file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
115
116         file.write(base64.b64decode(encoded))
117         file.close()
118         utils.header('boot cd created for %s'%hostname)
119         self.conffile('boot_file.iso',hostname, path)
120
121     def start_node (self,options):
122         model=self.node_spec['node_fields']['model']
123         if model.find("vmware") >= 0:
124             self.start_vmware(options)
125         elif model.find("qemu") >= 0:
126             self.start_qemu(options)
127         else:
128             utils.header("TestNode.start_node : ignoring model %s"%model)
129
130     def start_vmware (self,options):
131         hostname=self.node_spec['node_fields']['hostname']
132         path=options.path
133         display=options.display
134         utils.header('Starting vmplayer for node %s on %s'%(hostname,display))
135         utils.system('cd %s/vmplayer-%s ; DISPLAY=%s vmplayer node.vmx < /dev/null >/dev/null 2>/dev/null &'%(path,hostname,display))
136         
137     def start_qemu (self, options):
138         host_machine=self.node_spec['node_fields']['host_machine']
139         hostname=self.node_spec['node_fields']['hostname']
140         path=options.path
141         display=options.display
142         dest_dir="qemu-%s"%(hostname)
143         utils.header('Starting qemu for node %s '%(hostname))
144         utils.system("ssh root@%s ~/%s/env-qemu start "%(host_machine, dest_dir ))
145         utils.system("ssh  root@%s DISPLAY=%s  ~/%s/start-qemu-node %s & "%( host_machine, display, dest_dir, dest_dir))
146