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

« back to all changes in this revision

Viewing changes to jujupy.py

  • Committer: Curtis Hovey
  • Date: 2014-08-01 12:44:38 UTC
  • Revision ID: curtis@canonical.com-20140801124438-l48516pldkzh7g5n
Do not show all the files in the tarball because it distracts from the test output.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from __future__ import print_function
2
2
 
3
 
from collections import (
4
 
    defaultdict,
5
 
    namedtuple,
6
 
)
7
 
from contextlib import (
8
 
    contextmanager,
9
 
    nested,
10
 
)
11
 
from copy import deepcopy
 
3
__metaclass__ = type
 
4
 
 
5
import yaml
 
6
 
 
7
from collections import defaultdict
12
8
from cStringIO import StringIO
13
 
from datetime import timedelta
14
 
import errno
15
 
from itertools import chain
16
 
import logging
 
9
from datetime import datetime
17
10
import os
18
 
import re
19
 
from shutil import rmtree
20
11
import subprocess
21
12
import sys
22
 
from tempfile import NamedTemporaryFile
23
 
import time
24
 
 
25
 
import yaml
26
 
 
27
 
from jujuconfig import (
28
 
    get_environments_path,
29
 
    get_jenv_path,
30
 
    get_juju_home,
31
 
    get_selected_environment,
32
 
)
33
 
from utility import (
34
 
    check_free_disk_space,
35
 
    ensure_deleted,
36
 
    ensure_dir,
37
 
    is_ipv6_address,
38
 
    pause,
39
 
    scoped_environ,
40
 
    split_address_port,
41
 
    temp_dir,
42
 
    until_timeout,
43
 
)
44
 
 
45
 
 
46
 
__metaclass__ = type
47
 
 
48
 
AGENTS_READY = set(['started', 'idle'])
 
13
import tempfile
 
14
 
 
15
from jujuconfig import get_selected_environment
 
16
 
 
17
 
49
18
WIN_JUJU_CMD = os.path.join('\\', 'Progra~2', 'Juju', 'juju.exe')
50
19
 
51
 
JUJU_DEV_FEATURE_FLAGS = 'JUJU_DEV_FEATURE_FLAGS'
52
 
CONTROLLER = 'controller'
53
 
KILL_CONTROLLER = 'kill-controller'
54
 
SYSTEM = 'system'
55
 
 
56
 
_DEFAULT_BUNDLE_TIMEOUT = 3600
57
 
 
58
 
_jes_cmds = {KILL_CONTROLLER: {
59
 
    'create': 'create-environment',
60
 
    'kill': KILL_CONTROLLER,
61
 
}}
62
 
for super_cmd in [SYSTEM, CONTROLLER]:
63
 
    _jes_cmds[super_cmd] = {
64
 
        'create': '{} create-environment'.format(super_cmd),
65
 
        'kill': '{} kill'.format(super_cmd),
66
 
    }
67
 
 
68
 
log = logging.getLogger("jujupy")
69
 
 
70
 
 
71
 
def get_timeout_path():
72
 
    import timeout
73
 
    return os.path.abspath(timeout.__file__)
74
 
 
75
 
 
76
 
def get_timeout_prefix(duration, timeout_path=None):
77
 
    """Return extra arguments to run a command with a timeout."""
78
 
    if timeout_path is None:
79
 
        timeout_path = get_timeout_path()
80
 
    return (sys.executable, timeout_path, '%.2f' % duration, '--')
81
 
 
82
 
 
83
 
def parse_new_state_server_from_error(error):
84
 
    err_str = str(error)
85
 
    output = getattr(error, 'output', None)
86
 
    if output is not None:
87
 
        err_str += output
88
 
    matches = re.findall(r'Attempting to connect to (.*):22', err_str)
89
 
    if matches:
90
 
        return matches[-1]
91
 
    return None
92
 
 
93
20
 
94
21
class ErroredUnit(Exception):
95
22
 
96
23
    def __init__(self, unit_name, state):
97
24
        msg = '%s is in state %s' % (unit_name, state)
98
25
        Exception.__init__(self, msg)
99
 
        self.unit_name = unit_name
100
 
        self.state = state
101
 
 
102
 
 
103
 
class BootstrapMismatch(Exception):
104
 
 
105
 
    def __init__(self, arg_name, arg_val, env_name, env_val):
106
 
        super(BootstrapMismatch, self).__init__(
107
 
            '--{} {} does not match {}: {}'.format(
108
 
                arg_name, arg_val, env_name, env_val))
109
 
 
110
 
 
111
 
class UpgradeMongoNotSupported(Exception):
112
 
 
113
 
    def __init__(self):
114
 
        super(UpgradeMongoNotSupported, self).__init__(
115
 
            'This client does not support upgrade-mongo')
116
 
 
117
 
 
118
 
class JESNotSupported(Exception):
119
 
 
120
 
    def __init__(self):
121
 
        super(JESNotSupported, self).__init__(
122
 
            'This client does not support JES')
123
 
 
124
 
 
125
 
class JESByDefault(Exception):
126
 
 
127
 
    def __init__(self):
128
 
        super(JESByDefault, self).__init__(
129
 
            'This client does not need to enable JES')
130
 
 
131
 
 
132
 
Machine = namedtuple('Machine', ['machine_id', 'info'])
 
26
 
 
27
 
 
28
class until_timeout:
 
29
 
 
30
    """Yields remaining number of seconds.  Stops when timeout is reached.
 
31
 
 
32
    :ivar timeout: Number of seconds to wait.
 
33
    """
 
34
    def __init__(self, timeout):
 
35
        self.timeout = timeout
 
36
        self.start = self.now()
 
37
 
 
38
    def __iter__(self):
 
39
        return self
 
40
 
 
41
    @staticmethod
 
42
    def now():
 
43
        return datetime.now()
 
44
 
 
45
    def next(self):
 
46
        elapsed = self.now() - self.start
 
47
        remaining = self.timeout - elapsed.total_seconds()
 
48
        if remaining <= 0:
 
49
            raise StopIteration
 
50
        return remaining
133
51
 
134
52
 
135
53
def yaml_loads(yaml_str):
136
54
    return yaml.safe_load(StringIO(yaml_str))
137
55
 
138
56
 
139
 
def coalesce_agent_status(agent_item):
140
 
    """Return the machine agent-state or the unit agent-status."""
141
 
    state = agent_item.get('agent-state')
142
 
    if state is None and agent_item.get('agent-status') is not None:
143
 
        state = agent_item.get('agent-status').get('current')
144
 
    if state is None and agent_item.get('juju-status') is not None:
145
 
        state = agent_item.get('juju-status').get('current')
146
 
    if state is None:
147
 
        state = 'no-agent'
148
 
    return state
149
 
 
150
 
 
151
 
def make_client(juju_path, debug, env_name, temp_env_name):
152
 
    env = SimpleEnvironment.from_config(env_name)
153
 
    if temp_env_name is not None:
154
 
        env.set_model_name(temp_env_name)
155
 
    return EnvJujuClient.by_version(env, juju_path, debug)
156
 
 
157
 
 
158
57
class CannotConnectEnv(subprocess.CalledProcessError):
159
58
 
160
59
    def __init__(self, e):
161
60
        super(CannotConnectEnv, self).__init__(e.returncode, e.cmd, e.output)
162
61
 
163
62
 
164
 
class StatusNotMet(Exception):
165
 
 
166
 
    _fmt = 'Expected status not reached in {env}.'
167
 
 
168
 
    def __init__(self, environment_name, status):
169
 
        self.env = environment_name
170
 
        self.status = status
171
 
 
172
 
    def __str__(self):
173
 
        return self._fmt.format(env=self.env)
174
 
 
175
 
 
176
 
class AgentsNotStarted(StatusNotMet):
177
 
 
178
 
    _fmt = 'Timed out waiting for agents to start in {env}.'
179
 
 
180
 
 
181
 
class VersionsNotUpdated(StatusNotMet):
182
 
 
183
 
    _fmt = 'Some versions did not update.'
184
 
 
185
 
 
186
 
class WorkloadsNotReady(StatusNotMet):
187
 
 
188
 
    _fmt = 'Workloads not ready in {env}.'
189
 
 
190
 
 
191
 
@contextmanager
192
 
def temp_yaml_file(yaml_dict):
193
 
    temp_file = NamedTemporaryFile(suffix='.yaml', delete=False)
194
 
    try:
195
 
        with temp_file:
196
 
            yaml.safe_dump(yaml_dict, temp_file)
197
 
        yield temp_file.name
198
 
    finally:
199
 
        os.unlink(temp_file.name)
200
 
 
201
 
 
202
 
class EnvJujuClient:
203
 
 
204
 
    # The environments.yaml options that are replaced by bootstrap options.
205
 
    #
206
 
    # As described in bug #1538735, default-series and --bootstrap-series must
207
 
    # match.  'default-series' should be here, but is omitted so that
208
 
    # default-series is always forced to match --bootstrap-series.
209
 
    bootstrap_replaces = frozenset(['agent-version'])
210
 
 
211
 
    # What feature flags have existed that CI used.
212
 
    known_feature_flags = frozenset([
213
 
        'actions', 'jes', 'address-allocation', 'cloudsigma'])
214
 
 
215
 
    # What feature flags are used by this version of the juju client.
216
 
    used_feature_flags = frozenset(['address-allocation'])
217
 
 
218
 
    _show_status = 'show-status'
 
63
class JujuClientDevel:
 
64
    # This client is meant to work with the latest version of juju.
 
65
    # Subclasses will retain support for older versions of juju, so that the
 
66
    # latest version is easy to read, and older versions can be trivially
 
67
    # deleted.
 
68
 
 
69
    def __init__(self, version, full_path):
 
70
        self.version = version
 
71
        self.full_path = full_path
 
72
        self.debug = False
219
73
 
220
74
    @classmethod
221
 
    def get_version(cls, juju_path=None):
222
 
        if juju_path is None:
223
 
            juju_path = 'juju'
224
 
        return subprocess.check_output((juju_path, '--version')).strip()
225
 
 
226
 
    def enable_feature(self, flag):
227
 
        """Enable juju feature by setting the given flag.
228
 
 
229
 
        New versions of juju with the feature enabled by default will silently
230
 
        allow this call, but will not export the environment variable.
231
 
        """
232
 
        if flag not in self.known_feature_flags:
233
 
            raise ValueError('Unknown feature flag: %r' % (flag,))
234
 
        self.feature_flags.add(flag)
235
 
 
236
 
    def get_jes_command(self):
237
 
        """For Juju 2.0, this is always kill-controller."""
238
 
        return KILL_CONTROLLER
239
 
 
240
 
    def is_jes_enabled(self):
241
 
        """Does the state-server support multiple environments."""
242
 
        try:
243
 
            self.get_jes_command()
244
 
            return True
245
 
        except JESNotSupported:
246
 
            return False
247
 
 
248
 
    def enable_jes(self):
249
 
        """Enable JES if JES is optional.
250
 
 
251
 
        Specifically implemented by the clients that optionally support JES.
252
 
        This version raises either JESByDefault or JESNotSupported.
253
 
 
254
 
        :raises: JESByDefault when JES is always enabled; Juju has the
255
 
            'destroy-controller' command.
256
 
        :raises: JESNotSupported when JES is not supported; Juju does not have
257
 
            the 'system kill' command when the JES feature flag is set.
258
 
        """
259
 
        if self.is_jes_enabled():
260
 
            raise JESByDefault()
261
 
        else:
262
 
            raise JESNotSupported()
 
75
    def get_version(cls):
 
76
        return subprocess.check_output(('juju', '--version')).strip()
263
77
 
264
78
    @classmethod
265
79
    def get_full_path(cls):
268
82
        return subprocess.check_output(('which', 'juju')).rstrip('\n')
269
83
 
270
84
    @classmethod
271
 
    def by_version(cls, env, juju_path=None, debug=False):
272
 
        version = cls.get_version(juju_path)
273
 
        if juju_path is None:
274
 
            full_path = cls.get_full_path()
275
 
        else:
276
 
            full_path = os.path.abspath(juju_path)
 
85
    def by_version(cls):
 
86
        version = cls.get_version()
 
87
        full_path = cls.get_full_path()
