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.

  • 相关阅读:
    centos 无线网卡安装,网卡rtl8188ee
    centos mysqldb 安装
    centos 安装gcc时,出错:Found 10 pre-existing rpmdb problem(s), 'yum check' output follows:
    peewee 字段属性help_text的支持问题
    mysql查看字段注释(帮助信息)指令
    centos mysql 大量数据导入时1153 错误:1153
    php在centos下的脚本没有解析的问题
    [转]mysql分布式方案-分库拆表
    [转]Mysql海量数据存储和解决方案之一—分布式DB方案
    mysql 数据库字符集的指定
  • 原文地址:https://www.cnblogs.com/lexus/p/2362802.html
Copyright © 2011-2022 走看看