部署 根据《Agile_Web_Development_with_Rails_4th_for_Rails_3.2>使用 capistrano 部署出现的问题

QueXuQ · 2012年10月05日 · 最后由 QueXuQ 回复于 2012年10月09日 · 3428 次阅读

deploy.rb:

set :user, 'deploy'
set :domain, 'XX.XX.243.215'
set :application, 'saler'
# adjust if you are using RVM, remove if you are not
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user
# file paths
set :repository, "git@#{domain}:saler.git"
set :deploy_to, "/home/deploy/saler.com"
# distribute your applications across servers (the instructions below put them
# all on the same server, defined above as 'domain', adjust as necessary)
role :app, domain
role :web, domain
role :db, domain, :primary => true

# miscellaneous options
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
set :rails_env, :production

namespace :deploy do
  desc "cause Passenger to initiate a restart"
  task :restart do
    run "touch #{current_path}/tmp/restart.txt"
  end
  desc "reload the database with seed data"
  task :seed do
    run "cd #{current_path}; rake db:seed RAILS_ENV=#{rails_env}"
  end
end

after "deploy:update_code", :bundle_install
desc "install the necessary prerequisites"
task :bundle_install, :roles => :app do
  run "cd #{release_path} && bundle install"
end

这个 deploy 文件都是参考 Agile_Web_Development_with_Rails_4th_for_Rails_3.2 的,然后初始化也是按照该书进行,首先执行:

$ cap deploy:setup
$ cap deploy:check

这两行都没有出问题,后面运行cap deploy:migrations就报错了,错误如下:

$ cap deploy:migrations
  * executing `deploy:migrations'
  * executing `deploy:update_code'
    updating the cached checkout on all servers
    executing locally: "git ls-remote [email protected]:saler.git master"
    command finished in 12446ms
  * executing "if [ -d /home/deploy/saler.com/shared/cached-copy ]; then cd /home/deploy/saler.com/shared/cached-copy && git fetch  origin && git fetch --tags  origin && git reset  --hard 8e8aa8f849a9438851a3767b338ab711d6470299 && git clean  -d -x -f; else git clone [email protected]:saler.git /home/deploy/saler.com/shared/cached-copy && cd /home/deploy/saler.com/shared/cached-copy && git checkout -b deploy 8e8aa8f849a9438851a3767b338ab711d6470299; fi"
    servers: ["XX.XX.243.215"]
    [XX.XX.243.215] executing command
 ** [XX.XX.243.215 :: out] Cloning into /home/deploy/saler.com/shared/cached-copy...
 ** [XX.XX.243.215 :: err] Host key verification failed.
 ** [XX.XX.243.215 :: err] fatal: The remote end hung up unexpectedly
    command finished in 2009ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'if [ -d /home/deploy/saler.com/shared/cached-copy ]; then cd /home/deploy/saler.com/shared/cached-copy && git fetch  origin && git fetch --tags  origin && git reset  --hard 8e8aa8f849a9438851a3767b338ab711d6470299 && git clean  -d -x -f; else git clone git@XX.XX.243.215:saler.git /home/deploy/saler.com/shared/cached-copy && cd /home/deploy/saler.com/shared/cached-copy && git checkout -b deploy 8e8aa8f849a9438851a3767b338ab711d6470299; fi'" on XX.XX.243.215

其中这里说的是 [XX.XX.243.215 :: err] Host key verification failed.可是我可以使用 SSH 登陆 VPS,而且前面两条命令也顺利执行,查了很多相关资料都没有提到是什么问题,请问有人遇过同样的问题吗?

和你的 Git 有关系。好像问题处在从 git 上抓取代码这一块,跟 vps 没有关系。因为问题是出在 cloning 这句。你再好好检查下,看看能不能通过 vps 访问你的代码库

“我可以使用 SSH 登陆 VPS”,但是 VPS 不能登录代码仓库所在地服务器

解决方法:

  • 把 VPS 用户添加到 代码仓库的用户中,最好是只读的。
  • 使用 ssh agent forward。也就是在部署时,VPS 使用你的 SSH 密钥。

推荐使用后者,这样便于管理,尤其在管理多个服务器的时候。除了在 config/deploy.rb 里设置

ssh_options[:forward_agent] = true

