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.

  • 相关阅读:
    PHP:_SERVER变量和_ENV变量列表
    一些使用jQuery開發的在線Web富文本編輯器
    xhtml+css排版,清理浮动整理汇总
    PHP 6 的新特性
    用PHP的CURL写的一个采集Discuz的例子
    SA、SD、SE 这三者的含义及区分
    PHP 之父 Rasmus Lerdorf 谈PHP开发
    http头的组成
    Jquery插件 可以随着滚动条的滚动而即时加载图片
    在你的网页中嵌入PDF等文件,实现pdf在线阅读
  • 原文地址:https://www.cnblogs.com/lexus/p/2362802.html
Copyright © 2011-2022 走看看