def get_file_from_manager(remote_source_path, destination_path): key_filename = os.path.expanduser(utils.get_management_key()) with fab.settings( fab.hide('running', 'stdout'), host_string=utils.build_manager_host_string(), key_filename=key_filename): fab.get(remote_source_path, destination_path)
def put_file_in_manager(source_path, remote_source_path, use_sudo=True, key_filename=None, user=None): if not key_filename: key_filename = os.path.expanduser(utils.get_management_key()) with fab.settings( fab.hide('running', 'stdout'), host_string=utils.build_manager_host_string(user=user), key_filename=key_filename): fab.put(use_sudo=use_sudo, local_path=source_path, remote_path=remote_source_path)
def execute(): key_filename = os.path.expanduser(utils.get_management_key()) with fab.settings( host_string=utils.build_manager_host_string(), key_filename=key_filename, warn_only=True): if use_sudo: result = fab.sudo(command) else: result = fab.run(command) if result.failed: raise CloudifyCliError( 'Failed to execute: {0} ({1})'.format( result.read_command, result.stderr)) return result
def run_command_on_manager(command, use_sudo=False, open_shell=False, host_string='', force_output=False): """Runs an SSH command on a Manager. `open_shell` opens an interactive shell to the server. `host_string` can be explicitly provided to save on REST calls. `force_output` forces all output as if running in verbose. """ host_string = host_string or utils.build_manager_host_string() port = utils.get_management_port() def execute(): key_filename = os.path.expanduser(utils.get_management_key()) with fab.settings( host_string=host_string, key_filename=key_filename, port=port, warn_only=True): if use_sudo: output = fab.sudo(command) elif open_shell: fab.open_shell(command) return None else: output = fab.run(command) if output.failed: raise CloudifyCliError( 'Failed to execute: {0} ({1})'.format( output.real_command, output.stderr)) return output if get_global_verbosity() or force_output: return execute() else: with fab.hide('running', 'stdout', 'stderr', 'warnings'): return execute()
def ssh(ssh_command, host_session, sid, list_sessions): """Connects to a running manager via SSH. `host_session` starts a tmux session (e.g. tmux new -s "ssh_session_vi120m") after which a command for a client is printed in the tmux session for the host to send to the client (i.e. cfy ssh --sid ssh_session_vi120m). When starting a new session, the host creates an alias for "exit" so that when a client connects and exits, it will run "tmux detach" instead and not kill the session. When the host exits the tmux session, a command will be executed to kill the session. Passing an `ssh_command` will simply execute it on the manager while omitting a command will connect to an interactive shell. """ _validate_env(ssh_command, host_session, sid, list_sessions) host_string = utils.build_manager_host_string() if host_session or sid or list_sessions: _verify_tmux_exists_on_manager(host_string) logger = get_logger() logger.info('Connecting to {0}...'.format(host_string)) if host_session: sid = 'ssh_session_' + utils.generate_random_string() logger.info('Creating session {0}...'.format(sid)) try: run_command_on_manager('tmux new -d -A -s {0}'.format(sid), host_string=host_string) logger.info('Preparing environment...') _send_keys(logger, 'alias exit="tmux detach"; clear', sid, host_string=host_string) _send_keys(logger, '#Clients should run cfy ssh --sid {0} ' 'to join the session.'.format(sid), sid, host_string=host_string) _join_session(logger, sid, host_string) except Exception as ex: logger.error('Failed to create session ({0})'.format(ex)) logger.info('Killing session {0}...'.format(sid)) try: run_command_on_manager( 'tmux kill-session -t {0}'.format(sid), host_string=host_string) except Exception as ex: logger.warn('Failed to kill session ({0})'.format(ex)) elif sid: _join_session(logger, sid, host_string) elif list_sessions: sessions = _get_all_sessions(logger, host_string) if sessions: logger.info('Available Sessions are:\n{0}'.format(sessions.stdout)) else: logger.info('No sessions are available') else: if ssh_command: logger.info('Executing command {0}...'.format(ssh_command)) run_command_on_manager( ssh_command, host_string=host_string, force_output=True) else: _open_interactive_shell(host_string=host_string)