【问题标题】:Unable to reset/unset VERSION environment variable using Capistrano无法使用 Capistrano 重置/取消设置 VERSION 环境变量
【发布时间】:2018-04-24 12:13:13
【问题描述】:

我正在尝试使用 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(Rake 版本:12.3.1)

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

Rails 4.2.8

【问题讨论】:

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


    【解决方案1】:

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

    调查结果:

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

    2. 在我的例子中,VERSION env 变量是由私有云基础设施设置的,其中一个文件位于 /etc/profile.d/ 路径中。

    3. 来自 Capistrano guide:

      默认情况下,Capistrano 始终分配一个非登录、非交互式 shell。

    解决方案:

    1. 就我而言,我确信如果我 unset VERSION 不会影响我的环境。所以,我将unset VERSION 添加到我的~/.bashrc。它对我有用!

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2011-12-12
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      相关资源
      最近更新 更多