Announcing Capistrano-Remote
Ever needed to run some maintenance task in your production environment? For me this usually triggers a bunch of questions: What’s that hostname again? What user should I log in as? And where did we deploy the application? Is RVM loaded correctly? Do I need to bundle exec or whatever?
So we built a Capistrano extension to answer those questions and ease the pain.
With Capistrano-Remote launching a Rails console in your production environment is as simple as
$ cap production remote:console
Loading production environment (Rails 4.2.4)
irb(main):001:0>
There is even a
$ cap production remote:dbconsole
psql (9.3.5)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.
example_production=>
Getting started
Just follow the fairly simple installation and usage instructions at substancelab/capistrano-remote. Basically:
- Add it to your
Gemfile
:gem "capistrano-remote", :require => false
bundle install
- Add to your
Capfile
:require "capistrano/remote"
You should now be able to see both tasks in all their glory:
$ cap -T remote
cap remote:console # Run and attach to a remote Rails console
cap remote:dbconsole # Run and attach to a remote Rails database console
Backstory
If you need to work in your production environment for whatever reason, on the surface the process isn’t hard:
- SSH to a server (which server? which user? And on AWS, this gets even more confusing, as we don’t really know the actual hostnames)
- cd to the deployment directory (what directory again?)
- fire up
rails console production
(or was thatbundle exec
? How about rbenv?)
However, those steps (establishing an SSH connection and setting up the environment to run Rails commands) are pretty much exactly what Capistrano excels at, so why not use that?
Think of it as heroku run rails console
for those of us stuck on selfmanaged
nodes and Capistrano and grab it from RubyGems.