Class: NoradSpecRunner::RemoteTask
- 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
-
#cisco_enable_pw ⇒ Object
readonly
Returns the value of attribute cisco_enable_pw.
-
#disable_sudo ⇒ Object
readonly
Returns the value of attribute disable_sudo.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#results_file ⇒ Object
readonly
Returns the value of attribute results_file.
-
#ssh_port ⇒ Object
readonly
Returns the value of attribute ssh_port.
-
#sshkey ⇒ Object
readonly
Returns the value of attribute sshkey.
-
#tests_parent_dir ⇒ Object
readonly
Returns the value of attribute tests_parent_dir.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize(encoded_key, options) ⇒ RemoteTask
constructor
A new instance of RemoteTask.
- #run ⇒ Object
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, ) @host = .fetch(:host) @sshkey = .fetch(:sshkey) @cisco_enable_pw = .fetch(:cisco_enable_pw, '') @disable_sudo = .fetch(:disable_sudo, 'true') # Decode the key and store File.open(@sshkey, "w") do |f| f.write Base64.decode64(encoded_key) end @ssh_port = .fetch(:port, nil) @username = .fetch(:username, nil) @tests_parent_dir = ENV.fetch('TESTS_PARENT_DIR', '/') tests = .fetch(:tests) detect_os = .fetch(:detect_os, false) @obj = RSpec::Core::RakeTask.new(@host) do |_| true end @results_file = .fetch(:results_file) @obj.pattern = detect_os ? autodetect_test_pattern(tests) : tests @obj.rspec_opts = build_rspec_opts() rescue Exception => e write_error_to_results_file e. end |
Instance Attribute Details
#cisco_enable_pw ⇒ Object (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_sudo ⇒ Object (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 |
#host ⇒ Object (readonly)
Returns the value of attribute host.
14 15 16 |
# File 'lib/norad_spec_runner/remote_task.rb', line 14 def host @host end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
14 15 16 |
# File 'lib/norad_spec_runner/remote_task.rb', line 14 def obj @obj end |
#platform ⇒ Object (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_file ⇒ Object (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_port ⇒ Object (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 |
#sshkey ⇒ Object (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_dir ⇒ Object (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 |
#username ⇒ Object (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
#run ⇒ Object
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. ensure STDERR.reopen pstderr end |