Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano 3 - Error: sorry, you must have a tty to run sudo

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
like image 810
ajtrichards Avatar asked Jan 24 '14 11:01

ajtrichards


1 Answers

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
}
like image 61
ajtrichards Avatar answered Oct 06 '22 22:10

ajtrichards