277
88
        if version.startswith('1.16'):
278
89
            raise Exception('Unsupported juju: %s' % version)
279
 
        elif re.match('^1\.22[.-]', version):
280
 
            client_class = EnvJujuClient22
281
 
        elif re.match('^1\.24[.-]', version):
282
 
            client_class = EnvJujuClient24
283
 
        elif re.match('^1\.25[.-]', version):
284
 
            client_class = EnvJujuClient25
285
 
        elif re.match('^1\.26[.-]', version):
286
 
            client_class = EnvJujuClient26
287
 
        elif re.match('^1\.', version):
288
 
            client_class = EnvJujuClient1X
289
 
        elif re.match('^2\.0-alpha1', version):
290
 
            client_class = EnvJujuClient2A1
291
 
        elif re.match('^2\.0-alpha2', version):
292
 
            client_class = EnvJujuClient2A2
293
 
        elif re.match('^2\.0-(alpha3|beta[12])', version):
294
 
            client_class = EnvJujuClient2B2
295
90
        else:
296
 
            client_class = EnvJujuClient
297
 
        return client_class(env, version, full_path, debug=debug)
298
 
 
299
 
    def clone(self, env=None, version=None, full_path=None, debug=None,
300
 
              cls=None):
301
 
        """Create a clone of this EnvJujuClient.
302
 
 
303
 
        By default, the class, environment, version, full_path, and debug
304
 
        settings will match the original, but each can be overridden.
305
 
        """
306
 
        if env is None:
307
 
            env = self.env
308
 
        if version is None:
309
 
            version = self.version
310
 
        if full_path is None:
311
 
            full_path = self.full_path
312
 
        if debug is None:
313
 
            debug = self.debug
314
 
        if cls is None:
315
 
            cls = self.__class__
316
 
        other = cls(env, version, full_path, debug=debug)
317
 
        other.feature_flags.update(
318
 
            self.feature_flags.intersection(other.used_feature_flags))
319
 
        return other
320
 
 
321
 
    def get_cache_path(self):
322
 
        return get_cache_path(self.env.juju_home, models=True)
323
 
 
324
 
    def _full_args(self, command, sudo, args,
325
 
                   timeout=None, include_e=True, admin=False):
 
91
            return JujuClientDevel(version, full_path)
 
92
 
 
93
    def _full_args(self, environment, command, sudo, args, timeout=None):
326
94
        # sudo is not needed for devel releases.
327
 
        if admin:
328
 
            e_arg = ('-m', self.get_admin_model_name())
329
 
        elif self.env is None or not include_e:
330
 
            e_arg = ()
331
 
        else:
332
 
            e_arg = ('-m', self.env.environment)
 
95
        e_arg = () if environment is None else ('-e', environment.environment)
333
96
        if timeout is None:
334
97
            prefix = ()
335
98
        else:
336
 
            prefix = get_timeout_prefix(timeout, self._timeout_path)
 
99
            prefix = ('timeout', '%.2fs' % timeout)
337
100
        logging = '--debug' if self.debug else '--show-log'
338
 
 
339
 
        # If args is a string, make it a tuple. This makes writing commands
340
 
        # with one argument a bit nicer.
341
 
        if isinstance(args, basestring):
342
 
            args = (args,)
343
 
        # we split the command here so that the caller can control where the -e
344
 
        # <env> flag goes.  Everything in the command string is put before the
345
 
        # -e flag.
346
 
        command = command.split()
347
 
        return prefix + ('juju', logging,) + tuple(command) + e_arg + args
348
 
 
349
 
    @staticmethod
350
 
    def _get_env(env):
351
 
        if not isinstance(env, JujuData) and isinstance(env,
352
 
                                                        SimpleEnvironment):
353
 
            # FIXME: JujuData should be used from the start.
354
 
            env = JujuData.from_env(env)
355
 
        return env
356
 
 
357
 
    def __init__(self, env, version, full_path, juju_home=None, debug=False):
358
 
        self.env = self._get_env(env)
359
 
        self.version = version
360
 
        self.full_path = full_path
361
 
        self.debug = debug
362
 
        self.feature_flags = set()
363
 
        if env is not None:
364
 
            if juju_home is None:
365
 
                if env.juju_home is None:
366
 
                    env.juju_home = get_juju_home()
367
 
            else:
368
 
                env.juju_home = juju_home
369
 
        self.juju_timings = {}
370
 
        self._timeout_path = get_timeout_path()
371
 
 
372
 
    def _shell_environ(self):
373
 
        """Generate a suitable shell environment.
374
 
 
375
 
        Juju's directory must be in the PATH to support plugins.
376
 
        """
377
 
        env = dict(os.environ)
378
 
        if self.full_path is not None:
379
 
            env['PATH'] = '{}{}{}'.format(os.path.dirname(self.full_path),
380
 
                                          os.pathsep, env['PATH'])
381
 
        flags = self.feature_flags.intersection(self.used_feature_flags)
382
 
        if flags:
383
 
            env[JUJU_DEV_FEATURE_FLAGS] = ','.join(sorted(flags))
384
 
        env['JUJU_DATA'] = self.env.juju_home
385
 
        return env
386
 
 
387
 
    def add_ssh_machines(self, machines):
388
 
        for count, machine in enumerate(machines):
389
 
            try:
390
 
                self.juju('add-machine', ('ssh:' + machine,))
391
 
            except subprocess.CalledProcessError:
392
 
                if count != 0:
393
 
                    raise
394
 
                logging.warning('add-machine failed.  Will retry.')
395
 
                pause(30)
396
 
                self.juju('add-machine', ('ssh:' + machine,))
397
 
 
398
 
    @staticmethod
399
 
    def get_cloud_region(cloud, region):
400
 
        if region is None:
401
 
            return cloud
402
 
        return '{}/{}'.format(cloud, region)
403
 
 
404
 
    def get_bootstrap_args(self, upload_tools, config_filename,
405
 
                           bootstrap_series=None):
406
 
        """Return the bootstrap arguments for the substrate."""
407
 
        if self.env.maas:
408
 
            constraints = 'mem=2G arch=amd64'
409
 
        elif self.env.joyent:
410
 
            # Only accept kvm packages by requiring >1 cpu core, see lp:1446264
411
 
            constraints = 'mem=2G cpu-cores=1'
412
 
        else:
413
 
            constraints = 'mem=2G'
414
 
        cloud_region = self.get_cloud_region(self.env.get_cloud(),
415
 
                                             self.env.get_region())
416
 
        args = ['--constraints', constraints, self.env.environment,
417
 
                cloud_region, '--config', config_filename,
418
 
                '--default-model', self.env.environment]
419
 
        if upload_tools:
420
 
            args.insert(0, '--upload-tools')
421
 
        else:
422
 
            args.extend(['--agent-version', self.get_matching_agent_version()])
423
 
 
424
 
        if bootstrap_series is not None:
425
 
            args.extend(['--bootstrap-series', bootstrap_series])
426
 
        return tuple(args)
427
 
 
428
 
    @contextmanager
429
 
    def _bootstrap_config(self):
430
 
        config_dict = make_safe_config(self)
431
 
        # Strip unneeded variables.
432
 
        config_dict = dict((k, v) for k, v in config_dict.items() if k not in {
433
 
            'access-key',
434
 
            'admin-secret',
435
 
            'application-id',
436
 
            'application-password',
437
 
            'auth-url',
438
 
            'bootstrap-host',
439
 
            'client-email',
440
 
            'client-id',
441
 
            'control-bucket',
442
 
            'location',
443
 
            'maas-oauth',
444
 
            'maas-server',
445
 
            'manta-key-id',
446
 
            'manta-user',
447
 
            'name',
448
 
            'password',
449
 
            'private-key',
450
 
            'region',
451
 
            'sdc-key-id',
452
 
            'sdc-url',
453
 
            'sdc-user',
454
 
            'secret-key',
455
 
            'storage-account-name',
456
 
            'subscription-id',
457
 
            'tenant-id',
458
 
            'tenant-name',
459
 
            'type',
460
 
            'username',
461
 
        })
462
 
        with temp_yaml_file(config_dict) as config_filename:
463
 
            yield config_filename
464
 
 
465
 
    def _check_bootstrap(self):
466
 
        if self.env.environment != self.env.controller.name:
467
 
            raise AssertionError(
468
 
                'Controller and environment names should not vary (yet)')
469
 
 
470
 
    def bootstrap(self, upload_tools=False, bootstrap_series=None):
471
 
        """Bootstrap a controller."""
472
 
        self._check_bootstrap()
473
 
        with self._bootstrap_config() as config_filename:
474
 
            args = self.get_bootstrap_args(
475
 
                upload_tools, config_filename, bootstrap_series)
476
 
            self.juju('bootstrap', args, include_e=False)
477
 
 
478
 
    @contextmanager
479
 
    def bootstrap_async(self, upload_tools=False, bootstrap_series=None):
480
 
        self._check_bootstrap()
481
 
        with self._bootstrap_config() as config_filename:
482
 
            args = self.get_bootstrap_args(
483
 
                upload_tools, config_filename, bootstrap_series)
484
 
            with self.juju_async('bootstrap', args, include_e=False):
485
 
                yield
486
 
                log.info('Waiting for bootstrap of {}.'.format(
487
 
                    self.env.environment))
488
 
 
489
 
    def create_environment(self, controller_client, config_file):
490
 
        controller_client.controller_juju('create-model', (
491
 
            self.env.environment, '--config', config_file))
492
 
 
493
 
    def destroy_model(self):
494
 
        exit_status = self.juju(
495
 
            'destroy-model', (self.env.environment, '-y',),
496
 
            include_e=False, timeout=timedelta(minutes=10).total_seconds())
497
 
        return exit_status
498
 
 
499
 
    def kill_controller(self):
500
 
        """Kill a controller and its environments."""
501
 
        seen_cmd = self.get_jes_command()
 
101
        return prefix + ('juju', logging, command,) + e_arg + args
 
102
 
 
103
    def bootstrap(self, environment):
 
104
        """Bootstrap, using sudo if necessary."""
 
105
        if environment.hpcloud:
 
106
            constraints = 'mem=2G'
 
107
        else:
 
108
            constraints = 'mem=2G'
 
109
        self.juju(environment, 'bootstrap', ('--constraints', constraints),
 
110
                  environment.needs_sudo())
 
111
 
 
112
    def destroy_environment(self, environment):
502
113
        self.juju(
503
 
            _jes_cmds[seen_cmd]['kill'], (self.env.controller.name, '-y'),
504
 
            include_e=False, check=False, timeout=600)
505
 
 
506
 
    def get_juju_output(self, command, *args, **kwargs):
507
 
        """Call a juju command and return the output.
508
 
 
509
 
        Sub process will be called as 'juju <command> <args> <kwargs>'. Note
510
 
        that <command> may be a space delimited list of arguments. The -e
511
 
        <environment> flag will be placed after <command> and before args.
512
 
        """
513
 
        args = self._full_args(command, False, args,
514
 
                               timeout=kwargs.get('timeout'),
515
 
                               include_e=kwargs.get('include_e', True),
516
 
                               admin=kwargs.get('admin', False))
517
 
        env = self._shell_environ()
518
 
        log.debug(args)
519
 
        # Mutate os.environ instead of supplying env parameter so
520
 
        # Windows can search env['PATH']
521
 
        with scoped_environ(env):
522
 
            proc = subprocess.Popen(
523
 
                args, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
524
 
                stderr=subprocess.PIPE)
525
 
            sub_output, sub_error = proc.communicate()
526
 
            log.debug(sub_output)
527
 
            if proc.returncode != 0:
528
 
                log.debug(sub_error)
529
 
                e = subprocess.CalledProcessError(
530
 
                    proc.returncode, args, sub_output)
531
 
                e.stderr = sub_error
532
 
                if (
533
 
                    'Unable to connect to environment' in sub_error or
534
 
                        'MissingOrIncorrectVersionHeader' in sub_error or
535
 
                        '307: Temporary Redirect' in sub_error):
 
114
            None, 'destroy-environment',
 
115
            (environment.environment, '--force', '-y'),
 
116
            environment.needs_sudo(), check=False)
 
