Module: Capsaicin::Invocation

Defined in:
lib/capsaicin/invocation.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
# File 'lib/capsaicin/invocation.rb', line 5

def self.included(base)
  [:run, :sudo].each do |k|
   base.send :alias_method, :"#{k}_without_override", k
   base.send :alias_method, k, :"#{k}_with_override"
  end
end

Instance Method Details

#local_run(*args, &block) ⇒ Object

Capistrano’s system() override is only available from the base deployment strategy. Also, we could do with a few more windows checks.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capsaicin/invocation.rb', line 65

def local_run(*args, &block)
  args.pop if Hash===args.last
  cmd = args.join(' ')
  if RUBY_PLATFORM =~ /win32|mingw|mswin/
    cmd.gsub!('/','\\') # Replace / with \\
    cmd.gsub!(/^cd /,'cd /D ') # Replace cd with cd /D
    cmd.gsub!(/&& cd /,'&& cd /D ') # Replace cd with cd /D
    logger.trace "executing locally: #{cmd}"
    Kernel.system cmd
  else
    logger.trace "executing locally: #{cmd}"
    Kernel.system cmd
  end
end

#run_with_override(cmd, options = {}, &block) ⇒ Object

Allows overriding the default behavior of run by setting :override_run_method and (optionally) :override_runner



14
15
16
17
18
19
20
21
22
23
# File 'lib/capsaicin/invocation.rb', line 14

def run_with_override(cmd, options={}, &block)
  via = options[:via] || fetch(:override_run_method, :run)
    if cmd.include?(sudo) or :run == via
      run_without_override cmd, options, &block
    else
      options = options.dup
      as = fetch(:override_runner, nil) and options[:as] ||= as
     send via, cmd, options, &block
   end
end

#sudo_as(*args, &block) ⇒ Object

Always uses :runner to sudo as someone. Equivalent to: sudo “command”, :as => fetch(:runner,nil)



82
83
84
85
86
# File 'lib/capsaicin/invocation.rb', line 82

def sudo_as(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:as] ||= fetch(:runner, nil)
  sudo_without_override *args.push(options), &block
end

#sudo_su(*args, &block) ⇒ Object

Extremely helpful if you only have permission to: sudo su SOMEUSER -c ‘command’



89
90
91
92
93
94
95
# File 'lib/capsaicin/invocation.rb', line 89

def sudo_su(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:as] ||= fetch(:runner, nil)
  return sudo_as(options, &block) if args.empty?   # compatibility with capistrano's +sudo+
  args[0] = "su #{options.delete(:as)} -c '#{args[0].gsub("'","'\"'\"'")}'"
  sudo_without_override *args.push(options), &block
end

#sudo_su_to(*args, &block) ⇒ Object

Extremely helpful if you only have permission to: sudo su - SOMEUSER



98
99
100
101
102
103
104
105
106
# File 'lib/capsaicin/invocation.rb', line 98

def sudo_su_to(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:as] ||= fetch(:runner, nil)
  return sudo_as(options, &block) if args.empty?   # compatibility with capistrano's +sudo+
  options[:shell] = false
  cmd     = args[0].gsub(/[$\\`"]/) { |m| "\\#{m}" }
  args[0] = "echo \"#{cmd}\" | #{sudo_without_override} su - #{options.delete(:as)}"
  run_without_override *args.push(options), &block
end

#sudo_with_override(*args, &block) ⇒ Object

Allows overriding the default behavior of sudo by setting :override_sudo_method and (optionally) :override_sudo_runner



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/capsaicin/invocation.rb', line 27

def sudo_with_override(*args, &block)
    options = Hash===args.last ? args.pop.dup : {}
    as = fetch(:override_sudo_runner, nil) and options[:as] ||= as
    args << options unless options.empty?
    if :sudo == (via = fetch(:override_sudo_method, :sudo))
      options[:via] = :run
     sudo_without_override *args, &block
    else
     send via, *args, &block
	end
end

#vcapture(*args, &block) ⇒ Object

Automatically uses the :run_method variable to run things. Equivalent to capture *args, :via=>fetch(:run_method, :run)



49
50
51
52
53
# File 'lib/capsaicin/invocation.rb', line 49

def vcapture(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:via] ||= fetch(:run_method, :run)
  capture *args.push(options), &block
end

#vrun(*args, &block) ⇒ Object

Automatically uses the :run_method variable to run things. Equivalent to invoke_command *args, :via=>fetch(:run_method, :run)



41
42
43
44
45
# File 'lib/capsaicin/invocation.rb', line 41

def vrun(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:via] ||= fetch(:run_method, :run)
  invoke_command *args.push(options), &block
end

#vstream(*args, &block) ⇒ Object

Automatically uses the :run_method variable to run things. Equivalent to stream *args, :via=>fetch(:run_method, :run)



57
58
59
60
61
# File 'lib/capsaicin/invocation.rb', line 57

def vstream(*args, &block)
  options = Hash===args.last ? args.pop.dup : {}
  options[:via] ||= fetch(:run_method, :run)
  stream *args.push(options), &block
end