Asymmetrical View

Automating Capistrano Password Prompts with Expect

I just started using Capistrano for deploying my Rails applications (like Snapclean.me"). I also just started using capistrano_rsync_with_remote_cache to help push releases out faster than the :copy deploy strategy.

I’m very happy with how much faster it is than the a :copy, but I’m impatient and having to provide the password more than once per invocation is frustrating to me. I know the old Unix standby Expect can do this easily, my only problem is I don’t remember Tcl very well and every time I’ve done this I forget how / what I did. This post is a write up so I know where to come back to the next time I need to do this (and I know I’ll run into it again).

Spawn and Expect

Conceptually the basic use of Expect is very straight forward. You execute another program and you can register handlers that will be invoked when the program emits specific output. In the case of executing capistrano, and it executing other commands, I’m looking for anything that looks like a password prompt. When a password prompt is emitted I want to send the password.

The script below does exactly that, first asking the user for the password (disabling terminal echo) before spawning the capistrano command itself.

#!/usr/bin/env expect
stty -echo
send_user -- "Login Password: "
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set the_password $expect_out(1,string)

spawn cap deploy
expect {
  -re " *\[Pp\]assword: *" {
    send "$the_password\n"
    exp_continue
  }
}

I put the above script in a file called wrap-cap, and did a chmod 755 ./wrap-cap on the file.

The exp_continue is used to reset (or loop) expect so that it will continue to look for password prompts and provide the password as many times as it is requested – since there are various commands that may ask for it (rsync, ssh, sudo, etc.) and some of them are optional (especially sudo), this ‘looping’ behavior is very handy.

Expect is a great Unix tool for developers and system administrators to have in their toolboxes.

Kyle Burton, 6 Jul 2010 – Philadelphia PA


Tags: capistrano,ruby,rails,expect