Skip to content

Instantly share code, notes, and snippets.

@olegykz
Created March 27, 2015 10:51
Show Gist options
  • Save olegykz/30f2461a697ae736b352 to your computer and use it in GitHub Desktop.
Save olegykz/30f2461a697ae736b352 to your computer and use it in GitHub Desktop.
Capistrano 3 useful tasks to open ssh session and rails console
namespace :rails do
desc 'Open a rails console `cap [staging] rails:console [server_index default: 0]`'
task :console do
on roles(:app) do |server|
return if server != roles(:app)[ARGV[2].to_i]
puts "Opening a console on: #{host}...."
cmd = "ssh #{fetch(:ssh_options)[:user]}@#{host} -t 'source \"$HOME/.rvm/scripts/rvm\""\
" && cd #{fetch(:deploy_to)}/current && RAILS_ENV=#{fetch(:rails_env)} bundle exec rails console'"
exec cmd
end
end
end
# Bonus
desc 'Open ssh `cap [staging] ssh [server_index default: 0]`'
task :ssh do
on roles(:app) do |server|
return if server != roles(:app)[ARGV[2].to_i]
puts "Opening ssh session on: #{host}...."
cmd = "ssh #{fetch(:ssh_options)[:user]}@#{host}"
exec cmd
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment