Class: Bcome::Ssh::CommandExec

Inherits:
Object
  • Object
show all
Defined in:
lib/objects/ssh/command_exec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands) ⇒ CommandExec

Returns a new instance of CommandExec.



7
8
9
# File 'lib/objects/ssh/command_exec.rb', line 7

def initialize(commands)
  @commands = commands
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/objects/ssh/command_exec.rb', line 5

def commands
  @commands
end

Instance Method Details

#execute!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/objects/ssh/command_exec.rb', line 25

def execute!
  @commands.each do |command|
    node = command.node
    ssh = node.ssh_driver.ssh_connection

    begin
      ssh_exec!(ssh, command)
    rescue IOError # Typically occurs after a timeout if the session has been left idle
      node.reopen_ssh_connection
      ssh = node.ssh_driver.ssh_connection
      ssh_exec!(ssh, command) # retry, once
    end

    output_append("\n(#{node.namespace})$".terminal_prompt + ">\s#{command.raw}\n")
    output_append(command.output.to_s)
  end

  print_output unless ::Bcome::Orchestrator.instance.command_output_silenced? || ::Bcome::Orchestrator.instance.tail_all_command_output?
end

#log_windowObject



15
16
17
# File 'lib/objects/ssh/command_exec.rb', line 15

def log_window
  ::Bcome::Ssh::Window.instance
end

#output_append(output_string) ⇒ Object



11
12
13
# File 'lib/objects/ssh/command_exec.rb', line 11

def output_append(output_string)
  @output_string = "#{@output_string}#{output_string}"
end


19
20
21
22
23
# File 'lib/objects/ssh/command_exec.rb', line 19

def print_output
  print "#{@output_string}\n\n"
rescue StandardError => e
  puts "Could not print #{@output_string.inspect}"
end

#ssh_exec!(ssh, command) ⇒ Object

 NON PTY (i.e no pseudo-terminal)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/objects/ssh/command_exec.rb', line 45

def ssh_exec!(ssh, command) #  NON PTY (i.e no pseudo-terminal)
  ssh.open_channel do |channel|
    channel.exec(command.raw) do |_cha, success|
      abort "FAILED: couldn't execute command (ssh.channel.exec)" unless success

      channel.on_data do |_ch, data|
        log_window.add(command.node, data) if ::Bcome::Orchestrator.instance.tail_all_command_output?
        command.stdout += data
      end

      channel.on_extended_data do |_ch, _type, data|
        command.stderr += data
      end

      channel.on_request('exit-status') do |_ch, data|
        command.exit_code = data.read_long
      end

      channel.on_request('exit-signal') do |_ch, data|
        command.exit_code = data.read_long
      end
    end
  end
  ssh.loop
end