Ruby-on-rails Capistrano 3 须藤任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19948809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:02:02  来源:igfitidea点击:

Capistrano 3 sudo task

ruby-on-railscapistranocapistrano3

提问by crimi

I want to write a recipe with Capistrano 3 executing a task on the remote server with sudo.

我想用 Capistrano 3 编写一个使用 sudo 在远程服务器上执行任务的配方。

With Capistrano 2 this could be done for example:

使用 Capistrano 2 可以这样做,例如:

default_run_options[:pty] = true

task :hello do
  run "#{sudo} cp ~/something /something"
end

With Capistrano 3 I found:

使用 Capistrano 3 我发现:

set :pty, true

But I could not get to execute a task running with sudo.

但是我无法执行使用 sudo 运行的任务。

How can I run a task with sudo?

如何使用 sudo 运行任务?

回答by Ian

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.

Capistrano 3 指南推荐使用无密码 sudo。这允许您的低权限用户执行 sudo 命令,而无需通过 PTY 输入密码。

You can use the task that Kentaro wrote above, and add something like the following to your /etc/sudoers file:

您可以使用 Kentaro 上面编写的任务,并将如下内容添加到您的 /etc/sudoers 文件中:

deploy ALL=NOPASSWD:/bin/cp ~/something /something

http://www.capistranorb.com/documentation/getting-started/authentication-and-authorisation/#toc_8

http://www.capistranorb.com/documentation/getting-started/authentication-and-authorisation/#toc_8

回答by Kentaro Imai

I usually write like this:

我通常这样写:

task :hello do
  on roles(:all) do |host|
    execute :sudo, :cp, '~/something', '/something'
  end
end


Edit

编辑

Capistrano 3 does not support sudo with password.

Capistrano 3 不支持带密码的 sudo。

However, I created a small gem, which enables you to use sudo with password in Capistrano 3 task.

但是,我创建了一个小gem,它使您可以在 Capistrano 3 任务中使用带密码的 sudo 。

Add sshkit-sudoto your application's Gemfile:

sshkit-sudo添加到应用程序的 Gemfile 中:

# Gemfile
gem 'sshkit-sudo'

And require 'sshkit/sudo' in you Capfile:

并在您的 Capfile 中要求 'sshkit/sudo':

# Capfile
require 'sshkit/sudo'

Now, you can execute a command with sudo as follows:

现在,您可以使用 sudo 执行命令,如下所示:

task :hello do
  on roles(:all) do
    sudo :cp, '~/something', '/something'
  end
end

回答by ajtrichards

To resolve this issue I needed to add set :pty, trueto my deploy.rbfile.

为了解决这个问题,我需要添加set :pty, true到我的deploy.rb文件中。

I can now run the following:

我现在可以运行以下命令:

# config valid only for Capistrano 3.1
lock '3.1.0'

set :application, 'APP_NAME'
set :pty, true
set :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
  end

end

This task basically runs a shell script called restart.shthat has a command within sudo service nginx restart.

此任务基本上运行一个名为的 shell 脚本restart.sh,该脚本在sudo service nginx restart.

回答by Joe McDonagh

you want "as user do end", like

你想要“作为用户结束”,比如

as "root" do
  execute :something
end

回答by craigmcnamara

If you really need to use sudoyou can always map the command like SSHKit.config.command_map[:rm] = 'sudo rm'which will make execute :rminto the proper rmcommand invoked with sudo. If your deploy user is in the sudoers things will work as expected.

如果你真的需要使用sudo你总是可以映射命令一样SSHKit.config.command_map[:rm] = 'sudo rm',这将使execute :rm到合适的rm与调用的命令sudo。如果您的部署用户在 sudoers 中,事情将按预期工作。