2012-11-22 46 views
0

我只是跑耙分贝:没有问题,直接在我的服务器迁移:轨Capistrano的有问题与耙分贝:迁移

$ pwd 
/var/www/vhosts/example.com/current 
$ rake db:migrate 

然后我试图通过Capistrano的任务来运行它,我得到一个错误。任务和输出如下。为什么rake db:migrate无法通过我的capistrano任务工作?

任务

namespace :deploy do 
    # run the db migrations 
    task :run_migrations, :roles => :db do 
    puts "RUNNING DB MIGRATIONS" 
    run "cd #{current_path}; rake db:migrate RAILS_ENV=#{rails_env}" 
    end 
end 

帽部署:run_migrations

$ cap deploy:run_migrations 
    * executing `deploy:run_migrations' 
RUNNING DB MIGRATIONS 
    * executing "cd /var/www/vhosts/example.com/current; rake db:migrate" 
    servers: ["example.com"] 
    [example.com] executing command 
** [out :: example.com] (in /var/www/vhosts/example.com/releases/20121122011144) 
** [out :: example.com] rake aborted! 
** [out :: example.com] no such file to load -- rubygems 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121122011144/Rakefile:5:in `require' 
** [out :: example.com] (See full trace by running task with --trace) 
    command finished in 390ms 
failed: "sh -c 'cd /var/www/vhosts/example.com/current; rake db:migrate'" on example.com 

编辑

$ ruby -v 
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux] 
$ which ruby 
/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby 
$ which rails 
/usr/local/rvm/gems/ruby-1.9.3-p125/bin/rails 

编辑 当我加入--trace到数据库migrati它看起来像它使用/usr/lib/ruby/site_ruby/1.8/rake.rb,但从这看起来: $其中耙 /usr/local/rvm/gems/ruby-1.9.3-p125/bin/rake 我会期望它使用耙在/usr/local/rvm/gems/ruby-1.9.3-p125/bin/rake而不是/usr/lib/ruby/site_ruby/1.8/rake.rb。从部署

帽部署的输出:run_migrations任务

* executing `deploy:run_migrations' 
RUNNING DB MIGRATIONS 
    * executing "cd /var/www/vhosts/example.com/current; rake db:migrate RAILS_ENV=production --trace" 
    servers: ["example.com"] 
    [example.com] executing command 
** [out :: example.com] (in /var/www/vhosts/example.com/releases/20121123184358) 
** [out :: example.com] rake aborted! 
** [out :: example.com] no such file to load -- rubygems 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/boot.rb:1:in `require' 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/boot.rb:1 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/application.rb:1:in `require' 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/config/application.rb:1 
** [out :: example.com] /var/www/vhosts/example.com/releases/20121123184358/Rakefile:5:in `require' 
** [out :: example.com] /var/www/vhosts/famnfo.com/releases/20121123184358/Rakefile:5 
** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1828:in `load' 
** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1828:in `load_rakefile' 
** [out :: example.com] /usr/lib/ruby/site_ruby/1.8/rake.rb:1900:in `run' 
** [out :: example.com] /usr/bin/rake:8 
    command finished in 422ms 
failed: "sh -c 'cd /var/www/vhosts/example.com/current; rake db:migrate RAILS_ENV=production --trace'" on example.com 

编辑添加@Super工程师代码

