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

« back to all changes in this revision

Viewing changes to run_deployer.py

  • Committer: Martin Packman
  • Date: 2015-12-16 03:59:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1200.
  • Revision ID: martin.packman@canonical.com-20151216035938-881wbry7ezh8he5m
Add missing template test file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
from argparse import ArgumentParser
3
3
import logging
4
 
import pickle
5
4
import subprocess
6
 
import sys
7
5
 
8
6
from deploy_stack import (
9
7
    assess_upgrade,
10
8
    boot_context,
11
9
)
12
10
from jujupy import (
13
 
    client_from_config,
14
 
)
15
 
from remote import (
16
 
    remote_from_unit,
 
11
    EnvJujuClient,
 
12
    SimpleEnvironment,
17
13
)
18
14
from utility import (
19
15
    add_basic_testing_arguments,
20
16
    configure_logging,
21
 
    run_command,
22
 
    scoped_environ,
23
17
)
24
18
 
25
19
 
26
 
class ErrUnitCondition(Exception):
27
 
    """An exception for an unknown condition type."""
28
 
 
29
 
 
30
20
def parse_args(argv=None):
31
21
    parser = ArgumentParser()
32
22
    parser.add_argument('--upgrade', action="store_true", default=False,
34
24
    parser.add_argument('bundle_path',
35
25
                        help='URL or path to a bundle')
36
26
    add_basic_testing_arguments(parser)
37
 
    parser.add_argument('--allow-native-deploy', action='store_true',
38
 
                        help='Let juju 2 use native bundle deploying.')
39
 
    parser.add_argument('--bundle-verification-script',
40
 
                        help='Script to verify the bundle.')
41
27
    parser.add_argument('--bundle-name', default=None,
42
28
                        help='Name of the bundle to deploy.')
43
29
    parser.add_argument('--health-cmd', default=None,
44
30
                        help='A binary for checking the health of the'
45
31
                        ' deployed bundle.')
46
 
    parser.add_argument('--upgrade-condition', action='append', default=None,
47
 
                        help='unit_name:<conditions>'
48
 
                        ' One or more of the following conditions to apply'
49
 
                        ' to the given unit_name: clock_skew.')
50
 
    parser.add_argument('--agent-timeout', type=int, default=1200,
51
 
                        help='The time to wait for agents to start')
52
 
    parser.add_argument('--workload-timeout', type=int, default=1800,
53
 
                        help='The time to wait for workloads to active')
54
 
    args = parser.parse_args(argv)
55
 
    if args.allow_native_deploy and args.bundle_name:
56
 
        parser.error('cannot supply bundle name with native juju deploying')
57
 
    return args
58
 
 
59
 
 
60
 
def check_health(cmd_path, env_name='', environ=None):
 
32
    return parser.parse_args(argv)
 
33
 
 
34
 
 
35
def check_health(cmd_path, env_name=''):
61
36
    """Run the health checker command and raise on error."""
62
37
    try:
63
38
        cmd = (cmd_path, env_name)
64
 
        logging.info('Calling {}'.format(cmd))
65
 
        if environ:
66
 
            logging.info('PATH: {}'.format(environ.get('PATH')))
67
 
            logging.info('JUJU_HOME: {}'.format(environ.get('JUJU_HOME')))
68
 
        with scoped_environ(environ):
69
 
            sub_output = subprocess.check_output(cmd)
 
39
        logging.debug('Calling {}'.format(cmd))
 
40
        sub_output = subprocess.check_output(cmd)
70
41
        logging.info('Health check output: {}'.format(sub_output))
71
42
    except OSError as e:
72
43
        logging.error(
80
51
        raise
81
52
 
82
53
 
83
 
CLOCK_SKEW_SCRIPT = """
84
 
        now=$(date +%s)
85
 
        let future=$now+600
86
 
        sudo date --set @$future
87
 
    """
88
 
 
89
 
 
90
 
def apply_condition(client, condition):
91
 
    """Apply an adverse condition to the given unit."""
92
 
    unit, action = condition.split(':', 1)
93
 
    logging.info('Applying {} to unit {}'.format(action, unit))
94
 
    remote = remote_from_unit(client, unit)
95
 
    if not remote.is_windows():
96
 
        if action == 'clock_skew':
97
 
            result = remote.run(CLOCK_SKEW_SCRIPT)
98
 
            logging.info('Clock on {} set to: {}'.format(unit, result))
99
 
        else:
100
 
            raise ErrUnitCondition("%s: Unknown condition type." % action)
101
 
 
102
 
 
103
 
def assess_deployer(args, client, agent_timeout, workload_timeout):
104
 
    """Run juju-deployer, based on command line configuration values."""
105
 
    if args.allow_native_deploy:
106
 
        client.deploy_bundle(args.bundle_path)
107
 
        client.wait_for_started(timeout=agent_timeout)
108
 
    else:
109
 
        client.deployer(args.bundle_path, args.bundle_name)
110
 
    client.wait_for_workloads(timeout=workload_timeout)
111
 
    if args.health_cmd:
112
 
        environ = client._shell_environ()
113
 
        check_health(args.health_cmd, args.temp_env_name, environ)
114
 
    if args.upgrade:
115
 
        client.show_status()
116
 
        if args.upgrade_condition:
117
 
            for condition in args.upgrade_condition:
118
 
                apply_condition(client, condition)
119
 
        assess_upgrade(client, args.juju_bin)
120
 
        client.wait_for_workloads()
121
 
        if args.health_cmd:
122
 
            environ = client._shell_environ()
123
 
            check_health(args.health_cmd, args.temp_env_name, environ)
124
 
 
125
 
 
126
 
def main(argv=None):
 
54
def run_deployer(argv=None):
127
55
    args = parse_args(argv)
128
56
    configure_logging(args.verbose)
 
57
    env = SimpleEnvironment.from_config(args.env)
129
58
    start_juju_path = None if args.upgrade else args.juju_bin
130
 
    client = client_from_config(args.env, start_juju_path, debug=args.debug,
131
 
                                soft_deadline=args.deadline)
 
59
    client = EnvJujuClient.by_version(env, start_juju_path, debug=args.debug)
132
60
    with boot_context(args.temp_env_name, client, None, [], args.series,
133
61
                      args.agent_url, args.agent_stream, args.logs,
134
 
                      args.keep_env, upload_tools=args.upload_tools,
135
 
                      region=args.region):
136
 
        assess_deployer(
137
 
            args, client, args.agent_timeout, args.workload_timeout)
138
 
        if args.bundle_verification_script:
139
 
            client_ser = pickle.dumps(client)
140
 
            logging.info('Calling bundle verification script {}'.format(
141
 
                args.bundle_verification_script))
142
 
            run_command([args.bundle_verification_script, client_ser])
143
 
    return 0
144
 
 
145
 
 
 
62
                      args.keep_env, False, region=args.region):
 
63
        client.deployer(args.bundle_path, args.bundle_name)
 
64
        if args.health_cmd:
 
65
            check_health(args.health_cmd, args.temp_env_name)
 
66
        if args.upgrade:
 
67
            client.juju('status', ())
 
68
            assess_upgrade(client, args.juju_bin)
 
69
            if args.health_cmd:
 
70
                check_health(args.health_cmd, args.temp_env_name)
146
71
if __name__ == '__main__':
147
 
    sys.exit(main())
 
72
    run_deployer()