Class: ServerspecRunner::Task
- Inherits:
-
Object
- Object
- ServerspecRunner::Task
- Defined in:
- lib/serverspec_runner/task.rb
Overview
Class to run serverspec tests
Constant Summary collapse
- SSH_TIMEOUT =
10
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#ssh_port ⇒ Object
Returns the value of attribute ssh_port.
-
#sshkey ⇒ Object
Returns the value of attribute sshkey.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#initialize(tests, sub_tests, results_file) ⇒ Task
constructor
A new instance of Task.
- #run ⇒ Object
-
#unable_to_ssh? ⇒ Boolean
Check if given host/ip is reachable and we can ssh as root If not, then create empty log file for that host and return false.
Constructor Details
#initialize(tests, sub_tests, results_file) ⇒ Task
Returns a new instance of Task.
15 16 17 18 19 20 21 22 23 |
# File 'lib/serverspec_runner/task.rb', line 15 def initialize(tests, sub_tests, results_file) @obj = RSpec::Core::RakeTask.new(@host) do |_| true end @obj.pattern = tests @obj.rspec_opts = "-e #{sub_tests} --format json -o #{results_file}" @results_file = results_file end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
10 11 12 |
# File 'lib/serverspec_runner/task.rb', line 10 def host @host end |
#ssh_port ⇒ Object
Returns the value of attribute ssh_port.
13 14 15 |
# File 'lib/serverspec_runner/task.rb', line 13 def ssh_port @ssh_port end |
#sshkey ⇒ Object
Returns the value of attribute sshkey.
12 13 14 |
# File 'lib/serverspec_runner/task.rb', line 12 def sshkey @sshkey end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/serverspec_runner/task.rb', line 11 def username @username end |
Instance Method Details
#run ⇒ Object
38 39 40 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 |
# File 'lib/serverspec_runner/task.rb', line 38 def run pstderr = STDERR.dup ftmp = Tempfile.open('eout') FileUtils.touch @results_file return if unable_to_ssh? # Run the rspec tests if defined?(@host) and defined?(@username) and defined?(@sshkey) ENV['AUDIT_HOST'] = @host ENV['AUDIT_USERNAME'] = @username ENV['AUDIT_SSHKEY'] = @sshkey ENV['AUDIT_SSH_PORT'] = @ssh_port end # 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 |
#unable_to_ssh? ⇒ Boolean
Check if given host/ip is reachable and we can ssh as root If not, then create empty log file for that host and return false.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/serverspec_runner/task.rb', line 27 def unable_to_ssh? session = Net::SSH.start( @host, @username, :port => @ssh_port, :timeout => SSH_TIMEOUT, :auth_methods => ['publickey'], :keys => [@sshkey] ) session.exec('ls') session.close() false rescue Net::SSH::AuthenticationFailed, Net::SSH::ConnectionTimeout, Net::SSH::Timeout, Net::SSH::Exception, Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e p e write_error_to_results_file e. true end |