Class: Seijaku::SSHExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/seijaku/executors/ssh.rb

Overview

SSHExecutor connects to SSH host and runs command

Instance Method Summary collapse

Constructor Details

#initialize(raw, variables, task, ssh_hosts, ssh_settings) ⇒ SSHExecutor

Returns a new instance of SSHExecutor.

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/seijaku/executors/ssh.rb', line 9

def initialize(raw, variables, task, ssh_hosts, ssh_settings)
  @hosts = ssh_hosts
  @variables = variables.map do |key, value|
    "#{key}='#{value}'"
  end.join(" ")

  @command = ["#{@variables};", "#{raw}"]
  @task = task
  @ssh_hosts = ssh_hosts
  @ssh_settings = ssh_settings.transform_keys(&:to_sym)
  if @ssh_settings[:verify_host_key].eql?("never")
    @ssh_settings[:verify_host_key] = :never
  end

  raise SSHExecutorError, "no ssh host defined in payload", [] if ssh_hosts.nil?
end

Instance Method Details

#bastion_setupObject



45
46
47
48
49
50
51
# File 'lib/seijaku/executors/ssh.rb', line 45

def bastion_setup
  bastion_name = @ssh_hosts.hosts[@task.host].bastion
  bastion_host = @ssh_hosts.hosts[bastion_name]

  connect_str = "#{bastion_host.user}@#{bastion_host.host}:#{bastion_host.port}"
  Net::SSH::Proxy::Jump.new(connect_str)
end

#run!Object



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

def run!
  machine = @ssh_hosts.hosts[@task.host]
  result = { command: @command.join(" "), stdout: nil, stderr: nil }
  status = {}
  options = machine.bastion ? { proxy: bastion_setup, **@ssh_settings } : @ssh_settings

  ssh = Net::SSH.start(machine.host, machine.user, {port: machine.port, **@ssh_settings})

  ssh.exec!(@command.join(" "), status: status) do |_ch, stream, data|
    result[:stdout] = data.chomp if stream == :stdout
    result[:stderr] = data.chomp unless stream == :stdout
  end

  ssh.close

  result[:exit_status] = status[:exit_code]
  result
end