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.
- #continue_execution(cmd) ⇒ Object
-
#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(*parameters, &block) ⇒ Object
Returns the command string used by capistrano to invoke a comamnd via sudo.
-
#sudo_behavior_callback(fallback) ⇒ Object
Returns a Proc object that defines the behavior of the sudo callback.
-
#sudo_prompt ⇒ Object
Returns the prompt text to use with sudo.
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.
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 129 def () defaults = self[:default_run_options] = defaults.merge() env = self[:default_environment] env = env.merge([:env]) if [:env] [:env] = env unless env.empty? shell = [:shell] || self[:default_shell] [:shell] = shell unless shell.nil? end |
#continue_execution(cmd) ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 148 def continue_execution(cmd) case Capistrano::CLI.debug_prompt(cmd) when "y" true when "n" false when "a" exit(-1) end end |
#initialize_with_invocation(*args) ⇒ Object
:nodoc:
23 24 25 26 27 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 23 def initialize_with_invocation(*args) #:nodoc: initialize_without_invocation(*args) set :default_environment, {} set :default_run_options, {} 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.
33 34 35 36 37 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 33 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 45 def run(cmd, ={}, &block) block ||= self.class.default_io_proc logger.debug "executing #{cmd.strip.inspect}" return if debug && continue_execution(cmd) == false = () if cmd.include?(sudo) block = sudo_behavior_callback(block) end execute_on_servers() do |servers| targets = servers.map { |s| sessions[s] } Command.process(cmd, targets, .merge(:logger => logger), &block) end end |
#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"
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 79 def sudo(*parameters, &block) = parameters.last.is_a?(Hash) ? parameters.pop.dup : {} command = parameters.first user = [:as] && "-u #{.delete(:as)}" sudo_command = [fetch(:sudo, "sudo"), "-p '#{sudo_prompt}'", user].compact.join(" ") if command command = sudo_command + " " + command run(command, , &block) else return sudo_command end 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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 98 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 =~ /^#{Regexp.escape(sudo_prompt)}/ ch.send_data "#{self[:password]}\n" elsif out =~ /^Sorry, try again/ if prompt_host.nil? || prompt_host == ch[:server] prompt_host = ch[:server] logger.important out, "#{stream} :: #{ch[:server]}" reset! :password end elsif fallback fallback.call(ch, stream, out) end end end |
#sudo_prompt ⇒ Object
Returns the prompt text to use with sudo
144 145 146 |
# File 'lib/capistrano/configuration/actions/invocation.rb', line 144 def sudo_prompt fetch(:sudo_prompt, "sudo password: ") end |