Class: Serverspec::Backend::Ssh

Inherits:
Exec
  • Object
show all
Defined in:
lib/serverspec/backend/ssh.rb

Instance Method Summary collapse

Methods inherited from Exec

#check_executable, #check_monitored_by_monit, #check_mounted, #check_os, #check_readable, #check_routing_table, #check_running, #check_writable

Methods inherited from Base

#check_zero, #commands, #method_missing, #set_commands, #set_example

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Serverspec::Backend::Base

Instance Method Details

#add_pre_command(cmd) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/serverspec/backend/ssh.rb', line 29

def add_pre_command(cmd)
  cmd = super(cmd)
  user = RSpec.configuration.ssh.options[:user]
  pre_command = Serverspec.configuration.pre_command
  if pre_command && user != 'root'
    cmd = "#{sudo} #{cmd}"
  end
  cmd
end

#build_command(cmd) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/serverspec/backend/ssh.rb', line 19

def build_command(cmd)
  cmd = super(cmd)
  if RSpec.configuration.ssh.options[:user] != 'root'
    cmd = "#{sudo} #{cmd}"
    cmd.gsub!(/(\&\&\s*!?\(?\s*)/, "\\1#{sudo} ")
    cmd.gsub!(/(\|\|\s*!?\(?\s*)/, "\\1#{sudo} ")
  end
  cmd
end

#run_command(cmd, opt = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/serverspec/backend/ssh.rb', line 6

def run_command(cmd, opt={})
  cmd = build_command(cmd)
  cmd = add_pre_command(cmd)
  ret = ssh_exec!(cmd)

  if @example
    @example.[:command] = cmd
    @example.[:stdout]  = ret[:stdout]
  end

  ret
end