d94e4e94b5dfa3d904af5b75e3f78085ac58a537
[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     
66     def __init__(self, test_plc, bonding_spec, substrate, options):
67         """
68         test_plc is one local TestPlc instance
69         bonding_spec is a dictionary that gives details on
70         the build we want to be bonding with
71         """
72         # the local build & plc is described in options
73         # the bonding build is described in bonding_spec
74         self.test_plc = test_plc
75         self.bonding_spec = bonding_spec
76         self.substrate = substrate
77         self.options = options
78     def nodefamily(self):
79         return "{pldistro}-{fcdistro}-{arch}".format(**self.bonding_spec)
80         
81     def init_partial(self):
82         """
83         runs partial-repo.sh for the bonding build
84         this action takes place on the build host
85         """
86         test_ssh = TestSsh (self.bonding_spec['build_www_host'])
87         command = "{build_www_git}/partial-repo.sh -i {build_www_dir}".\
88                   format(**self.bonding_spec)
89                          
90         return test_ssh.run (command, dry_run = self.options.dry_run) == 0
91         
92
93     def add_yum(self):
94         """
95         creates a separate yum.repo file in the myplc box
96         where our own build runs, and that points at the partial
97         repo for the bonding build
98         """
99
100         # create a .repo file locally
101         yumrepo_contents = """
102 [{buildname}]
103 name=Partial repo from bonding build {buildname}
104 baseurl={base_url}
105 enabled=1
106 gpgcheck=0
107 """.format(**self.bonding_spec)
108
109         yumrepo_local = '{buildname}-partial.repo'.\
110                         format(**self.bonding_spec)
111         with open(yumrepo_local, 'w') as yumrepo_file:
112             yumrepo_file.write(yumrepo_contents)
113         utils.header("(Over)wrote {}".format(yumrepo_local))
114
115         # push onto our myplc instance
116         test_ssh = TestSsh (self.test_plc.vserverip)
117
118         yumrepo_remote = '/etc/yum.repos.d/{bonding_buildname}-partial.repo'.\
119                          format(bonding_buildname = self.bonding_spec['buildname'])
120
121         if test_ssh.copy_abs (yumrepo_local, yumrepo_remote,
122                               dry_run=self.options.dry_run) != 0:
123             return False
124
125         # xxx TODO looks like drupal also needs to be excluded
126         # from the 2 entries in building.repo
127         # otherwise subsequent yum update calls will fail
128
129         return True
130         
131     def install_rpms(self):
132         """
133         once the 2 operations above have been performed, we can 
134         actually install the various rpms that provide support for the 
135         nodeflavour/slicefamily offered byt the bonding build to our own build
136         """
137
138         test_ssh = TestSsh (self.test_plc.vserverip)
139         
140         command1 = "yum -y update --exclude drupal"
141         if test_ssh.run (command1, dry_run = self.options.dry_run) != 0:
142             return False
143
144         nodefamily = self.nodefamily()
145         extra_list = [ 'bootcd', 'nodeimage', 'noderepo' ]
146
147         extra_rpms = [ "{}-{}".format(rpm, nodefamily) for rpm in extra_list]
148
149         command2 = "yum -y install " + " ".join(extra_rpms) 
150         if test_ssh.run (command2, dry_run = self.options.dry_run) != 0:
151             return False
152
153         command3 = "/etc/plc.d/packages force"
154         if test_ssh.run (command3, dry_run = self.options.dry_run) != 0:
155             return False
156
157         return True
158
159 ### probably obsolete already    
160 if __name__ == '__main__':
161
162     from TestPlc import TestPlc    
163
164     from config_default import sample_test_plc_spec
165     test_plc_spec = sample_test_plc_spec()
166     test_plc = TestPlc (test_plc_spec)
167     test_plc.show()
168
169     print(test_plc.host_box)
170
171     from argparse import ArgumentParser
172     parser = ArgumentParser()
173     parser.add_argument ("-n", "--dry-run", dest='dry_run', default=False,
174                          action='store_true', help="dry run")
175     parser.add_argument ("build_name")
176     args = parser.parse_args()
177
178     test_bonding = TestBonding (test_plc,
179                                 onelab_bonding_spec(args.build_name),
180                                 dry_run = args.dry_run)
181
182     test_bonding.bond ()
183