117
 
 
118
    def get_juju_output(self, environment, command, *args, **kwargs):
 
119
        args = self._full_args(environment, command, False, args,
 
120
                               timeout=kwargs.get('timeout'))
 
121
        with tempfile.TemporaryFile() as stderr:
 
122
            try:
 
123
                return subprocess.check_output(args, stderr=stderr)
 
124
            except subprocess.CalledProcessError as e:
 
125
                stderr.seek(0)
 
126
                e.stderr = stderr.read()
 
127
                if ('Unable to connect to environment' in e.stderr
 
128
                        or 'MissingOrIncorrectVersionHeader' in e.stderr
 
129
                        or '307: Temporary Redirect' in e.stderr):
536
130
                    raise CannotConnectEnv(e)
537
 
                raise e
538
 
        return sub_output
539
 
 
540
 
    def show_status(self):
541
 
        """Print the status to output."""
542
 
        self.juju(self._show_status, ('--format', 'yaml'))
543
 
 
544
 
    def get_status(self, timeout=60, raw=False, admin=False, *args):
 
131
                print('!!! ' + e.stderr)
 
132
                raise
 
133
 
 
134
    def get_status(self, environment, timeout=60):
545
135
        """Get the current status as a dict."""
546
 
        # GZ 2015-12-16: Pass remaining timeout into get_juju_output call.
547
 
        for ignored in until_timeout(timeout):
548
 
            try:
549
 
                if raw:
550
 
                    return self.get_juju_output(self._show_status, *args)
551
 
                return Status.from_text(
552
 
                    self.get_juju_output(
553
 
                        self._show_status, '--format', 'yaml', admin=admin))
554
 
            except subprocess.CalledProcessError:
555
 
                pass
556
 
        raise Exception(
557
 
            'Timed out waiting for juju status to succeed')
558
 
 
559
 
    @staticmethod
560
 
    def _dict_as_option_strings(options):
561
 
        return tuple('{}={}'.format(*item) for item in options.items())
562
 
 
563
 
    def set_config(self, service, options):
564
 
        option_strings = self._dict_as_option_strings(options)
565
 
        self.juju('set-config', (service,) + option_strings)
566
 
 
567
 
    def get_config(self, service):
568
 
        return yaml_loads(self.get_juju_output('get-config', service))
569
 
 
570
 
    def get_service_config(self, service, timeout=60):
571
 
        for ignored in until_timeout(timeout):
572
 
            try:
573
 
                return self.get_config(service)
574
 
            except subprocess.CalledProcessError:
575
 
                pass
576
 
        raise Exception(
577
 
            'Timed out waiting for juju get %s' % (service))
578
 
 
579
 
    def set_model_constraints(self, constraints):
580
 
        constraint_strings = self._dict_as_option_strings(constraints)
581
 
        return self.juju('set-model-constraints', constraint_strings)
582
 
 
583
 
    def get_model_config(self):
584
 
        """Return the value of the environment's configured option."""
585
 
        return yaml.safe_load(self.get_juju_output('get-model-config'))
586
 
 
587
 
    def get_env_option(self, option):
588
 
        """Return the value of the environment's configured option."""
589
 
        return self.get_juju_output('get-model-config', option)
590
 
 
591
 
    def set_env_option(self, option, value):
 
136
        for ignored in until_timeout(timeout):
 
137
            try:
 
138
                return Status(yaml_loads(
 
139
                    self.get_juju_output(environment, 'status')))
 
140
            except subprocess.CalledProcessError as e:
 
141
                pass
 
142
        raise Exception(
 
143
            'Timed out waiting for juju status to succeed: %s' % e)
 
144
 
 
145
    def get_env_option(self, environment, option):
 
146
        """Return the value of the environment's configured option."""
 
147
        return self.get_juju_output(environment, 'get-env', option)
 
148
 
 
149
    def set_env_option(self, environment, option, value):
592
150
        """Set the value of the option in the environment."""
593
151
        option_value = "%s=%s" % (option, value)
594
 
        return self.juju('set-model-config', (option_value,))
595
 
 
596
 
    def set_testing_tools_metadata_url(self):
597
 
        url = self.get_env_option('tools-metadata-url')
598
 
        if 'testing' not in url:
599
 
            testing_url = url.replace('/tools', '/testing/tools')
600
 
            self.set_env_option('tools-metadata-url', testing_url)
601
 
 
602
 
    def juju(self, command, args, sudo=False, check=True, include_e=True,
603
 
             timeout=None, extra_env=None):
 
152
        return self.juju(environment, 'set-env', (option_value,))
 
153
 
 
154
    def juju(self, environment, command, args, sudo=False, check=True):
604
155
        """Run a command under juju for the current environment."""
605
 
        args = self._full_args(command, sudo, args, include_e=include_e,
606
 
                               timeout=timeout)
607
 
        log.info(' '.join(args))
608
 
        env = self._shell_environ()
609
 
        if extra_env is not None:
610
 
            env.update(extra_env)
 
156
        args = self._full_args(environment, command, sudo, args)
 
157
        print(' '.join(args))
 
158
        sys.stdout.flush()
611
159
        if check:
612
 
            call_func = subprocess.check_call
613
 
        else:
614
 
            call_func = subprocess.call
615
 
        start_time = time.time()
616
 
        # Mutate os.environ instead of supplying env parameter so Windows can
617
 
        # search env['PATH']
618
 
        with scoped_environ(env):
619
 
            rval = call_func(args)
620
 
        self.juju_timings.setdefault(args, []).append(
621
 
            (time.time() - start_time))
622
 
        return rval
623
 
 
624
 
    def controller_juju(self, command, args):
625
 
        args = ('-c', self.env.controller.name) + args
626
 
        return self.juju(command, args, include_e=False)
627
 
 
628
 
    def get_juju_timings(self):
629
 
        stringified_timings = {}
630
 
        for command, timings in self.juju_timings.items():
631
 
            stringified_timings[' '.join(command)] = timings
632
 
        return stringified_timings
633
 
 
634
 
    @contextmanager
635
 
    def juju_async(self, command, args, include_e=True, timeout=None):
636
 
        full_args = self._full_args(command, False, args, include_e=include_e,
637
 
                                    timeout=timeout)
638
 
        log.info(' '.join(args))
639
 
        env = self._shell_environ()
640
 
        # Mutate os.environ instead of supplying env parameter so Windows can
641
 
        # search env['PATH']
642
 
        with scoped_environ(env):
643
 
            proc = subprocess.Popen(full_args)
644
 
        yield proc
645
 
        retcode = proc.wait()
646
 
        if retcode != 0:
647
 
            raise subprocess.CalledProcessError(retcode, full_args)
648
 
 
649
 
    def deploy(self, charm, repository=None, to=None, series=None,
650
 
               service=None, force=False):
651
 
        args = [charm]
652
 
        if to is not None:
653
 
            args.extend(['--to', to])
654
 
        if series is not None:
655
 
            args.extend(['--series', series])
656
 
        if service is not None:
657
 
            args.extend([service])
658
 
        if force is True:
659
 
            args.extend(['--force'])
660
 
        return self.juju('deploy', tuple(args))
661
 
 
662
 
    def remove_service(self, service):
663
 
        self.juju('remove-service', (service,))
664
 
 
665
 
    def deploy_bundle(self, bundle, timeout=_DEFAULT_BUNDLE_TIMEOUT):
666
 
        """Deploy bundle using native juju 2.0 deploy command."""
667
 
        self.juju('deploy', bundle, timeout=timeout)
668
 
 
669
 
    def deployer(self, bundle, name=None, deploy_delay=10, timeout=3600):
670
 
        """deployer, using sudo if necessary."""
671
 
        args = (
672
 
            '--debug',
673
 
            '--deploy-delay', str(deploy_delay),
674
 
            '--timeout', str(timeout),
675
 
            '--config', bundle,
676
 
        )
677
 
        if name:
678
 
            args += (name,)
679
 
        self.juju('deployer', args, self.env.needs_sudo())
680
 
 
681
 
    def _get_substrate_constraints(self):
682
 
        if self.env.maas:
683
 
            return 'mem=2G arch=amd64'
684
 
        elif self.env.joyent:
685
 
            # Only accept kvm packages by requiring >1 cpu core, see lp:1446264
686
 
            return 'mem=2G cpu-cores=1'
687
 
        else:
688
 
            return 'mem=2G'
689
 
 
690
 
    def quickstart(self, bundle, upload_tools=False):
691
 
        """quickstart, using sudo if necessary."""
692
 
        if self.env.maas:
693
 
            constraints = 'mem=2G arch=amd64'
694
 
        else:
695
 
            constraints = 'mem=2G'
696
 
        args = ('--constraints', constraints)
697
 
        if upload_tools:
698
 
            args = ('--upload-tools',) + args
699
 
        args = args + ('--no-browser', bundle,)
700
 
        self.juju('quickstart', args, self.env.needs_sudo(),
701
 
                  extra_env={'JUJU': self.full_path})
702
 
 
703
 
    def status_until(self, timeout, start=None):
704
 
        """Call and yield status until the timeout is reached.
705
 
 
706
 
        Status will always be yielded once before checking the timeout.
707
 
 
708
 
        This is intended for implementing things like wait_for_started.
709
 
 
710
 
        :param timeout: The number of seconds to wait before timing out.
711
 
        :param start: If supplied, the time to count from when determining
712
 
            timeout.
713
 
        """
714
 
        yield self.get_status()
715
 
        for remaining in until_timeout(timeout, start=start):
716
 
            yield self.get_status()
717
 
 
718
 
    def _wait_for_status(self, reporter, translate, exc_type=StatusNotMet,
719
 
                         timeout=1200, start=None):
720
 
        """Wait till status reaches an expected state with pretty reporting.
721
 
 
722
 
        Always tries to get status at least once. Each status call has an
723
 
        internal timeout of 60 seconds. This is independent of the timeout for
724
 
        the whole wait, note this means this function may be overrun.
725
 
 
726
 
        :param reporter: A GroupReporter instance for output.
727
 
        :param translate: A callable that takes status to make states dict.
728
 
        :param exc_type: Optional StatusNotMet subclass to raise on timeout.
729
 
        :param timeout: Optional number of seconds to wait before timing out.
730
 
        :param start: Optional time to count from when determining timeout.
731
 
        """
732
 
        status = None
733
 
        try:
734
 
            for _ in chain([None], until_timeout(timeout, start=start)):
735
 
                try:
736
 
                    status = self.get_status()
737
 
                except CannotConnectEnv:
738
 
                    log.info('Suppressing "Unable to connect to environment"')
739
 
                    continue
740
 
                states = translate(status)
741
 
                if states is None:
742
 
                    break
743
 
                reporter.update(states)
744
 
            else:
745
 
                if status is not None:
746
 
                    log.error(status.status_text)
747
 
                raise exc_type(self.env.environment, status)
748
 
        finally:
749
 
            reporter.finish()
750
 
        return status
751
 
 
752
 
    def wait_for_started(self, timeout=1200, start=None):
753
 
        """Wait until all unit/machine agents are 'started'."""
754
 
        reporter = GroupReporter(sys.stdout, 'started')
755
 
        return self._wait_for_status(
756
 
            reporter, Status.check_agents_started, AgentsNotStarted,
757
 
            timeout=timeout, start=start)
758
 
 
759
 
    def wait_for_subordinate_units(self, service, unit_prefix, timeout=1200,
760
 
                                   start=None):
761
 
        """Wait until all service units have a started subordinate with
762
 
        unit_prefix."""
763
 
        def status_to_subordinate_states(status):
764
 
            service_unit_count = status.get_service_unit_count(service)
765
 
            subordinate_unit_count = 0
766
 
            unit_states = defaultdict(list)
767
 
            for name, unit in status.service_subordinate_units(service):
768
 
                if name.startswith(unit_prefix + '/'):
769
 
                    subordinate_unit_count += 1
770
 
                    unit_states[coalesce_agent_status(unit)].append(name)
771
 
            if (subordinate_unit_count == service_unit_count and
772
 
                    set(unit_states.keys()).issubset(AGENTS_READY)):
773
 
                return None
774
 
            return unit_states
775
 
        reporter = GroupReporter(sys.stdout, 'started')
