~andrewjbeach/juju-ci-tools/get-juju-dict

1438.2.1 by Aaron Bentley
Add convert_config.py
1
#!/usr/bin/env python
2
from __future__ import print_function
3
4
from argparse import ArgumentParser
5
import os
6
import sys
7
from textwrap import dedent
8
9
import yaml
10
11
from jujuconfig import get_environments
12
from jujupy import (
13
    JujuData,
14
    )
15
from tests.test_jujupy import fake_juju_client
16
1438.2.2 by Aaron Bentley
Cleanups
17
1438.2.1 by Aaron Bentley
Add convert_config.py
18
def write_new_config(env, out):
19
    client = fake_juju_client(env=env)
20
    out.write('# cloud/region: {}\n'.format(client.get_cloud_region(
21
        client.env.get_cloud(), client.env.get_region())))
22
    config = client.make_model_config()
23
    if 'agent-version' in env.config:
24
        config['agent-version'] = env.config['agent-version']
25
    else:
26
        config.pop('agent-version', None)
27
    yaml.dump(config, out, default_flow_style=False)
28
29
30
def main():
31
    parser = ArgumentParser(
32
        description=dedent('''\
1438.2.3 by Aaron Bentley
Cleanups
33
            Convert environments.yaml to 2.0 format.
1438.2.1 by Aaron Bentley
Add convert_config.py
34
1438.2.2 by Aaron Bentley
Cleanups
35
            environments.yaml from JUJU_HOME will be used.
1438.2.1 by Aaron Bentley
Add convert_config.py
36
            Existing configs in the output directory will be overwritten.
1438.2.3 by Aaron Bentley
Cleanups
37
38
            Does not support configs of type 'local'.
1438.2.1 by Aaron Bentley
Add convert_config.py
39
            '''))
40
    parser.add_argument('config_dir', metavar='OUTPUT_DIR',
41
                        help='Directory to write updated configs to.')
42
    args = parser.parse_args()
43
    clouds_credentials = JujuData('', {})
44
    clouds_credentials.load_yaml()
45
    for environment, config in get_environments().items():
46
        if config['type'] == 'local':
47
            continue
48
        env = JujuData(environment, config)
49
        env.clouds = clouds_credentials.clouds
50
        env.credentials = clouds_credentials.credentials
51
        print(environment)
52
        sys.stdout.flush()
53
        out_path = os.path.join(args.config_dir,
54
                                '{}.yaml'.format(environment))
55
        with open(out_path, 'w') as out_file:
56
            write_new_config(env, out_file)
57
58
59
if __name__ == '__main__':
60
    main()