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

天涯浪子 提交于 2019-12-11 19:23:38

问题


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.


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!