776
 
        self._wait_for_status(
777
 
            reporter, status_to_subordinate_states, AgentsNotStarted,
778
 
            timeout=timeout, start=start)
779
 
 
780
 
    def wait_for_version(self, version, timeout=300, start=None):
781
 
        def status_to_version(status):
782
 
            versions = status.get_agent_versions()
783
 
            if versions.keys() == [version]:
784
 
                return None
785
 
            return versions
786
 
        reporter = GroupReporter(sys.stdout, version)
787
 
        self._wait_for_status(reporter, status_to_version, VersionsNotUpdated,
788
 
                              timeout=timeout, start=start)
789
 
 
790
 
    def list_models(self):
791
 
        """List the models registered with the current controller."""
792
 
        self.controller_juju('list-models', ())
793
 
 
794
 
    def get_models(self):
795
 
        """return a models dict with a 'models': [] key-value pair."""
796
 
        output = self.get_juju_output(
797
 
            'list-models', '-c', self.env.environment, '--format', 'yaml',
798
 
            include_e=False)
799
 
        models = yaml_loads(output)
800
 
        return models
801
 
 
802
 
    def _get_models(self):
803
 
        """return a list of model dicts."""
804
 
        return self.get_models()['models']
805
 
 
806
 
    def iter_model_clients(self):
807
 
        """Iterate through all the models that share this model's controller.
808
 
 
809
 
        Works only if JES is enabled.
810
 
        """
811
 
        models = self._get_models()
812
 
        if not models:
813
 
            yield self
814
 
        for model in models:
815
 
            yield self._acquire_model_client(model['name'])
816
 
 
817
 
    def get_admin_model_name(self):
818
 
        """Return the name of the 'admin' model.
819
 
 
820
 
        Return the name of the environment when an 'admin' model does
821
 
        not exist.
822
 
        """
823
 
        return 'admin'
824
 
 
825
 
    def _acquire_model_client(self, name):
826
 
        """Get a client for a model with the supplied name.
827
 
 
828
 
        If the name matches self, self is used.  Otherwise, a clone is used.
829
 
        """
830
 
        if name == self.env.environment:
831
 
            return self
832
 
        else:
833
 
            env = self.env.clone(model_name=name)
834
 
            return self.clone(env=env)
835
 
 
836
 
    def get_admin_client(self):
837
 
        """Return a client for the admin model.  May return self.
838
 
 
839
 
        This may be inaccurate for models created using create_environment
840
 
        rather than bootstrap.
841
 
        """
842
 
        return self._acquire_model_client(self.get_admin_model_name())
843
 
 
844
 
    def list_controllers(self):
845
 
        """List the controllers."""
846
 
        self.juju('list-controllers', (), include_e=False)
847
 
 
848
 
    def get_controller_endpoint(self):
849
 
        """Return the address of the controller leader."""
850
 
        controller = self.env.controller.name
851
 
        output = self.get_juju_output(
852
 
            'show-controller', controller, include_e=False)
853
 
        info = yaml_loads(output)
854
 
        endpoint = info[controller]['details']['api-endpoints'][0]
855
 
        address, port = split_address_port(endpoint)
856
 
        return address
857
 
 
858
 
    def get_controller_members(self):
859
 
        """Return a list of Machines that are members of the controller.
860
 
 
861
 
        The first machine in the list is the leader. the remaining machines
862
 
        are followers in a HA relationship.
863
 
        """
864
 
        members = []
865
 
        status = self.get_status()
866
 
        for machine_id, machine in status.iter_machines():
867
 
            if self.get_controller_member_status(machine):
868
 
                members.append(Machine(machine_id, machine))
869
 
        if len(members) <= 1:
870
 
            return members
871
 
        # Search for the leader and make it the first in the list.
872
 
        # If the endpoint address is not the same as the leader's dns_name,
873
 
        # the members are return in the order they were discovered.
874
 
        endpoint = self.get_controller_endpoint()
875
 
        log.debug('Controller endpoint is at {}'.format(endpoint))
876
 
        members.sort(key=lambda m: m.info.get('dns-name') != endpoint)
877
 
        return members
878
 
 
879
 
    def get_controller_leader(self):
880
 
        """Return the controller leader Machine."""
881
 
        controller_members = self.get_controller_members()
882
 
        return controller_members[0]
883
 
 
884
 
    @staticmethod
885
 
    def get_controller_member_status(info_dict):
886
 
        """Return the controller-member-status of the machine if it exists."""
887
 
        return info_dict.get('controller-member-status')
888
 
 
889
 
    def wait_for_ha(self, timeout=1200):
890
 
        desired_state = 'has-vote'
891
 
        reporter = GroupReporter(sys.stdout, desired_state)
892
 
        try:
893
 
            for remaining in until_timeout(timeout):
894
 
                status = self.get_status(admin=True)
895
 
                states = {}
896
 
                for machine, info in status.iter_machines():
897
 
                    status = self.get_controller_member_status(info)
898
 
                    if status is None:
899
 
                        continue
900
 
                    states.setdefault(status, []).append(machine)
901
 
                if states.keys() == [desired_state]:
902
 
                    if len(states.get(desired_state, [])) >= 3:
903
 
                        # XXX sinzui 2014-12-04: bug 1399277 happens because
904
 
                        # juju claims HA is ready when the monogo replica sets
905
 
                        # are not. Juju is not fully usable. The replica set
906
 
                        # lag might be 5 minutes.
907
 
                        pause(300)
908
 
                        return
909
 
                reporter.update(states)
910
 
            else:
911
 
                raise Exception('Timed out waiting for voting to be enabled.')
912
 
        finally:
913
 
            reporter.finish()
914
 
 
915
 
    def wait_for_deploy_started(self, service_count=1, timeout=1200):
916
 
        """Wait until service_count services are 'started'.
917
 
 
918
 
        :param service_count: The number of services for which to wait.
919
 
        :param timeout: The number of seconds to wait.
920
 
        """
921
 
        for remaining in until_timeout(timeout):
922
 
            status = self.get_status()
923
 
            if status.get_service_count() >= service_count:
924
 
                return
925
 
        else:
926
 
            raise Exception('Timed out waiting for services to start.')
927
 
 
928
 
    def wait_for_workloads(self, timeout=600, start=None):
929
 
        """Wait until all unit workloads are in a ready state."""
930
 
        def status_to_workloads(status):
931
 
            unit_states = defaultdict(list)
932
 
            for name, unit in status.iter_units():
933
 
                workload = unit.get('workload-status')
934
 
                if workload is not None:
935
 
                    state = workload['current']
936
 
                else:
937
 
                    state = 'unknown'
938
 
                unit_states[state].append(name)
939
 
            if set(('active', 'unknown')).issuperset(unit_states):
940
 
                return None
941
 
            unit_states.pop('unknown', None)
942
 
            return unit_states
943
 
        reporter = GroupReporter(sys.stdout, 'active')
944
 
        self._wait_for_status(reporter, status_to_workloads, WorkloadsNotReady,
945
 
                              timeout=timeout, start=start)
946
 
 
947
 
    def wait_for(self, thing, search_type, timeout=300):
948
 
        """ Wait for a something (thing) matching none/all/some machines.
949
 
 
950
 
        Examples:
951
 
          wait_for('containers', 'all')
952
 
          This will wait for a container to appear on all machines.
953
 
 
954
 
          wait_for('machines-not-0', 'none')
955
 
          This will wait for all machines other than 0 to be removed.
956
 
 
957
 
        :param thing: string, either 'containers' or 'not-machine-0'
958
 
        :param search_type: string containing none, some or all
959
 
        :param timeout: number of seconds to wait for condition to be true.
960
 
        :return:
961
 
        """
962
 
        try:
963
 
            for status in self.status_until(timeout):
964
 
                hit = False
965
 
                miss = False
966
 
 
967
 
                for machine, details in status.status['machines'].iteritems():
968
 
                    if thing == 'containers':
969
 
                        if 'containers' in details:
970
 
                            hit = True
971
 
                        else:
972
 
                            miss = True
973
 
 
974
 
                    elif thing == 'machines-not-0':
975
 
                        if machine != '0':
976
 
                            hit = True
977
 
                        else:
978
 
                            miss = True
979
 
 
980
 
                    else:
981
 
                        raise ValueError("Unrecognised thing to wait for: %s",
982
 
                                         thing)
983
 
 
984
 
                if search_type == 'none':
985
 
                    if not hit:
986
 
                        return
987
 
                elif search_type == 'some':
988
 
                    if hit:
989
 
                        return
990
 
                elif search_type == 'all':
991
 
                    if not miss:
992
 
                        return
993
 
        except Exception:
994
 
            raise Exception("Timed out waiting for %s" % thing)
995
 
 
996
 
    def get_matching_agent_version(self, no_build=False):
997
 
        # strip the series and srch from the built version.
998
 
        version_parts = self.version.split('-')
999
 
        if len(version_parts) == 4:
1000
 
            version_number = '-'.join(version_parts[0:2])
1001
 
        else:
1002
 
            version_number = version_parts[0]
1003
 
        if not no_build and self.env.local:
1004
 
            version_number += '.1'
1005
 
        return version_number
1006
 
 
1007
 
    def upgrade_juju(self, force_version=True):
1008
 
        args = ()
1009
 
        if force_version:
1010
 
            version = self.get_matching_agent_version(no_build=True)
1011
 
            args += ('--version', version)
1012
 
        if self.env.local:
1013
 
            args += ('--upload-tools',)
1014
 
        self.juju('upgrade-juju', args)
1015
 
 
1016
 
    def upgrade_mongo(self):
1017
 
        self.juju('upgrade-mongo', ())
1018
 
 
1019
 
    def backup(self):
1020
 
        environ = self._shell_environ()
1021
 
        try:
1022
 
            # Mutate os.environ instead of supplying env parameter so Windows
1023
 
            # can search env['PATH']
1024
 
            with scoped_environ(environ):
1025
 
                args = self._full_args(
1026
 
                    'create-backup', False, (), include_e=True)
1027
 
                log.info(' '.join(args))
1028
 
                output = subprocess.check_output(args)
1029
 
        except subprocess.CalledProcessError as e:
1030
 
            log.info(e.output)
1031
 
            raise
1032
 
        log.info(output)
1033
 
        backup_file_pattern = re.compile('(juju-backup-[0-9-]+\.(t|tar.)gz)')
1034
 
        match = backup_file_pattern.search(output)
1035
 
        if match is None:
1036
 
            raise Exception("The backup file was not found in output: %s" %
1037
 
                            output)
1038
 
        backup_file_name = match.group(1)
1039
 
        backup_file_path = os.path.abspath(backup_file_name)
1040
 
        log.info("State-Server backup at %s", backup_file_path)
1041
 
        return backup_file_path
1042
 
 
1043
 
    def restore_backup(self, backup_file):
1044
 
        return self.get_juju_output('restore-backup', '-b', '--constraints',
1045
 
                                    'mem=2G', '--file', backup_file)
1046
 
 
1047
 
    def restore_backup_async(self, backup_file):
1048
 
        return self.juju_async('restore-backup', ('-b', '--constraints',
1049
 
                               'mem=2G', '--file', backup_file))
1050
 
 
1051
 
    def enable_ha(self):
1052
 
        self.juju('enable-ha', ('-n', '3'))
1053
 
 
1054
 
    def action_fetch(self, id, action=None, timeout="1m"):
1055
 
        """Fetches the results of the action with the given id.
1056
 
 
1057
 
        Will wait for up to 1 minute for the action results.
1058
 
        The action name here is just used for an more informational error in
1059
 
        cases where it's available.
1060
 
        Returns the yaml output of the fetched action.
1061
 
        """
1062
 
        out = self.get_juju_output("show-action-output", id, "--wait", timeout)
1063
 
        status = yaml_loads(out)["status"]
1064
 
        if status != "completed":
1065
 
            name = ""
1066
 
            if action is not None:
1067
 
                name = " " + action
1068
 
            raise Exception(
1069
 
                "timed out waiting for action%s to complete during fetch" %
1070
 
                name)
1071
 
        return out
1072
 
 
1073
 
    def action_do(self, unit, action, *args):
