Module: Utils::LitaPuppet::SSH

Included in:
Lita::Handlers::Puppet
Defined in:
lib/utils/lita_puppet/ssh.rb

Overview

Utility methods for doing things over SSH

Instance Method Summary collapse

Instance Method Details

#calculate_result(output, exception) ⇒ Object

Only really used for the over_ssh method to determine success or failure



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/utils/lita_puppet/ssh.rb', line 6

def calculate_result(output, exception)
  result = {}
  if exception
    result[:exception] = exception
  else
    result[:exit_status] = output.exit_status
    result[:stdout] = output.stdout
    result[:stderr] = output.stderr
  end
  result
end

#over_ssh(opts = {}) ⇒ Object

Intelligently do some things over SSH



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/utils/lita_puppet/ssh.rb', line 19

def over_ssh(opts = {})
  raise 'MissingSSHHost' unless opts[:host]
  raise 'MissingSSHUser' unless opts[:user]
  opts[:timeout] ||= 300 # default to a 5 minute timeout

  remote = Rye::Box.new(
    opts[:host],
    user: opts[:user],
    auth_methods: ['publickey'],
    password_prompt: false,
    error: STDOUT # send STDERR to STDOUT for things that actually print
  )

  exception = nil

  # Getting serious about not crashing Lita...
  output = begin
    # pass our host back to the user to work with
    Timeout.timeout(opts[:timeout]) { yield remote }
  rescue Rye::Err, StandardError => e
    exception = e
  ensure
    remote.disconnect
  end

  remote = nil # Try to force the destruction of the Rye box
  calculate_result(output, exception)
end

#simple_ssh_command(host, user, command, timeout = 300) ⇒ Object

Provides a super simple way to just run a single command over ssh



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/utils/lita_puppet/ssh.rb', line 49

def simple_ssh_command(host, user, command, timeout = 300)
  over_ssh(host: host, user: user, timeout: timeout) do |server|
    server.cd '/tmp'
    # Need to use sudo
    server.enable_sudo
    # scary...
    server.disable_safe_mode

    server.execute command
  end
end