attempt to support testing on centos5 again
[tests.git] / system / TestResources.py
1 #
2 # $Id$
3 #
4
5 import sys
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 OnelabTestResources
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             sys.exit(1)
23         try:
24             plcs = self.localize_nodes(plcs,options)
25         except Exception,e:
26             print '* Could not localize nodes','--',e,'--','exiting'
27             sys.exit(1)
28         try:
29             plcs = self.localize_plcs(plcs,options)
30         except Exception,e:
31             print '* Could not localize plcs','--',e,'--','exiting'
32             sys.exit(1)
33         try:
34             plcs = self.localize_rspec(plcs,options)
35         except Exception,e:
36             print '* Could not localize RSpec','--',e,'--','exiting'
37             sys.exit(1)
38         return plcs
39
40     def qemus_ip_pool(self):
41         return self.qemus_ip_pool_64()
42     def max_qemus(self):
43         return len(self.qemus_ip_pool())
44     def max_plcs(self):
45         return len(self.plcs_ip_pool())
46
47     def localize_qemus (self,plcs,options):
48
49         # all plcs on the same vserver box
50         plc_box = self.plc_boxes()[0]
51
52         # informative
53         label=options.personality.replace("linux","")
54
55         node_map = []
56         qemu_pool = TestPoolQemu (self.qemus_ip_pool(), options)
57
58         for index in range(options.size):
59             index += 1
60             if options.ips_qemu:
61                 ip_or_hostname=options.ips_qemu.pop()
62                 (hostname,ip,unused)=qemu_pool.locate_entry(ip_or_hostname)
63             else:
64                 (hostname,ip,unused) = qemu_pool.next_free()
65
66             node_map += [ ('node%d'%index, {'host_box':hostname},) ]
67
68         mapper = {'plc': [ ('*' , {'hostname':plc_box,
69                                    'PLC_DB_HOST':plc_box,
70                                    'PLC_API_HOST':plc_box,
71                                    'PLC_BOOT_HOST':plc_box,
72                                    'PLC_WWW_HOST':plc_box,
73                                    'name':'%s-'+label } ) 
74                            ],
75                   'node': node_map,
76                   }
77     
78         return TestMapper(plcs,options).map(mapper)
79         
80
81     def localize_nodes (self, plcs, options):
82         
83         ip_pool = TestPoolIP (self.nodes_ip_pool(),options)
84         network_dict = self.network_dict()
85
86         test_mapper = TestMapper (plcs, options)
87     
88         all_nodenames = test_mapper.node_names()
89         maps = []
90         for nodename in all_nodenames:
91             if options.ips_node:
92                 ip_or_hostname=options.ips_node.pop()
93                 print 'debug','in',ip_or_hostname,'out',ip_pool.locate_entry(ip_or_hostname)
94                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
95             else:
96                 (hostname,ip,mac) = ip_pool.next_free()
97             utils.header('Attaching node %s to %s (%s)'%(nodename,hostname,ip))
98             node_dict= {'node_fields:hostname':hostname,
99                         'interface_fields:ip':ip, 
100                         'interface_fields:mac':mac,
101                         }
102         
103             node_dict.update(network_dict)
104             maps.append ( ( nodename, node_dict) )
105     
106         plc_map = [ ( '*' , { 'PLC_NET_DNS1' : network_dict [ 'interface_fields:dns1' ],
107                               'PLC_NET_DNS2' : network_dict [ 'interface_fields:dns2' ], } ) ]
108     
109         return test_mapper.map ({'node': maps, 'plc' : plc_map } )
110         
111
112     def localize_plcs (self,plcs,options):
113         
114         utils.header ("Turning configuration into a vserver-based one for onelab")
115         ip_pool = TestPoolIP (self.plcs_ip_pool(),options)
116     
117         plc_counter=0
118         for plc in plcs:
119             if options.ips_plc :
120                 ip_or_hostname=options.ips_plc.pop()
121                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
122                 if options.verbose:
123                     utils.header("Using user-provided %s %s for plc %s"%(
124                             hostname,ip_or_hostname,plc['name']))
125             else:
126                 (hostname,ip,mac)=ip_pool.next_free()
127                 if options.verbose:
128                     utils.header("Using auto-allocated %s %s for plc %s"%(
129                             hostname,ip,plc['name']))
130     
131             ### rewrite fields in plc
132             # compute a helpful vserver name - remove domain in hostname
133             simplehostname=hostname.split('.')[0]
134             vservername = options.buildname
135             if len(plcs) == 1 :
136                 vservername = "%s-%s" % (vservername,simplehostname)
137             else:
138                 plc_counter += 1
139                 vservername = "%s-%d-%s" % (vservername,plc_counter,simplehostname)
140             # apply
141             plc['vservername']=vservername
142             plc['vserverip']=ip
143             plc['name'] = "%s_%s"%(plc['name'],simplehostname)
144             utils.header("Attaching plc %s to vserver %s (%s)"%(
145                     plc['name'],plc['vservername'],plc['vserverip']))
146             for key in [ 'PLC_DB_HOST', 'PLC_API_HOST', 'PLC_WWW_HOST', 'PLC_BOOT_HOST',]:
147                 plc[key] = hostname
148     
149         return plcs
150
151     # as a plc step this should return a boolean
152     def step_pre (self,plc):
153         return self.trqemu_record (plc) and self.trqemu_free(plc)
154
155     def step_post (self,plc):
156         return self.trplc_record (plc) and self.trplc_free(plc)
157
158     def step_cleanup (self,plc):
159         return self.trqemu_cleanup(plc) and self.trplc_cleanup(plc)
160
161     def step_list (self,plc):
162         return self.trqemu_list(plc) and self.trplc_list(plc)
163
164     ####################
165     def trqemu_record (self,plc):
166         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
167         for site_spec in plc.plc_spec['sites']:
168             for node_spec in site_spec['nodes']:
169                 tracker.record(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
170         tracker.store()
171         return True
172
173     def trqemu_free (self,plc):
174         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
175         for site_spec in plc.plc_spec['sites']:
176             for node_spec in site_spec['nodes']:
177                 tracker.free()
178         tracker.store()
179         return True
180
181     ###
182     def trplc_record (self,plc):
183         tracker = TrackerPlc(plc.options,instances=self.max_plcs()-1)
184         tracker.record(plc.test_ssh.hostname,plc.vservername)
185         tracker.store()
186         return True
187
188     def trplc_free (self,plc):
189         tracker = TrackerPlc(plc.options,instances=self.max_plcs()-1)
190         tracker.free()
191         tracker.store()
192         return True
193
194     ###
195     def trqemu_cleanup (self,plc):
196         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
197         for site_spec in plc.plc_spec['sites']:
198             for node_spec in site_spec['nodes']:
199                 tracker.cleanup()
200         tracker.store()
201         return True
202
203     def trplc_cleanup (self,plc):
204         tracker = TrackerPlc(plc.options,instances=self.max_plcs()-1)
205         tracker.cleanup()
206         tracker.store()
207         return True
208
209     def trqemu_list (self,plc):
210         TrackerQemu(plc.options,instances=self.max_qemus()-1).list()
211         return True
212
213     def trplc_list (self,plc):
214         TrackerPlc(plc.options,instances=self.max_plcs()-1).list()
215         return True
216
217     def localize_rspec (self,plcs,options):
218        
219         utils.header ("Localize SFA Slice RSpec")
220
221         for plc in plcs:
222             for site in plc['sites']:
223                 for node in site['nodes']:
224                     plc['sfa']['sfa_slice_rspec']['part4'] = node['node_fields']['hostname']
225             plc['sfa']['SFA_REGISTRY_HOST'] = plc['PLC_DB_HOST']
226             plc['sfa']['SFA_AGGREGATE_HOST'] = plc['PLC_DB_HOST']
227             plc['sfa']['SFA_SM_HOST'] = plc['PLC_DB_HOST']
228             plc['sfa']['SFA_PLC_DB_HOST'] = plc['PLC_DB_HOST']
229             plc['sfa']['SFA_PLC_URL'] = 'https://' + plc['PLC_API_HOST'] + ':443/PLCAPI/' 
230         
231         return plcs