简体   繁体   中英

How to set a default shell or sudo to a user in capistrano 3

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:

  1. Setting the :default_shell option does not work as mentioned.
  2. 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.
  3. 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.
  4. 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM