Implemented new runner & dealt with issues regarding facts resource

This commit is contained in:
Tristan Ancelet 2024-07-17 13:13:57 -04:00
parent 08e132a4d2
commit fc56af0621
3 changed files with 63 additions and 9 deletions

View File

@ -22,6 +22,11 @@ module RubyQA
@gather_command = "" @gather_command = ""
end end
def [](key)
@data[key]
end
# TODO: Create validate_host method to replace REQUIREMENTS for a bit more flexiblity on # TODO: Create validate_host method to replace REQUIREMENTS for a bit more flexiblity on
# determining if a host is valid for this resource to be added to it. # determining if a host is valid for this resource to be added to it.
@ -57,16 +62,26 @@ module RubyQA
## Gathers the facts from the remote machine via the facter utility ## Gathers the facts from the remote machine via the facter utility
class Facts < Resource class Facts < Resource
require 'json' require 'json'
REQUIREMENTS = {} REQUIREMENTS = {}
def initialize (host) def initialize (host)
super host super host
@name = 'facts' @name = 'facts'
@gather_command = "facter -j" @gather_command = "sudo puppet facts"
end end
def parse (output) def parse (output)
@data = JSON.load(output)
# TODO: Find a better way of doing this.
## Only reason this exists is becuase when doing the command over telnet some extra special characters get added
## to the beginning of the opening bracket. Only quick way I found to remove it is to just split, reasssign
## & join it back into a string.
if @host.client.is_a? Telnet_Runner
output_lines = output.split(/\n/)
output_lines[0]='{'
output = output_lines.join("\n")
end
@data = JSON.parse(output)
end end
end end
@ -91,5 +106,4 @@ module RubyQA
end end
end end
end end

View File

@ -39,7 +39,7 @@ module RubyQA
if @data[:password].empty? if @data[:password].empty?
raise "Password was not provided for host" raise "Password was not provided for host"
end end
new_command = <<EOT sudo_command = <<EOT
if [[ $UID -ne 0 ]]; then if [[ $UID -ne 0 ]]; then
command_file=/tmp/sudo-command-$(date +%Y-%m-%d_%H-%M) command_file=/tmp/sudo-command-$(date +%Y-%m-%d_%H-%M)
cat >$command_file <<EOF cat >$command_file <<EOF
@ -49,13 +49,15 @@ echo "#{@data[:password]}" | sudo -S /bin/bash $command_file 2>/dev/null
rm $command_file rm $command_file
else else
#{command} #{command}
fi fi > sudo-output
EOT EOT
run_command(new_command) run_command(sudo_command)
run_command('cat sudo-output')
end end
end end
require 'net/ssh'
class SSH_Runner < CommandRunner class SSH_Runner < CommandRunner
def initialize_runner def initialize_runner
begin begin
@ -73,6 +75,44 @@ EOT
def run_command(command) def run_command(command)
@client.exec!(command).strip @client.exec!(command).strip
end end
end
class Telnet_Runner < CommandRunner
require 'net/ssh/telnet'
LOGIN_HANDLER=nil
TELNET_OPTS={
"Timeout" => 10,
"Prompt" => /[$:#]/
}
def initialize_runner
begin
@client = Net::SSH::Telnet.new( "Host" => @data[:ip], "Username" => @data[:user], "Password" => @data[:password])
# Since this is telnet, it doesn't filter out command sequences, so we have to set the PS1/prompt to something
# that is easily matched for filtering purposes
@client.cmd({"String" => "PS1='<filter_line>'; PROMPT_COMMANDS='';", "Match" => /filter_line/})
rescue SocketError
raise "Failed to make ssh client for #{@data[:hostname]}"
end
end
def run_command(command)
output = ""
## Doing intitial command (doesn't send white-space/newline)
lines = Array.new
filter_regex=/(^|#{command}|^\n$)/
output = @client.cmd({"String" => command, "Waittime" => 100, "Match" => /filter_line/})
# Have to clean output since telnet session will return all output, both stdin & stdout.
output = output.split(/\n/).reject do |line|
line.match?(/filter_line/) || line.match(/#{command}/)
end.join("\n")
output
end
end end
end end

View File

@ -1,6 +1,6 @@
RubyQA Test Report RubyQA Test Report
QA Date: <%= Time.now.strftime("%Y/%m/%d %l:%M:%S %Z") %> QA Date: <%= Time.now.strftime("%Y/%m/%d %l:%M:%S %p %Z") %>
QA Runner: <%= ENV['USER'] %> QA Runner: <%= ENV['USER'] %>
QA Tests: <%= Manager.tests.count %> QA Tests: <%= Manager.tests.count %>
<% Manager.tests.each do |test| -%> <% Manager.tests.each do |test| -%>
@ -8,7 +8,7 @@ QA Tests: <%= Manager.tests.count %>
<% end -%> <% end -%>
QA Hosts: QA Hosts:
<% Manager.hosts.each do |host| -%> <% Manager.hosts.each do |host| -%>
- <%= host.name %> (<%= host.data[:ip] %>) - <%= host.name %> (<%= host[:ip] %>)
<% end -%> <% end -%>
############### ###############