module RubyQA require 'net/ssh' class Host DEFAULT_HOSTDATA={ :hostname => "", :ip => "", :port => 22, :user => ENV["USER"], :password => "", :site => "", :cluster => false, :sudo_password => "", } attr_accessor *DEFAULT_HOSTDATA.keys attr_reader :client, :data, :resources def initialize( data = {} ) @resources = Hash.new @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 end def exec (command) @client.exec!(command) end def [](key) @data[key] end def name if @data[:hostname] != "" return @data[:hostname] elsif @resources['facts'].data['networking']['hostname'] != "" return @resources['facts'].data['networking']['hostname'] else return @data[:ip] end end def add_resource (object) ## If the requirements are met by the host, add the resource if object::REQUIREMENTS.all?{|key,val| @data[key] == val} resource = object.new(self) @resources[resource.name]=resource end end 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