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