问题
I'm currently in the process of moving from Capistrano 2.x to 3.x for a rails application and running into the following issue. The general setup on the server contains a deploy user that should be the one responsible for deploying, however when a user goes to deploy, they connect to the server with their own user name.
In Capistrano 2.x, I was able to accomplish this using the default_shell
option.
set :default_shell, "sudo -u deploy /bin/sh"
This does not work in Capistrano 3.x. Is there any way to either set the shell, or sudo to the deploy user in a before hook?
Things I have tried:
- Setting the
:default_shell
option does not work as mentioned. - I tried sudo-ing to the user in a before hook. Something like
sudo -s -u deploy
, which works fine outside of capistrano. My user is set up for password-less sudo to the deploy user by the way. But in Capistrano it runs the command and then just hangs. - Per this SO question, it seems possible to prepend
sudo -u deploy
to every command, but this doesn't seem to fully work. A couple commands in, the deploy still aborts with an exception executing as my user on a Permission denied. - I can individually map some commands as
SSHKit.config.command_map[:git] = 'sudo -u deploy git'
, but this does not seem like the best method and I do not have a list of all the commands that I would need to map.
UPDATE: Per my third attempt, mapping all of the commands generally seems to work.
SSHKit.config.command_map = Hash.new do |hash, command|
hash[command] = "sudo -u deploy #{command}"
end
But not completely. One of the first things capistrano does is run git:check which uploads the file /tmp/git-ssh.sh
. But this file gets created with my user, not the deploy user.
回答1:
If you just need to change the deployment user, in capistrano 3 it's a lot easier. Assuming you have this file structure:
config
|__deploy
|__production.rb
|__staging.rb
Capfile
Then in your production.rb or staging.rb (depending on what environment you're deploying to):
set :user, 'deploy'
server 'example.com', user: fetch(:user)
来源:https://stackoverflow.com/questions/31059625/how-to-set-a-default-shell-or-sudo-to-a-user-in-capistrano-3