first very rough draft of the LocalTestResources config mechanism
[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:
21             print 'Could not localize qemus - exiting'
22             sys.exit(1)
23         try:
24             plcs = self.localize_nodes(plcs,options)
25         except:
26             print 'Could not localize nodes - exiting'
27             sys.exit(1)
28         try:
29             plcs = self.localize_plcs(plcs,options)
30         except:
31             print 'Could not localize plcs - exiting'
32             sys.exit(1)
33         return plcs
34
35     def localize_qemus (self,plcs,options):
36
37         # all plcs on the same vserver box
38         plc_box = self.plc_boxes()[0]
39
40         # informative
41         label=options.personality.replace("linux","")
42
43         node_map = []
44         qemu_pool = TestPoolQemu (self.qemus_ip_pool(), options)
45
46         for index in range(options.size):
47             index += 1
48             if options.ips_qemu:
49                 ip_or_hostname=options.ips_qemu.pop()
50                 (hostname,ip,unused)=qemu_pool.locate_entry(ip_or_hostname)
51             else:
52                 (hostname,ip,unused) = qemu_pool.next_free()
53
54             node_map += [ ('node%d'%index, {'host_box':hostname},) ]
55
56         mapper = {'plc': [ ('*' , {'hostname':plc_box,
57                                    'PLC_DB_HOST':plc_box,
58                                    'PLC_API_HOST':plc_box,
59                                    'PLC_BOOT_HOST':plc_box,
60                                    'PLC_WWW_HOST':plc_box,
61                                    'name':'%s-'+label } ) 
62                            ],
63                   'node': node_map,
64                   }
65     
66         return TestMapper(plcs,options).map(mapper)
67         
68
69     def localize_nodes (self, plcs, options):
70         
71         ip_pool = TestPoolIP (self.nodes_ip_pool(),options)
72         network_dict = self.network_dict()
73
74         test_mapper = TestMapper (plcs, options)
75     
76         all_nodenames = test_mapper.node_names()
77         maps = []
78         for nodename in all_nodenames:
79             if options.ips_node:
80                 ip_or_hostname=options.ips_node.pop()
81                 print 'debug','in',ip_or_hostname,'out',ip_pool.locate_entry(ip_or_hostname)
82                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
83             else:
84                 (hostname,ip,mac) = ip_pool.next_free()
85             utils.header('Attaching node %s to %s (%s)'%(nodename,hostname,ip))
86             node_dict= {'node_fields:hostname':hostname,
87                         'interface_fields:ip':ip, 
88                         'interface_fields:mac':mac,
89                         }
90         
91             node_dict.update(network_dict)
92             maps.append ( ( nodename, node_dict) )
93     
94         plc_map = [ ( '*' , { 'PLC_NET_DNS1' : network_dict [ 'interface_fields:dns1' ],
95                               'PLC_NET_DNS2' : network_dict [ 'interface_fields:dns2' ], } ) ]
96     
97         return test_mapper.map ({'node': maps, 'plc' : plc_map } )
98         
99
100     def localize_plcs (self,plcs,options):
101         
102         utils.header ("Turning configuration into a vserver-based one for onelab")
103     
104         ip_pool = TestPoolIP (self.plcs_ip_pool(),options)
105     
106         plc_counter=0
107         for plc in plcs:
108             if options.ips_plc :
109                 ip_or_hostname=options.ips_plc.pop()
110                 (hostname,ip,mac)=ip_pool.locate_entry(ip_or_hostname)
111                 if options.verbose:
112                     utils.header("Using user-provided %s %s for plc %s"%(
113                             hostname,ip_or_hostname,plc['name']))
114             else:
115                 (hostname,ip,mac)=ip_pool.next_free()
116                 if options.verbose:
117                     utils.header("Using auto-allocated %s %s for plc %s"%(
118                             hostname,ip,plc['name']))
119     
120             ### rewrite fields in plc
121             # compute a helpful vserver name - remove domain in hostname
122             simplehostname=hostname.split('.')[0]
123             vservername = options.buildname
124             if len(plcs) == 1 :
125                 vservername = "%s-%s" % (vservername,simplehostname)
126             else:
127                 plc_counter += 1
128                 vservername = "%s-%d-%s" % (vservername,plc_counter,simplehostname)
129             # apply
130             plc['vservername']=vservername
131             plc['vserverip']=ip
132             plc['name'] = "%s_%s"%(plc['name'],simplehostname)
133             utils.header("Attaching plc %s to vserver %s (%s)"%(
134                     plc['name'],plc['vservername'],plc['vserverip']))
135             for key in [ 'PLC_DB_HOST', 'PLC_API_HOST', 'PLC_WWW_HOST', 'PLC_BOOT_HOST',]:
136                 plc[key] = hostname
137     
138         return plcs
139
140     # as a plc step this should return a boolean
141     def step_pre (self,plc):
142         return self.trqemu_record (plc) and self.trqemu_free(plc)
143
144     def step_post (self,plc):
145         return self.trplc_record (plc) and self.trplc_free(plc)
146
147     def step_cleanup (self,plc):
148         return self.trqemu_cleanup(plc) and self.trplc_cleanup(plc)
149
150     ####################
151     def trqemu_record (self,plc):
152         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
153         for site_spec in plc.plc_spec['sites']:
154             for node_spec in site_spec['nodes']:
155                 tracker.record(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
156         tracker.store()
157         return True
158
159     def trqemu_free (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.free()
164         tracker.store()
165         return True
166
167     ###
168     def trplc_record (self):
169         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
170         tracker.record(self.test_ssh.hostname,self.vservername)
171         tracker.store()
172         return True
173
174     def trplc_free (self):
175         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
176         tracker.free()
177         tracker.store()
178         return True
179
180     ###
181     def trqemu_cleanup (self,plc):
182         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
183         for site_spec in plc.plc_spec['sites']:
184             for node_spec in site_spec['nodes']:
185                 tracker.cleanup()
186         tracker.store()
187         return True
188
189     def trplc_cleanup (self,plc):
190         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
191         tracker.cleanup()
192         tracker.store()
193         return True
194