Merge "Revert "Fix build failure in fuel base & qemu""
[fuel.git] / deploy / cloud / configure_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 yaml
12 import io
13
14 from common import (
15     exec_cmd,
16     check_file_exists,
17     log,
18     backup,
19 )
20
21
22 class ConfigureSettings(object):
23
24     def __init__(self, yaml_config_dir, env_id, dea):
25         self.yaml_config_dir = yaml_config_dir
26         self.env_id = env_id
27         self.dea = dea
28
29     def download_settings(self):
30         log('Download settings for environment %s' % self.env_id)
31         exec_cmd('fuel settings --env %s --download --dir %s'
32                  % (self.env_id, self.yaml_config_dir))
33
34     def upload_settings(self):
35         log('Upload settings for environment %s' % self.env_id)
36         exec_cmd('fuel settings --env %s --upload --dir %s'
37                  % (self.env_id, self.yaml_config_dir))
38
39     def config_settings(self):
40         log('Configure settings')
41         self.download_settings()
42         self.modify_settings()
43         self.upload_settings()
44
45     def modify_settings(self):
46         log('Modify settings for environment %s' % self.env_id)
47         settings_yaml = ('%s/settings_%s.yaml'
48                          % (self.yaml_config_dir, self.env_id))
49         check_file_exists(settings_yaml)
50
51         with io.open(settings_yaml, 'r') as stream:
52             orig_dea = yaml.load(stream)
53
54         backup(settings_yaml)
55         settings = self.dea.get_property('settings')
56         # Copy fuel defined plugin_id's to user defined settings
57         for plugin in orig_dea['editable']:
58             if 'metadata' in orig_dea['editable'][plugin] and 'plugin_id' in orig_dea['editable'][plugin]['metadata']:
59                 if not plugin in settings['editable']:
60                     settings['editable'][plugin] = orig_dea['editable'][plugin]
61                 else:
62                     settings['editable'][plugin]["metadata"]["plugin_id"] = orig_dea['editable'][plugin]["metadata"]["plugin_id"]
63
64         with io.open(settings_yaml, 'w') as stream:
65             yaml.dump(settings, stream, default_flow_style=False)