Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capistrano task to open a rails console on a remote server. Require this file in your deploy.rb and run "cap rails:console"
# encoding: UTF-8
Capistrano::Configuration.instance(:must_exist).load do
namespace :rails do
desc "Open the rails console on one of the remote servers"
task :console, :roles => :app do
hostname = find_servers_for_task(current_task).first
exec "ssh -l #{user} #{hostname} -t 'source ~/.profile && #{current_path}/script/rails c #{rails_env}'"
end
end
end
@colszowka

This comment has been minimized.

Copy link

colszowka commented Sep 21, 2012

Very useful, thanks a lot! Much better to just go with good old system commands than trying to reimplement interactive ssh shells like many other snippets for cap rails console do.

In my situation, I have to switch to the app user after connecting via SSH, so my slightly modified code looks like this:

namespace :rails do
  desc "Remote console"
  task :console, :roles => :app do
    run_interactively "bundle exec rails console #{rails_env}"
  end

  desc "Remote dbconsole"
  task :dbconsole, :roles => :app do
    run_interactively "bundle exec rails dbconsole #{rails_env}"
  end
end

def run_interactively(command, server=nil)
  server ||= find_servers_for_task(current_task).first
  exec %Q(ssh #{server.host} -t 'sudo su - #{application} -c "cd #{current_path} && #{command}"')
end

This makes some assumptions which do apply in my config setup, being that the server host (I prefer to write that out explicitly as server.host, even though server.to_s does the same in capistrano) is set up in ~/.ssh/config, that the user name equals the app name, rails_env cap config is set etc, but it works like a charm.

@jimryan

This comment has been minimized.

Copy link

jimryan commented Mar 14, 2013

Awesome, thank you! I modified it very slightly to accomodate our non-standard SSH port.

namespace :rails do
  desc "Open the rails console on one of the remote servers"
  task :console, :roles => :app do
    hostname = find_servers_for_task(current_task).first
    port = exists?(:port) ? fetch(:port) : 22
    exec "ssh -l #{user} #{hostname} -p #{port} -t 'source ~/.profile && #{current_path}/script/rails c #{rails_env}'"
  end
end
@toobulkeh

This comment has been minimized.

Copy link

toobulkeh commented Jan 2, 2014

Since Capistrano 3 is now out, someone else might want the updated version of this code. You can find it here.

@kacperk

This comment has been minimized.

Copy link

kacperk commented Feb 27, 2014

For capistrano 2 you can also use https://github.com/subsis/capistrano-cook - it has a lot of nice recipes included

@henrik

This comment has been minimized.

Copy link

henrik commented Aug 8, 2014

Had to do this for my Rails 4 app on RVM to find the ruby binary:

ssh myserver -t 'source "$HOME/.rvm/scripts/rvm"; cd ~/apps/myapp.com/current; RAILS_ENV=production bin/rails console'
@mariozaizar

This comment has been minimized.

Copy link

mariozaizar commented Mar 26, 2015

Had to change it a bit (rails 3.2 + rvm)

namespace :rails do
  desc "Open the rails console on one of the remote servers"
  task :console, :roles => :app do
    hostname = find_servers_for_task(current_task).first
    exec "ssh -l #{user} #{hostname} -t 'source \"$HOME/.rvm/scripts/rvm\" && cd #{current_path}; bundle exec rails console #{rails_env}'"
  end

  desc "Open the rails dbconsole on one of the remote servers"
  task :dbconsole, :roles => :app do
    hostname = find_servers_for_task(current_task).first
    exec "ssh -l #{user} #{hostname} -t 'source \"$HOME/.rvm/scripts/rvm\" && cd #{current_path}; bundle exec rails dbconsole #{rails_env}'"
  end

  # Short aliases
  task :c, :roles => :app do
    console
  end

  task :dbc, :roles => :app do
    dbconsole
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.