Revision 362adf50 kamaki/cli/__init__.py
b/kamaki/cli/__init__.py | ||
---|---|---|
195 | 195 |
def _check_config_version(cnf): |
196 | 196 |
guess = cnf.guess_version() |
197 | 197 |
if guess < 3.0: |
198 |
print('Configuration file "%s" is not updated to v3.0' % ( |
|
198 |
print('Config file format version >= 3.0 is required') |
|
199 |
print('Configuration file "%s" format is not up to date' % ( |
|
199 | 200 |
cnf.path)) |
200 |
print('Calculating changes while preserving information ...') |
|
201 |
print('but kamaki can fix this:') |
|
202 |
print('Calculating changes while preserving information') |
|
201 | 203 |
lost_terms = cnf.rescue_old_file() |
202 |
if lost_terms: |
|
203 |
print 'The following information will not be preserved:' |
|
204 |
print '...', '\n... '.join(lost_terms) |
|
205 | 204 |
print('... DONE') |
206 |
print('Kamaki is ready to transform config file to version 3.0') |
|
205 |
if lost_terms: |
|
206 |
print 'The following information will NOT be preserved:' |
|
207 |
print '\t', '\n\t'.join(lost_terms) |
|
208 |
print('Kamaki is ready to convert the config file to version 3.0') |
|
207 | 209 |
stdout.write('Overwrite file %s ? [Y, y] ' % cnf.path) |
208 | 210 |
from sys import stdin |
209 | 211 |
reply = stdin.readline() |
... | ... | |
214 | 216 |
print('... ABORTING') |
215 | 217 |
raise CLIError( |
216 | 218 |
'Invalid format for config file %s' % cnf.path, |
217 |
importance=3, details=['Please, update config file to v3.0']) |
|
219 |
importance=3, details=[ |
|
220 |
'Please, update config file to v3.0', |
|
221 |
'For automatic conversion, rerun and say Y']) |
|
218 | 222 |
|
219 | 223 |
|
220 |
def _init_session(arguments): |
|
224 |
def _init_session(arguments, is_non_API=False):
|
|
221 | 225 |
global _help |
222 | 226 |
_help = arguments['help'].value |
223 | 227 |
global _debug |
... | ... | |
228 | 232 |
_verbose = arguments['verbose'].value |
229 | 233 |
_cnf = arguments['config'] |
230 | 234 |
_check_config_version(_cnf.value) |
231 |
raise CLIError( |
|
232 |
'Your file is OK, but i am not ready to proceed', |
|
233 |
importance=3, details=['DO NOT PANIC, EXIT THE BUILDING QUIETLY']) |
|
234 | 235 |
|
235 | 236 |
global _colors |
236 |
_colors = _cnf.get('global', 'colors')
|
|
237 |
_colors = _cnf.value.get_global('colors')
|
|
237 | 238 |
if not (stdout.isatty() and _colors == 'on'): |
238 | 239 |
from kamaki.cli.utils import remove_colors |
239 | 240 |
remove_colors() |
240 | 241 |
_silent = arguments['silent'].value |
241 | 242 |
_setup_logging(_silent, _debug, _verbose, _include) |
242 |
picked_cloud = arguments['cloud'].value |
|
243 |
if picked_cloud: |
|
244 |
global_url = _cnf.get('remotes', picked_cloud) |
|
245 |
if not global_url: |
|
246 |
raise CLIError( |
|
247 |
'No remote cloud "%s" in kamaki configuration' % picked_cloud, |
|
248 |
importance=3, details=[ |
|
249 |
'To check if this remote cloud alias is declared:', |
|
250 |
' /config get remotes.%s' % picked_cloud, |
|
251 |
'To set a remote authentication URI aliased as "%s"' % ( |
|
252 |
picked_cloud), |
|
253 |
' /config set remotes.%s <URI>' % picked_cloud |
|
254 |
]) |
|
255 |
else: |
|
256 |
global_url = _cnf.get('global', 'auth_url') |
|
257 |
global_token = _cnf.get('global', 'token') |
|
243 |
|
|
244 |
if _help or is_non_API: |
|
245 |
return None |
|
246 |
|
|
247 |
cloud = arguments['cloud'].value or 'default' |
|
248 |
if not cloud in _cnf.value.keys('remote'): |
|
249 |
raise CLIError( |
|
250 |
'No cloud remote "%s" is configured' % cloud, |
|
251 |
importance=3, details=[ |
|
252 |
'To configure a new cloud remote, find and set the', |
|
253 |
'single authentication URL and token:', |
|
254 |
' kamaki config set remote.%s.url <URL>' % cloud, |
|
255 |
' kamaki config set remote.%s.token <t0k3n>' % cloud]) |
|
256 |
url = _cnf.get_remote(cloud, 'url') |
|
257 |
if not url: |
|
258 |
kloger.warning( |
|
259 |
'WARNING: No remote.%s.url, use service urls instead' % cloud) |
|
260 |
return cloud |
|
261 |
token = _cnf.get_remote(cloud, 'token') |
|
262 |
if not token: |
|
263 |
raise CLIError( |
|
264 |
'No authentication token provided for %s cloud' % cloud, |
|
265 |
importance=3, details=[ |
|
266 |
'Get and set a token for %s cloud:' % cloud, |
|
267 |
' kamaki config set remote.%s.token <t0k3n>' % cloud]) |
|
268 |
|
|
258 | 269 |
from kamaki.clients.astakos import AstakosClient as AuthCachedClient |
259 | 270 |
try: |
260 |
return AuthCachedClient(global_url, global_token)
|
|
271 |
return AuthCachedClient(url, token)
|
|
261 | 272 |
except AssertionError as ae: |
262 |
kloger.warning('WARNING: Failed to load auth_url %s [ %s ]' % (
|
|
263 |
global_url, ae))
|
|
273 |
kloger.warning( |
|
274 |
'WARNING: Failed to load auth_url %s [ %s ]' % (url, ae))
|
|
264 | 275 |
return None |
265 | 276 |
|
266 | 277 |
|
267 | 278 |
def _load_spec_module(spec, arguments, module): |
268 |
#spec_name = arguments['config'].get('cli', spec) |
|
269 | 279 |
if not spec: |
270 | 280 |
return None |
271 | 281 |
pkg = None |
... | ... | |
304 | 314 |
|
305 | 315 |
def _load_all_commands(cmd_tree, arguments): |
306 | 316 |
_cnf = arguments['config'] |
307 |
#specs = [spec for spec in _cnf.get_groups() if _cnf.get(spec, 'cli')] |
|
308 | 317 |
for cmd_group, spec in _cnf.get_cli_specs(): |
309 | 318 |
try: |
310 | 319 |
spec_module = _load_spec_module(spec, arguments, '_commands') |
... | ... | |
411 | 420 |
def run_one_cmd(exe_string, parser, auth_base): |
412 | 421 |
global _history |
413 | 422 |
_history = History( |
414 |
parser.arguments['config'].get('history', 'file'))
|
|
423 |
parser.arguments['config'].get_global('history_file'))
|
|
415 | 424 |
_history.add(' '.join([exe_string] + argv[1:])) |
416 | 425 |
from kamaki.cli import one_command |
417 | 426 |
one_command.run(auth_base, parser, _help) |
... | ... | |
424 | 433 |
shell.run(auth_base, parser) |
425 | 434 |
|
426 | 435 |
|
436 |
def is_non_API(parser): |
|
437 |
nonAPIs = ('history', 'config') |
|
438 |
for term in parser.unparsed: |
|
439 |
if not term.startswith('-'): |
|
440 |
if term in nonAPIs: |
|
441 |
return True |
|
442 |
return False |
|
443 |
return False |
|
444 |
|
|
445 |
|
|
427 | 446 |
def main(): |
428 | 447 |
try: |
429 | 448 |
exe = basename(argv[0]) |
... | ... | |
432 | 451 |
if parser.arguments['version'].value: |
433 | 452 |
exit(0) |
434 | 453 |
|
435 |
log_file = parser.arguments['config'].get('global', 'log_file')
|
|
454 |
log_file = parser.arguments['config'].get_global('log_file')
|
|
436 | 455 |
if log_file: |
437 | 456 |
logger.set_log_filename(log_file) |
438 | 457 |
global filelog |
439 | 458 |
filelog = logger.add_file_logger(__name__.split('.')[0]) |
440 | 459 |
filelog.info('* Initial Call *\n%s\n- - -' % ' '.join(argv)) |
441 | 460 |
|
442 |
auth_base = _init_session(parser.arguments)
|
|
461 |
remote_base = _init_session(parser.arguments, is_non_API(parser))
|
|
443 | 462 |
|
444 | 463 |
from kamaki.cli.utils import suggest_missing |
445 | 464 |
suggest_missing() |
446 | 465 |
|
447 | 466 |
if parser.unparsed: |
448 |
run_one_cmd(exe, parser, auth_base)
|
|
467 |
run_one_cmd(exe, parser, remote_base)
|
|
449 | 468 |
elif _help: |
450 | 469 |
parser.parser.print_help() |
451 | 470 |
_groups_help(parser.arguments) |
452 | 471 |
else: |
453 |
run_shell(exe, parser, auth_base)
|
|
472 |
run_shell(exe, parser, remote_base)
|
|
454 | 473 |
except CLIError as err: |
455 | 474 |
print_error_message(err) |
456 | 475 |
if _debug: |
Also available in: Unified diff