Capistrano 3 sudo task
The Capistrano 3 guide recommends the use of passwordless sudo. This allows your less-priveleged user execute the sudo command without having to enter a password via the PTY.
You can use the task that Kentaro wrote above, and add something like the following to your /etc/sudoers file:
deploy ALL=NOPASSWD:/bin/cp ~/something /something
http://www.capistranorb.com/documentation/getting-started/authentication-and-authorisation/#toc_8
I usually write like this:
task :hello do on roles(:all) do |host| execute :sudo, :cp, '~/something', '/something' endend
Edit
Capistrano 3 does not support sudo with password.
However, I created a small gem, which enables you to use sudo with password in Capistrano 3 task.
Add sshkit-sudo to your application's Gemfile:
# Gemfilegem 'sshkit-sudo'
And require 'sshkit/sudo' in you Capfile:
# Capfilerequire 'sshkit/sudo'
Now, you can execute a command with sudo as follows:
task :hello do on roles(:all) do sudo :cp, '~/something', '/something' endend
To resolve this issue I needed to add set :pty, true
to my deploy.rb
file.
I can now run the following:
# config valid only for Capistrano 3.1lock '3.1.0'set :application, 'APP_NAME'set :pty, trueset :ssh_options, {:forward_agent => true}namespace :deploy do desc 'Restart NGINX' task :restart do on roles(:app), in: :sequence, wait: 1 do execute :sudo, "./restart.sh" end endend
This task basically runs a shell script called restart.sh
that has a command within sudo service nginx restart
.