ee915fdbee83cdb9ab5bdd7ffeff1f609f465343
[fuel.git] / deploy / transplant_fuel_settings.py
1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ###############################################################################
9
10
11 import sys
12 import io
13 import yaml
14 import re
15 import os
16 from dea import DeploymentEnvironmentAdapter
17
18 from common import (
19     check_file_exists,
20     exec_cmd,
21     log,
22 )
23
24 ASTUTE_YAML = '/etc/fuel/astute.yaml'
25 FUEL_BOOTSTRAP_CLI_YAML = '/opt/opnfv/fuel_bootstrap_cli.yaml'
26
27
28 def usage():
29     print '''
30     Usage:
31     python transplant_fuel_settings.py <deafile>
32     '''
33
34
35 def parse_arguments():
36     if len(sys.argv) != 2:
37         usage()
38         sys.exit(1)
39     dea_file = sys.argv[-1]
40     check_file_exists(dea_file)
41     return dea_file
42
43 def write_ifcfg_file(key, fuel_conf):
44     config = ('BOOTPROTO=none\n'
45               'ONBOOT=yes\n'
46               'TYPE=Ethernet\n'
47               'NM_CONTROLLED=yes\n')
48     for skey in ('ipaddress', 'device', 'netmask', 'gateway'):
49         if not fuel_conf[key].get(skey):
50             log('Warning: missing key %s for %s' % (skey, key))
51             config += '%s=\n' % skey.upper()
52         elif skey == 'ipaddress':
53             config += 'IPADDR=%s\n' % fuel_conf[key][skey]
54         else:
55             config += '%s=%s\n' % (skey.upper(), fuel_conf[key][skey])
56
57     fname = os.path.join('/etc/sysconfig/network-scripts/',
58                          key.lower().replace('_','-'))
59     with open(fname, 'wc') as f:
60         f.write(config)
61
62 def transplant(dea, astute):
63     fuel_conf = dea.get_fuel_config()
64     require_network_restart = False
65     for key in fuel_conf.iterkeys():
66         if key == 'ADMIN_NETWORK':
67             for skey in fuel_conf[key].iterkeys():
68                 astute[key][skey] = fuel_conf[key][skey]
69         elif re.match('^IFCFG', key):
70             log('Adding interface configuration for: %s' % key.lower())
71             require_network_restart = True
72             write_ifcfg_file(key, fuel_conf)
73             if astute.has_key(key):
74                 astute.pop(key, None)
75         else:
76             astute[key] = fuel_conf[key]
77     if require_network_restart:
78         admin_ifcfg = '/etc/sysconfig/network-scripts/ifcfg-eth0'
79         exec_cmd('echo "DEFROUTE=no" >> %s' % admin_ifcfg)
80         log('At least one interface was reconfigured, restart network manager')
81         exec_cmd('systemctl restart network')
82     return astute
83
84
85 def transplant_bootstrap(astute, fuel_bootstrap_cli):
86     if 'BOOTSTRAP' in astute:
87         for skey in astute['BOOTSTRAP'].iterkeys():
88             # FIXME: astute.yaml repos point to public ones instead of
89             # local mirrors, this filter should be removed when in sync
90             if skey != 'repos':
91                 fuel_bootstrap_cli[skey] = astute['BOOTSTRAP'][skey]
92     return fuel_bootstrap_cli
93
94 def main():
95     dea_file = parse_arguments()
96     check_file_exists(ASTUTE_YAML)
97     # Temporarily disabled for Fuel 10.
98     # check_file_exists(FUEL_BOOTSTRAP_CLI_YAML)
99     dea = DeploymentEnvironmentAdapter(dea_file)
100     log('Reading astute file %s' % ASTUTE_YAML)
101     with io.open(ASTUTE_YAML) as stream:
102         astute = yaml.load(stream)
103     log('Initiating transplant')
104     transplant(dea, astute)
105     with io.open(ASTUTE_YAML, 'w') as stream:
106         yaml.dump(astute, stream, default_flow_style=False)
107     log('Transplant done')
108     # Update bootstrap config yaml with info from DEA/astute.yaml
109     # Temporarily disabled for Fuel 10.
110     # with io.open(FUEL_BOOTSTRAP_CLI_YAML) as stream:
111     #     fuel_bootstrap_cli = yaml.load(stream)
112     # transplant_bootstrap(astute, fuel_bootstrap_cli)
113     # with io.open(FUEL_BOOTSTRAP_CLI_YAML, 'w') as stream:
114     #     yaml.dump(fuel_bootstrap_cli, stream, default_flow_style=False)
115
116
117 if __name__ == '__main__':
118     main()