1074
 
        """Performs the given action on the given unit.
1075
 
 
1076
 
        Action params should be given as args in the form foo=bar.
1077
 
        Returns the id of the queued action.
1078
 
        """
1079
 
        args = (unit, action) + args
1080
 
 
1081
 
        output = self.get_juju_output("run-action", *args)
1082
 
        action_id_pattern = re.compile(
1083
 
            'Action queued with id: ([a-f0-9\-]{36})')
1084
 
        match = action_id_pattern.search(output)
1085
 
        if match is None:
1086
 
            raise Exception("Action id not found in output: %s" %
1087
 
                            output)
1088
 
        return match.group(1)
1089
 
 
1090
 
    def action_do_fetch(self, unit, action, timeout="1m", *args):
1091
 
        """Performs given action on given unit and waits for the results.
1092
 
 
1093
 
        Action params should be given as args in the form foo=bar.
1094
 
        Returns the yaml output of the action.
1095
 
        """
1096
 
        id = self.action_do(unit, action, *args)
1097
 
        return self.action_fetch(id, action, timeout)
1098
 
 
1099
 
    def list_space(self):
1100
 
        return yaml.safe_load(self.get_juju_output('list-space'))
1101
 
 
1102
 
    def add_space(self, space):
1103
 
        self.juju('add-space', (space),)
1104
 
 
1105
 
    def add_subnet(self, subnet, space):
1106
 
        self.juju('add-subnet', (subnet, space))
1107
 
 
1108
 
 
1109
 
class EnvJujuClient2B2(EnvJujuClient):
1110
 
 
1111
 
    def get_bootstrap_args(self, upload_tools, config_filename,
1112
 
                           bootstrap_series=None):
1113
 
        """Return the bootstrap arguments for the substrate."""
1114
 
        if self.env.maas:
1115
 
            constraints = 'mem=2G arch=amd64'
1116
 
        elif self.env.joyent:
1117
 
            # Only accept kvm packages by requiring >1 cpu core, see lp:1446264
1118
 
            constraints = 'mem=2G cpu-cores=1'
1119
 
        else:
1120
 
            constraints = 'mem=2G'
1121
 
        cloud_region = self.get_cloud_region(self.env.get_cloud(),
1122
 
                                             self.env.get_region())
1123
 
        args = ['--constraints', constraints, self.env.environment,
1124
 
                cloud_region, '--config', config_filename]
1125
 
        if upload_tools:
1126
 
            args.insert(0, '--upload-tools')
1127
 
        else:
1128
 
            args.extend(['--agent-version', self.get_matching_agent_version()])
1129
 
 
1130
 
        if bootstrap_series is not None:
1131
 
            args.extend(['--bootstrap-series', bootstrap_series])
1132
 
        return tuple(args)
1133
 
 
1134
 
    def get_admin_client(self):
1135
 
        """Return a client for the admin model.  May return self."""
1136
 
        return self
1137
 
 
1138
 
    def get_admin_model_name(self):
1139
 
        """Return the name of the 'admin' model.
1140
 
 
1141
 
        Return the name of the environment when an 'admin' model does
1142
 
        not exist.
1143
 
        """
1144
 
        models = self.get_models()
1145
 
        # The dict can be empty because 1.x does not support the models.
1146
 
        # This is an ambiguous case for the jes feature flag which supports
1147
 
        # multiple models, but none is named 'admin' by default. Since the
1148
 
        # jes case also uses '-e' for models, the env is the admin model.
1149
 
        for model in models.get('models', []):
1150
 
            if 'admin' in model['name']:
1151
 
                return 'admin'
1152
 
        return self.env.environment
1153
 
 
1154
 
 
1155
 
class EnvJujuClient2A2(EnvJujuClient2B2):
1156
 
    """Drives Juju 2.0-alpha2 clients."""
1157
 
 
1158
 
    @classmethod
1159
 
    def _get_env(cls, env):
1160
 
        if isinstance(env, JujuData):
1161
 
            raise ValueError(
1162
 
                'JujuData cannot be used with {}'.format(cls.__name__))
1163
 
        return env
1164
 
 
1165
 
    def _shell_environ(self):
1166
 
        """Generate a suitable shell environment.
1167
 
 
1168
 
        For 2.0-alpha2 set both JUJU_HOME and JUJU_DATA.
1169
 
        """
1170
 
        env = super(EnvJujuClient2A2, self)._shell_environ()
1171
 
        env['JUJU_HOME'] = self.env.juju_home
1172
 
        return env
1173
 
 
1174
 
    def bootstrap(self, upload_tools=False, bootstrap_series=None):
1175
 
        """Bootstrap a controller."""
1176
 
        self._check_bootstrap()
1177
 
        args = self.get_bootstrap_args(upload_tools, bootstrap_series)
1178
 
        self.juju('bootstrap', args, self.env.needs_sudo())
1179
 
 
1180
 
    @contextmanager
1181
 
    def bootstrap_async(self, upload_tools=False):
1182
 
        self._check_bootstrap()
1183
 
        args = self.get_bootstrap_args(upload_tools)
1184
 
        with self.juju_async('bootstrap', args):
1185
 
            yield
1186
 
            log.info('Waiting for bootstrap of {}.'.format(
1187
 
                self.env.environment))
1188
 
 
1189
 
    def get_bootstrap_args(self, upload_tools, bootstrap_series=None):
1190
 
        """Return the bootstrap arguments for the substrate."""
1191
 
        constraints = self._get_substrate_constraints()
1192
 
        args = ('--constraints', constraints,
1193
 
                '--agent-version', self.get_matching_agent_version())
1194
 
        if upload_tools:
1195
 
            args = ('--upload-tools',) + args
1196
 
        if bootstrap_series is not None:
1197
 
            args = args + ('--bootstrap-series', bootstrap_series)
1198
 
        return args
1199
 
 
1200
 
    def deploy(self, charm, repository=None, to=None, series=None,
1201
 
               service=None, force=False):
1202
 
        args = [charm]
1203
 
        if repository is not None:
1204
 
            args.extend(['--repository', repository])
1205
 
        if to is not None:
1206
 
            args.extend(['--to', to])
1207
 
        if service is not None:
1208
 
            args.extend([service])
1209
 
        return self.juju('deploy', tuple(args))
1210
 
 
1211
 
 
1212
 
class EnvJujuClient2A1(EnvJujuClient2A2):
1213
 
    """Drives Juju 2.0-alpha1 clients."""
1214
 
 
1215
 
    _show_status = 'status'
1216
 
 
1217
 
    def get_cache_path(self):
1218
 
        return get_cache_path(self.env.juju_home, models=False)
1219
 
 
1220
 
    def _full_args(self, command, sudo, args,
1221
 
                   timeout=None, include_e=True, admin=False):
1222
 
        # sudo is not needed for devel releases.
1223
 
        # admin is ignored. only environment exists.
1224
 
        if self.env is None or not include_e:
1225
 
            e_arg = ()
1226
 
        else:
1227
 
            e_arg = ('-e', self.env.environment)
1228
 
        if timeout is None:
1229
 
            prefix = ()
1230
 
        else:
1231
 
            prefix = get_timeout_prefix(timeout, self._timeout_path)
1232
 
        logging = '--debug' if self.debug else '--show-log'
1233
 
 
1234
 
        # If args is a string, make it a tuple. This makes writing commands
1235
 
        # with one argument a bit nicer.
1236
 
        if isinstance(args, basestring):
1237
 
            args = (args,)
1238
 
        # we split the command here so that the caller can control where the -e
1239
 
        # <env> flag goes.  Everything in the command string is put before the
1240
 
        # -e flag.
1241
 
        command = command.split()
1242
 
        return prefix + ('juju', logging,) + tuple(command) + e_arg + args
1243
 
 
1244
 
    def _shell_environ(self):
1245
 
        """Generate a suitable shell environment.
1246
 
 
1247
 
        For 2.0-alpha1 and earlier set only JUJU_HOME and not JUJU_DATA.
1248
 
        """
1249
 
        env = super(EnvJujuClient2A1, self)._shell_environ()
1250
 
        env['JUJU_HOME'] = self.env.juju_home
1251
 
        del env['JUJU_DATA']
1252
 
        return env
1253
 
 
1254
 
    def remove_service(self, service):
1255
 
        self.juju('destroy-service', (service,))
1256
 
 
1257
 
    def backup(self):
1258
 
        environ = self._shell_environ()
1259
 
        # juju-backup does not support the -e flag.
1260
 
        environ['JUJU_ENV'] = self.env.environment
1261
 
        try:
1262
 
            # Mutate os.environ instead of supplying env parameter so Windows
1263
 
            # can search env['PATH']
1264
 
            with scoped_environ(environ):
1265
 
                args = ['juju', 'backup']
1266
 
                log.info(' '.join(args))
1267
 
                output = subprocess.check_output(args)
1268
 
        except subprocess.CalledProcessError as e:
1269
 
            log.info(e.output)
1270
 
            raise
1271
 
        log.info(output)
1272
 
        backup_file_pattern = re.compile('(juju-backup-[0-9-]+\.(t|tar.)gz)')
1273
 
        match = backup_file_pattern.search(output)
1274
 
        if match is None:
1275
 
            raise Exception("The backup file was not found in output: %s" %
1276
 
                            output)
1277
 
        backup_file_name = match.group(1)
1278
 
        backup_file_path = os.path.abspath(backup_file_name)
1279
 
        log.info("State-Server backup at %s", backup_file_path)
1280
 
        return backup_file_path
1281
 
 
1282
 
    def restore_backup(self, backup_file):
1283
 
        return self.get_juju_output('restore', '--constraints', 'mem=2G',
1284
 
                                    backup_file)
1285
 
 
1286
 
    def restore_backup_async(self, backup_file):
1287
 
        return self.juju_async('restore', ('--constraints', 'mem=2G',
1288
 
                                           backup_file))
1289
 
 
1290
 
    def enable_ha(self):
1291
 
        self.juju('ensure-availability', ('-n', '3'))
1292
 
 
1293
 
    def list_models(self):
1294
 
        """List the models registered with the current controller."""
1295
 
        log.info('The model is environment {}'.format(self.env.environment))
1296
 
 
1297
 
    def get_models(self):
1298
 
        """return a models dict with a 'models': [] key-value pair."""
1299
 
        return {}
1300
 
 
1301
 
    def _get_models(self):
1302
 
        """return a list of model dicts."""
1303
 
        # In 2.0-alpha1, 'list-models' produced a yaml list rather than a
1304
 
        # dict, but the command and parsing are the same.
1305
 
        return super(EnvJujuClient2A1, self).get_models()
1306
 
 
1307
 
    def list_controllers(self):
1308
 
        """List the controllers."""
1309
 
        log.info(
1310
 
            'The controller is environment {}'.format(self.env.environment))
1311
 
 
1312
 
    @staticmethod
1313
 
    def get_controller_member_status(info_dict):
1314
 
        return info_dict.get('state-server-member-status')
1315
 
 
1316
 
    def action_fetch(self, id, action=None, timeout="1m"):
1317
 
        """Fetches the results of the action with the given id.
1318
 
 
1319
 
        Will wait for up to 1 minute for the action results.
1320
 
        The action name here is just used for an more informational error in
1321
 
        cases where it's available.
1322
 
        Returns the yaml output of the fetched action.
1323
 
        """
1324
 
        # the command has to be "action fetch" so that the -e <env> args are
1325
 
        # placed after "fetch", since that's where action requires them to be.
1326
 
        out = self.get_juju_output("action fetch", id, "--wait", timeout)
1327
 
        status = yaml_loads(out)["status"]
1328
 
        if status != "completed":
1329
 
            name = ""
1330
 
            if action is not None:
1331
 
                name = " " + action
1332
 
            raise Exception(
1333
 
                "timed out waiting for action%s to complete during fetch" %
1334
 
                name)
1335
 
        return out
1336
 
 
1337
 
    def action_do(self, unit, action, *args):
1338
 
        """Performs the given action on the given unit.
1339
 
 
1340
 
        Action params should be given as args in the form foo=bar.
1341
 
        Returns the id of the queued action.
1342
 
        """
1343
 
        args = (unit, action) + args
1344
 
 
1345
 
        # the command has to be "action do" so that the -e <env> args are
