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