Fabric and ssh-agent (aka. Running GIT in Fabric)
2012-02-22 11:14
429 查看
Fabric and ssh-agent (aka. Running GIT in Fabric)
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.
) '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:
Fabric and ssh-agent (aka. Running GIT in Fabric)
By Lee on November 9, 2010 in Programming, PythonWhen 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
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.
About Lee
View all posts by Lee →相关文章推荐
- ssh出错 sign_and_send_pubkey: signing failed: agent refused operation
- Problem of testing SSH in git behind proxy
- git中出现sign_and_send_pubkey: signing failed: agent refused operation【已解决】
- Hadoop问题:There are 0 datanode(s) running and no node(s) are excluded in this operation.
- git push ‘No refs in common and none specified’doing nothing
- git中出现sign_and_send_pubkey: signing failed: agent refused operation【已解决】
- Problem of testing SSH in git behind proxy
- If no other git process is currently running, this probably means a git process crashed in this rep
- Scenario 8 -HP Virtual Connect FlexFabricNAG in a Shared Uplink Set-Ethernet and FCoE SAN
- SSH Server And “Permission denied (publickey) git
- the android virtual Device AVD2.3.3 is currently running in an emulator and cannot be deleted.
- npm ERR! Please check if you have git installed and in your PATH.
- git ssh-add 报错 ssh-add Could not open a connection to your authentication agent
- Disable or Enable SSH Root Login and Limit SSH Access in Linux
- Problem of testing SSH in git behind proxy
- 使用Git 出现 Another git process seems to be running in this repository...
- 自动化运维工具Fabric - 密码管理(env.password and ssh key)
- Git: untrack a file in local repo only and keep it in the remote repo
- Problems when building and running Nutch in eclipse
- Git 使用中显示“Another git process seems to be running in this repository...”的提示问题解决办法