簡體   English   中英

Rails Capistrano耙db:migrate有問題

[英]rails capistrano having problems with rake db:migrate

我只是直接在服務器上運行rake db:migrate而沒有任何問題:

$ 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時,它看起來像是在使用/usr/lib/ruby/site_ruby/1.8/rake.rb ,但是從這樣的外觀上看:$ which rake / 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使用rake /usr/lib/ruby/site_ruby/1.8/rake.rb

限制deploy:run_migrations任務中deploy的輸出

* 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 Engineers代碼后編輯輸出

* 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

這是因為紅寶石沖突。 我遇到了同樣的錯誤。 您將需要使用rvm/capistrano gem 我將從我的Cap文件中粘貼一些代碼,這將使您對如何在部署時設置ruby版本有所了解。

安裝寶石

gem install rvm-capistrano

在您的deploy.rb文件中添加以下行

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

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

有關更多信息,您可以訪問此鏈接rvm / capistrano

這里的相關錯誤似乎是no such file to load -- rubygems

很可能是由於生產服務器上安裝了多個ruby。

看看以前的解決方案是否有類似問題: https : //stackoverflow.com/a/2896596/429758

另外,capistrano郵件列表上的該線程也很有幫助: https ://groups.google.com/forum/?fromgroups =#!topic/ capistrano/JzVPRbQclY4

您在使用rvm / capistrano嗎? 這樣,您就可以在Capistrano配方中指定要在服務器上使用的紅寶石版本。 我懷疑您在運行capistrano時未使用rvm安裝程序,而在登錄並運行rake任務時,將您的用戶設置為使用rvm ruby​​版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM