[Rails] Make capistrano ask for the password first

7 Jun

I frequently find myself doing rake deploy with capistrano, and then wander off to do something else, while the checkout process runs its minute-long course. And then I forget what I was doing, and only 15 minutes later stumble upon that shell buffer again, only to discover that it’s still sitting there, waiting for my sudo password so it can restart the dispatchers.

Well, with this little snippet in my deploy.rb, it now asks for my sudo password up-front, so it’ll still be cached and valid when it really needs it at the end:

task :before_deploy, :roles => :app do
  send(run_method, "date")
end

That’s it: Force it to execute some shell command using sudo. It doesn’t matter which, so long as it doesn’t actually do anything. I chose date. Calling run_method instead of sudo directly will ensure that if you’re not using sudo, it won’t ask for your password here, either.

4 Responses to “[Rails] Make capistrano ask for the password first”

  1. Sean Treadway 07. Jun, 2006 at 10:22 PM #

    Brilliant. I actually just ran into the same problem today.

  2. Lars Pind 07. Jun, 2006 at 10:22 PM #

    I’m glad it could be of use. Rock on.

  3. DHH 07. Jun, 2006 at 10:22 PM #

    You can also just pass "-p" to your capistrano call. Like "cap deploy -p". That’ll force the question up front.

  4. Lars Pind 07. Jun, 2006 at 10:22 PM #

    Haha, thanks, David, I was always using the "rake" version directly, so I never noticed.