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

1653.2.1 by Aaron Bentley
Add raw bootstrap test.
1
#!/usr/bin/env python
1704.1.4 by Andrew Beach
Added tests for main.
2
from __future__ import print_function
3
1653.2.5 by Aaron Bentley
Improve error logging, add --start support.
4
from argparse import ArgumentParser
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
5
import logging
6
import os
7
import sys
8
9
import yaml
10
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
11
from deploy_stack import (
12
    BootstrapManager,
13
    )
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
14
from jujuconfig import get_juju_home
1653.2.6 by Aaron Bentley
Truncate env name, fix destroy-controller.
15
from jujupy import (
16
    client_from_config,
1704.1.13 by Andrew Beach
Fixed the terrible mistake of dupplication.
17
    )
18
from assess_cloud import (
19
    assess_cloud_combined,
1653.2.6 by Aaron Bentley
Truncate env name, fix destroy-controller.
20
    )
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
21
from utility import (
22
    _clean_dir,
23
    configure_logging,
1695.3.2 by Andrew Beach
Work on tests.
24
    _generate_default_binary,
1695.3.8 by Andrew Beach
Updates in responce to merge comments.
25
    LoggedException,
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
26
    _to_deadline,
27
    )
28
29
1704.1.8 by Andrew Beach
Moved bootstrap_public_clouds.py => assess_public_clouds.py.
30
log = logging.getLogger('assess_public_clouds')
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
31
32
33
def make_logging_dir(base_dir, config, region):
34
    log_dir = os.path.join(base_dir, config, region)
1704.1.3 by Andrew Beach
Added tests for the helpers in bootstrap_public_clouds.py.
35
    os.makedirs(log_dir)
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
36
    return log_dir
37
38
1704.1.12 by Andrew Beach
assess_public_clouds does a more complete set of tests on each cloud.
39
def prepare_cloud(config, region, client, log_dir):
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
40
    env_name = 'boot-cpc-{}-{}'.format(client.env.get_cloud(), region)[:30]
1695.3.8 by Andrew Beach
Updates in responce to merge comments.
41
    logging_dir = make_logging_dir(log_dir, config, region)
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
42
    bs_manager = BootstrapManager(
1695.3.8 by Andrew Beach
Updates in responce to merge comments.
43
        env_name, client, client, bootstrap_host=None, machines=[],
44
        series=None, agent_url=None, agent_stream=None, region=region,
1704.1.5 by Andrew Beach
Tired, can't get TestBootstrapCloud working.
45
        log_dir=logging_dir, keep_env=False, permanent=True, jes_enabled=True)
1704.1.13 by Andrew Beach
Fixed the terrible mistake of dupplication.
46
    assess_cloud_combined(bs_manager)
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
47
48
49
def iter_cloud_regions(public_clouds, credentials):
50
    configs = {
51
        'aws': 'default-aws',
1653.2.12 by Aaron Bentley
Add comment on aws-china.
52
        # sinzui: We may lose this access. No one remaining at Canonical can
53
        # access the account. There is talk of terminating it.
1653.2.3 by Aaron Bentley
Exceptions are non-terminal, fix aws-cn.
54
        'aws-china': 'default-aws-cn',
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
55
        'azure': 'default-azure',
1653.2.10 by Aaron Bentley
default-gce, not default-google.
56
        'google': 'default-gce',
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
57
        'joyent': 'default-joyent',
58
        'rackspace': 'default-rackspace',
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
59
        }
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
60
    for cloud, info in sorted(public_clouds.items()):
61
        if cloud not in credentials:
62
            logging.warning('No credentials for {}.  Skipping.'.format(cloud))
63
            continue
64
        config = configs[cloud]
65
        for region in sorted(info['regions']):
66
            yield config, region
67
1668.1.1 by Andrew Beach
Fixed bootstrap_public_clouds.py to lint standard.
68
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
69
def bootstrap_cloud_regions(public_clouds, credentials, args):
1653.2.3 by Aaron Bentley
Exceptions are non-terminal, fix aws-cn.
70
    cloud_regions = list(iter_cloud_regions(public_clouds, credentials))
71
    for num, (config, region) in enumerate(cloud_regions):
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
72
        if num < args.start:
