简体   繁体   English

Capistrano 3 sudo任务

[英]Capistrano 3 sudo task

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运行任务?

The Capistrano 3 guide recommends the use of passwordless sudo. Capistrano 3指南建议使用无密码的sudo。 This allows your less-priveleged user execute the sudo command without having to enter a password via the PTY. 这允许您较少的用户执行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

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. 但是,我创建了一个小宝石 ,它允许您在Capistrano 3任务中使用带密码的sudo。

Add sshkit-sudo to 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

To resolve this issue I needed to add set :pty, true to my deploy.rb file. 要解决此问题,我需要将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.sh that has a command within sudo service nginx restart . 此任务基本上运行一个名为restart.sh的shell脚本,该脚本在sudo service nginx restart中有一个命令。

you want "as user do end", like 你想要“像用户那样结束”,比如

as "root" do
  execute :something
end

If you really need to use sudo you can always map the command like SSHKit.config.command_map[:rm] = 'sudo rm' which will make execute :rm into the proper rm command invoked with sudo . 如果你真的需要使用sudo你总是可以映射命令,如SSHKit.config.command_map[:rm] = 'sudo rm' ,这将使execute :rm成为用sudo调用的正确rm命令。 If your deploy user is in the sudoers things will work as expected. 如果您的部署用户在sudoers中,事情将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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