好的,
如果 Headers 不够具有描述性,我很抱歉,但请允许我解释一下我想要达到的目标:
-
我有一个Rails 3应用程序
-
在部署期间,需要使用正确的参数调用
pg_dump
以恢复备份 -
任务需要在部署完成后但在迁移之前运行 .
然而,我遇到的问题是,对于这个任务,我想访问Rails特定的代码,这不起作用,因为Capistrano不断向我抛出很多错误,例如宝石不可用或模块未定义 .
这是我的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 . 甚至可以做我们想要一起构建的东西吗?
1 回答
我建议在你的Rails应用程序中编写一个rake任务,并从你的Capistrano任务中调用它 . 这是怎么回事Capistrano rails tasks work .