Module | Capistrano::Configuration::Actions::Invocation |
In: |
lib/capistrano/configuration/actions/invocation.rb
lib/capistrano/configuration/actions/invocation.rb |
Merges the various default command options into the options hash and returns the result. The default command options that are understand are:
# File lib/capistrano/configuration/actions/invocation.rb, line 129 129: def add_default_command_options(options) 130: defaults = self[:default_run_options] 131: options = defaults.merge(options) 132: 133: env = self[:default_environment] 134: env = env.merge(options[:env]) if options[:env] 135: options[:env] = env unless env.empty? 136: 137: shell = options[:shell] || self[:default_shell] 138: options[:shell] = shell unless shell.nil? 139: 140: options 141: end
Merges the various default command options into the options hash and returns the result. The default command options that are understand are:
# File lib/capistrano/configuration/actions/invocation.rb, line 129 129: def add_default_command_options(options) 130: defaults = self[:default_run_options] 131: options = defaults.merge(options) 132: 133: env = self[:default_environment] 134: env = env.merge(options[:env]) if options[:env] 135: options[:env] = env unless env.empty? 136: 137: shell = options[:shell] || self[:default_shell] 138: options[:shell] = shell unless shell.nil? 139: 140: options 141: end
# File lib/capistrano/configuration/actions/invocation.rb, line 148 148: def continue_execution(cmd) 149: case Capistrano::CLI.debug_prompt(cmd) 150: when "y" 151: true 152: when "n" 153: false 154: when "a" 155: exit(-1) 156: end 157: end
# File lib/capistrano/configuration/actions/invocation.rb, line 148 148: def continue_execution(cmd) 149: case Capistrano::CLI.debug_prompt(cmd) 150: when "y" 151: true 152: when "n" 153: false 154: when "a" 155: exit(-1) 156: end 157: end
Invokes the given command. If a via key is given, it will be used to determine what method to use to invoke the command. It defaults to :run, but may be :sudo, or any other method that conforms to the same interface as run and sudo.
# File lib/capistrano/configuration/actions/invocation.rb, line 33 33: def invoke_command(cmd, options={}, &block) 34: options = options.dup 35: via = options.delete(:via) || :run 36: send(via, cmd, options, &block) 37: end
Invokes the given command. If a via key is given, it will be used to determine what method to use to invoke the command. It defaults to :run, but may be :sudo, or any other method that conforms to the same interface as run and sudo.
# File lib/capistrano/configuration/actions/invocation.rb, line 33 33: def invoke_command(cmd, options={}, &block) 34: options = options.dup 35: via = options.delete(:via) || :run 36: send(via, cmd, options, &block) 37: end
Execute the given command on all servers that are the target of the current task. If a block is given, it is invoked for all output generated by the command, and should accept three parameters: the SSH channel (which may be used to send data back to the remote process), the stream identifier (:err for stderr, and :out for stdout), and the data that was received.
# File lib/capistrano/configuration/actions/invocation.rb, line 45 45: def run(cmd, options={}, &block) 46: block ||= self.class.default_io_proc 47: logger.debug "executing #{cmd.strip.inspect}" 48: 49: return if debug && continue_execution(cmd) == false 50: 51: options = add_default_command_options(options) 52: 53: if cmd.include?(sudo) 54: block = sudo_behavior_callback(block) 55: end 56: 57: execute_on_servers(options) do |servers| 58: targets = servers.map { |s| sessions[s] } 59: Command.process(cmd, targets, options.merge(:logger => logger), &block) 60: end 61: end
Execute the given command on all servers that are the target of the current task. If a block is given, it is invoked for all output generated by the command, and should accept three parameters: the SSH channel (which may be used to send data back to the remote process), the stream identifier (:err for stderr, and :out for stdout), and the data that was received.
# File lib/capistrano/configuration/actions/invocation.rb, line 45 45: def run(cmd, options={}, &block) 46: block ||= self.class.default_io_proc 47: logger.debug "executing #{cmd.strip.inspect}" 48: 49: return if debug && continue_execution(cmd) == false 50: 51: options = add_default_command_options(options) 52: 53: if cmd.include?(sudo) 54: block = sudo_behavior_callback(block) 55: end 56: 57: execute_on_servers(options) do |servers| 58: targets = servers.map { |s| sessions[s] } 59: Command.process(cmd, targets, options.merge(:logger => logger), &block) 60: end 61: end
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"
# File lib/capistrano/configuration/actions/invocation.rb, line 79 79: def sudo(*parameters, &block) 80: options = parameters.last.is_a?(Hash) ? parameters.pop.dup : {} 81: command = parameters.first 82: user = options[:as] && "-u #{options.delete(:as)}" 83: 84: sudo_command = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user].compact.join(" ") 85: 86: if command 87: command = sudo_command + " " + command 88: run(command, options, &block) 89: else 90: return sudo_command 91: end 92: end
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"
# File lib/capistrano/configuration/actions/invocation.rb, line 79 79: def sudo(*parameters, &block) 80: options = parameters.last.is_a?(Hash) ? parameters.pop.dup : {} 81: command = parameters.first 82: user = options[:as] && "-u #{options.delete(:as)}" 83: 84: sudo_command = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user].compact.join(" ") 85: 86: if command 87: command = sudo_command + " " + command 88: run(command, options, &block) 89: else 90: return sudo_command 91: end 92: end