~veebers/juju-ci-tools/model_migration_check_all_units_of_charm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /usr/bin/env python


from argparse import ArgumentParser
import json
import os

from jujuconfig import get_selected_environment


def describe_env(env):
    environ = get_selected_environment(env)[0]
    if environ['type'] == 'local':
        return {
            'kvm': 'KVM (local)',
            'lxc': 'LXC (local)'
        }[environ.get('container', 'lxc')]
    elif environ['type'] == 'openstack':
        if environ['auth-url'].endswith('hpcloudsvc.com:35357/v2.0/'):
            return 'HPCloud'
        elif environ['auth-url'] == (
                'https://keystone.canonistack.canonical.com:443/v2.0/'):
            return 'Canonistack'
    try:
        return {
            'ec2': 'AWS',
            'joyent': 'Joyent',
            'azure': 'Azure',
        }[environ['type']]
    except KeyError:
        return environ['type']


def main():
    parser = ArgumentParser()
    parser.add_argument('file')
    parser.add_argument('env', nargs='*', default=('local',))
    args = parser.parse_args()
    with open(args.file) as file_obj:
        test_json = json.load(file_obj)
    metadata = test_json.setdefault('metadata', {})
    metadata['job_name'] = os.environ['JOB_NAME']
    metadata['build_number'] = os.environ['BUILD_NUMBER']
    metadata['build_url'] = os.environ['BUILD_URL']
    metadata['environments'] = dict((env, {'substrate': describe_env(env)})
             for env in args.env)
    with open(args.file, 'w') as file_obj:
        test_json = json.dump(test_json, file_obj, indent=2)

if __name__ == '__main__':
    main()