~sseman/juju-ci-tools/model-change-watcher-py3-2

169.1.1 by Aaron Bentley
Add cloud_deploy script.
1
#!/usr/bin/env python
587 by Curtis Hovey
Add support for -debug=true when testing clouds. --debug is still unsafe.
2
from __future__ import print_function
3
169.1.1 by Aaron Bentley
Add cloud_deploy script.
4
from argparse import ArgumentParser
587 by Curtis Hovey
Add support for -debug=true when testing clouds. --debug is still unsafe.
5
import os
181.1.2 by Aaron Bentley
Fix import.
6
import subprocess
169.1.1 by Aaron Bentley
Add cloud_deploy script.
7
import sys
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
8
from time import sleep
169.1.1 by Aaron Bentley
Add cloud_deploy script.
9
1485.1.1 by Martin
Switch all imports of local_charm_path to using jujucharm over utility
10
from jujucharm import (
11
    local_charm_path,
12
)
169.1.1 by Aaron Bentley
Add cloud_deploy script.
13
from jujupy import (
185.1.1 by Aaron Bentley
Fix cloud test handling of 'Unable to connect to environment.'
14
    CannotConnectEnv,
1465.5.15 by Aaron Bentley
Switch cloud_deploy to client_from_config.
15
    client_from_config,
719.1.3 by John George
Move libvirt_domain functions from jujupy to substrate.
16
)
17
from substrate import (
719.1.4 by John George
Add LIBVIRT_DOMAIN state constants and sleep statements, to increase the interval between virsh calls.
18
    LIBVIRT_DOMAIN_RUNNING,
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
19
    start_libvirt_domain,
20
    stop_libvirt_domain,
719.1.3 by John George
Move libvirt_domain functions from jujupy to substrate.
21
    verify_libvirt_domain,
169.1.1 by Aaron Bentley
Add cloud_deploy script.
22
)
23
24
1092.2.2 by Aaron Bentley
Fix lint.
25
__metaclass__ = type
26
27
725.1.1 by Abel Deuring
cloud-deploy.py: Deploy only one charm; do this only when the cmd line parameter --deploy-charm is specified.
28
def deploy_stack(environment, debug, machines, deploy_charm):
169.1.1 by Aaron Bentley
Add cloud_deploy script.
29
    """"Deploy a test stack in the specified environment.
30
31
    :param environment: The name of the desired environment.
32
    """
1465.5.15 by Aaron Bentley
Switch cloud_deploy to client_from_config.
33
    client = client_from_config(environment, None, debug=debug)
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
34
    running_domains = dict()
1674.1.4 by Aaron Bentley
More conversion.
35
    if client.env.provider == 'maas':
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
36
        # Split the hypervisor_URI and machine name
37
        for machine in machines:
38
            name, URI = machine.split('@')
39
            # Record already running domains, so they can be left running,
40
            # if already running; otherwise start them.
719.1.4 by John George
Add LIBVIRT_DOMAIN state constants and sleep statements, to increase the interval between virsh calls.
41
            if verify_libvirt_domain(URI, name, LIBVIRT_DOMAIN_RUNNING):
727.1.1 by John George
Remove the conditional stop of MaaS servers, to ensure a clean environment.
42
                print("%s is already running" % name)
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
43
                running_domains = {machine: True}
44
            else:
45
                running_domains = {machine: False}
46
                print("Attempting to start %s at %s" % (name, URI))
47
                status_msg = start_libvirt_domain(URI, name)
48
                print("%s" % status_msg)
169.1.2 by Aaron Bentley
Start by destroying environment to make cloud test more robust.
49
    # Clean up any leftover junk
1080.1.2 by Aaron Bentley
Switch cloud-deploy to EnvJujuClient.
50
    client.destroy_environment()
51
    client.bootstrap()
169.1.1 by Aaron Bentley
Add cloud_deploy script.
52
    try:
53
        # wait for status info....
181.1.1 by Aaron Bentley
Emit stderr for failures running status.
54
        try:
185.1.1 by Aaron Bentley
Fix cloud test handling of 'Unable to connect to environment.'
55
            try:
1080.1.2 by Aaron Bentley
Switch cloud-deploy to EnvJujuClient.
56
                client.get_status()
185.1.1 by Aaron Bentley
Fix cloud test handling of 'Unable to connect to environment.'
57
            except CannotConnectEnv:
587 by Curtis Hovey
Add support for -debug=true when testing clouds. --debug is still unsafe.
58
                print("Status got Unable to connect to env.  Retrying...")
1080.1.2 by Aaron Bentley
Switch cloud-deploy to EnvJujuClient.
59
                client.get_status()
60
            client.wait_for_started()
725.1.1 by Abel Deuring
cloud-deploy.py: Deploy only one charm; do this only when the cmd line parameter --deploy-charm is specified.
61
            if deploy_charm:
1699.1.2 by Aaron Bentley
Convert direct config access to get_option.
62
                series = client.env.get_option('default-series', 'trusty')
1345.1.3 by Seman
Deploy charm by path.
63
                charm_path = local_charm_path(
64
                    'dummy-source', juju_ver=client.version, series=series)
65
                client.deploy(charm_path, series=series)
1080.1.2 by Aaron Bentley
Switch cloud-deploy to EnvJujuClient.
66
                client.wait_for_started()
181.1.1 by Aaron Bentley
Emit stderr for failures running status.
67
        except subprocess.CalledProcessError as e:
68
            if getattr(e, 'stderr', None) is not None:
69
                sys.stderr.write(e.stderr)
70
            raise
169.1.1 by Aaron Bentley
Add cloud_deploy script.
71
    finally:
1080.1.2 by Aaron Bentley
Switch cloud-deploy to EnvJujuClient.
72
        client.destroy_environment()
1674.1.4 by Aaron Bentley
More conversion.
73
        if client.env.provider == 'maas':
727.1.2 by John George
Move MaaS server shutdown inside the finally block, for cloud_deploy.py
74
            sleep(90)
75
            for machine, running in running_domains.items():
727.1.3 by John George
Move name and URI definition before first use.
76
                name, URI = machine.split('@')
727.1.2 by John George
Move MaaS server shutdown inside the finally block, for cloud_deploy.py
77
                if running:
78
                    print("WARNING: %s at %s was running when deploy_job "
79
                          "started. Shutting it down to ensure a clean "
80
                          "environment."
81
                          % (name, URI))
82
                status_msg = stop_libvirt_domain(URI, name)
83
                print("%s" % status_msg)
169.1.1 by Aaron Bentley
Add cloud_deploy script.
84
85
86
def main():
87
    parser = ArgumentParser('Test a cloud')
88
    parser.add_argument('env', help='The juju environment to test')
687.1.1 by John George
Add use of KVM start and stop functions, when env type is maas, to deploy_stack.py and cloud_deploy.py
89
    parser.add_argument('--machine', help='KVM machine to start.',
90
                        action='append', default=[])
725.1.1 by Abel Deuring
cloud-deploy.py: Deploy only one charm; do this only when the cmd line parameter --deploy-charm is specified.
91
    parser.add_argument('--deploy-charm', action='store_true')
169.1.1 by Aaron Bentley
Add cloud_deploy script.
92
    args = parser.parse_args()
587 by Curtis Hovey
Add support for -debug=true when testing clouds. --debug is still unsafe.
93
    debug = bool(os.environ.get('DEBUG') == 'true')
169.1.1 by Aaron Bentley
Add cloud_deploy script.
94
    try:
725.1.1 by Abel Deuring
cloud-deploy.py: Deploy only one charm; do this only when the cmd line parameter --deploy-charm is specified.
95
        deploy_stack(args.env, debug, args.machine, args.deploy_charm)
169.1.1 by Aaron Bentley
Add cloud_deploy script.
96
    except Exception as e:
587 by Curtis Hovey
Add support for -debug=true when testing clouds. --debug is still unsafe.
97
        print('%s: %s' % (type(e), e))
169.1.1 by Aaron Bentley
Add cloud_deploy script.
98
        sys.exit(1)
99
100
101
if __name__ == '__main__':
102
    main()