1346
 
        # placed after "do", since that's where action requires them to be.
1347
 
        output = self.get_juju_output("action do", *args)
1348
 
        action_id_pattern = re.compile(
1349
 
            'Action queued with id: ([a-f0-9\-]{36})')
1350
 
        match = action_id_pattern.search(output)
1351
 
        if match is None:
1352
 
            raise Exception("Action id not found in output: %s" %
1353
 
                            output)
1354
 
        return match.group(1)
1355
 
 
1356
 
    def list_space(self):
1357
 
        return yaml.safe_load(self.get_juju_output('space list'))
1358
 
 
1359
 
    def add_space(self, space):
1360
 
        self.juju('space create', (space),)
1361
 
 
1362
 
    def add_subnet(self, subnet, space):
1363
 
        self.juju('subnet add', (subnet, space))
1364
 
 
1365
 
    def set_model_constraints(self, constraints):
1366
 
        constraint_strings = self._dict_as_option_strings(constraints)
1367
 
        return self.juju('set-constraints', constraint_strings)
1368
 
 
1369
 
    def set_config(self, service, options):
1370
 
        option_strings = ['{}={}'.format(*item) for item in options.items()]
1371
 
        self.juju('set', (service,) + tuple(option_strings))
1372
 
 
1373
 
    def get_config(self, service):
1374
 
        return yaml_loads(self.get_juju_output('get', service))
1375
 
 
1376
 
    def get_model_config(self):
1377
 
        """Return the value of the environment's configured option."""
1378
 
        return yaml.safe_load(self.get_juju_output('get-env'))
1379
 
 
1380
 
    def get_env_option(self, option):
1381
 
        """Return the value of the environment's configured option."""
1382
 
        return self.get_juju_output('get-env', option)
1383
 
 
1384
 
    def set_env_option(self, option, value):
1385
 
        """Set the value of the option in the environment."""
1386
 
        option_value = "%s=%s" % (option, value)
1387
 
        return self.juju('set-env', (option_value,))
1388
 
 
1389
 
 
1390
 
class EnvJujuClient1X(EnvJujuClient2A1):
1391
 
    """Base for all 1.x client drivers."""
1392
 
 
1393
 
    # The environments.yaml options that are replaced by bootstrap options.
1394
 
    # For Juju 1.x, no bootstrap options are used.
1395
 
    bootstrap_replaces = frozenset()
1396
 
 
1397
 
    def get_bootstrap_args(self, upload_tools, bootstrap_series=None):
1398
 
        """Return the bootstrap arguments for the substrate."""
1399
 
        constraints = self._get_substrate_constraints()
1400
 
        args = ('--constraints', constraints)
1401
 
        if upload_tools:
1402
 
            args = ('--upload-tools',) + args
1403
 
        if bootstrap_series is not None:
1404
 
            env_val = self.env.config.get('default-series')
1405
 
            if bootstrap_series != env_val:
1406
 
                raise BootstrapMismatch(
1407
 
                    'bootstrap-series', bootstrap_series, 'default-series',
1408
 
                    env_val)
1409
 
        return args
1410
 
 
1411
 
    def get_jes_command(self):
1412
 
        """Return the JES command to destroy a controller.
1413
 
 
1414
 
        Juju 2.x has 'kill-controller'.
1415
 
        Some intermediate versions had 'controller kill'.
1416
 
        Juju 1.25 has 'system kill' when the jes feature flag is set.
1417
 
 
1418
 
        :raises: JESNotSupported when the version of Juju does not expose
1419
 
            a JES command.
1420
 
        :return: The JES command.
1421
 
        """
1422
 
        commands = self.get_juju_output('help', 'commands', include_e=False)
1423
 
        for line in commands.splitlines():
1424
 
            for cmd in _jes_cmds.keys():
1425
 
                if line.startswith(cmd):
1426
 
                    return cmd
1427
 
        raise JESNotSupported()
1428
 
 
1429
 
    def create_environment(self, controller_client, config_file):
1430
 
        seen_cmd = self.get_jes_command()
1431
 
        if seen_cmd == SYSTEM:
1432
 
            controller_option = ('-s', controller_client.env.environment)
1433
 
        else:
1434
 
            controller_option = ('-c', controller_client.env.environment)
1435
 
        self.juju(_jes_cmds[seen_cmd]['create'], controller_option + (
1436
 
            self.env.environment, '--config', config_file), include_e=False)
1437
 
 
1438
 
    def destroy_model(self):
1439
 
        """With JES enabled, destroy-environment destroys the model."""
1440
 
        self.destroy_environment(force=False)
1441
 
 
1442
 
    def destroy_environment(self, force=True, delete_jenv=False):
1443
 
        if force:
1444
 
            force_arg = ('--force',)
1445
 
        else:
1446
 
            force_arg = ()
1447
 
        exit_status = self.juju(
1448
 
            'destroy-environment',
1449
 
            (self.env.environment,) + force_arg + ('-y',),
1450
 
            self.env.needs_sudo(), check=False, include_e=False,
1451
 
            timeout=timedelta(minutes=10).total_seconds())
1452
 
        if delete_jenv:
1453
 
            jenv_path = get_jenv_path(self.env.juju_home, self.env.environment)
1454
 
            ensure_deleted(jenv_path)
1455
 
        return exit_status
1456
 
 
1457
 
    def _get_models(self):
1458
 
        """return a list of model dicts."""
1459
 
        return yaml.safe_load(self.get_juju_output(
1460
 
            'environments', '-s', self.env.environment, '--format', 'yaml',
1461
 
            include_e=False))
1462
 
 
1463
 
    def deploy_bundle(self, bundle, timeout=_DEFAULT_BUNDLE_TIMEOUT):
1464
 
        """Deploy bundle using deployer for Juju 1.X version."""
1465
 
        self.deployer(bundle, timeout=timeout)
1466
 
 
1467
 
    def get_controller_endpoint(self):
1468
 
        """Return the address of the state-server leader."""
1469
 
        endpoint = self.get_juju_output('api-endpoints')
1470
 
        address, port = split_address_port(endpoint)
1471
 
        return address
1472
 
 
1473
 
    def upgrade_mongo(self):
1474
 
        raise UpgradeMongoNotSupported()
1475
 
 
1476
 
 
1477
 
class EnvJujuClient22(EnvJujuClient1X):
1478
 
 
1479
 
    used_feature_flags = frozenset(['actions'])
1480
 
 
1481
 
    def __init__(self, *args, **kwargs):
1482
 
        super(EnvJujuClient22, self).__init__(*args, **kwargs)
1483
 
        self.feature_flags.add('actions')
1484
 
 
1485
 
 
1486
 
class EnvJujuClient26(EnvJujuClient1X):
1487
 
    """Drives Juju 2.6-series clients."""
1488
 
 
1489
 
    used_feature_flags = frozenset(['address-allocation', 'cloudsigma', 'jes'])
1490
 
 
1491
 
    def __init__(self, *args, **kwargs):
1492
 
        super(EnvJujuClient26, self).__init__(*args, **kwargs)
1493
 
        if self.env is None or self.env.config is None:
1494
 
            return
1495
 
        if self.env.config.get('type') == 'cloudsigma':
1496
 
            self.feature_flags.add('cloudsigma')
1497
 
 
1498
 
    def enable_jes(self):
1499
 
        """Enable JES if JES is optional.
1500
 
 
1501
 
        :raises: JESByDefault when JES is always enabled; Juju has the
1502
 
            'destroy-controller' command.
1503
 
        :raises: JESNotSupported when JES is not supported; Juju does not have
1504
 
            the 'system kill' command when the JES feature flag is set.
1505
 
        """
1506
 
 
1507
 
        if 'jes' in self.feature_flags:
1508
 
            return
1509
 
        if self.is_jes_enabled():
1510
 
            raise JESByDefault()
1511
 
        self.feature_flags.add('jes')
1512
 
        if not self.is_jes_enabled():
1513
 
            self.feature_flags.remove('jes')
1514
 
            raise JESNotSupported()
1515
 
 
1516
 
    def disable_jes(self):
1517
 
        if 'jes' in self.feature_flags:
1518
 
            self.feature_flags.remove('jes')
1519
 
 
1520
 
    def enable_container_address_allocation(self):
1521
 
        self.feature_flags.add('address-allocation')
1522
 
 
1523
 
 
1524
 
class EnvJujuClient25(EnvJujuClient26):
1525
 
    """Drives Juju 2.5-series clients."""
1526
 
 
1527
 
 
1528
 
class EnvJujuClient24(EnvJujuClient25):
1529
 
    """Similar to EnvJujuClient25, but lacking JES support."""
1530
 
 
1531
 
    used_feature_flags = frozenset(['cloudsigma'])
1532
 
 
1533
 
    def enable_jes(self):
1534
 
        raise JESNotSupported()
1535
 
 
1536
 
    def add_ssh_machines(self, machines):
1537
 
        for machine in machines:
1538
 
            self.juju('add-machine', ('ssh:' + machine,))
1539
 
 
1540
 
 
1541
 
def get_local_root(juju_home, env):
1542
 
    return os.path.join(juju_home, env.environment)
1543
 
 
1544
 
 
1545
 
def bootstrap_from_env(juju_home, client):
1546
 
    with temp_bootstrap_env(juju_home, client):
1547
 
        client.bootstrap()
1548
 
 
1549
 
 
1550
 
def quickstart_from_env(juju_home, client, bundle):
1551
 
    with temp_bootstrap_env(juju_home, client):
1552
 
        client.quickstart(bundle)
1553
 
 
1554
 
 
1555
 
@contextmanager
1556
 
def maybe_jes(client, jes_enabled, try_jes):
1557
 
    """If JES is desired and not enabled, try to enable it for this context.
1558
 
 
1559
 
    JES will be in its previous state after exiting this context.
1560
 
    If jes_enabled is True or try_jes is False, the context is a no-op.
1561
 
    If enable_jes() raises JESNotSupported, JES will not be enabled in the
1562
 
    context.
1563
 
 
1564
 
    The with value is True if JES is enabled in the context.
1565
 
    """
1566
 
 
1567
 
    class JESUnwanted(Exception):
1568
 
        """Non-error.  Used to avoid enabling JES if not wanted."""
1569
 
 
1570
 
    try:
1571
 
        if not try_jes or jes_enabled:
1572
 
            raise JESUnwanted
1573
 
        client.enable_jes()
1574
 
    except (JESNotSupported, JESUnwanted):
1575
 
        yield jes_enabled
1576
 
        return
1577
 
    else:
1578
 
        try:
1579
 
            yield True
1580
 
        finally:
1581
 
            client.disable_jes()
1582
 
 
1583
 
 
1584
 
def tear_down(client, jes_enabled, try_jes=False):
1585
 
    """Tear down a JES or non-JES environment.
1586
 
 
1587
 
    JES environments are torn down via 'controller kill' or 'system kill',
1588
 
    and non-JES environments are torn down via 'destroy-environment --force.'
1589
 
    """
1590
 
    with maybe_jes(client, jes_enabled, try_jes) as jes_enabled:
1591
 
        if jes_enabled:
1592
 
            client.kill_controller()
1593
 
        else:
1594
 
            if client.destroy_environment(force=False) != 0:
1595
 
                client.destroy_environment(force=True)
1596
 
 
1597
 
 
1598
 
def uniquify_local(env):
1599
 
    """Ensure that local environments have unique port settings.
1600
 
 
1601
 
    This allows local environments to be duplicated despite
1602
 
    https://bugs.launchpad.net/bugs/1382131
1603
 
    """
1604
 
    if not env.local:
1605
 
        return
1606
 
    port_defaults = {
1607
 
        'api-port': 17070,
1608
 
        'state-port': 37017,
1609
 
        'storage-port': 8040,
1610
 
        'syslog-port': 6514,
1611
 
    }
1612
 
    for key, default in port_defaults.items():
1613
 
        env.config[key] = env.config.get(key, default) + 1
1614
 
 
1615
 
 
1616
 
def dump_environments_yaml(juju_home, config):
1617
 
    environments_path = get_environments_path(juju_home)
1618
 
    with open(environments_path, 'w') as config_file:
1619
 
        yaml.safe_dump(config, config_file)
