Example #1
0
def _run_with_agent(command, shell=True, pty=True, ssh_options='-A'):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent.~
    This helper uses your system's ssh to do so.
    """
    env = fabric.api.env
    real_command = command
    if shell:
        cwd = env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        real_command = '%s "%s"' % (env.shell,
            _shell_escape(cwd + real_command))

    if fabric.api.output.debug:
        print("[%s] run: %s" % (env.host_string, real_command))
    elif fabric.api.output.running:
        print("[%s] run: %s" % (env.host_string, command))

    return fabric.api.local("ssh {options} -p {env.port} -l {env.user} {env.host} '{command}'".format(
            env=env,
            command=real_command,
            options=ssh_options,
        )
    )
Example #2
0
def _run_with_agent(command, shell=True, pty=True, ssh_options='-A'):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent.~
    This helper uses your system's ssh to do so.
    """
    env = fabric.api.env
    real_command = command
    if shell:
        cwd = env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        real_command = '%s "%s"' % (env.shell,
                                    _shell_escape(cwd + real_command))

    if fabric.api.output.debug:
        print("[%s] run: %s" % (env.host_string, real_command))
    elif fabric.api.output.running:
        print("[%s] run: %s" % (env.host_string, command))

    return fabric.api.local(
        "ssh {options} -p {env.port} -l {env.user} {env.host} '{command}'".
        format(
            env=env,
            command=real_command,
            options=ssh_options,
        ))
Example #3
0
def docker_compose_run(command_name, service, user="******", no_deps=False):
    args = ['run ' '--rm ' '-u %s ' % _shell_escape(user)]

    if no_deps:
        args.append('--no-deps ')

    docker_compose(
        '%s %s /bin/bash -c "%s"' %
        (' '.join(args), _shell_escape(service), _shell_escape(command_name)))
Example #4
0
def test_shell_wrap_escapes_command_if_shell_is_true():
    """
    _shell_wrap() escapes given command if shell=True
    """
    cmd = "cd \"Application Support\""
    eq_(_shell_wrap(cmd, shell=True),
        '%s "%s"' % (env.shell, _shell_escape(cmd)))
Example #5
0
def test_shell_wrap_escapes_command_if_shell_is_true():
    """
    _shell_wrap() escapes given command if shell=True
    """
    cmd = "cd \"Application Support\""
    eq_(
        _shell_wrap(cmd, shell_escape=True, shell=True),
        '%s "%s"' % (env.shell, _shell_escape(cmd))
    )
Example #6
0
def sshagent_run(command, shell=True, pty=True):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent.
    This helper uses your system's ssh to do so.
    """
    real_command = command
    if shell:
        cwd = env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        real_command = '%s "%s"' % (env.shell,
            _shell_escape(cwd + real_command))
    if output.debug:
        print("[%s] run: %s" % (env.host_string, real_command))
    elif output.running:
        print("[%s] run: %s" % (env.host_string, command))
    local("ssh -A %s '%s'" % (env.host_string, real_command))
Example #7
0
def docker_compose_run(command_name, service="builder", user="******", no_deps=False, workdir=None, port_mapping=False):
    args = [
        'run ',
        '--rm ',
        '-u %s ' % _shell_escape(user),
    ]

    if no_deps:
        args.append('--no-deps ')

    if port_mapping:
        args.append('--service-ports ')

    if workdir is not None:
        args.append('-w %s ' % _shell_escape(workdir))

    docker_compose('%s %s /bin/bash -c "exec %s"' % (
        ' '.join(args),
        _shell_escape(service),
        _shell_escape(command_name)
    ))
Example #8
0
def _run(command,
         shell=True,
         pty=True,
         combine_stderr=True,
         sudo=False,
         user=None):
    real_command = command
    if shell:
        # Handle cwd munging via 'cd' context manager
        cwd = env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        # Construct final real, full command
        real_command = '%s "%s"' % (env.shell,
                                    _shell_escape(cwd + real_command))
        # Remove cwd from context
        env.cwd = ''
    if output.debug:
        print("[%s] run: %s" % (env.host_string, real_command))
    elif output.running:
        print("[%s] run: %s" % (env.host_string, command))
    local("ssh -A %s@%s '%s'" % (env.user, env.host_string, real_command))
Example #9
0
def _sshagent_run(command, shell=True, pty=True):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent.
    This helper uses your system's ssh to do so.
    """
    real_command = command
    if shell:
        cwd = api.env.get('cwd', '')
        if cwd:
            cwd = 'cd %s && ' % _shell_escape(cwd)
        real_command = '%s "%s"' % (api.env.shell,
            _shell_escape(cwd + real_command))
    if output.debug:
        print("[%s] run: %s" % (api.env.host_string, real_command))
    elif output.running:
        print("[%s] run: %s" % (api.env.host_string, command))
    with api.settings(api.hide('warnings', 'running', 'stdout', 'stderr'),
                  warn_only=True):
        return api.local(
            "ssh -A %s '%s'" % (api.env.host_string, real_command))
Example #10
0
def test_shell_escape_escapes_doublequotes():
    """
    _shell_escape() escapes double-quotes
    """
    cmd = "cd \"Application Support\""
    eq_(_shell_escape(cmd), 'cd \\"Application Support\\"')
Example #11
0
def test_shell_escape_escapes_backticks():
    """
    _shell_escape() escapes backticks
    """
    cmd = "touch test.pid && kill `cat test.pid`"
    eq_(_shell_escape(cmd), "touch test.pid && kill \`cat test.pid\`")
Example #12
0
def test_shell_escape_escapes_dollar_signs():
    """
    _shell_escape() escapes dollar signs
    """
    cmd = "cd $HOME"
    eq_(_shell_escape(cmd), 'cd \$HOME')
Example #13
0
def test_shell_escape_escapes_doublequotes():
    """
    _shell_escape() escapes double-quotes
    """
    cmd = "cd \"Application Support\""
    eq_(_shell_escape(cmd), 'cd \\"Application Support\\"')
Example #14
0
def test_shell_escape_escapes_backslashes():
    """
    _shell_escape() escapes backslashes
    """
    cmd = r'echo "\""'
    eq_(_shell_escape(cmd), r'echo \"\\\"\"')
Example #15
0
def test_shell_escape_escapes_dollar_signs():
    """
    _shell_escape() escapes dollar signs
    """
    cmd = "cd $HOME"
    eq_(_shell_escape(cmd), 'cd \$HOME')
Example #16
0
def test_shell_escape_escapes_backticks():
    """
    _shell_escape() escapes backticks
    """
    cmd = "touch test.pid && kill `cat test.pid`"
    eq_(_shell_escape(cmd), "touch test.pid && kill \`cat test.pid\`")
Example #17
0
def test_shell_escape_escapes_backslashes():
    """
    _shell_escape() escapes backslashes
    """
    cmd = r'echo "\""'
    eq_(_shell_escape(cmd), r"echo \"\\\"\"")