Skip to content

Instantly share code, notes, and snippets.

@existentialmutt
Forked from joost/deploy.rb
Last active December 6, 2016 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save existentialmutt/c904d6e55cd09dda81ea9751d2e66e6d to your computer and use it in GitHub Desktop.
Save existentialmutt/c904d6e55cd09dda81ea9751d2e66e6d to your computer and use it in GitHub Desktop.
Capistrano 3 rails console tasks and log tailing tasks
# Place in lib/capistrano/tasks/rails.cap and require it in your Capfile
# See: https://gist.github.com/existentialmutt/c904d6e55cd09dda81ea9751d2e66e6d
# Adapted to work with RVM installed to /usr/local
namespace :rails do
desc "tail rails logs"
task :tail_logs do
on roles(:app) do
execute "tail -f #{shared_path}/log/#{fetch(:rails_env)}.log"
end
end
desc "Open the rails console on primary app server"
task :console do
on roles(:app), primary: true do
rails_env = fetch(:stage)
execute_interactively "bin/rails console #{rails_env}"
end
end
desc "Open the rails dbconsole on primary db server"
task :dbconsole do
on roles(:db), primary: true do
rails_env = fetch(:stage)
execute_interactively "#{current_path}/bin/rails dbconsole #{rails_env}"
end
end
def execute_interactively(command)
user = fetch(:user)
port = fetch(:port) || 22
cmd = "ssh"
if user
cmd << " -l #{user}"
end
cmd << " #{host} -p #{port} -t 'cd #{fetch(:deploy_to)}/current && /usr/local/rvm/bin/rvm `cat .ruby-version` do #{bundle_cmd} #{command}'"
info "Connecting to #{host} with command"
info cmd
exec cmd
end
def bundle_cmd
"bundle exec"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment