2012-11-22 50 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​​版本。