Module: Capistrano::Configuration::Actions::Invocation
- Included in:
- Capistrano::Configuration
- Defined in:
- lib/capistrano/configuration/actions/invocation.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_default_command_options(options) ⇒ Object
Merges the various default command options into the options hash and returns the result.
-
#initialize_with_invocation(*args) ⇒ Object
:nodoc:.
-
#invoke_command(cmd, options = {}, &block) ⇒ Object
Invokes the given command.
-
#run(cmd, options = {}, &block) ⇒ Object
Execute the given command on all servers that are the target of the current task.
-
#sudo(command, options = {}, &block) ⇒ Object
Like #run, but executes the command via
sudo. -
#sudo_behavior_callback(fallback) ⇒ Object
Returns a Proc object that defines the behavior of the sudo callback.
Class Method Details
.included(base) ⇒ Object
:nodoc:
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 7 def self.included(base) #:nodoc: base.extend(ClassMethods) base.send :alias_method, :initialize_without_invocation, :initialize base.send :alias_method, :initialize, :initialize_with_invocation base.default_io_proc = Proc.new do |ch, stream, out| level = stream == :err ? :important : :info ch[:options][:logger].send(level, out, "#{stream} :: #{ch[:server]}") end end |
Instance Method Details
#add_default_command_options(options) ⇒ Object
Merges the various default command options into the options hash and returns the result. The default command options that are understand are:
-
:default_environment: If the :env key already exists, the :env key is merged into default_environment and then added back into options.
-
:default_shell: if the :shell key already exists, it will be used. Otherwise, if the :default_shell key exists in the configuration, it will be used. Otherwise, no :shell key is added.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 112 def () = .dup env = self[:default_environment] env = env.merge([:env]) if [:env] [:env] = env unless env.empty? shell = [:shell] || self[:default_shell] [:shell] = shell if shell end |
#initialize_with_invocation(*args) ⇒ Object
:nodoc:
23 24 25 26 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 23 def initialize_with_invocation(*args) #:nodoc: initialize_without_invocation(*args) set :default_environment, {} end |
#invoke_command(cmd, options = {}, &block) ⇒ Object
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.
32 33 34 35 36 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 32 def invoke_command(cmd, ={}, &block) = .dup via = .delete(:via) || :run send(via, cmd, , &block) end |
#run(cmd, options = {}, &block) ⇒ Object
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.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 44 def run(cmd, ={}, &block) block ||= self.class.default_io_proc logger.debug "executing #{cmd.strip.inspect}" = () execute_on_servers() do |servers| targets = servers.map { |s| sessions[s] } Command.process(cmd, targets, .merge(:logger => logger), &block) end end |
#sudo(command, options = {}, &block) ⇒ Object
Like #run, but executes the command via sudo. This assumes that the sudo password (if required) is the same as the password for logging in to the server.
Also, this module accepts 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"
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 65 def sudo(command, ={}, &block) block ||= self.class.default_io_proc = .dup as = .delete(:as) user = as && "-u #{as}" command = [fetch(:sudo, "sudo"), user, command].compact.join(" ") run(command, , &sudo_behavior_callback(block)) end |
#sudo_behavior_callback(fallback) ⇒ Object
Returns a Proc object that defines the behavior of the sudo callback. The returned Proc will defer to the fallback argument (which should also be a Proc) for any output it does not explicitly handle.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 81 def sudo_behavior_callback(fallback) #:nodoc: # in order to prevent _each host_ from prompting when the password # was wrong, let's track which host prompted first and only allow # subsequent prompts from that host. prompt_host = nil Proc.new do |ch, stream, out| if out =~ /password:/i ch.send_data "#{self[:password]}\n" elsif out =~ /try again/ if prompt_host.nil? || prompt_host == ch[:server] prompt_host = ch[:server] logger.important out, "#{stream} :: #{ch[:server]}" reset! :password end else fallback.call(ch, stream, out) end end end |