Class: Blower::Puff

Inherits:
Object
  • Object
show all
Defined in:
lib/blower/puff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(huff, host) ⇒ Puff

Returns a new instance of Puff.



9
10
11
12
13
14
# File 'lib/blower/puff.rb', line 9

def initialize (huff, host)
  @huff = huff
  @log = huff.log
  @host = host
  @env = {}
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



7
8
9
# File 'lib/blower/puff.rb', line 7

def env
  @env
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/blower/puff.rb', line 7

def host
  @host
end

#huffObject

Returns the value of attribute huff.



7
8
9
# File 'lib/blower/puff.rb', line 7

def huff
  @huff
end

#logObject

Returns the value of attribute log.



7
8
9
# File 'lib/blower/puff.rb', line 7

def log
  @log
end

Instance Method Details

#env2shellObject



16
17
18
19
20
21
# File 'lib/blower/puff.rb', line 16

def env2shell ()
  huff.env.merge(@env).map do |name, value|
    next unless name =~ /\A[A-Za-z\d_]+\z/
    "#{name}=#{value.shellescape}"
  end.join("\n") + "\n"
end

#shell(task) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/blower/puff.rb', line 23

def shell (task)
  Net::SSH.start(host, "root") do |ssh|
    command = File.read(task)
    status, signal = nil, nil
    ssh.open_channel do |ch|
      stdout, stderr = "", ""
      ch.exec(env2shell + command) do |_, success|
        fail "failed to execute command" unless success
        ch.on_data do |_, data|
          stdout << data
          if i = stdout.rindex(/[\n\r]/)
            data, stdout = stdout[0..i], (stdout[(i + 1)..-1] || "")
            log.log data
          end
        end
        ch.on_extended_data do |_, _, data|
          stderr << data
          if i = stderr.rindex(/[\n\r]/)
            data, stderr = stderr[0..i], (stderr[(i + 1)..-1] || "")
            log.fail data
          end
        end
        ch.on_request("exit-status") { |_, data| status = data.read_long }
        ch.on_request("exit-signal") { |_, data| signal = data.read_long }
      end
    end
    ssh.loop
    if status != 0
      fail "exit status #{status}"
    end
  end
end