134
136
'revision: {}'.format(charm))
139
def _save_calable(parser, env_file, env_db):
141
envs.save(env_file, env_db)
142
except OSError as err:
143
return parser.error(bytes(err))
137
146
def _validate_env(options, parser):
138
147
"""Validate and process the provided environment related options.
149
Also start the environments management interactive session if required.
140
151
Exit with an error if options are not valid.
142
153
logging.debug('ensuring juju environments available')
154
env_name = options.env_name
143
155
env_file = os.path.abspath(os.path.expanduser(options.env_file))
156
interactive = options.interactive
157
if not os.path.exists(env_file):
158
# If the Juju home is not set up, create an empty environments file,
159
# force the interactive mode and ignore the user provided env name.
160
envs.create(env_file)
144
163
# Validate the environment name.
145
env_name = options.env_name
164
if env_name is None and not interactive:
165
# The user forced non-interactive mode but a default env name cannot
166
# be retrieved. In this case, just exit with an error.
147
167
return parser.error(
148
168
'unable to find an environment name to use\n'
149
'It is possible to specify the environment name by either:\n'
169
'It is possible to specify the environment to use by either:\n'
170
' - selecting one from the quickstart interactive session;\n'
150
171
' - passing the -e or --environment argument;\n'
151
172
' - setting the JUJU_ENV environment variable;\n'
152
173
' - using "juju switch" to select the default environment;\n'
155
176
# Validate the environment file.
157
env_type, admin_secret, default_series = (
158
envs.parse_env_file(env_file, env_name))
178
env_db = envs.load(env_file)
159
179
except ValueError as err:
160
180
return parser.error(bytes(err))
181
# Validate the environment.
182
env_type_db = envs.get_env_type_db()
184
# Start the interactive session.
185
save_callable = functools.partial(_save_calable, parser, env_file)
186
new_env_db, env_data = views.show(
187
views.env_index, env_type_db, env_db, save_callable)
188
if new_env_db != env_db:
189
print('changes to the environments file have been saved')
191
# The user exited the interactive session without selecting an
192
# environment to start: this means this was just an environment
193
# editing session and we can just quit now.
196
# This is a non-interactive session and we need to validate the
197
# selected environment before proceeding.
199
env_data = envs.get_env_data(env_db, env_name)
200
except ValueError as err:
201
# The specified environment does not exist.
202
return parser.error(bytes(err))
203
env_metadata = envs.get_env_metadata(env_type_db, env_data)
204
errors = envs.validate(env_metadata, env_data)
206
msg = 'cannot use the {} environment:\n{}'.format(
207
env_name, '\n'.join(errors.values()))
208
return parser.error(msg.encode('utf-8'))
161
209
# Update the options namespace with the new values.
162
options.admin_secret = admin_secret
210
options.admin_secret = env_data['admin-secret']
163
211
options.env_file = env_file
164
options.env_type = env_type
165
options.default_series = default_series
212
options.env_name = env_data['name']
213
options.env_type = env_data['type']
214
options.default_series = env_data.get('default-series')
215
options.interactive = interactive
168
218
def _configure_logging(level):
204
254
- env_file: the absolute path of the Juju environments.yaml file;
205
255
- env_name: the name of the Juju environment to use;
206
256
- env_type: the provider type of the selected Juju environment;
257
- interactive: whether to start the interactive session;
207
258
- open_browser: whether the GUI browser must be opened.
209
260
The following attributes will also be included in the namespace if a bundle
219
270
default_env_name = envs.get_default_env_name()
220
271
# Define the help message for the --environment option.
221
272
env_help = 'The name of the Juju environment to use'
222
# XXX 2013-12-02 makyo:
223
# ensure_environments will be removed with more robust environment
224
# management code in the next few weeks.
225
if default_env_name is None:
226
default_env_name = app.ensure_environments()
227
273
if default_env_name is not None:
228
274
env_help = '{} (%(default)s)'.format(env_help)
229
275
# Create and set up the arguments parser.
245
291
'required if the bundle YAML/JSON only contains one bundle. This '
246
292
'option is ignored if the bundle file is not specified')
247
293
parser.add_argument(
294
'-i', '--interactive', action='store_true', dest='interactive',
295
help='Start the environments management interactive session')
248
297
'--environments-file', dest='env_file',
249
298
default=os.path.join(settings.JUJU_HOME, 'environments.yaml'),
250
299
help='The path to the Juju environments YAML file (%(default)s)')