Class: SpecInfra::Backend::Ssh

Inherits:
Exec
  • Object
show all
Defined in:
lib/specinfra/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, #run_with_no_ruby_environment

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 SpecInfra::Backend::Base

Instance Method Details

#add_pre_command(cmd) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/specinfra/backend/ssh.rb', line 33

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

#build_command(cmd) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/specinfra/backend/ssh.rb', line 21

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

#copy_file(from, to) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/specinfra/backend/ssh.rb', line 44

def copy_file(from, to)
  scp = SpecInfra.configuration.scp
  begin
    scp.upload!(from, to)
  rescue => e
    return false
  end
  true
end

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



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

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

  ret[:stdout].gsub!(/\r\n/, "\n")

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

  CommandResult.new ret
end