run -g with bonding build can provision node (and locate free IP) and run most usual...
[tests.git] / system / TestBonding.py
1 """
2 Utilities to create a setup made of 2 different builds
3 read : 2 different node flavours 
4 so that each myplc knows about the nodeflavour/slicefamily supported
5 by the other one
6 ---
7 This would be the basics for running tests on multi-node myplc, 
8 in particular for node upgrades
9 """
10
11 #################### WARNING
12
13 # this feature relies on a few assumptions that need to be taken care of
14 # more or less manually; this is based on the onelab.eu setup
15
16 # (*) the build host is expected to have /root/git-build.sh reasonably up-to-date
17 # with our build module, so we can locate partial-repo.sh
18 # this utility needs to be run on the build host so we can point at a PARTIAL-RPMS
19 # sub-repo that exposes the
20 # bootcd/bootstraps/ and the like rpms from one flavour to another
21
22 # a utility to create a bonding_plc_spec from
23 # a plc_spec and just a buildname
24
25 def onelab_bonding_spec (buildname):
26
27     # essentially generic ..
28     buildname      = buildname
29     
30     # visit the other build's test directory to figure its characteristics
31     with open ("../{}/arg-fcdistro".format(buildname)) as input:
32         fcdistro   = input.read().strip()
33     with open ("../{}/arg-pldistro".format(buildname)) as input:
34         pldistro   = input.read().strip()
35     with open ("../{}/arg-ips-bplc".format(buildname)) as input:
36         plc_box    = input.read().strip().split()[0]
37     # e.g. http://build.onelab.eu/onelab//2015.03.15--f14/RPMS/x86_64
38     with open ("../{}/arg-arch-rpms-url".format(buildname)) as input:
39         arch_rpms_url = input.read().strip()
40     arch           = arch_rpms_url.split('/')[-1]
41     build_www_host = arch_rpms_url.split('/')[2]
42     base_url       = arch_rpms_url.replace("RPMS/{}".format(arch), "PARTIAL-RPMS")
43         
44     # onelab specifics
45     build_www_git = '/root/git-build/'
46     build_www_dir  = '/build/{}/{}'.format(pldistro, buildname)
47     
48     return locals()
49
50 ####################
51 import os, os.path
52
53 import utils
54 from TestSsh import TestSsh
55
56 ####################
57 class TestBonding(object):
58
59     """
60     Holds details about a 'bonding' build
61     so we can configure the local myplc (test_plc)
62     for multi-flavour nodes and slices
63     options is a TestMain options
64
65     details for a bonding node (like hostname and IP) are
66     computed from the underlying Substrate object and 
67     stored in arg-bonding-{buildname}
68     """
69     
70     def __init__(self, test_plc, bonding_spec, substrate, options):
71         """
72         test_plc is one local TestPlc instance
73         bonding_spec is a dictionary that gives details on
74         the build we want to be bonding with
75         """
76         # the local build & plc is described in options
77         # the bonding build is described in bonding_spec
78         self.test_plc = test_plc
79         self.bonding_spec = bonding_spec
80         self.substrate = substrate
81         self.options = options
82         # a little hacky : minimal provisioning and modify plc_spec on the fly
83         self.provision()
84     
85     def nodefamily(self):
86         return "{pldistro}-{fcdistro}-{arch}".format(**self.bonding_spec)
87         
88     #################### provisioning
89     def persistent_name(self):
90         return "arg-bonding-{}".format(self.bonding_spec['buildname'])
91     def persistent_store(self):
92         with open(self.persistent_name(),'w') as f:
93             f.write("{} {}\n".format(self.vnode_hostname, self.vnode_ip))
94     def persistent_load(self):
95         try:
96             with open(self.persistent_name()) as f:
97                 self.vnode_hostname, self.vnode_ip = f.read().strip().split()
98             return True
99         except:
100             return False
101
102     def provision(self):
103         # locate the first node in our own spec
104         site_spec = self.test_plc.plc_spec['sites'][0]
105         node_spec = site_spec['nodes'][0]
106         # find a free IP for node
107         if self.persistent_load():
108             print("Re-using bonding nodes attributes from {}".format(self.persistent_name()))
109         else:
110             print("Could not load bonding nodes attributes from {}".format(self.persistent_name()))
111             vnode_pool = self.substrate.vnode_pool
112             vnode_pool.sense()
113             try:
114                 hostname, mac = vnode_pool.next_free()
115                 self.vnode_hostname = self.substrate.fqdn(hostname)
116                 self.vnode_ip = vnode_pool.get_ip(hostname)
117                 self.vnode_mac = mac
118                 self.persistent_store()
119             except:
120                 raise Exception("Cannot provision bonding node")
121
122         # implement the node on another IP
123         node_spec['node_fields']['hostname'] = self.vnode_hostname
124         node_spec['interface_fields']['ip'] = self.vnode_ip
125         # with the node flavour that goes with bonding plc
126         for tag in ['arch', 'fcdistro', 'pldistro']:
127             node_spec['tags'][tag] = self.bonding_spec[tag]
128
129     #################### steps
130     def init_partial(self):
131         """
132         runs partial-repo.sh for the bonding build
133         this action takes place on the build host
134         """
135         test_ssh = TestSsh (self.bonding_spec['build_www_host'])
136         command = "{build_www_git}/partial-repo.sh -i {build_www_dir}".\
137                   format(**self.bonding_spec)
138                          
139         return test_ssh.run (command, dry_run = self.options.dry_run) == 0
140         
141
142     def add_yum(self):
143         """
144         creates a separate yum.repo file in the myplc box
145         where our own build runs, and that points at the partial
146         repo for the bonding build
147         """
148
149         # create a .repo file locally
150         yumrepo_contents = """
151 [{buildname}]
152 name=Partial repo from bonding build {buildname}
153 baseurl={base_url}
154 enabled=1
155 gpgcheck=0
156 """.format(**self.bonding_spec)
157
158         yumrepo_local = '{buildname}-partial.repo'.\
159                         format(**self.bonding_spec)
160         with open(yumrepo_local, 'w') as yumrepo_file:
161             yumrepo_file.write(yumrepo_contents)
162         utils.header("(Over)wrote {}".format(yumrepo_local))
163
164         # push onto our myplc instance
165         test_ssh = TestSsh (self.test_plc.vserverip)
166
167         yumrepo_remote = '/etc/yum.repos.d/{bonding_buildname}-partial.repo'.\
168                          format(bonding_buildname = self.bonding_spec['buildname'])
169
170         if test_ssh.copy_abs (yumrepo_local, yumrepo_remote,
171                               dry_run=self.options.dry_run) != 0:
172             return False
173
174         # xxx TODO looks like drupal also needs to be excluded
175         # from the 2 entries in building.repo
176         # otherwise subsequent yum update calls will fail
177
178         return True
179         
180     def install_rpms(self):
181         """
182         once the 2 operations above have been performed, we can 
183         actually install the various rpms that provide support for the 
184         nodeflavour/slicefamily offered byt the bonding build to our own build
185         """
186
187         test_ssh = TestSsh (self.test_plc.vserverip)
188         
189         command1 = "yum -y update --exclude drupal"
190         if test_ssh.run (command1, dry_run = self.options.dry_run) != 0:
191             return False
192
193         nodefamily = self.nodefamily()
194         extra_list = [ 'bootcd', 'nodeimage', 'noderepo' ]
195
196         extra_rpms = [ "{}-{}".format(rpm, nodefamily) for rpm in extra_list]
197
198         command2 = "yum -y install " + " ".join(extra_rpms) 
199         if test_ssh.run (command2, dry_run = self.options.dry_run) != 0:
200             return False
201
202         command3 = "/etc/plc.d/packages force"
203         if test_ssh.run (command3, dry_run = self.options.dry_run) != 0:
204             return False
205
206         return True
207
208 ### probably obsolete already    
209 if __name__ == '__main__':
210
211     from TestPlc import TestPlc    
212
213     from config_default import sample_test_plc_spec
214     test_plc_spec = sample_test_plc_spec()
215     test_plc = TestPlc (test_plc_spec)
216     test_plc.show()
217
218     print(test_plc.host_box)
219
220     from argparse import ArgumentParser
221     parser = ArgumentParser()
222     parser.add_argument ("-n", "--dry-run", dest='dry_run', default=False,
223                          action='store_true', help="dry run")
224     parser.add_argument ("build_name")
225     args = parser.parse_args()
226
227     test_bonding = TestBonding (test_plc,
228                                 onelab_bonding_spec(args.build_name),
229                                 dry_run = args.dry_run)
230
231     test_bonding.bond ()
232