another round of rework
[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 buildname(self):
31         return self.test_plc.options.buildname
32         
33     def nodedir (self):
34         if self.is_qemu():
35             return "qemu-%s"%self.name()
36         else:
37             return "real-%s"%self.name()
38
39     # this returns a hostname
40     def host_box (self):
41         if self.is_real ():
42             return 'localhost'
43         else:
44             try:
45                 return self.node_spec['host_box']
46             except:
47                 utils.header("WARNING : qemu nodes need a host box")
48                 return 'localhost'
49
50     # this returns a TestBox instance - cached in .test_box_value
51     def test_box (self):
52         try:
53             return self.test_box_value
54         except:
55             self.test_box_value = TestBox (self.host_box(),self.buildname())
56             return self.test_box_value
57
58     def create_node (self):
59         ownername = self.node_spec['owner']
60         user_spec = self.test_site.locate_user(ownername)
61         test_user = TestUser(self.test_plc,self.test_site,user_spec)
62         userauth = test_user.auth()
63         utils.header("node %s created by user %s"%(self.name(),test_user.name()))
64         rootauth=self.test_plc.auth_root()
65         server = self.test_plc.apiserver
66         server.AddNode(userauth,
67                        self.test_site.site_spec['site_fields']['login_base'],
68                        self.node_spec['node_fields'])
69         # create as reinstall to avoid user confirmation
70         server.UpdateNode(userauth, self.name(), {'boot_state':'rins'})
71         # populate network interfaces - primary
72         server.AddNodeNetwork(userauth,self.name(),
73                                             self.node_spec['network_fields'])
74         # populate network interfaces - others
75         if self.node_spec.has_key('extra_interfaces'):
76             for interface in self.node_spec['extra_interfaces']:
77                 server.AddNodeNetwork(userauth,self.name(),
78                                                     interface['network_fields'])
79                 if interface.has_key('settings'):
80                     for (attribute,value) in interface['settings'].iteritems():
81                         # locate node network
82                         nn = server.GetNodeNetworks(userauth,{'ip':interface['network_fields']['ip']})[0]
83                         nnid=nn['nodenetwork_id']
84                         # locate or create node network attribute type
85                         try:
86                             nnst = server.GetNodeNetworkSettingTypes(userauth,{'name':attribute})[0]
87                         except:
88                             nnst = server.AddNodeNetworkSettingType(rootauth,{'category':'test',
89                                                                               'name':attribute})
90                         # attach value
91                         server.AddNodeNetworkSetting(userauth,nnid,attribute,value)
92
93     def delete_node (self):
94         # uses the right auth as far as poss.
95         try:
96             ownername = self.node_spec['owner']
97             user_spec = self.test_site.locate_user(ownername)
98             test_user = TestUser(self.test_plc,self.test_site,user_spec)
99             auth = test_user.auth()
100         except:
101             auth=self.test_plc.auth_root()
102         self.test_plc.apiserver.DeleteNode(auth,self.name())
103
104     # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
105     def init_node(self):
106         utils.system("rm -rf %s"%self.nodedir())
107         utils.system("mkdir %s"%self.nodedir())
108         if not self.is_qemu():
109             return True
110         return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
111
112     def bootcd(self):
113         utils.header("Calling GetBootMedium for %s"%self.name())
114         options = []
115         if self.is_qemu():
116             options=['serial']
117         encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(), self.name(), 'node-iso', '', options)
118         if (encoded == ''):
119             raise Exception, 'GetBootmedium failed'
120
121         filename="%s/%s.iso"%(self.nodedir(),self.name())
122         utils.header('Storing boot medium into %s'%filename)
123         if self.test_plc.options.dry_run:
124             print "Dry_run: skipped writing of iso image"
125             return True
126         else:
127             file(filename,'w').write(base64.b64decode(encoded))
128             return True
129     
130     def configure_qemu(self):
131         if not self.is_qemu():
132             return
133         mac=self.node_spec['network_fields']['mac']
134         hostname=self.node_spec['node_fields']['hostname']
135         auth=self.test_plc.auth_root()
136         target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
137         conf_filename="%s/qemu.conf"%(self.nodedir())
138         if self.test_plc.options.dry_run:
139             print "dry_run: skipped actual storage of qemu.conf"
140             return
141         utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
142         file=open(conf_filename,'w')
143         file.write('MACADDR=%s\n'%mac)
144         file.write('NODE_ISO=%s.iso\n'%self.name())
145         file.write('HOSTNAME=%s\n'%hostname)
146         file.write('TARGET_ARCH=%s\n'%target_arch)
147         file.close()
148
149         # if relevant, push the qemu area onto the host box
150         if self.test_box().is_local():
151             return True
152         utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
153         return self.test_box().copy(self.nodedir(),recursive=True)==0
154             
155     def start_node (self,options):
156         model=self.node_spec['node_fields']['model']
157         #starting the Qemu nodes before 
158         if self.is_qemu():
159             self.start_qemu(options)
160         else:
161             utils.header("TestNode.start_node : %s model %s taken as real node"%(self.name(),model))
162
163     def start_qemu (self, options):
164         test_box = self.test_box()
165         utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
166
167         test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/qemu.log"%(self.nodedir(),self.nodedir()))
168         # kick it off in background, as it would otherwise hang
169         test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/qemu.log"%(self.nodedir(),self.nodedir()),True)
170
171     def list_qemu (self):
172         utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
173         command="qemu-%s/qemu-kill-node -l %s"%(self.name(),self.name())
174         self.test_box().run_in_buildname(command)
175         return True
176
177     def kill_qemu (self):
178         #Prepare the log file before killing the nodes
179         test_box = self.test_box()
180         if(not test_box.tar_logs()):
181             utils.header("Failed to get the nodes log files")
182         # kill the right processes 
183         utils.header("Stopping qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
184         command="qemu-%s/qemu-kill-node %s"%(self.name(),self.name())
185         self.test_box().run_in_buildname(command)
186         return True
187
188     def gather_qemu_logs (self):
189         if not self.is_qemu():
190             return True
191         remote_log="%s/qemu.log"%self.nodedir()
192         local_log="logs/%s-qemu.log"%self.name()
193         self.test_box().test_ssh.fetch(remote_log,local_log)