1620
 
 
1621
 
 
1622
 
@contextmanager
1623
 
def _temp_env(new_config, parent=None, set_home=True):
1624
 
    """Use the supplied config as juju environment.
1625
 
 
1626
 
    This is not a fully-formed version for bootstrapping.  See
1627
 
    temp_bootstrap_env.
1628
 
    """
1629
 
    with temp_dir(parent) as temp_juju_home:
1630
 
        dump_environments_yaml(temp_juju_home, new_config)
1631
 
        if set_home:
1632
 
            context = scoped_environ()
1633
 
        else:
1634
 
            context = nested()
1635
 
        with context:
1636
 
            if set_home:
1637
 
                os.environ['JUJU_HOME'] = temp_juju_home
1638
 
                os.environ['JUJU_DATA'] = temp_juju_home
1639
 
            yield temp_juju_home
1640
 
 
1641
 
 
1642
 
def jes_home_path(juju_home, dir_name):
1643
 
    return os.path.join(juju_home, 'jes-homes', dir_name)
1644
 
 
1645
 
 
1646
 
def get_cache_path(juju_home, models=False):
1647
 
    if models:
1648
 
        root = os.path.join(juju_home, 'models')
1649
 
    else:
1650
 
        root = os.path.join(juju_home, 'environments')
1651
 
    return os.path.join(root, 'cache.yaml')
1652
 
 
1653
 
 
1654
 
def make_safe_config(client):
1655
 
    config = dict(client.env.config)
1656
 
    if 'agent-version' in client.bootstrap_replaces:
1657
 
        config.pop('agent-version', None)
1658
 
    else:
1659
 
        config['agent-version'] = client.get_matching_agent_version()
1660
 
    # AFAICT, we *always* want to set test-mode to True.  If we ever find a
1661
 
    # use-case where we don't, we can make this optional.
1662
 
    config['test-mode'] = True
1663
 
    # Explicitly set 'name', which Juju implicitly sets to env.environment to
1664
 
    # ensure MAASAccount knows what the name will be.
1665
 
    config['name'] = client.env.environment
1666
 
    if config['type'] == 'local':
1667
 
        config.setdefault('root-dir', get_local_root(client.env.juju_home,
1668
 
                          client.env))
1669
 
        # MongoDB requires a lot of free disk space, and the only
1670
 
        # visible error message is from "juju bootstrap":
1671
 
        # "cannot initiate replication set" if disk space is low.
1672
 
        # What "low" exactly means, is unclear, but 8GB should be
1673
 
        # enough.
1674
 
        ensure_dir(config['root-dir'])
1675
 
        check_free_disk_space(config['root-dir'], 8000000, "MongoDB files")
1676
 
        if client.env.kvm:
1677
 
            check_free_disk_space(
1678
 
                "/var/lib/uvtool/libvirt/images", 2000000,
1679
 
                "KVM disk files")
1680
 
        else:
1681
 
            check_free_disk_space(
1682
 
                "/var/lib/lxc", 2000000, "LXC containers")
1683
 
    return config
1684
 
 
1685
 
 
1686
 
@contextmanager
1687
 
def temp_bootstrap_env(juju_home, client, set_home=True, permanent=False):
1688
 
    """Create a temporary environment for bootstrapping.
1689
 
 
1690
 
    This involves creating a temporary juju home directory and returning its
1691
 
    location.
1692
 
 
1693
 
    :param set_home: Set JUJU_HOME to match the temporary home in this
1694
 
        context.  If False, juju_home should be supplied to bootstrap.
1695
 
    """
1696
 
    new_config = {
1697
 
        'environments': {client.env.environment: make_safe_config(client)}}
1698
 
    # Always bootstrap a matching environment.
1699
 
    jenv_path = get_jenv_path(juju_home, client.env.environment)
1700
 
    if permanent:
1701
 
        context = client.env.make_jes_home(
1702
 
            juju_home, client.env.environment, new_config)
1703
 
    else:
1704
 
        context = _temp_env(new_config, juju_home, set_home)
1705
 
    with context as temp_juju_home:
1706
 
        if os.path.lexists(jenv_path):
1707
 
            raise Exception('%s already exists!' % jenv_path)
1708
 
        new_jenv_path = get_jenv_path(temp_juju_home, client.env.environment)
1709
 
        # Create a symlink to allow access while bootstrapping, and to reduce
1710
 
        # races.  Can't use a hard link because jenv doesn't exist until
1711
 
        # partway through bootstrap.
1712
 
        ensure_dir(os.path.join(juju_home, 'environments'))
1713
 
        # Skip creating symlink where not supported (i.e. Windows).
1714
 
        if not permanent and getattr(os, 'symlink', None) is not None:
1715
 
            os.symlink(new_jenv_path, jenv_path)
1716
 
        old_juju_home = client.env.juju_home
1717
 
        client.env.juju_home = temp_juju_home
1718
 
        try:
1719
 
            yield temp_juju_home
1720
 
        finally:
1721
 
            if not permanent:
1722
 
                # replace symlink with file before deleting temp home.
1723
 
                try:
1724
 
                    os.rename(new_jenv_path, jenv_path)
1725
 
                except OSError as e:
1726
 
                    if e.errno != errno.ENOENT:
1727
 
                        raise
1728
 
                    # Remove dangling symlink
1729
 
                    try:
1730
 
                        os.unlink(jenv_path)
1731
 
                    except OSError as e:
1732
 
                        if e.errno != errno.ENOENT:
1733
 
                            raise
1734
 
                client.env.juju_home = old_juju_home
1735
 
 
1736
 
 
1737
 
def get_machine_dns_name(client, machine, timeout=600):
1738
 
    """Wait for dns-name on a juju machine."""
1739
 
    for status in client.status_until(timeout=timeout):
1740
 
        try:
1741
 
            return _dns_name_for_machine(status, machine)
1742
 
        except KeyError:
1743
 
            log.debug("No dns-name yet for machine %s", machine)
1744
 
 
1745
 
 
1746
 
def _dns_name_for_machine(status, machine):
1747
 
    host = status.status['machines'][machine]['dns-name']
1748
 
    if is_ipv6_address(host):
1749
 
        log.warning("Selected IPv6 address for machine %s: %r", machine, host)
1750
 
    return host
1751
 
 
1752
 
 
1753
 
class Controller:
1754
 
 
1755
 
    def __init__(self, name):
1756
 
        self.name = name
 
160
            return subprocess.check_call(args)
 
161
        return subprocess.call(args)
1757
162
 
1758
163
 
1759
164
class Status:
1760
165
 
1761
 
    def __init__(self, status, status_text):
 
166
    def __init__(self, status):
1762
167
        self.status = status
1763
 
        self.status_text = status_text
1764
 
 
1765
 
    @classmethod
1766
 
    def from_text(cls, text):
1767
 
        status_yaml = yaml_loads(text)
1768
 
        return cls(status_yaml, text)
1769
 
 
1770
 
    def iter_machines(self, containers=False, machines=True):
 
168
 
 
169
    def iter_machines(self):
1771
170
        for machine_name, machine in sorted(self.status['machines'].items()):
1772
 
            if machines:
1773
 
                yield machine_name, machine
1774
 
            if containers:
1775
 
                for contained, unit in machine.get('containers', {}).items():
1776
 
                    yield contained, unit
1777
 
 
1778
 
    def iter_new_machines(self, old_status):
1779
 
        for machine, data in self.iter_machines():
1780
 
            if machine in old_status.status['machines']:
1781
 
                continue
1782
 
            yield machine, data
1783
 
 
1784
 
    def iter_units(self):
1785
 
        for service_name, service in sorted(self.status['services'].items()):
1786
 
            for unit_name, unit in sorted(service.get('units', {}).items()):
 
171
            yield machine_name, machine
 
172
 
 
173
    def agent_items(self):
 
174
        for result in self.iter_machines():
 
175
            yield result
 
176
        for service in sorted(self.status['services'].values()):
 
177
            for unit_name, unit in service.get('units', {}).items():
1787
178
                yield unit_name, unit
1788
 
                subordinates = unit.get('subordinates', ())
1789
 
                for sub_name in sorted(subordinates):
1790
 
                    yield sub_name, subordinates[sub_name]
1791
 
 
1792
 
    def agent_items(self):
1793
 
        for machine_name, machine in self.iter_machines(containers=True):
1794
 
            yield machine_name, machine
1795
 
        for unit_name, unit in self.iter_units():
1796
 
            yield unit_name, unit
1797
179
 
1798
180
    def agent_states(self):
1799
181
        """Map agent states to the units and machines in those states."""
1800
182
        states = defaultdict(list)
1801
183
        for item_name, item in self.agent_items():
1802
 
            states[coalesce_agent_status(item)].append(item_name)
 
184
            states[item.get('agent-state', 'no-agent')].append(item_name)
1803
185
        return states
1804
186
 
1805
 
    def check_agents_started(self, environment_name=None):
 
187
    def check_agents_started(self, environment_name):
1806
188
        """Check whether all agents are in the 'started' state.
1807
189
 
1808
190
        If not, return agent_states output.  If so, return None.
1809
191
        If an error is encountered for an agent, raise ErroredUnit
1810
192
        """
1811
 
        bad_state_info = re.compile(
1812
 
            '(.*error|^(cannot set up groups|cannot run instance)).*')
 
193
        # Look for errors preventing an agent from being installed
1813
194
        for item_name, item in self.agent_items():
1814
195
            state_info = item.get('agent-state-info', '')
1815
 
            if bad_state_info.match(state_info):
 
196
            if 'error' in state_info:
1816
197
                raise ErroredUnit(item_name, state_info)
1817
198
        states = self.agent_states()
1818
 
        if set(states.keys()).issubset(AGENTS_READY):
 
199
        if states.keys() == ['started']:
1819
200
            return None
1820
201
        for state, entries in states.items():
1821
202
            if 'error' in state:
1822
 
                raise ErroredUnit(entries[0], state)
 
203
                raise ErroredUnit(entries[0],  state)
1823
204
        return states
1824
205
 
1825
 
    def get_service_count(self):
1826
 
        return len(self.status.get('services', {}))
1827
 
 
1828
 
    def get_service_unit_count(self, service):
1829
 
        return len(
1830
 
            self.status.get('services', {}).get(service, {}).get('units', {}))
1831
 
 
1832
206
    def get_agent_versions(self):
1833
207
        versions = defaultdict(set)
1834
208
        for item_name, item in self.agent_items():
1835
 
            if item.get('juju-status', None):
1836
 
                version = item['juju-status'].get('version', 'unknown')
1837
 
                versions[version].add(item_name)
1838
 
            else:
1839
 
                versions[item.get('agent-version', 'unknown')].add(item_name)
 
209
            versions[item.get('agent-version', 'unknown')].add(item_name)
1840
210
        return versions
1841
211
 
1842
 
    def get_instance_id(self, machine_id):
1843
 
        return self.status['machines'][machine_id]['instance-id']
1844
 
 
1845
 
    def get_unit(self, unit_name):
1846
 
        """Return metadata about a unit."""
1847
 
        for service in sorted(self.status['services'].values()):
1848
 
            if unit_name in service.get('units', {}):
1849
 
                return service['units'][unit_name]
1850
 
        raise KeyError(unit_name)
1851
 
 
1852
 
    def service_subordinate_units(self, service_name):
1853
 
        """Return subordinate metadata for a service_name."""
1854
 
        services = self.status.get('services', {})
1855
 
        if service_name in services:
1856
 
            for unit in sorted(services[service_name].get(
1857
 
                    'units', {}).values()):
1858
 
                for sub_name, sub in unit.get('subordinates', {}).items():
1859
 
                    yield sub_name, sub
1860
 
 
1861
 
    def get_open_ports(self, unit_name):
1862
 
        """List the open ports for the specified unit.
1863
 
 
1864
 
        If no ports are listed for the unit, the empty list is returned.
1865
 
        """
1866
 
        return self.get_unit(unit_name).get('open-ports', [])
1867
 
 
1868
 
 
1869
 
class SimpleEnvironment:
1870
 
 
1871
 
    def __init__(self, environment, config=None, juju_home=None,
1872
 
                 controller=None):
1873
 
        if controller is None:
1874
 
            controller = Controller(environment)