还要确保本地机器、部署机器都开启了 agent forward 才可以。可以参考 Github 的一篇文档 https://help.github.com/articles/using-ssh-agent-forwarding

#2 楼 @zhangyuan 不对呀,我代码仓库就是在 VPS 里,只不过是 git 用户,而部署的用户是 deploy。

#3 楼 @QueXuQ 你用 deploy 账户在 VPS 上,能 clone 代码吗?

要使用 ssh_options[:forward_agent] = true,deploy 用户应该不能做 git 的 clone 的吧。cap 是做 ssh 到你的 vps 上做 git clone 的,如果不做 ssh 的 forward,是无法认证 git 的。建议配置密钥访问。

#2 楼 @zhangyuan 赞,正解。 优美的办法是用 ssh forward_agent。但是需要你做一些配置。我第一次也小花了一些时间。 不优美的办法就是你把你 服务器的 public key 也扔到你的 git server 上去...

#4 楼 @zhangyuan #5 楼 @jimrokliu

deploy@li85-215:~$ cp -r /home/git/saler.git .
deploy@li85-215:~$ ls
  saler.git
deploy@li85-215:~$ git clone git@XX.XX.243.215:saler.git
Cloning into saler...
The authenticity of host 'XX.XX.243.215 (XX.XX.243.215)' can't be established.
ECDSA key fingerprint is d0:f0:6c:8e:3a:e4:34:09:c5:2f:38:91:fd:41:81:6a.
Are you sure you want to continue connecting (yes/no)? 
Host key verification failed.
fatal: The remote end hung up unexpectedly

原来真的如你们所说的,git clone 是用不到的,第一次见过这样的问题。一直以为可以顺利操作复制之类的问题,就可以把项目复制过来,原来正常的复制和 git clone 是不一样的。

解决方法: 在本地开发的电脑上,先创建 config 文件,如果已经存在,就不需要另外创建了。

touch ~/.ssh/config

然后在 config 中,加入下面的内容 example.com 为服务器的网址,或者 IP 都可以。

Host example.com
  ForwardAgent yes

@zhangyuan @jimrokliu @poshboytl 我并未在 deploy.rb 上添加ssh_options[:forward_agent] = true 只是加了个 config 在 SSH 中,就正常运行了,请问是什么原因呢?

你做的事情是保证你可以连接到 app server 但是并不能保证你的 server 能在 git server clone/pull 代码...

这是两个问题... :)

ssh_options[:forward_agent] = true 是让你的 app server 可以用你本地的 key 来 访问 git server...

#10 楼 @poshboytl 哦。但是我按照 8 楼那写的那个做法,就可以进行 git clone 了,是不是说需要在 deploy 处加上 ssh_options[:forward_agent] = true,更确保可以 clone 和 pull 呢?

#11 楼 @QueXuQ 据我所知,你没设 ssh_options[:forward_agent] = true 理论上不行的。你在 ssh config 里设 ForwardAgent yes 只是在配置 ssh 允许其 做 Forward Agent。 但是至于为什么你居然可以,是不是你可以研究一下,然后 update 一下 :)

#9 楼 @QueXuQ 据我对 cap 的了解,好像 cap 是使用 ruby 的 Net::SSH 来连接服务器的。而并非使用 ssh 的客户端连接的。至于你说可以,我也不能解释,也许现在的 Net::SSH 会使用系统的设置?

#12 楼 @poshboytl #13 楼 @jimrokliu OK.原来这是新发现,我去查查原因。有结果了,把结果发上来。~^_^

#14 楼 @QueXuQ 恩,第一个要检查的是....你在 app server 上直接去 clone 看是否可以成功 :p

#15 楼 @poshboytl 恩。直接在 server 上可以直接 git clone 的~

#16 楼 @QueXuQ 那一切都可以解释了啊,他本来就可以 clone,说明你 app server 的 public key 已经放在 git server 了。

“优美的办法是用 ssh forward_agent。但是需要你做一些配置。我第一次也小花了一些时间。 不优美的办法就是你把你 服务器的 public key 也扔到你的 git server 上去...”

你就是我说的第二种情况哈 :)

#17 楼 @poshboytl 不对呀。一开始是不可以使用的,详见 7 楼,在我配上 8 楼的方法的时候,是不可以的。后面我也没有把 public key 加到 git 账户中,等我看看是什么情况。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号