ruby-qa/lib/host.rb
2024-07-17 13:14:28 -04:00

69 lines
1.6 KiB
Ruby

module RubyQA
require 'net/ssh'
class Host
DEFAULT_HOSTDATA={
:hostname => "",
:ip => "",
:port => 22,
:user => ENV["USER"],
:password => "",
:site => "",
:cluster => false,
:sudo_password => "",
:runner => SSH_Runner
}
attr_reader :client, :data, :resources
def initialize( data = {} )
@resources = Hash.new
@data = DEFAULT_HOSTDATA.merge data
if not @data[:hostname].empty? and @data[:site].empty?
data[:site]=@data[:hostname][0,3]
end
## initialize client for usage tests
@client = @data[:runner].new(@data)
end
def exec (command)
## Send a command from the host
@client.exec(command)
end
def exec_sudo (command)
## Send a sudo command from the host
@client.exec_sudo(command)
end
def [](key)
## Provide Access to data (:ip, etc)
@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
end
end