~nskaggs/juju-ci-tools/add-assess-terms

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 jujupy import (
1845.1.1 by Aaron Bentley
Provide fakejuju functionality from jujupy
12
    fake_juju_client,
1438.2.1 by Aaron Bentley
Add convert_config.py
13
    JujuData,
14
    )
1889.1.5 by Aaron Bentley
Extract jujuconfig to jujupy.configuration.
15
from jujupy.configuration import get_environments
1438.2.1 by Aaron Bentley
Add convert_config.py
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()
1699.1.2 by Aaron Bentley
Convert direct config access to get_option.
23
    agent_version = env.get_option('agent-version')
24
    if agent_version is not None:
25
        config['agent-version'] = agent_version
1438.2.1 by Aaron Bentley
Add convert_config.py
26
    else:
27
        config.pop('agent-version', None)
28
    yaml.dump(config, out, default_flow_style=False)
29
30
31
def main():
32
    parser = ArgumentParser(
33
        description=dedent('''\
1438.2.3 by Aaron Bentley
Cleanups
34
            Convert environments.yaml to 2.0 format.
1438.2.1 by Aaron Bentley
Add convert_config.py
35
1438.2.2 by Aaron Bentley
Cleanups
36
            environments.yaml from JUJU_HOME will be used.
1438.2.1 by Aaron Bentley
Add convert_config.py
37
            Existing configs in the output directory will be overwritten.
1438.2.3 by Aaron Bentley
Cleanups
38
39
            Does not support configs of type 'local'.
1438.2.1 by Aaron Bentley
Add convert_config.py
40
            '''))
41
    parser.add_argument('config_dir', metavar='OUTPUT_DIR',
42
                        help='Directory to write updated configs to.')
43
    args = parser.parse_args()
44
    clouds_credentials = JujuData('', {})
45
    clouds_credentials.load_yaml()
46
    for environment, config in get_environments().items():
47
        if config['type'] == 'local':
48
            continue
49
        env = JujuData(environment, config)
50
        env.clouds = clouds_credentials.clouds
51
        env.credentials = clouds_credentials.credentials
52
        print(environment)
53
        sys.stdout.flush()
54
        out_path = os.path.join(args.config_dir,
55
                                '{}.yaml'.format(environment))
56
        with open(out_path, 'w') as out_file:
57
            write_new_config(env, out_file)
58
59
60
if __name__ == '__main__':
61
    main()