无法使用Capistrano

时间:2018-04-24 12:13:13

标签: ruby-on-rails centos capistrano rails-migrations capistrano3

我正在尝试使用 Capistrano 配方将 Ruby on Rails 应用程序部署到 CentOS Linux release 7.4.1708 (Core)

CentOS服务器的环境变量为VERSION,不允许Capistrano运行rake db:migrate任务。

deploy.rb中,我创建了一个挂钩unset VERSION,如下所示,但这不起作用。

namespace :deploy do
  before :migrating, :get_version_info 
  task :get_version_info do
    on roles(:app) do
      set :default_env, { VERSION: nil }
      execute("echo $VERSION")
      execute("unset VERSION")
      execute("echo $VERSION")
    end
  end
end

我收到以下错误:

01:13 deploy:migrate
  [deploy:migrate] Run `rake db:migrate`
01:13 deploy:get_version_info
  01 echo $VERSION
  01 7
✔ 01 ssh_user@remote_server_address 0.966s
  02 unset VERSION
✔ 02 ssh_user@remote_server_address 0.960s
  01 7
✔ 01 ssh_user@remote_server_address 0.967s
01:17 deploy:migrating
  01 bundle exec rake db:migrate
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/net-ldap-0.5.1/lib/net/ber/core_ext.rb:38: warning: constant ::Bignum is deprecated
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/net-ldap-0.5.1/lib/net/ber/core_ext.rb:45: warning: constant ::Fixnum is deprecated
  01 The PGconn, PGresult, and PGError constants are deprecated, and will be
  01 removed as of version 1.0.
  01
  01 You should use PG::Connection, PG::Result, and PG::Error instead, respectively.
  01
  01 Called from /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
  01 rake aborted!
  01 ActiveRecord::UnknownMigrationVersionError:
  01
  01 No migration with version number 7
  01
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/migration.rb:952:in `migrate'
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/migration.rb:830:in `down'
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/migration.rb:805:in `migrate'
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/activerecord-4.2.8/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
  01 /opt/apps/my_app/shared/bundle/ruby/2.4.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
  01 /usr/bin/bundle:22:in `load'
  01 /usr/bin/bundle:22:in `<main>'
  01 Tasks: TOP => db:migrate
  01 (See full trace by running task with --trace)
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as 
ssh_user@remote_server_address: Exception while executing as 
ssh_user@remote_server_address: rake exit status: 1

任何帮助或解决方法都将受到高度赞赏。

  

工具信息

     

Capistrano版本:3.10.2(耙子版本:12.3.1)

     

ruby​​ 2.4.1p111(2017-03-22修订版58053)

     

Rails 4.2.8

1 个答案:

答案 0 :(得分:0)

这些是我克服这个问题的发现和解决方案:

<强> 调查结果:

  1. CentOS 默认情况下没有VERSION环境变量。

  2. 在我的情况下,VERSION env变量由私有云基础架构设置,文件位于/etc/profile.d/路径。

  3. 来自Capistrano guide

      

    默认情况下,Capistrano始终指定一个非登录的非交互式shell。

  4. <强> 解决方案:

    1. 在我的情况下,我确信如果我unset VERSION那么它不会影响我的环境。所以,我在unset VERSION添加了~/.bashrc。它对我有用!

      注意:如果您在~/bash_profile中取消设置VERSION,那么由于Capistrano默认情况下非交互式非登录性质,它无法正常工作。

    2. 另一种解决方法可能是使用Capistrano command_map 执行bundle exec rake db:migrate任务。