【发布时间】:2015-08-24 07:54:56
【问题描述】:
好的,
如果标题描述性不够,我很抱歉,但请允许我解释一下我想要实现的目标:
- 我有一个 Rails 3 应用程序
- 在我的部署过程中,它需要使用正确的参数调用
pg_dump来恢复备份 - 该任务需要在部署完成后迁移之前运行。
然而,我遇到的问题是,对于这个任务,我想访问 Rails 特定的代码,但由于 Capistrano 不断向我抛出很多错误,例如 gems 不可用或模块未定义,所以该代码不起作用。
这是我的 Rake 任务:
namespace :deploy do
namespace :rm do
desc 'Backup the database'
task :backup do
# Generates the command to invoke the Rails runner
# Used by the cfg method to execute the ActiveRecord configuration in the rails config.
def runner
dir = "#{fetch(:deploy_to)}/current"
bundler = "#{SSHKit.config.command_map.prefix[:bundle].first} bundle exec"
env = fetch(:rails_env)
"cd #{dir}; #{bundler} rails r -e #{env}"
end
def cfg(name)
env = fetch(:rails_env)
command = "\"puts ActiveRecord::Base.configurations['#{env}']['#{name}']\""
"#{runner} #{command}"
end
on roles(:db) do
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
backups = File.expand_path(File.join(fetch(:deploy_to), '..', 'backups'))
execute :mkdir, '-p', backups
dump = "PGPASSWORD=`#{cfg('password')}` pg_dump -h `#{cfg'host')}` -U `#{cfg('username')}` `#{cfg('database')}`"
fn = "#{timestamp}_#{fetch(:stage)}.sql.gz"
path = File.join(backups, fn)
execute "#{dump} | gzip > #{path}"
end
end
end
end
在它的当前形式中,它只是使用runner 方法生成一个字符串并将其转储到cfg 方法中。
我尝试重写runner 方法,但由于某种原因,我不断从远程服务器获取runner --help 输出,但输出中生成的命令是正确的,并且可以在本地正常工作。
我们在远程服务器上使用Ruby 2.2.2 和 RVM。
甚至有可能做我们正在尝试共同构建的东西吗?
【问题讨论】:
标签: ruby ruby-on-rails-3 deployment capistrano3