Created & Implemented Runner classes
This commit is contained in:
		
							
								
								
									
										44
									
								
								lib/host.rb
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								lib/host.rb
									
									
									
									
									
								
							@@ -10,6 +10,7 @@ module RubyQA
 | 
			
		||||
      :site          => "",
 | 
			
		||||
      :cluster       => false,
 | 
			
		||||
      :sudo_password => "",
 | 
			
		||||
      :runner        => SSH_Runner
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    attr_accessor *DEFAULT_HOSTDATA.keys
 | 
			
		||||
@@ -20,28 +21,16 @@ module RubyQA
 | 
			
		||||
 | 
			
		||||
      @data = DEFAULT_HOSTDATA.merge data
 | 
			
		||||
 | 
			
		||||
      ## Verifying the (Needed) variables are set to valid values (and/or can be resolved to valid values)
 | 
			
		||||
      verify_data
 | 
			
		||||
 | 
			
		||||
      ## initialize client for usage tests
 | 
			
		||||
      init_client
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def init_client
 | 
			
		||||
      begin 
 | 
			
		||||
        if @data[:password]
 | 
			
		||||
          @client = Net::SSH.start(@data[:ip], @data[:user], password: @data[:password])
 | 
			
		||||
        else
 | 
			
		||||
          @client = Net::SSH.start(@data[:ip], @data[:user])
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
      rescue SocketError
 | 
			
		||||
        raise "Failed to make ssh client for #{@data[:hostname]}"
 | 
			
		||||
      end
 | 
			
		||||
      @client = @data[:runner].new(@data)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def exec (command)
 | 
			
		||||
      @client.exec!(command)
 | 
			
		||||
      @client.exec(command)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def exec_sudo (command)
 | 
			
		||||
      @client.exec_sudo(command)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def [](key)
 | 
			
		||||
@@ -69,24 +58,5 @@ module RubyQA
 | 
			
		||||
    def update_resources
 | 
			
		||||
      @resources.each_value(&:gather)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def verify_data 
 | 
			
		||||
      if @data[:ip].empty?
 | 
			
		||||
        if not @data[:hostname].empty?
 | 
			
		||||
          begin
 | 
			
		||||
            @data[:ip] = Resolv.getaddress(@data[:hostname])
 | 
			
		||||
          rescue Resolv::ResolvError => error
 | 
			
		||||
            raise "Host: Hostname provided, but did not resolve to IP"
 | 
			
		||||
          end
 | 
			
		||||
        else
 | 
			
		||||
          raise "Host: No hostname or IP provided"
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      if @data[:user].empty? 
 | 
			
		||||
        raise ArgumentError, "Host: User not provided"
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										78
									
								
								lib/runner.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								lib/runner.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,78 @@
 | 
			
		||||
module RubyQA
 | 
			
		||||
  class CommandRunner
 | 
			
		||||
    # Handles creating an interface for running arbitrary commands on a host/target
 | 
			
		||||
    #
 | 
			
		||||
    # To basically provide a unified interface to run commands via different methods (ssh, telnet, etc)
 | 
			
		||||
    # Params:
 | 
			
		||||
    # - ip
 | 
			
		||||
    # - hostname (optional)
 | 
			
		||||
    def initialize (data)
 | 
			
		||||
      @data = data
 | 
			
		||||
      verify_data
 | 
			
		||||
      initialize_runner
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def verify_data 
 | 
			
		||||
      if @data[:ip].empty?
 | 
			
		||||
        if not @data[:hostname].empty?
 | 
			
		||||
          begin
 | 
			
		||||
            @data[:ip] = Resolv.getaddress(@data[:hostname])
 | 
			
		||||
          rescue Resolv::ResolvError => error
 | 
			
		||||
            raise "Host: Hostname provided, but did not resolve to IP"
 | 
			
		||||
          end
 | 
			
		||||
        else
 | 
			
		||||
          raise "Host: No hostname or IP provided"
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
      client_validations
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def client_validations
 | 
			
		||||
      # Handles client specific validations (port-connect test, etc)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def exec (command)
 | 
			
		||||
      run_command(command)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def exec_sudo(command)
 | 
			
		||||
      if @data[:password].empty?
 | 
			
		||||
        raise "Password was not provided for host"
 | 
			
		||||
      end
 | 
			
		||||
      new_command = <<EOT
 | 
			
		||||
if [[ $UID -ne 0 ]]; then
 | 
			
		||||
command_file=/tmp/sudo-command-$(date +%Y-%m-%d_%H-%M)
 | 
			
		||||
cat >$command_file <<EOF
 | 
			
		||||
#{command}
 | 
			
		||||
EOF
 | 
			
		||||
echo "#{@data[:password]}" | sudo -S /bin/bash $command_file 2>/dev/null
 | 
			
		||||
rm $command_file
 | 
			
		||||
else
 | 
			
		||||
#{command}
 | 
			
		||||
fi
 | 
			
		||||
EOT
 | 
			
		||||
    run_command(new_command)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  class SSH_Runner < CommandRunner
 | 
			
		||||
    def initialize_runner
 | 
			
		||||
      begin
 | 
			
		||||
        if @data[:password]
 | 
			
		||||
          @client = Net::SSH.start(@data[:ip], @data[:user], password: @data[:password])
 | 
			
		||||
        else
 | 
			
		||||
          @client = Net::SSH.start(@data[:ip], @data[:user])
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
      rescue SocketError
 | 
			
		||||
        raise "Failed to make ssh client for #{@data[:hostname]}"
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def run_command(command)
 | 
			
		||||
      @client.exec!(command).strip
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user