1653.2.5 by Aaron Bentley
Improve error logging, add --start support.
73
            continue
1653.2.3 by Aaron Bentley
Exceptions are non-terminal, fix aws-cn.
74
        logging.info('Bootstrapping {} {} #{}'.format(config, region, num))
75
        try:
1695.3.8 by Andrew Beach
Updates in responce to merge comments.
76
            client = client_from_config(
77
                config, args.juju_bin, args.debug, args.deadline)
1704.1.12 by Andrew Beach
assess_public_clouds does a more complete set of tests on each cloud.
78
            prepare_cloud(config, region, client, args.logs)
1695.3.8 by Andrew Beach
Updates in responce to merge comments.
79
        except LoggedException as error:
80
            yield config, region, error.exception
81
        except Exception as error:
82
            yield config, region, error
1653.2.3 by Aaron Bentley
Exceptions are non-terminal, fix aws-cn.
83
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
84
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
85
def parse_args(argv):
86
    """Parse all arguments."""
87
    parser = ArgumentParser(
88
        description="This is a quick hack to test 0052b26.  HERE BE DRAGONS!")
89
    parser.add_argument('juju_bin', nargs='?',
90
                        help='Full path to the Juju binary. By default, this'
91
                        ' will use $GOPATH/bin/juju or /usr/bin/juju in that'
92
                        ' order.', default=_generate_default_binary())
93
    parser.add_argument('logs', nargs='?', type=_clean_dir,
94
                        help='A directory in which to store logs. By default,'
95
                        ' this will use the current directory', default=None)
96
    parser.add_argument('--start', type=int, default=0)
97
    parser.add_argument('--debug', action='store_true', default=False,
98
                        help='Pass --debug to Juju.')
99
    parser.add_argument('--timeout', dest='deadline', type=_to_deadline,
100
                        help="The script timeout, in seconds.")
101
    return parser.parse_args(argv)
102
103
104
def yaml_file_load(file_name):
105
    with open(os.path.join(get_juju_home(), file_name)) as file:
106
        yaml_data = yaml.safe_load(file)
107
    return yaml_data
108
109
110
def default_log_dir(settings):
111
    if settings.logs is None:
112
        settings.logs = BootstrapManager._generate_default_clean_dir(
1704.1.8 by Andrew Beach
Moved bootstrap_public_clouds.py => assess_public_clouds.py.
113
            'assess_public_clouds')
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
114
1695.3.7 by Andrew Beach
Fixed to lint standard.
115
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
116
def main():
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
117
    configure_logging(logging.INFO)
118
    args = parse_args(None)
119
    default_log_dir(args)
1653.2.3 by Aaron Bentley
Exceptions are non-terminal, fix aws-cn.
120
    logging.warning('This is a quick hack to test 0052b26.  HERE BE DRAGONS!')
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
121
    public_clouds = yaml_file_load('public-clouds.yaml')['clouds']
122
    credentials = yaml_file_load('credentials.yaml')['credentials']
1653.2.4 by Aaron Bentley
Handle failures, shorten env names.
123
    failures = []
124
    try:
1653.2.5 by Aaron Bentley
Improve error logging, add --start support.
125
        for failure in bootstrap_cloud_regions(public_clouds, credentials,
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
126
                                               args):
1653.2.5 by Aaron Bentley
Improve error logging, add --start support.
127
            failures.append(failure)
1653.2.4 by Aaron Bentley
Handle failures, shorten env names.
128
    finally:
129
        if len(failures) == 0:
130
            print('No failures!')
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
131
            return 0
1653.2.4 by Aaron Bentley
Handle failures, shorten env names.
132
        else:
1653.2.7 by Aaron Bentley
Nicer failure formatting.
133
            print('Failed:')
134
            for config, region, e in failures:
135
                print(' * {} {} {}'.format(config, region, e))
1695.3.1 by Andrew Beach
Working on upgrading bootstrap_public_clouds, now I just need tests.
136
            return 1
1653.2.7 by Aaron Bentley
Nicer failure formatting.
137
1653.2.1 by Aaron Bentley
Add raw bootstrap test.
138
139
if __name__ == '__main__':
140
    sys.exit(main())