34
39
def bootstrap(env_name):
35
40
"""Bootstrap the Juju environment with the given name.
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.
45
retcode, _, error = utils.call(
46
'juju', 'bootstrap', '--environment', env_name)
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':
53
raise ProgramExit(error)
57
def get_api_url(env_name):
58
"""Return a Juju API URL for the given environment name.
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".
63
retcode, output, error = utils.call(
64
'juju', 'api-endpoints', '--environment', env_name, '--format', 'json')
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)