deploy: Delete old Fuel env if present
[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 from dea import DeploymentEnvironmentAdapter
15
16 from common import (
17     check_file_exists,
18 )
19
20 ASTUTE_YAML = '/etc/fuel/astute.yaml'
21 FUEL_BOOTSTRAP_CLI_YAML = '/opt/opnfv/fuel_bootstrap_cli.yaml'
22
23
24 def usage():
25     print '''
26     Usage:
27     python transplant_fuel_settings.py <deafile>
28     '''
29
30
31 def parse_arguments():
32     if len(sys.argv) != 2:
33         usage()
34         sys.exit(1)
35     dea_file = sys.argv[-1]
36     check_file_exists(dea_file)
37     return dea_file
38
39
40 def transplant(dea, astute):
41     fuel_conf = dea.get_fuel_config()
42     for key in fuel_conf.iterkeys():
43         if key == 'ADMIN_NETWORK':
44             for skey in fuel_conf[key].iterkeys():
45                 astute[key][skey] = fuel_conf[key][skey]
46         else:
47             astute[key] = fuel_conf[key]
48     return astute
49
50
51 def transplant_bootstrap(astute, fuel_bootstrap_cli):
52     if 'BOOTSTRAP' in astute:
53         for skey in astute['BOOTSTRAP'].iterkeys():
54             # FIXME: astute.yaml repos point to public ones instead of
55             # local mirrors, this filter should be removed when in sync
56             if skey != 'repos':
57                 fuel_bootstrap_cli[skey] = astute['BOOTSTRAP'][skey]
58     return fuel_bootstrap_cli
59
60 def main():
61     dea_file = parse_arguments()
62     check_file_exists(ASTUTE_YAML)
63     check_file_exists(FUEL_BOOTSTRAP_CLI_YAML)
64     dea = DeploymentEnvironmentAdapter(dea_file)
65     with io.open(ASTUTE_YAML) as stream:
66         astute = yaml.load(stream)
67     transplant(dea, astute)
68     with io.open(ASTUTE_YAML, 'w') as stream:
69         yaml.dump(astute, stream, default_flow_style=False)
70     # Update bootstrap config yaml with info from DEA/astute.yaml
71     with io.open(FUEL_BOOTSTRAP_CLI_YAML) as stream:
72         fuel_bootstrap_cli = yaml.load(stream)
73     transplant_bootstrap(astute, fuel_bootstrap_cli)
74     with io.open(FUEL_BOOTSTRAP_CLI_YAML, 'w') as stream:
75         yaml.dump(fuel_bootstrap_cli, stream, default_flow_style=False)
76
77
78 if __name__ == '__main__':
79     main()