【问题标题】:SSH agent forwarding with Capistrano 3 not working when deploying Rails app部署 Rails 应用程序时,使用 Capistrano 3 的 SSH 代理转发不起作用
【发布时间】:2016-04-27 20:27:57
【问题描述】:

我的 deploy.rb 中有以下设置

set :application, 'sample_app'
set :repo_url, 'user@123.45.67.100:/home/user/railsapps/sample_app'
set :deploy_to, '/var/www/sample_app'
set :user, "user"
set :ssh_options, { :forward_agent => true }

还有我的 deploy/production.rb 文件:

set :stage, :production
server '123.45.67.200', user: 'user', roles: %w{app db web}

运行 cap production deploy 时出现以下错误:检查

DEBUG [] ssh: connect to host 123.45.67.100 port 22: Connection timed out
DEBUG [] fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user@123.45.67.200: git exit status: 128
git stdout: Nothing written
git stderr: ssh: connect to host 123.45.67.200 port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

在其中一行中,我看到它尝试以 user@123.45.67.200 身份访问存储库,这是生产服务器的部署用户:

INFO [] Running /usr/bin/env git ls-remote --heads user@123.45.67.100:/home/user/railsapps/sample_app as user@123.45.67.200

不应该说它以本地用户的身份使用本地 ssh 密钥连接吗? Capistrano 是否登录到生产服务器,然后从存储库中提取代码?如果是,有没有办法让它将代码从存储库推送到生产服务器?

【问题讨论】:

  • Capistrano 通过拉取更新的代码来工作。它将登录到生产服务器,然后从那里执行 git pull。如果您要转发密钥,则您的本地密钥将可用,但请检查您是否可以以部署用户身份克隆存储库

标签: ruby-on-rails ruby-on-rails-4 ssh capistrano capistrano3


【解决方案1】:

您的 Git URL 似乎无效。您可以通过连接到远程系统 (user@123.45.67.200) 来测试这一点,并尝试使用简单的 git ls-remote --heads 访问远程 Git 存储库,这将证明连接性。

git ls-remote --heads user@123.45.67.100:/home/user/railsapps/sample_app

我怀疑您可能需要将 .git 附加到您的 URL (user@123.45.67.100:/home/user/railsapps/sample_app.git) 但这实际上取决于您如何设置远程存储库。

Git 确实使用 SSH 进行连接,但它没有在 Capistrano 输出中明确显示。您将看到的只是显式的git 命令。

或者,如果您希望使用代理转发,那么您的 ssh 转发配置可能会遇到问题,无论是本地还是远程。您可以通过检查本地计算机然后连接到远程计算机并查看您的身份是否被转发来测试它。你可以这样做:

local-host$ ssh-add -l
local-host$ ssh user@remote-host
remote-host$ ssh-add -l

如果您看到如下输出:

Error connecting to agent: No such file or directory

或:

Could not open a connection to your authentication agent.

或:

The agent has no identities.

那么你需要在 Capistrano 正常工作之前解决这个问题。

您可以查看这篇文章“Using ssh-agent with ssh”以帮助进行 SSH 配置。

【讨论】:

  • 代理转发可以在 capistrano 的配置中启用,但不能在 ssh 配置中启用。然后运行“ssh-add -l”会产生一个错误,即使它在实际部署期间工作。
  • 所以通过ssh连接的时候加上-A选项。另一种方法是创建一个自定义任务,在“开始”之前运行 ssh-add -l。
【解决方案2】:

Capistrano 将登录到服务器,然后从服务器上从您的 VCS 中下载代码。

通常有两种验证方式:

  1. ssh-agent 转发,这将使远程会话能够访问您的开发者密钥,或者
  2. 部署密钥,这将使服务器用户的密钥能够访问您的代码。

本文档页面的后半部分描述了 Git 与 Capistrano 的工作方式:http://capistranorb.com/documentation/getting-started/cold-start/

根据您发布的错误,您可能需要设置上述选项之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    相关资源
    最近更新 更多