~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to convert_config.py

  • Committer: Curtis Hovey
  • Date: 2016-05-25 20:59:08 UTC
  • Revision ID: curtis@canonical.com-20160525205908-1yndw393rgkmxxcc
Revert git_gate.py change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
 
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('''\
33
 
            Convert environments.yaml to 2.0 format.
34
 
 
35
 
            environments.yaml from JUJU_HOME will be used.
36
 
            Existing configs in the output directory will be overwritten.
37
 
 
38
 
            Does not support configs of type 'local'.
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()