oops
[tests.git] / system / TestNode.py
1 import os, sys, time, base64
2 import xmlrpclib
3
4 import utils
5 from TestUser import TestUser
6 from TestBox import TestBox
7
8 class TestNode:
9
10     def __init__ (self,test_plc,test_site,node_spec):
11         self.test_plc=test_plc
12         self.test_site=test_site
13         self.node_spec=node_spec
14
15     def name(self):
16         return self.node_spec['node_fields']['hostname']
17     
18     @staticmethod
19     def is_qemu_model (model):
20         return model.find("qemu") >= 0
21     def is_qemu (self):
22         return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
23
24     @staticmethod
25     def is_real_model (model):
26         return not TestNode.is_qemu_model(model)
27     def is_real (self):
28         return TestNode.is_real_model (self.node_spec['node_fields']['model'])
29
30     def host_box (self):
31         if self.is_real ():
32             utils.header("WARNING : real nodes dont have a host box")
33             return None
34         else:
35             try:
36                 return self.node_spec['host_box']
37             except:
38                 utils.header("WARNING : qemu nodes need a host box")
39                 return 'localhost'
40
41     def create_node (self):
42         ownername = self.node_spec['owner']
43         user_spec = self.test_site.locate_user(ownername)
44         test_user = TestUser(self.test_plc,self.test_site,user_spec)
45         userauth = test_user.auth()
46         utils.header("node %s created by user %s"%(self.name(),test_user.name()))
47         rootauth=self.test_plc.auth_root()
48         server = self.test_plc.server
49         server.AddNode(userauth,
50                        self.test_site.site_spec['site_fields']['login_base'],
51                        self.node_spec['node_fields'])
52         # create as reinstall to avoid user confirmation
53         server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
54         # populate network interfaces - primary
55         server.AddNodeNetwork(userauth,self.name(),
56                                             self.node_spec['network_fields'])
57         # populate network interfaces - others
58         if self.node_spec.has_key('extra_interfaces'):
59             for interface in self.node_spec['extra_interfaces']:
60                 server.AddNodeNetwork(userauth,self.name(),
61                                                     interface['network_fields'])
62                 if interface.has_key('attributes'):
63                     for (attribute,value) in interface['attributes'].iteritems():
64                         # locate node network
65                         nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
66                         nnid=nn['nodenetwork_id']
67                         # locate or create node network attribute type
68                         try:
69                             nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
70                         except:
71                             nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
72                                                                           'name':attribute})
73                         # attach value
74                         server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
75
76     def delete_node (self):
77         # uses the right auth as far as poss.
78         try:
79             ownername = self.node_spec['owner']
80             user_spec = self.test_site.locate_user(ownername)
81             test_user = TestUser(self.test_plc,self.test_site,user_spec)
82             auth = test_user.auth()
83         except:
84             auth=self.test_plc.auth_root()
85         self.test_plc.server.DeleteNode(auth,self.name())
86
87     def get_node_status(self,hostname):
88         filter=['boot_state']
89         status=False
90         node_status=self.test_plc.server.GetNodes(self.test_plc.auth_root(),hostname, filter)
91         utils.header('Actual status for node %s is [%s]'%(hostname,node_status))
92         if (node_status[0]['boot_state'] == 'boot'):
93             utils.header('%s has reached boot state'%hostname)
94             status=True 
95         elif (node_status[0]['boot_state'] == 'dbg' ):
96             utils.header('%s has reached debug state'%hostname)
97         return status
98
99     def conffile(self,image,hostname,path):
100         model=self.node_spec['node_fields']['model']
101         if 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("qemu") >= 0:
127             clean_dir="rm -rf %s/qemu-%s"%(path,hostname)
128             mkdir_command="mkdir -p %s/qemu-%s"%(path,hostname)
129             utils.system(clean_dir)
130             utils.system(mkdir_command)
131             copy_command="cp -r  %s/template-Qemu/* %s/qemu-%s"%(path,path,hostname)
132             utils.system(copy_command)
133             utils.header('Creating boot medium for node %s'%hostname)
134             file=open(path+'/qemu-'+hostname+'/boot_file.iso','w')
135         else:
136             nodepath="%s/real-%s"%(path,hostname)
137             utils.system("rm -rf %s"%nodepath)
138             utils.system("mkdir %s"%nodepath)
139             file=open("%s/%s"%(nodepath,"/boot_file.iso"),'w')
140
141         file.write(base64.b64decode(encoded))
142         file.close()
143         utils.header('boot cd created for %s'%hostname)
144         self.conffile('boot_file.iso',hostname, path)
145     
146     def start_node (self,options):
147         model=self.node_spec['node_fields']['model']
148         #starting the Qemu nodes before 
149         if model.find("qemu") >= 0:
150             self.start_qemu(options)
151         else:
152             utils.header("TestNode.start_node : ignoring model %s"%model)
153
154     def get_host_in_hostbox(self,hostbox,test_site):
155         hosts=[]
156         for node_spec in test_site.site_spec['nodes']:
157             if (node_spec['host_box'] == hostbox):
158                 hosts.append((node_spec['node_fields']['hostname'],node_spec['node_fields']['model']))
159         return hosts
160         
161     def start_qemu (self, options):
162         utils.header("Starting Qemu nodes")
163         host_box=self.host_box()
164         hostname=self.node_spec['node_fields']['hostname']
165         path=options.path
166         display=options.display
167         dest_dir="qemu-%s"%(hostname)
168         utils.header('Starting qemu for node %s '%(hostname))
169         self.test_plc.run_in_host("ssh root@%s ~/%s/%s/env-qemu start"%(host_box, path, dest_dir ))
170         self.test_plc.run_in_host("ssh  root@%s DISPLAY=%s  ~/%s/start-qemu-node %s & "%( host_box, display, dest_dir, dest_dir))
171         
172     def kill_qemu (self):
173         hostname = self.name()
174         # kill the right processes 
175         command="./qemu_kill.sh %s"%hostname
176         utils.header("Stopping qemu for host %s on box %s"%(hostname,self.host_box()))
177         TestBox(self.host_box()).run(command)
178         return True