c75bebb011518b066c63690d91370171f3c38c50
[tests.git] / system / TestResources.py
1 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
2 # Copyright (C) 2010 INRIA 
3 #
4 import sys
5 import traceback
6
7 import utils
8 from TestMapper import TestMapper
9 from TestPool import TestPoolQemu, TestPoolIP
10 from Trackers import TrackerPlc, TrackerQemu
11
12 class TestResources:
13
14     # need more specialization, see an example in LocalTestResources.sample.inria
15
16     ########## 
17     def localize (self,plcs,options):
18         try:
19             plcs = self.localize_qemus(plcs,options)
20         except Exception, e:
21             print '* Could not localize qemus','--',e,'--','exiting'
22             traceback.print_exc()
23             sys.exit(1)
24         try:
25             plcs = self.localize_nodes(plcs,options)
26         except Exception,e:
27             print '* Could not localize nodes','--',e,'--','exiting'
28             sys.exit(1)
29         try:
30             plcs = self.localize_plcs(plcs,options)
31         except Exception,e:
32             print '* Could not localize plcs','--',e,'--','exiting'
33             sys.exit(1)
34         try:
35             plcs = self.localize_rspec(plcs,options)
36         except Exception,e:
37             print '* Could not localize RSpec','--',e,'--','exiting'
38             sys.exit(1)
39         return plcs
40
41     def localize_qemus (self,plcs,options):
42
43         # all plcs on the same vserver box
44         plc_box = self.plc_boxes()[0]
45
46         # informative
47         label=options.personality.replace("linux","")
48
49         node_map = []
50         qemu_pool = TestPoolQemu (self.qemus_ip_pool(), options)
51
52         for index in range(1,options.size+1):
53             if options.ips_qemu:
54                 ip_or_hostname=options.ips_qemu.pop()
55                 (hostname,ip,unused)=qemu_pool.locate_entry(ip_or_hostname)
56             else:
57                 tracker=TrackerQemu(options,instances=self.max_qemus()-1)
58                 (hostname,ip,unused) = qemu_pool.next_free(tracker.hostnames())
59
60             node_map += [ ('node%d'%index, {'host_box':hostname},) ]
61
62         mapper = {'plc': [ ('*' , {'hostname':plc_box,
63                                    'PLC_DB_HOST':plc_box,
64                                    'PLC_API_HOST':plc_box,
65                                    'PLC_BOOT_HOST':plc_box,
66                                    'PLC_WWW_HOST':plc_box,
67                                    'name':'%s-'+label } ) 
68                            ],
69                   'node': node_map,
70                   }
71     
72         return TestMapper(plcs,options).map(mapper)
73         
74
75     def localize_nodes (self, plcs, options):
76         
77         ip_pool = TestPoolIP (self.nodes_ip_pool(),options)
78         network_dict = self.network_dict()
79
80         test_mapper = TestMapper (plcs, options)
81     
82         all_nodenames = test_mapper.node_names()
83         maps = []
84         for nodename in all_nodenames:
85             if options.ips_node:
86                 ip_or_hostname=options.ips_node.pop()
87                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
88             else:
89                 tracker=TrackerQemu(options,instances=self.max_qemus()-1)
90                 (hostname,ip,mac) = ip_pool.next_free(tracker.nodenames())
91             # myplc needs a fqdn or whines otherwise
92             if hostname.find('.')<0: hostname += "." + self.domain()
93             utils.header('Attaching node %s to %s (%s)'%(nodename,hostname,ip))
94             node_dict= {'node_fields:hostname':hostname,
95                         'interface_fields:ip':ip, 
96                         'interface_fields:mac':mac,
97                         }
98         
99             node_dict.update(network_dict)
100             maps.append ( ( nodename, node_dict) )
101     
102         plc_map = [ ( '*' , { 'PLC_NET_DNS1' : network_dict [ 'interface_fields:dns1' ],
103                               'PLC_NET_DNS2' : network_dict [ 'interface_fields:dns2' ], } ) ]
104     
105         return test_mapper.map ({'node': maps, 'plc' : plc_map } )
106         
107
108     def localize_plcs (self,plcs,options):
109         
110         ip_pool = TestPoolIP (self.plcs_ip_pool(),options)
111     
112         plc_counter=0
113         for plc in plcs:
114             if options.ips_plc :
115                 ip_or_hostname=options.ips_plc.pop()
116                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
117                 if options.verbose:
118                     utils.header("Using user-provided %s %s for plc %s"%(
119                             hostname,ip_or_hostname,plc['name']))
120             else:
121                 tracker = TrackerPlc(options,instances=self.max_plcs())
122                 (hostname,ip,mac)=ip_pool.next_free(tracker.plcnames())
123                 if options.verbose:
124                     utils.header("Using auto-allocated %s %s for plc %s"%(
125                             hostname,ip,plc['name']))
126     
127             ### rewrite fields in plc
128             # compute a helpful vserver name - remove domain in hostname
129             simplehostname = hostname.split('.')[0]
130             preferred_hostname = self.preferred_hostname()
131             vservername = options.buildname
132             if len(plcs) == 1 :
133                 vservername = "%s-%s" % (vservername,simplehostname)
134                 #ugly hack for "vuname: vc_set_vhi_name(): Arg list too long" errors
135                 if len(vservername) > 38 and preferred_hostname is not None:
136                     vservername = "%s-%s" % (options.buildname,preferred_hostname)
137             else:
138                 plc_counter += 1
139                 vservername = "%s-%d-%s" % (vservername,plc_counter,simplehostname)
140                 #ugly hack for "vuname: vc_set_vhi_name(): Arg list too long" errors
141                 if len(vservername) > 38 and preferred_hostname is not None:
142                     vservername = "%s-%d-%s" % (options.buildname,plc_counter,preferred_hostname)
143
144             # apply
145             plc['vservername']=vservername
146             plc['vserverip']=ip
147             plc['name'] = "%s_%s"%(plc['name'],simplehostname)
148             utils.header("Attaching plc %s to vserver %s (%s)"%(
149                     plc['name'],plc['vservername'],plc['vserverip']))
150             for key in [ 'PLC_DB_HOST', 'PLC_API_HOST', 'PLC_WWW_HOST', 'PLC_BOOT_HOST',]:
151                 plc[key] = hostname
152     
153         return plcs
154
155     # as a plc step this should return a boolean
156     def step_pre (self,plc):
157         return self.trqemu_record (plc) and self.trqemu_make_space(plc) \
158            and self.trplc_record (plc) and self.trplc_make_space(plc)
159
160     def step_post (self,plc):
161         return True
162
163     def step_release (self,plc):
164         return self.trqemu_release(plc) and self.trplc_release(plc)
165
166     def step_release_plc (self,plc):
167         return self.trplc_release(plc) 
168
169     def step_release_qemu (self,plc):
170         return self.trqemu_release(plc) 
171
172     def step_list (self,plc):
173         return self.trqemu_list(plc) and self.trplc_list(plc)
174
175     ####################
176     def trplc_record (self,plc):
177         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
178         tracker.record(plc.test_ssh.hostname,plc.vservername)
179         tracker.store()
180         return True
181
182     def trplc_release (self,plc):
183         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
184         tracker.release(plc.test_ssh.hostname,plc.vservername)
185         tracker.store()
186         return True
187
188     def trplc_make_space (self,plc):
189         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
190         tracker.make_space()
191         tracker.store()
192         return True
193
194     def trplc_list (self,plc):
195         TrackerPlc(plc.options,instances=self.max_plcs()).list()
196         return True
197
198     ###
199     def trqemu_record (self,plc):
200         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
201         for site_spec in plc.plc_spec['sites']:
202             for node_spec in site_spec['nodes']:
203                 tracker.record(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
204         tracker.store()
205         return True
206
207     def trqemu_release (self,plc):
208         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
209         for site_spec in plc.plc_spec['sites']:
210             for node_spec in site_spec['nodes']:
211                 tracker.release(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
212         tracker.store()
213         return True
214
215     def trqemu_make_space (self,plc):
216         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
217         for site_spec in plc.plc_spec['sites']:
218             for node_spec in site_spec['nodes']:
219                 tracker.make_space()
220         tracker.store()
221         return True
222
223     def trqemu_list (self,plc):
224         TrackerQemu(plc.options,instances=self.max_qemus()-1).list()
225         return True
226
227     ###
228     def localize_rspec (self,plcs,options):
229        
230         utils.header ("Localize SFA Slice RSpec")
231
232         for plc in plcs:
233             for site in plc['sites']:
234                 for node in site['nodes']:
235                     plc['sfa']['sfa_slice_rspec']['part4'] = node['node_fields']['hostname']
236             plc['sfa']['SFA_REGISTRY_HOST'] = plc['PLC_DB_HOST']
237             plc['sfa']['SFA_AGGREGATE_HOST'] = plc['PLC_DB_HOST']
238             plc['sfa']['SFA_SM_HOST'] = plc['PLC_DB_HOST']
239             plc['sfa']['SFA_PLC_DB_HOST'] = plc['PLC_DB_HOST']
240             plc['sfa']['SFA_PLC_URL'] = 'https://' + plc['PLC_API_HOST'] + ':443/PLCAPI/' 
241         
242         return plcs