93dd9f0e4bcde47759ac840b127c29a45fbc6066
[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         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     def step_list (self,plc):
151         return self.trqemu_list(plc) and self.trplc_list(plc)
152
153     ####################
154     def trqemu_record (self,plc):
155         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
156         for site_spec in plc.plc_spec['sites']:
157             for node_spec in site_spec['nodes']:
158                 tracker.record(node_spec['host_box'],plc.options.buildname,node_spec['node_fields']['hostname'])
159         tracker.store()
160         return True
161
162     def trqemu_free (self,plc):
163         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
164         for site_spec in plc.plc_spec['sites']:
165             for node_spec in site_spec['nodes']:
166                 tracker.free()
167         tracker.store()
168         return True
169
170     ###
171     def trplc_record (self,plc):
172         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
173         tracker.record(plc.test_ssh.hostname,plc.vservername)
174         tracker.store()
175         return True
176
177     def trplc_free (self,plc):
178         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
179         tracker.free()
180         tracker.store()
181         return True
182
183     ###
184     def trqemu_cleanup (self,plc):
185         tracker=TrackerQemu(plc.options,instances=self.max_qemus()-1)
186         for site_spec in plc.plc_spec['sites']:
187             for node_spec in site_spec['nodes']:
188                 tracker.cleanup()
189         tracker.store()
190         return True
191
192     def trplc_cleanup (self,plc):
193         tracker = TrackerPlc(plc.options,instances=self.max_plcs())
194         tracker.cleanup()
195         tracker.store()
196         return True
197
198     def trqemu_list (self,plc):
199         TrackerQemu(plc.options,instances=self.max_qemus()-1).list()
200         return True
201
202     def trplc_list (self,plc):
203         TrackerPlc(plc.options,instances=self.max_plcs()).list()
204         return True