Class: Ndo::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/ndo/host.rb

Overview

Runs a command via ssh on a host. This is initially stolen from vlad (the deployer), then rewritten and modified.

Defined Under Namespace

Classes: Accumulator, ExecutionFailure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ Host

Returns a new instance of Host.



8
9
10
# File 'lib/ndo/host.rb', line 8

def initialize(hostname)
  @name = hostname
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ndo/host.rb', line 7

def name
  @name
end

Instance Method Details

#run(command) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ndo/host.rb', line 47

def run(command)
  cmd = ['ssh', name, command].flatten
  
  process = Ndo.popen(*cmd)
  accums = [
    Accumulator.new(process.stdout), 
    Accumulator.new(process.stderr)]
  
  # Copy stdout, stderr to buffers
  loop do
    ios = [process.stdout, process.stderr]
    ready,_,_ = IO.select(ios)
    
    # Test for process closed
    break if accums.any? { |acc| acc.eof? }
    
    # Copy data
    accums.each { |acc| acc.copy_if_ready(ready) }
  end
  
  # We're done reading: prepare return value
  buffers = accums.map { |acc| acc.buffer  }
  
  process.wait 
  
  # Raise ExecutionFailure if the command failed
  unless process.success?
    status = process.status
    raise ExecutionFailure.new(
      "Command failed (#{status.inspect})", 
      *buffers
    )
  end
  
  # Return [STDOUT, STDERR] buffers
  buffers
ensure
  process.close_all
end