~frankban/juju-gui/quickstart-real-bootstrap

« back to all changes in this revision

Viewing changes to quickstart/app.py

  • Committer: Francesco Banconi
  • Date: 2013-10-15 17:33:10 UTC
  • Revision ID: francesco.banconi@canonical.com-20131015173310-qt2pq83kttj5nial
Implemented the main app functions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Juju Quickstart base application functions."""
18
18
 
19
19
 
 
20
import json
 
21
 
 
22
from quickstart import utils
 
23
 
 
24
 
20
25
class ProgramExit(Exception):
21
26
    """An error occurred while setting up the Juju environment.
22
27
 
34
39
def bootstrap(env_name):
35
40
    """Bootstrap the Juju environment with the given name.
36
41
 
37
 
    Return the environment API address (e.g. "api.example.com:17070").
 
42
    Return True if the environment is already bootstrapped, False otherwise.
38
43
    Raise a ProgramExit if any error occurs in the bootstrap process.
39
 
    Otherwise return when the environment is bootstrapped and the API server
40
 
    is ready to accept connections.
41
 
    """
42
 
    # TODO: everything!
 
44
    """
 
45
    retcode, _, error = utils.call(
 
46
        'juju', 'bootstrap', '--environment', env_name)
 
47
    if retcode:
 
48
        # Relying on the subprocess output can be fragile. In this case, if
 
49
        # the output changes in the future, the program will exit with an error
 
50
        # without any other side effects.
 
51
        if error.strip() == 'ERROR environment is already bootstrapped':
 
52
            return True
 
53
        raise ProgramExit(error)
 
54
    return False
 
55
 
 
56
 
 
57
def get_api_url(env_name):
 
58
    """Return a Juju API URL for the given environment name.
 
59
 
 
60
    Use the Juju CLI in a subprocess in order to retrieve the API addresses.
 
61
    Return the complete URL, e.g. "wss://api.example.com:17070".
 
62
    """
 
63
    retcode, output, error = utils.call(
 
64
        'juju', 'api-endpoints', '--environment', env_name, '--format', 'json')
 
65
    if retcode:
 
66
        raise ProgramExit(error)
 
67
    # Grab the first API address from the JSON output.
 
68
    api_address = json.loads(output)[0]
 
69
    return 'wss://{}'.format(api_address)