Ouvrir une console Rails avec Capistrano

Posted by ZedTuX 0n R00t on October 15, 2015

Voici comment écrire une “recipe” Capistrano qui vous permettra d’ouvrir une console Rails depuis votre machine locale.

Créez le fichier lib/capistrano/tasks/rails.rake et y copier/coller ceci:

namespace :rails do
desc 'Remote console'
task :console do
on roles(:app) do |h|
command = 'rails console'
run_interactively "RAILS_ENV=#{fetch(:rails_env)} bundle exec #{command}",
h.user
end
end
desc 'Remote dbconsole'
task :dbconsole do
on roles(:app) do |h|
command = 'rails dbconsole'
run_interactively "RAILS_ENV=#{fetch(:rails_env)} bundle exec #{command}",
h.user
end
end
end
def run_interactively(command, user)
info "Connecting with #{user}@#{host}:#{fetch(:deploy_to)}/current"
cmd = "ssh #{user}@#{host} -t 'cd #{fetch(:deploy_to)}/current && " \
'PATH=/home/triggr/.rbenv/shims:/home/triggr/.rbenv/bin:$PATH ' \
"#{command}'"
exec cmd
end
view raw rails.rake hosted with ❤ by GitHub

Maintenant vous pouvez faire cap staging rails:console et vous avez une console en staging :)