2011-10-14 31 views
5

我正在運行Fabric需要檢查/更新Mercurial存儲庫到機器然後執行相應的複製/配置的部署任務。每當我安裝一臺新機器(我們目前在我們的基礎設施上使用EC2),或者當我在機器上運行hg pull時,它會詢問我的ssh密鑰密碼,當我們需要初始化時,這有點煩人一次有十幾臺機器。在機器上運行ssh-add與織物

我嘗試在Fabric中運行ssh-add,當新的EC2實例初始化時,但看起來ssh-agent未運行於該shell,並且從Fabric的輸出中收到Could not open a connection to your authentication agent.消息。

如何使ssh-add在由Fabric腳本連接到實例時起作用?

回答

2

A comment關於面料的問題跟蹤器解決了這個問題。它是lincolnloop solution的修改版本。使用這個「運行」而不是結構的將會通過ssh在本地管道你的命令,允許你的本地ssh-agent提供密鑰。

from fabric.api import env, roles, local, output 
from fabric.operations import _shell_escape 

def run(command, shell=True, pty=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 = command 
    if shell: 
     cwd = env.get('cwd', '') 
     if cwd: 
      cwd = 'cd %s && ' % _shell_escape(cwd) 
     real_command = '%s "%s"' % (env.shell, 
      _shell_escape(cwd + real_command)) 
    if output.debug: 
     print("[%s] run: %s" % (env.host_string, real_command)) 
    elif output.running: 
     print("[%s] run: %s" % (env.host_string, command)) 
    local("ssh -A %s '%s'" % (env.host_string, real_command)) 

請注意,我正在運行Fabric 1.3.2,此修復將不再需要更長的時間。