1875
 
        self.controller = controller
 
212
 
 
213
class Environment:
 
214
 
 
215
    def __init__(self, environment, client=None, config=None):
1876
216
        self.environment = environment
 
217
        self.client = client
1877
218
        self.config = config
1878
 
        self.juju_home = juju_home
1879
219
        if self.config is not None:
1880
220
            self.local = bool(self.config.get('type') == 'local')
1881
221
            self.kvm = (
1882
222
                self.local and bool(self.config.get('container') == 'kvm'))
1883
 
            self.maas = bool(self.config.get('type') == 'maas')
1884
 
            self.joyent = bool(self.config.get('type') == 'joyent')
 
223
            self.hpcloud = bool(
 
224
                'hpcloudsvc' in self.config.get('auth-url', ''))
1885
225
        else:
1886
226
            self.local = False
1887
 
            self.kvm = False
1888
 
            self.maas = False
1889
 
            self.joyent = False
1890
 
 
1891
 
    def clone(self, model_name=None):
1892
 
        config = deepcopy(self.config)
1893
 
        if model_name is None:
1894
 
            model_name = self.environment
1895
 
        else:
1896
 
            config['name'] = model_name
1897
 
        result = self.__class__(model_name, config, self.juju_home,
1898
 
                                self.controller)
1899
 
        result.local = self.local
1900
 
        result.kvm = self.kvm
1901
 
        result.maas = self.maas
1902
 
        result.joyent = self.joyent
1903
 
        return result
1904
 
 
1905
 
    def __eq__(self, other):
1906
 
        if type(self) != type(other):
1907
 
            return False
1908
 
        if self.environment != other.environment:
1909
 
            return False
1910
 
        if self.config != other.config:
1911
 
            return False
1912
 
        if self.local != other.local:
1913
 
            return False
1914
 
        if self.maas != other.maas:
1915
 
            return False
1916
 
        return True
1917
 
 
1918
 
    def __ne__(self, other):
1919
 
        return not self == other
1920
 
 
1921
 
    def set_model_name(self, model_name, set_controller=True):
1922
 
        if set_controller:
1923
 
            self.controller.name = model_name
1924
 
        self.environment = model_name
1925
 
        self.config['name'] = model_name
 
227
            self.hpcloud = False
1926
228
 
1927
229
    @classmethod
1928
230
    def from_config(cls, name):
1929
 
        return cls._from_config(name)
1930
 
 
1931
 
    @classmethod
1932
 
    def _from_config(cls, name):
1933
 
        config, selected = get_selected_environment(name)
1934
 
        if name is None:
1935
 
            name = selected
1936
 
        return cls(name, config)
 
231
        client = JujuClientDevel.by_version()
 
232
        return cls(name, client, get_selected_environment(name)[0])
1937
233
 
1938
234
    def needs_sudo(self):
1939
235
        return self.local
1940
236
 
1941
 
    @contextmanager
1942
 
    def make_jes_home(self, juju_home, dir_name, new_config):
1943
 
        home_path = jes_home_path(juju_home, dir_name)
1944
 
        if os.path.exists(home_path):
1945
 
            rmtree(home_path)
1946
 
        os.makedirs(home_path)
1947
 
        self.dump_yaml(home_path, new_config)
1948
 
        yield home_path
1949
 
 
1950
 
    def dump_yaml(self, path, config):
1951
 
        dump_environments_yaml(path, config)
1952
 
 
1953
 
 
1954
 
class JujuData(SimpleEnvironment):
1955
 
 
1956
 
    def __init__(self, environment, config=None, juju_home=None,
1957
 
                 controller=None):
1958
 
        if juju_home is None:
1959
 
            juju_home = get_juju_home()
1960
 
        super(JujuData, self).__init__(environment, config, juju_home,
1961
 
                                       controller)
1962
 
        self.credentials = {}
1963
 
        self.clouds = {}
1964
 
 
1965
 
    def clone(self, model_name=None):
1966
 
        result = super(JujuData, self).clone(model_name)
1967
 
        result.credentials = deepcopy(self.credentials)
1968
 
        result.clouds = deepcopy(self.clouds)
1969
 
        return result
1970
 
 
1971
 
    @classmethod
1972
 
    def from_env(cls, env):
1973
 
        juju_data = cls(env.environment, env.config, env.juju_home)
1974
 
        juju_data.load_yaml()
1975
 
        return juju_data
1976
 
 
1977
 
    def load_yaml(self):
1978
 
        with open(os.path.join(self.juju_home, 'credentials.yaml')) as f:
1979
 
            self.credentials = yaml.safe_load(f)
1980
 
        with open(os.path.join(self.juju_home, 'clouds.yaml')) as f:
1981
 
            self.clouds = yaml.safe_load(f)
1982
 
 
1983
 
    @classmethod
1984
 
    def from_config(cls, name):
1985
 
        juju_data = cls._from_config(name)
1986
 
        juju_data.load_yaml()
1987
 
        return juju_data
1988
 
 
1989
 
    def dump_yaml(self, path, config):
1990
 
        """Dump the configuration files to the specified path.
1991
 
 
1992
 
        config is unused, but is accepted for compatibility with
1993
 
        SimpleEnvironment and make_jes_home().
1994
 
        """
1995
 
        with open(os.path.join(path, 'credentials.yaml'), 'w') as f:
1996
 
            yaml.safe_dump(self.credentials, f)
1997
 
        with open(os.path.join(path, 'clouds.yaml'), 'w') as f:
1998
 
            yaml.safe_dump(self.clouds, f)
1999
 
 
2000
 
    def find_endpoint_cloud(self, cloud_type, endpoint):
2001
 
        for cloud, cloud_config in self.clouds['clouds'].items():
2002
 
            if cloud_config['type'] != cloud_type:
2003
 
                continue
2004
 
            if cloud_config['endpoint'] == endpoint:
2005
 
                return cloud
2006
 
        raise LookupError('No such endpoint: {}'.format(endpoint))
2007
 
 
2008
 
    def get_cloud(self):
2009
 
        provider = self.config['type']
2010
 
        # Separate cloud recommended by: Juju Cloud / Credentials / BootStrap /
2011
 
        # Model CLI specification
2012
 
        if provider == 'ec2' and self.config['region'] == 'cn-north-1':
2013
 
            return 'aws-china'
2014
 
        if provider not in ('maas', 'openstack'):
2015
 
            return {
2016
 
                'ec2': 'aws',
2017
 
                'gce': 'google',
2018
 
            }.get(provider, provider)
2019
 
        if provider == 'maas':
2020
 
            endpoint = self.config['maas-server']
2021
 
        elif provider == 'openstack':
2022
 
            endpoint = self.config['auth-url']
2023
 
        return self.find_endpoint_cloud(provider, endpoint)
2024
 
 
2025
 
    def get_region(self):
2026
 
        provider = self.config['type']
2027
 
        if provider == 'azure':
2028
 
            if 'tenant-id' not in self.config:
2029
 
                raise ValueError('Non-ARM Azure not supported.')
2030
 
            return self.config['location']
2031
 
        elif provider == 'joyent':
2032
 
            matcher = re.compile('https://(.*).api.joyentcloud.com')
2033
 
            return matcher.match(self.config['sdc-url']).group(1)
2034
 
        elif provider == 'lxd':
2035
 
            return 'localhost'
2036
 
        elif provider == 'manual':
2037
 
            return self.config['bootstrap-host']
2038
 
        elif provider in ('maas', 'manual'):
2039
 
            return None
2040
 
        else:
2041
 
            return self.config['region']
2042
 
 
2043
 
 
2044
 
class GroupReporter:
2045
 
 
2046
 
    def __init__(self, stream, expected):
2047
 
        self.stream = stream
2048
 
        self.expected = expected
2049
 
        self.last_group = None
2050
 
        self.ticks = 0
2051
 
        self.wrap_offset = 0
2052
 
        self.wrap_width = 79
2053
 
 
2054
 
    def _write(self, string):
2055
 
        self.stream.write(string)
2056
 
        self.stream.flush()
2057
 
 
2058
 
    def finish(self):
2059
 
        if self.last_group:
2060
 
            self._write("\n")
2061
 
 
2062
 
    def update(self, group):
2063
 
        if group == self.last_group:
2064
 
            if (self.wrap_offset + self.ticks) % self.wrap_width == 0:
2065
 
                self._write("\n")
2066
 
            self._write("." if self.ticks or not self.wrap_offset else " .")
2067
 
            self.ticks += 1
2068
 
            return
2069
 
        value_listing = []
2070
 
        for value, entries in sorted(group.items()):
2071
 
            if value == self.expected:
2072
 
                continue
2073
 
            value_listing.append('%s: %s' % (value, ', '.join(entries)))
2074
 
        string = ' | '.join(value_listing)
2075
 
        lead_length = len(string) + 1
2076
 
        if self.last_group:
2077
 
            string = "\n" + string
2078
 
        self._write(string)
2079
 
        self.last_group = group
2080
 
        self.ticks = 0
2081
 
        self.wrap_offset = lead_length if lead_length < self.wrap_width else 0
 
237
    def bootstrap(self):
 
238
        return self.client.bootstrap(self)
 
239
 
 
240
    def upgrade_juju(self):
 
241
        args = ('--version', self.get_matching_agent_version(no_build=True))
 
242
        if self.local:
 
243
            args += ('--upload-tools',)
 
244
        self.client.juju(self, 'upgrade-juju', args)
 
245
 
 
246
    def destroy_environment(self):
 
247
        return self.client.destroy_environment(self)
 
248
 
 
249
    def deploy(self, charm):
 
250
        args = (charm,)
 
251
        return self.juju('deploy', *args)
 
252
 
 
253
    def juju(self, command, *args):
 
254
        return self.client.juju(self, command, args)
 
255
 
 
256
    def get_status(self, timeout=60):
 
257
        return self.client.get_status(self, timeout)
 
258
 
 
259
    def wait_for_started(self, timeout=1200):
 
260
        """Wait until all unit/machine agents are 'started'."""
 
261
        for ignored in until_timeout(timeout):
 
262
            try:
 
263
                status = self.get_status()
 
264
            except CannotConnectEnv:
 
265
                print('Supressing "Unable to connect to environment"')
 
266
                continue
 
267
            states = status.check_agents_started(self.environment)
 
268
            if states is None:
 
269
                break
 
270
            print(format_listing(states, 'started'))
 
271
            sys.stdout.flush()
 
272
        else:
 
273
            raise Exception('Timed out waiting for agents to start in %s.' %
 
274
                            self.environment)
 
275
        return status
 
276
 
 
277
    def wait_for_version(self, version, timeout=300):
 
278
        for ignored in until_timeout(timeout):
 
279
            try:
 
280
                versions = self.get_status(120).get_agent_versions()
 
281
            except CannotConnectEnv:
 
282
                print('Supressing "Unable to connect to environment"')
 
283
                continue
 
284
            if versions.keys() == [version]:
 
285
                break
 
286
            print(format_listing(versions, version))
 
287
            sys.stdout.flush()
 
288
        else:
 
289
            raise Exception('Some versions did not update.')
 
290
 
 
291
    def get_matching_agent_version(self, no_build=False):
 
292
        # strip the series and srch from the built version.
 
293
        version_parts = self.client.version.split('-')
 
294
        if len(version_parts) == 4:
 
295
            version_number = '-'.join(version_parts[0:2])
 
296
        else:
 
297
            version_number = version_parts[0]
 
298
        if not no_build and self.local:
 
299
            version_number += '.1'
 
300
        return version_number
 
301
 
 
302
    def set_testing_tools_metadata_url(self):
 
303
        url = self.client.get_env_option(self, 'tools-metadata-url')
 
304
        if 'testing' not in url:
 
305
            testing_url = url.replace('/tools', '/testing/tools')
 
306
            self.client.set_env_option(self, 'tools-metadata-url',  testing_url)
 
307
 
 
308
 
 
309
def format_listing(listing, expected):
 
310
    value_listing = []
 
311
    for value, entries in listing.items():
 
312
        if value == expected:
 
313
            continue
 
314
        value_listing.append('%s: %s' % (value, ', '.join(entries)))
 
315
    return ' | '.join(value_listing)