Method: Capistrano::Configuration::Actions::Invocation#sudo

Defined in:
lib/capistrano/configuration/actions/invocation.rb

#sudo(*parameters, &block) ⇒ Object

Returns the command string used by capistrano to invoke a comamnd via sudo.

run "#{sudo :as => 'bob'} mkdir /path/to/dir"

It can also be invoked like #run, but executing the command via sudo. This assumes that the sudo password (if required) is the same as the password for logging in to the server.

sudo "mkdir /path/to/dir"

Also, this method understands a :sudo configuration variable, which (if specified) will be used as the full path to the sudo executable on the remote machine:

set :sudo, "/opt/local/bin/sudo"

If you know what you’re doing, you can also set :sudo_prompt, which tells capistrano which prompt sudo should use when asking for a password. (This is so that capistrano knows what prompt to look for in the output.) If you set :sudo_prompt to an empty string, Capistrano will not send a preferred prompt.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/capistrano/configuration/actions/invocation.rb', line 224

def sudo(*parameters, &block)
  options = parameters.last.is_a?(Hash) ? parameters.pop.dup : {}
  command = parameters.first
  user = options[:as] && "-u #{options.delete(:as)}"

  sudo_prompt_option = "-p '#{sudo_prompt}'" unless sudo_prompt.empty?
  sudo_command = [fetch(:sudo, "sudo"), sudo_prompt_option, user].compact.join(" ")

  if command
    command = sudo_command + " " + command
    run(command, options, &block)
  else
    return sudo_command
  end
end