7

I've just upgraded Capistrano from v2 to v3.1.

I've re-written my tasks including one that runs a shell script that restarts NGINX among other things. To restart NGINX I have to run as sudo which causes the error:

Sorry, you must have a TTY to run sudo

In Capistrano 2, to resolve this I added to my Capfile:

default_run_options[:pty] = true

What is the equivalent for Capistrano v3?

My deploy.rb file looks like this:

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'APP_NAME'

namespace :deploy do

  desc 'Restart NGINX'
  task :restart do

    on roles(:app), in: :sequence, wait: 5 do
       execute :sudo, "./restart.sh"
    end
  end

end
ajtrichards
  • 29,723
  • 13
  • 94
  • 101

1 Answers1

13

To resolve this issue I needed to add set :pty, true to my deploy.rb file. I had to dig around a few places to find this answer so I thought i'd share incase anyone else had the same issue.

Updated deploy.rb file

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'APP_NAME'
set :pty, true

namespace :deploy do

  desc 'Restart NGINX'
  task :restart do
    on roles(:app), in: :sequence, wait: 1 do
       execute :sudo, "./restart.sh"
    end
  end

end

To connect without being prompted for a password, you'll need to set up SSH key's. My production.rb and staging.rb look something like this:

set :stage, :production

role :app, %{ec2-000-000-000-000.eu-west-1.compute.amazonaws.com}

set :ssh_options, {
    user: 'ubuntu',
    keys: %w(/path/to/key/file/my_access_key.pem),
    forward_agent: false
}
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
  • I read somewhere ([here](https://semaphoreapp.com/blog/2013/11/26/capistrano-3-upgrade-guide.html)) that `:pty` is `true` in v3 by default, but I couldn't find any documentation on the official site... like really nothing. Good job figuring that one out ;) – frhd Jan 24 '14 at 12:44
  • Cheers - thought it may be useful to someone :-) – ajtrichards Jan 24 '14 at 13:45
  • @ajtrichards_wales But how do you provide the password when you are prompted? – Mohamad Apr 11 '14 at 19:29
  • @Mohamad you need to setup SSH keys so you aren't prompted. I've updated the answer to show you what my `production.rb` file looks like. – ajtrichards Apr 14 '14 at 08:33
  • @ajtrichards_wales Interesting. I have already copied my SSH key to the server. I would have thought Capistrano would automatically pick it up since I'm deploying as a user who already has sudo rights. In the end I added a no-password option to my deploy user to get it to work. `pty: true` never worked for me. I will give your variation a crack since it's more preferable than a no-password user. Thanks. – Mohamad Apr 14 '14 at 15:09