~frankban/juju-quickstart/env-creation-proto

« back to all changes in this revision

Viewing changes to quickstart/utils.py

  • Committer: Francesco Banconi
  • Date: 2013-11-30 01:02:59 UTC
  • Revision ID: francesco.banconi@canonical.com-20131130010259-tpc31hihheizt3bz
Checkpoint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import httplib
22
22
import json
23
23
import logging
24
 
import os
25
24
import pipes
26
 
import re
27
25
import socket
28
26
import subprocess
29
27
import urllib2
35
33
    settings
36
34
)
37
35
 
38
 
# Compile the regular expression used to parse the "juju switch" output.
39
 
_juju_switch_expression = re.compile(r'Current environment: "([\w-]+)"\n')
40
 
 
41
36
 
42
37
def add_apt_repository(repository):
43
38
    """Add the given APT repository to the current list of APT sources.
134
129
    return charm_url
135
130
 
136
131
 
137
 
def get_default_env_name():
138
 
    """Return the current Juju environment name.
139
 
 
140
 
    The environment name can be set either by
141
 
        - setting the JUJU_ENV environment variable;
142
 
        - using "juju switch my-env-name";
143
 
        - setting the default environment in the environments.yaml file.
144
 
    The former overrides the latter.
145
 
 
146
 
    Return None if a default environment is not found.
147
 
    """
148
 
    env_name = os.getenv('JUJU_ENV', '').strip()
149
 
    if env_name:
150
 
        return env_name
151
 
    # XXX 2013-10-16 frankban bug=1193244:
152
 
        # Support the new behavior of juju-switch, currently under development:
153
 
        # the command will just output the environment name, or exit with an
154
 
        # error if no default environment is configured.
155
 
    # The "juju switch" command parses ~/.juju/current-environment file. If the
156
 
    # environment name is not found there, then it tries to retrieve the name
157
 
    # from the "default" section of the ~/.juju/environments.yaml file.
158
 
    retcode, output, _ = call('juju', 'switch')
159
 
    if retcode:
160
 
        return None
161
 
    match = _juju_switch_expression.match(output)
162
 
    if match is not None:
163
 
        return match.groups()[0]
164
 
 
165
 
 
166
132
def get_service_info(status, service_name):
167
133
    """Retrieve information on the given service and on its first alive unit.
168
134
 
258
224
    return bundle_name, bundle_services
259
225
 
260
226
 
261
 
def parse_env_file(env_file, env_name):
262
 
    """Parse the provided Juju environments.yaml file.
263
 
 
264
 
    Return a tuple containing the provider type and the admin secret associated
265
 
    with the given environment name.
266
 
 
267
 
    Raise a ValueError if:
268
 
      - the environment file is not found;
269
 
      - the environment file contents are not parsable by YAML;
270
 
      - the YAML contents are not properly structured;
271
 
      - the environment named envname is not found;
272
 
      - the environment does not include the "type" field;
273
 
      - the environment does not include the "admin_secret" field.
274
 
    """
275
 
    # Load the Juju environments file.
276
 
    try:
277
 
        environments_file = open(env_file)
278
 
    except IOError as err:
279
 
        msg = 'unable to open environments file: {}'.format(err)
280
 
        raise ValueError(msg)
281
 
    # Parse the Juju environments file.
282
 
    try:
283
 
        environments = yaml.safe_load(environments_file)
284
 
    except Exception as err:
285
 
        msg = 'unable to parse environments file {}: {}'.format(env_file, err)
286
 
        raise ValueError(msg)
287
 
    # Retrieve the information about the current environment.
288
 
    try:
289
 
        environment = environments.get('environments', {}).get(env_name)
290
 
    except AttributeError as err:
291
 
        msg = 'invalid YAML contents in {}: {}'.format(env_file, environments)
292
 
        raise ValueError(msg)
293
 
    if environment is None:
294
 
        msg = 'environment {} not found in {}'.format(env_name, env_file)
295
 
        raise ValueError(msg)
296
 
    # Retrieve the provider type and the admin secret.
297
 
    env_type = environment.get('type')
298
 
    if env_type is None:
299
 
        msg = '{} provider type not found in {}'.format(env_name, env_file)
300
 
        raise ValueError(msg)
301
 
    admin_secret = environment.get('admin-secret')
302
 
    if admin_secret is None:
303
 
        msg = '{} admin secret not found in {}'.format(env_name, env_file)
304
 
        raise ValueError(msg)
305
 
    return env_type, admin_secret
306
 
 
307
 
 
308
227
def parse_status_output(output):
309
228
    """Parse the output of juju status.
310
229