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