def cmd_env(*args): """ :env format [FORMAT] Show or set the format of environmental variable declarations. Use '%(key)s' and '%(value)s' to specify the substitutions in your string. :env list List all environmental variables that are being set on each command. :env set <VAR>=<VAL> Before executing each command on a remote host set the environmental variable <VAR> to the value given by <VAL>. :env unset <VAR> Before executing each command on a remote host set the environmental variable <VAR> to the value given by <VAL>. """ envvars = config.get('envvars', {}) if len(args) == 1 and args[0] == 'list': print terminal.format_columns( ('Variable', 'Value'), sorted(envvars.items()) ) elif len(args) == 1 and args[0] == 'format': print config.get('envvar_format', '') elif len(args) > 1 and args[0] == 'format': config.config['envvar_format'] = ' '.join(args[1:]) elif len(args) > 1 and args[0] == 'set': k, _, v = ' '.join(args[1:]).partition('=') envvars[k] = v elif len(args) == 2 and args[0] == 'unset': k = args[1] if k in envvars: del envvars[k] else: return os.EX_USAGE config.config['envvars'] = envvars return os.EX_OK
def cmd_env(*args): """ :env format [FORMAT] Show or set the format of environmental variable declarations. Use '%(key)s' and '%(value)s' to specify the substitutions in your string. :env list List all environmental variables that are being set on each command. :env set <VAR>=<VAL> Before executing each command on a remote host set the environmental variable <VAR> to the value given by <VAL>. :env unset <VAR> Before executing each command on a remote host set the environmental variable <VAR> to the value given by <VAL>. """ envvars = config.get('envvars', {}) if len(args) == 1 and args[0] == 'list': print terminal.format_columns(('Variable', 'Value'), sorted(envvars.items())) elif len(args) == 1 and args[0] == 'format': print config.get('envvar_format', '') elif len(args) > 1 and args[0] == 'format': config.config['envvar_format'] = ' '.join(args[1:]) elif len(args) > 1 and args[0] == 'set': k, _, v = ' '.join(args[1:]).partition('=') envvars[k] = v elif len(args) == 2 and args[0] == 'unset': k = args[1] if k in envvars: del envvars[k] else: return os.EX_USAGE config.config['envvars'] = envvars return os.EX_OK
def cmd_history(*args): """ :history <host> Show the command history for <host>. """ if len(args) == 1: history = connections.conns.get_history(args[0]) if not history: terminal.error(text="No history available for %s" % args[0]) else: headers = ('\n#', 'Command', 'Exit code') output = [] for i, h in enumerate(history): output.append(( '%s' % i, h.get('command'), h.get('exitcode'), )) print terminal.format_columns(headers, output), '\n' else: return os.EX_USAGE return os.EX_OK
def cmd_history(*args): """ :history <host> Show the command history for <host>. """ if len(args) == 1: history = connections.conns.get_history(args[0]) if not history: terminal.error(text = "No history available for %s" % args[0]) else: headers = ('\n#', 'Command', 'Exit code') output = [] for i, h in enumerate(history): output.append(( '%s' % i, h.get('command'), h.get('exitcode'), )) print terminal.format_columns(headers, output), '\n' else: return os.EX_USAGE return os.EX_OK
def cmd_list(*args): """ :list List all hosts in the current hosts list. """ output = [] for idx, item in enumerate(connections.conns.items()): host, hostinfo = item output.append(('[%s]' % idx, host, hostinfo.get('connected'), hostinfo.get('exitcode'))) if not len(output): terminal.error(text='No hosts.') return os.EX_OK headers = ('', 'Hostname', 'Connected', 'Last exit') print '\n', terminal.format_columns(headers, output), '\n' return os.EX_OK
def cmd_list(*args): """ :list List all hosts in the current hosts list. """ output = [] for idx, item in enumerate(connections.conns.items()): host, hostinfo = item output.append(( '[%s]' % idx, host, hostinfo.get('connected'), hostinfo.get('exitcode') )) if not len(output): terminal.error(text = 'No hosts.') return os.EX_OK headers = ('', 'Hostname', 'Connected', 'Last exit') print '\n', terminal.format_columns(headers, output), '\n' return os.EX_OK
def cmd_status(*args): """ :status Show the status of all jobs from the last command. """ if not len(args): output = [] for idx, item in enumerate(connections.conns.status()): host, exit_status = item if exit_status is None: exit_status = '(still pending)' elif exit_status is -1: exit_status = '-1 (stopped)' output.append(( '[%s]' % idx, host, exit_status, )) headers = ('', 'Host', 'Exit code') print '\n', terminal.format_columns(headers, output), '\n' else: return os.EX_USAGE return os.EX_OK