Class: NoradSpecRunner::RemoteTask

Inherits:
Task
  • Object
show all
Defined in:
lib/norad_spec_runner/remote_task.rb

Overview

Class to run Rspec tests over SSH

Constant Summary collapse

SSH_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoded_key, options) ⇒ RemoteTask



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/norad_spec_runner/remote_task.rb', line 16

def initialize(encoded_key, options)
  @host = options.fetch(:host)
  @sshkey = options.fetch(:sshkey)
  @cisco_enable_pw = options.fetch(:cisco_enable_pw, '')
  @disable_sudo = options.fetch(:disable_sudo, 'true')
  # Decode the key and store
  File.open(@sshkey, "w") do |f|
    f.write Base64.decode64(encoded_key)
  end
  @ssh_port = options.fetch(:port, nil)
  @username = options.fetch(:username, nil)
  @tests_parent_dir = ENV.fetch('TESTS_PARENT_DIR', '/')
  tests = options.fetch(:tests)
  detect_os = options.fetch(:detect_os, false)
  @obj = RSpec::Core::RakeTask.new(@host) do |_|
    true
  end

  @results_file = options.fetch(:results_file)
  @obj.pattern = detect_os ? autodetect_test_pattern(tests) : tests
  @obj.rspec_opts = build_rspec_opts(options)
rescue Exception => e
  write_error_to_results_file e.message
end

Instance Attribute Details

#cisco_enable_pwObject (readonly)

Returns the value of attribute cisco_enable_pw.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def cisco_enable_pw
  @cisco_enable_pw
end

#disable_sudoObject (readonly)

Returns the value of attribute disable_sudo.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def disable_sudo
  @disable_sudo
end

#hostObject (readonly)

Returns the value of attribute host.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def host
  @host
end

#objObject (readonly)

Returns the value of attribute obj.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def obj
  @obj
end

#platformObject (readonly)

Returns the value of attribute platform.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def platform
  @platform
end

#results_fileObject (readonly)

Returns the value of attribute results_file.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def results_file
  @results_file
end

#ssh_portObject (readonly)

Returns the value of attribute ssh_port.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def ssh_port
  @ssh_port
end

#sshkeyObject (readonly)

Returns the value of attribute sshkey.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def sshkey
  @sshkey
end

#tests_parent_dirObject (readonly)

Returns the value of attribute tests_parent_dir.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def tests_parent_dir
  @tests_parent_dir
end

#usernameObject (readonly)

Returns the value of attribute username.



14
15
16
# File 'lib/norad_spec_runner/remote_task.rb', line 14

def username
  @username
end

Instance Method Details

#runObject



41
42
43
44
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
70
71
72
73
# File 'lib/norad_spec_runner/remote_task.rb', line 41

def run
  pstderr = STDERR.dup
  ftmp = Tempfile.open('eout')
  FileUtils.touch results_file
  return if platform.nil? && unable_to_ssh?

  ENV['AUDIT_HOST'] = host
  ENV['AUDIT_USERNAME'] = username
  ENV['AUDIT_SSHKEY'] = sshkey
  ENV['AUDIT_SSH_PORT'] = ssh_port
  ENV['AUDIT_CISCO_ENABLE_PASSWORD'] = cisco_enable_pw
  ENV['AUDIT_DISABLE_SUDO'] = disable_sudo.to_s

  # Capture STDERR for SSH related errors
  STDERR.reopen(ftmp)
  obj.run_task(true)
rescue SystemExit => e
  ftmp.rewind
  err = ftmp.read
  ftmp.close
  p err
  # We land here even on successful run (SystemExit exception), only report error if stderr is not empty
  if err =~ /Please set sudo password to Specinfra.configuration.sudo_password|set :request_pty, true/
    write_error_to_results_file "SSH user #{username} requires SUDO permission with NOPASSWD: option set."
  elsif not err.empty?
    write_error_to_results_file err
  end
rescue Exception => e
  # Unknown exception!
  write_error_to_results_file e.message
ensure
  STDERR.reopen pstderr
end