38
# Compile the regular expression used to parse the "juju switch" output.
39
_juju_switch_expression = re.compile(r'Current environment: "([\w-]+)"\n')
42
37
def add_apt_repository(repository):
43
38
"""Add the given APT repository to the current list of APT sources.
137
def get_default_env_name():
138
"""Return the current Juju environment name.
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.
146
Return None if a default environment is not found.
148
env_name = os.getenv('JUJU_ENV', '').strip()
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')
161
match = _juju_switch_expression.match(output)
162
if match is not None:
163
return match.groups()[0]
166
132
def get_service_info(status, service_name):
167
133
"""Retrieve information on the given service and on its first alive unit.
258
224
return bundle_name, bundle_services
261
def parse_env_file(env_file, env_name):
262
"""Parse the provided Juju environments.yaml file.
264
Return a tuple containing the provider type and the admin secret associated
265
with the given environment name.
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.
275
# Load the Juju environments file.
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.
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.
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')
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
308
227
def parse_status_output(output):
309
228
"""Parse the output of juju status.