* executing `bundle:install' 
    * executing "ls -x /var/www/vhosts/example.com/releases" 
    servers: ["example.com"] 
    [example.com] executing command 
    command finished in 572ms 
    * executing "cd /var/www/vhosts/example.com/releases/20121124160218 && bundle install --gemfile /var/www/vhosts/example.com/releases/20121124160218/Gemfile --path /var/www/vhosts/example.com/shared/bundle --deployment --quiet --without development test" 
    servers: ["example.com"] 
    [example.com] executing command 
** [out :: example.com] You are trying to install in deployment mode after changing 
** [out :: example.com] your Gemfile. Run `bundle install` elsewhere and add the 
** [out :: example.com] updated Gemfile.lock to version control. 
** [out :: example.com] 
** [out :: example.com] You have added to the Gemfile: 
** [out :: example.com] * mysql2 
** [out :: example.com] * therubyracer 
** [out :: example.com] * rvm-capistrano 
** [out :: example.com] * passenger 
** [out :: example.com] 
** [out :: example.com] You have deleted from the Gemfile: 
** [out :: example.com] * mysql 
    command finished in 999ms 
*** [deploy:update_code] rolling back 
    * executing "rm -rf /var/www/vhosts/example.com/releases/20121124160218; true" 
    servers: ["example.com"] 
    [example.com] executing command 
    command finished in 826ms 
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell 'ruby-1.9.3-p125' -c 'cd /var/www/vhosts/example.com/releases/20121124160218 && bundle install --gemfile /var/www/vhosts/example.com/releases/20121124160218/Gemfile --path /var/www/vhosts/example.com/shared/bundle --deployment --quiet --without development test'" on example.com 

编辑 整个deploy.rb文件

后 输出
require 'bundler/capistrano' 
require 'rvm/capistrano' 

# set environment 
set :rails_env, "production" 

# set the ruby version 
set :rvm_ruby_string, 'ruby-1.9.3-p125' 
set :rvm_type, 'webadmin' 

# server username and password 
set :user, 'super' 
set :password, 'secret' 

# subversion repo username and password 
set :scm, :subversion 
set :scm_username, "super" 
set :scm_password, 'secret' 
set :svnserver, "myreposerver" 
set :repository, "myrepo" 

# project info 
set :server, '' 
set :application, "FamNFo" 
set :applicationdir, '/Volumes/Macintosh HD/Users/myname/Sites/example' 

role :web, "example.com" 
role :app, "example.com" 
role :db, "example.com", :primary => true 
set :use_sudo, true 

# database config 
#set :migrate_env, "#{rails_env}" 

# specify the rvm type. We just want to use the system wide one since we're not currently specifying gemsets for each project 
set :rvm_type, :system 

# where to put the files 
set :deploy_to, "/var/www/vhosts/example.com" 

# fixes the "sorry, you must have a tty to run sudo" issue 
default_run_options[:pty] = true 

# precompiles the assets 
load 'deploy/assets' 

# If you are using Passenger mod_rails uncomment this: 
namespace :deploy do 

    # restart the server 
    task :restart, :roles => :app do 
     run "/etc/init.d/http restart graceful" 
    end 

    # chmod the files 
    task :after_update_code, :roles => [:web, :db, :app] do 
     run "chmod 755 #{release_path}/public -R" 
    end 

    # install new gems 
    desc "run bundle install and ensure all gem requirements are met" 
    task :install do 
     run "cd #{current_path} && bundle install --without=test --no-update-sources --trace" 
    end 

    # run the db migrations 
    task :run_migrations, :roles => :db do 
     puts "RUNNING DB MIGRATIONS" 
     run "cd #{current_path}; rake db:migrate RAILS_ENV=#{rails_env} --trace" 
    end 

    # precompile assets 
    task :precompile_assets do 
     run "cd #{release_path}; bundle exec rake assets:precompile RAILS_ENV=#{rails_env}" 
    end 

    task :start do ; end 
    task :stop do ; end 

    # restart the server 
    task :restart, :roles => :app, :except => { :no_release => true } do 
     run "#{sudo} /etc/init.d/httpd restart graceful" 
    end 
end 

# hook to run db migrations after code update 
after("deploy:update", "deploy:run_migrations") 
#after("deploy:update", "deploy:precompile_assets") 
after "deploy:update", "deploy:install" 
# if you want to clean up old releases on each deploy uncomment this: 
after "deploy:restart", "deploy:cleanup" # leave the last 5 releases only 

回答

4

它是因为红宝石冲突。我遇到了同样的错误。您将需要使用rvm/capistrano gem。我会粘贴我的cap文件中的一些代码,这将给你一些关于如何在部署时设置ruby版本的想法。

安装宝石

gem install rvm-capistrano 

在你部署。RB文件中添加这些行

require 'bundler/capistrano' 
require 'rvm/capistrano' 

set :rvm_ruby_string, "ruby-1.9.3-p125" 
set :rvm_type, 'webadmin' 

欲了解更多信息,您可以访问此链接rvm/capistrano

+0

好吧,我添加了这些,现在我得到了一个不同的错误。我把它发布到我的问题的结尾。 – Catfish

+0

此问题的主要原因可能是您手动更改了gemfile.lock。我建议你在本地机器上运行bundle install(如果你使用的是git,推送更改),然后尝试部署。此外,它会帮助,如果你可以发布你deploy.rb文件 –

+0

我张贴我的deploy.rb文件,我认为问题是我没有提交我的gemfile.lock。我没有意识到它需要承诺。谢谢你的帮助。 – Catfish

1

这里的相关错误似乎是no such file to load -- rubygems

很可能是因为生产服务器上安装了多个ruby。

检查出类似的问题,这之前的解决方案:https://stackoverflow.com/a/2896596/429758

而且,这个线程Capistrano的邮件列表上是有帮助的:https://groups.google.com/forum/?fromgroups=#!topic/capistrano/JzVPRbQclY4

+0

我想我只有1个版本的红宝石我的机器上。我用细节更新了我的问题。而且,当我手动执行rake db:migrate on my server时,它仍然显得很奇怪。 – Catfish

+0

运行'which -a ruby​​'来查看有多少个红宝石安装在那里。正如matov在他的回答中提到的那样,当你手工运行它时,以及当capistrano运行它时,很可能会使用不同的红宝石。 –

+0

其中-a红宝石返回这个:'$ which -a ruby​​ /usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby /usr/local/rvm/bin/ruby​​ /usr /斌/ ruby​​'。我试过你发布的链接,我试着说设置RUBYLIB的解决方案,而不是删除红宝石安装,并看到我的第二个编辑我的问题。 – Catfish

0

是否使用rvm/capistrano?通过这个,你可以在你的capistrano食谱中指定你想在服务器上使用的ruby版本。我怀疑当你运行capistrano它不使用你的rvm设置,当你登录并运行rake任务时,你的用户被设置为使用rvm ruby​​版本。