• Fabric and sshagent (aka. Running GIT in Fabric)


    Fabric and ssh-agent (aka. Running GIT in Fabric)

    Fabric and ssh-agent (aka. Running GIT in Fabric)

    When using Fabric for deployment there is one limitation that used to cause me no end of grief; it, thanks to Paramiko, does not allow ssh-agent usage over the connection so doing git stuff tends to fail… miserably.

    There are a few examples around. However I found they didn’t work with the Context stuff that Fabric provides (namely the cd decorator) so… I came up with my own.

    def _shell_escape(string):
        """
        Escape double quotes, backticks and dollar signs in given ``string``.
    
        For example::
    
            >>> _shell_escape('abc

    ) 'abc\\\\ >>> _shell_escape('"') '\\\\"' """ for char in ('"', ' , '`'): string = string.replace(char, '\%s' % char) return string def sshagent_run(cmd, shell=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 = cmd # We have to grab the env['cwd'] and then munge it blank # otherwise the call to local() will not run as it'll try and # do a local cd - that is not desired. cwd = env.get('cwd', '') env['cwd'] = '' if shell: # Handle cwd munging via 'cd' context manager cwd_cmd = '' if cwd: cwd_cmd = 'cd %s && ' % _shell_escape(cwd) # Construct final real, full command real_command = '%s \\"%s\\"' % (env.shell, _shell_escape(cwd_cmd + real_command)) print("[%s] sshagent_run: %s" % (env.host_string, cmd)) try: print local('ssh -p %s -A %s@%s "%s"' % (env.port, env.user, env.host, real_command), capture=False) except ValueError, v: print v finally: # Put the cwd back if needed env['cwd'] = cwd

    Ok, so what’s going on here? First off, local() is context sensitive. We have to pull out the working directory context and stash it locally and ensure we put it back afterwards. This is why the finally is used. I essence this will run the command in the same way as run() would and you can specify that you do/don’t want the shell wrapped around it as required. The context is lost if you don’t use the shell – something that the normal run() command seems to do as well.

    I’ve made this more of a drop in replacement – which allows me to do things like:

    def update_myproject():
        with cd('/home/user/venv/djangostuff'):
            sshagent_run('/var/lib/gems/1.8/bin/git-up')
    

    Oh and git-up is really your friend… Really really. Hope this is useful to someone.

  • 相关阅读:
    起床困难综合症[NOI2014]
    暗之链锁
    平凡的测试数据
    烤鸡翅
    高一寒假集训总结
    聪聪和可可[NOI2005]
    方伯伯的玉米田[SCOI2014]
    aix5下安装python和cx_Oracle
    同样的一句SQL语句在pl/sql 代码块中count 没有数据,但是直接用SQl 执行却可以count 得到结果
    关于SQL优化的一个小试例子
  • 原文地址:https://www.cnblogs.com/lexus/p/2362802.html
Copyright © 2020-2023  润新知