コード例 #1
0
ファイル: install.py プロジェクト: funkyHat/cloudify-cli
def _generate_suffixed_id(id):
    return '{0}_{1}'.format(id, utils.generate_random_string())
コード例 #2
0
ファイル: ssh.py プロジェクト: wksw/cloudify-cli
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)