Class: Caterer::Communication::SSH

Inherits:
Object
  • Object
show all
Includes:
Util::ANSIEscapeCodeRemover, Util::Retryable
Defined in:
lib/caterer/communication/ssh.rb

Instance Method Summary collapse

Methods included from Util::Retryable

#retryable

Methods included from Util::ANSIEscapeCodeRemover

#remove_ansi_escape_codes

Constructor Details

#initialize(server) ⇒ SSH

Returns a new instance of SSH.



12
13
14
15
16
# File 'lib/caterer/communication/ssh.rb', line 12

def initialize(server)
  @server     = server
  @connection = nil
  @logger     = Log4r::Logger.new("caterer::communication::ssh")
end

Instance Method Details

#execute(command, opts = {}, &block) ⇒ Object



35
36
37
38
39
# File 'lib/caterer/communication/ssh.rb', line 35

def execute(command, opts={}, &block)
  connect do |connection|
    shell_execute(connection, command, opts, &block)
  end
end

#ready?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/caterer/communication/ssh.rb', line 18

def ready?
  @logger.debug("Checking whether SSH is ready...")

  Timeout.timeout(30) do
    connect
  end

  # If we reached this point then we successfully connected
  @logger.info("SSH is ready!")
  true
rescue => e
  # The above errors represent various reasons that SSH may not be
  # ready yet. Return false.
  @logger.info("SSH not up: #{e.inspect}")
  return false
end

#sudo(command, opts = {}, &block) ⇒ Object



41
42
43
44
# File 'lib/caterer/communication/ssh.rb', line 41

def sudo(command, opts={}, &block)
  sudo = (@server.username == 'root') ? false : true
  execute(command, opts.merge({:sudo => sudo}), &block)
end

#upload(from, to) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/caterer/communication/ssh.rb', line 46

def upload(from, to)
  @logger.debug("Uploading: #{from} to #{to}")

  # Do an SCP-based upload...
  connect do |connection|
    opts = {}
    opts[:recursive] = true if from.is_a?(String) and File.directory?(from)
    scp = Net::SCP.new(connection)
    scp.upload!(from, to, opts)
  end
rescue Net::SCP::Error => e
  # If we get the exit code of 127, then this means SCP is unavailable.
  raise "scp unavailable" if e.message =~ /\(127\)/

    # Otherwise, just raise the error up
    raise
end