~juju-deployers/juju-deployer/darwin

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python
"""
Juju Deployer

Deployment automation for juju.

"""


import argparse
import logging
import os
import sys
import time

from deployer.config import ConfigStack
from deployer.env import select_runtime
from deployer.action import diff, importer
from deployer.utils import ErrorExit, setup_logging


def setup_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c', '--config',
        help=('File containing deployment(s) json config. This '
              'option can be repeated, with later files overriding '
              'values in earlier ones.'),
        dest='configs', action='append')
    parser.add_argument(
        '-d', '--debug', help='Enable debugging to stdout',
        dest="debug",
        action="store_true", default=False)
    parser.add_argument(
        '-L', '--local-mods',
        help='Disallow deployment of locally-modified charms',
        dest="no_local_mods", default=True, action='store_false')
    parser.add_argument(
        '-u', '--update-charms',
        help='Update existing charm branches',
        dest="update_charms", default=False, action="store_true")
    parser.add_argument(
        '-l', '--ls', help='List available deployments',
        dest="list_deploys", action="store_true", default=False)
    parser.add_argument(
        '-D', '--destroy-services',
        help='Destroy all services (do not terminate machines)',
        dest="destroy_services", action="store_true",
        default=False)
    parser.add_argument(
        '-T', '--terminate-machines',
        help=('Terminate all machines but the bootstrap node.  '
              'Destroy any services that exist on each'),
        dest="terminate_machines", action="store_true",
        default=False)
    parser.add_argument(
        '-t', '--timeout',
        help='Timeout (sec) for entire deployment (45min default)',
        dest='timeout', action='store', type=int, default=2700)
    parser.add_argument(
        "-f", '--find-service', action="store", type=str,
        help='Find hostname from first unit of a specific service.',
        dest="find_service")
    parser.add_argument(
        "-b", '--branch-only', action="store_true",
        help='Update vcs branches and exit.',
        dest="branch_only")
    parser.add_argument(
        '-s', '--deploy-delay', action='store', type=float,
        help=("Time in seconds to sleep between 'deploy' commands, "
              "to allow machine provider to process requests. On "
              "terminate machines this also signals waiting for "
              "machine removal."),
        dest="deploy_delay", default=0)
    parser.add_argument(
        '-e', '--environment', action='store', dest='juju_env',
        help='Deploy to a specific Juju environment.',
        default=os.getenv('JUJU_ENV'))
    parser.add_argument(
        '-o', '--override', action='append', type=str,
        help=('Override *all* config options of the same name '
              'across all services.  Input as key=value.'),
        dest='overrides', default=None)
    parser.add_argument(
        '-v', '--verbose', action='store_true', default=False,
        dest="verbose", help='Verbose output')
    parser.add_argument(
        '-W', '--watch', help='Watch environment changes on console',
        dest="watch", action="store_true", default=False)
    parser.add_argument(
        '-r', "--retry", default=0, type=int, dest="retry_count",
        help=("Resolve CLI and unit errors via number of retries (default: 0)."
              " Either standalone or in a deployment"))
    parser.add_argument(
        "--diff", action="store_true", default=False,
        help=("Generate a delta between a configured deployment and a running"
             " environment."))
    parser.add_argument(
        '-w', '--relation-wait', action='store', dest='rel_wait',
        default=60, type=int,
        help=('Number of seconds to wait before checking for '
              'relation errors after all relations have been added '
              'and subordinates started. (default: 60)'))
    parser.add_argument("--description", help=argparse.SUPPRESS,
                        action="store_true")
    parser.add_argument("deployment", nargs="?")
    return parser


def main():
    stime = time.time()
    try:
        run()
    except ErrorExit:
        logging.getLogger('deployer.cli').info(
            "Deployment stopped. run time: %0.2f", time.time() - stime)
        sys.exit(1)


def run():
    parser = setup_parser()
    options = parser.parse_args()

    if options.description:
        print "Tool for declarative management of complex deployments."
        sys.exit(0)

    # Debug implies watching and verbose
    if options.debug:
        options.watch = options.verbose = True
    setup_logging(options.verbose, options.debug)

    log = logging.getLogger("deployer.cli")
    start_time = time.time()

    env = select_runtime(options.juju_env, options)
    log.debug('Using runtime %s', env.__class__.__name__)

    config = ConfigStack(options.configs or [])

    # Destroy services and exit
    if options.destroy_services or options.terminate_machines:
        log.info("Resetting environment...")
        env.connect()
        env.reset(terminate_machines=options.terminate_machines,
                  terminate_delay=options.deploy_delay,
                  watch=options.watch)
        log.info("Environment reset in %0.2f", time.time() - start_time)
        sys.exit(0)

    # Display service info and exit
    if options.find_service:
        address = env.get_service_address(options.find_service)
        if address is None:
            log.error("Service not found %r", options.find_service)
            sys.exit(1)
        elif not address:
            log.warning("Service: %s has no address for first unit",
                        options.find_service)
        else:
            log.info("Service: %s address: %s", options.find_service, address)
            print address
        sys.exit(0)

    # Just resolve/retry hooks in the environment
    if not options.deployment and options.retry_count:
        log.info("Retrying hooks for error resolution")
        env.connect()
        env.resolve_errors(
            options.retry_count, watch=options.watch, timeout=options.timeout)

    # Arg check on config files and deployment name.
    if not options.configs:
        log.error("Config files must be specified")
        sys.exit(1)

    config.load()

    # Just list the available deployments
    if options.list_deploys:
        print "\n".join(sorted(config.keys()))
        sys.exit(0)

    # Do something to a deployment
    if not options.deployment:
        log.error(
            "Deployment name must be specified. available: %s", tuple(
                sorted(config.keys())))
        sys.exit(1)

    deployment = config.get(options.deployment)

    if options.diff:
        diff.Diff(env, deployment, options).run()
        return

    # Import it
    log.info("Starting deployment of %s", options.deployment)
    importer.Importer(env, deployment, options).run()

    # Deploy complete
    log.info("Deployment complete in %0.2f seconds" % (
        time.time() - start_time))


if __name__ == '__main__':
    main()