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

« back to all changes in this revision

Viewing changes to assess_bootstrap.py

  • Committer: John George
  • Date: 2016-02-05 22:13:41 UTC
  • mto: This revision was merged to the branch mainline in revision 1275.
  • Revision ID: john.george@canonical.com-20160205221341-3v2px3j23ik1zw5r
virtual maas templates and setup script.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from __future__ import print_function
3
3
 
4
4
from argparse import ArgumentParser
5
 
from contextlib import contextmanager
6
5
import logging
 
6
import os.path
7
7
import sys
8
8
 
9
9
from deploy_stack import (
10
10
    BootstrapManager,
11
11
    tear_down,
12
12
    )
 
13
from jujupy import (
 
14
    EnvJujuClient,
 
15
    SimpleEnvironment,
 
16
    )
13
17
from utility import (
14
 
    add_basic_testing_arguments,
15
18
    configure_logging,
16
 
    JujuAssertionError,
 
19
    LoggedException,
 
20
    scoped_environ,
17
21
    temp_dir,
18
 
    )
 
22
)
19
23
 
20
24
 
21
25
log = logging.getLogger("assess_bootstrap")
22
26
 
23
27
 
24
 
INVALID_URL = 'example.com/invalid'
25
 
 
26
 
 
27
 
def assess_base_bootstrap(bs_manager):
28
 
    client = bs_manager.client
29
 
    with bs_manager.top_context() as machines:
30
 
        with bs_manager.bootstrap_context(machines):
31
 
            tear_down(client, client.is_jes_enabled())
32
 
            client.bootstrap()
33
 
        with bs_manager.runtime_context(machines):
34
 
            client.get_status(1)
35
 
            log.info('Environment successfully bootstrapped.')
36
 
 
37
 
 
38
 
def prepare_metadata(client, local_dir):
39
 
    """Fill the given directory with metadata using sync_tools."""
40
 
    client.sync_tools(local_dir)
41
 
 
42
 
 
43
 
@contextmanager
44
 
def prepare_temp_metadata(client, source_dir=None):
45
 
    """Fill a temporary directory with metadata using sync_tools."""
46
 
    if source_dir is not None:
47
 
        yield source_dir
48
 
    else:
49
 
        with temp_dir() as md_dir:
50
 
            prepare_metadata(client, md_dir)
51
 
            yield md_dir
52
 
 
53
 
 
54
 
def assess_metadata(bs_manager, local_source):
55
 
    client = bs_manager.client
56
 
    # This disconnects from the metadata source, as INVALID_URL is different.
57
 
    # agent-metadata-url | tools-metadata-url
58
 
    client.env.config['agent-metadata-url'] = INVALID_URL
59
 
    with prepare_temp_metadata(client, local_source) as metadata_dir:
60
 
        log.info('Metadata written to: {}'.format(metadata_dir))
 
28
def assess_bootstrap(juju, env, debug, region, temp_env_name):
 
29
    with scoped_environ():
 
30
        juju_bin = os.path.dirname(os.path.abspath(juju))
 
31
        os.environ['PATH'] = '{}:{}'.format(juju_bin, os.environ['PATH'])
 
32
        client = EnvJujuClient.by_version(SimpleEnvironment.from_config(env),
 
33
                                          juju, debug)
 
34
    jes_enabled = client.is_jes_enabled()
 
35
    if temp_env_name is None:
 
36
        temp_env_name = client.env.environment
 
37
    with temp_dir() as log_dir:
 
38
        bs_manager = BootstrapManager(
 
39
            temp_env_name, client, client, region=region,
 
40
            permanent=jes_enabled, jes_enabled=jes_enabled, log_dir=log_dir,
 
41
            bootstrap_host=None, machines=[], series=None, agent_url=None,
 
42
            agent_stream=None, keep_env=False)
61
43
        with bs_manager.top_context() as machines:
62
44
            with bs_manager.bootstrap_context(machines):
63
 
                tear_down(client, client.is_jes_enabled())
64
 
                client.bootstrap(metadata_source=metadata_dir)
 
45
                tear_down(client, jes_enabled)
 
46
                client.bootstrap()
65
47
            with bs_manager.runtime_context(machines):
66
 
                log.info('Metadata bootstrap successful.')
67
 
                data = client.get_model_config()
68
 
                if INVALID_URL != data['agent-metadata-url']['value']:
69
 
                    raise JujuAssertionError('Error, possible web metadata.')
70
 
 
71
 
 
72
 
def assess_bootstrap(args):
73
 
    bs_manager = BootstrapManager.from_args(args)
74
 
    if 'base' == args.part:
75
 
        assess_base_bootstrap(bs_manager)
76
 
    elif 'metadata' == args.part:
77
 
        assess_metadata(bs_manager, args.local_metadata_source)
 
48
                client.get_status(1)
 
49
                log.info('Environment successfully bootstrapped.')
78
50
 
79
51
 
80
52
def parse_args(argv=None):
81
 
    """Parse all arguments.
82
 
 
83
 
    In addition to the basic testing arguments this script also accepts:
84
 
    part: The first argument, which is the name of test part to run.
85
 
    --local-metadata-source: If given it should be a directory that contains
86
 
    the agent to use in the test. This skips downloading them."""
87
 
    parser = ArgumentParser(description='Test the bootstrap command.')
88
 
    parser.add_argument('part', choices=['base', 'metadata'],
89
 
                        help='Which part of bootstrap to assess')
90
 
    add_basic_testing_arguments(parser)
91
 
    parser.add_argument('--local-metadata-source',
92
 
                        action='store', default=None,
93
 
                        help='Directory with pre-loaded metadata.')
 
53
    parser = ArgumentParser()
 
54
    parser.add_argument('juju', help="The Juju client to use.")
 
55
    parser.add_argument('env', help="The environment to test with.")
 
56
    parser.add_argument('temp_env_name', nargs='?',
 
57
                        help="Temporary environment name to use.")
 
58
    parser.add_argument('--debug', action="store_true", default=False,
 
59
                        help='Use --debug juju logging.')
 
60
    parser.add_argument('--region', help='Override environment region.')
94
61
    return parser.parse_args(argv)
95
62
 
96
63
 
97
 
def main(argv=None):
98
 
    args = parse_args(argv)
99
 
    configure_logging(args.verbose)
100
 
    assess_bootstrap(args)
101
 
    return 0
 
64
def main():
 
65
    args = parse_args()
 
66
    configure_logging(logging.INFO)
 
67
    try:
 
68
        assess_bootstrap(**args.__dict__)
 
69
    except LoggedException:
 
70
        sys.exit(1)
102
71
 
103
72
 
104
73
if __name__ == '__main__':
105
 
    sys.exit(main())
 
74
    main()