zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    最小二乘拟合(转)good
    会议论文重新投稿算不算侵权?这肯定是所多人都遇到过的问题(转)
    吝啬的国度
    压力单位MPa、Psi和bar之间换算公式
    Oracle建立表空间和用户
    layoutSubviews总结
    C++中出现的计算机术语4
    445port入侵具体解释
    hdu
    ORM框架
  • 原文地址:https://www.cnblogs.com/lexus/p/2362802.html
Copyright © 2011-2022 走看看