如何在capistrano中为用户设置默认shell或sudo 3 [英] How to set a default shell or sudo to a user in capistrano 3
问题描述
目前,我正在从Capistrano 2.x迁移到3.x,以便应用Rails应用程序并运行以下问题。服务器上的常规设置包含一个部署用户,该用户应该是部署的负责人,但是当用户进行部署时,他们将使用自己的用户名连接到服务器。
在Capistrano 2.x中,我可以使用 default_shell
选项完成此操作。
set:default_shell,sudo -u deploy / bin / sh
这在Capistrano 3.x中不起作用。有什么办法可以设置shell或sudo到部署用户的前钩子?
我已经尝试过的事情:
- 设置
:default_shell
选项不能像上面提到的那样工作。 在挂钩之前向用户提示。就像sudo -s -u deploy
,它在capistrano以外工作正常。顺便说一下,我的用户是为部署用户设置无密码sudo的。但在Capistrano运行命令,然后挂起。 这个SO问题,似乎可以在每个命令前加上sudo -u deploy
,但这似乎不是充分工作。一对夫妇的命令中,部署仍然中止执行异常,因为我的用户拒绝Permission。 - 我可以单独映射一些命令为
SSHKit.config.command_map [:git] ='sudo -u deploy git'
,但这看起来不是最好的方法,我没有列出需要映射的所有命令。 >
更新:
在我的第三次尝试中,映射所有命令似乎都起作用。 p>
SSHKit.config.command_map = Hash.new do | hash,command |
hash [command] =sudo -u deploy#{command}
end
但不完全。 capistrano做的第一件事之一是运行git:检查是否上传文件 /tmp/git-ssh.sh
。但是这个文件是由我的用户创建的,而不是部署用户创建的。 解决方案
如果您只需要更改部署用户, capistrano 3它更容易。假设你有这个文件结构:
config
| __deploy
| __production.rb
| __staging.rb
Capfile
然后在您的production.rb或staging .rb(取决于您部署的环境):
set:user,'deploy'
server'example.com',user:fetch(:user)
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.
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)
这篇关于如何在capistrano中为用户设置默认shell或sudo 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!