ruby-qa/lib/rubyqa/host.rb

68 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2024-06-09 22:16:20 -04:00
module RubyQA
class Host
DEFAULT_HOSTDATA={
:hostname => "",
:ip => "",
:port => 22,
2024-06-28 21:13:45 -04:00
:user => ENV["USER"],
2024-06-09 22:16:20 -04:00
:password => "",
:site => "",
:cluster => false,
:sudo_password => "",
2024-06-29 15:00:22 -04:00
:runner => SSH_Runner
2024-06-09 22:16:20 -04:00
}
attr_reader :client, :data, :resources
def initialize( data = {} )
@resources = Hash.new
@data = DEFAULT_HOSTDATA.merge data
2024-07-17 13:14:28 -04:00
if not @data[:hostname].empty? and @data[:site].empty?
data[:site]=@data[:hostname][0,3]
end
2024-06-09 22:16:20 -04:00
## initialize client for usage tests
2024-06-29 15:00:22 -04:00
@client = @data[:runner].new(@data)
2024-06-09 22:16:20 -04:00
end
2024-06-29 15:00:22 -04:00
def exec (command)
2024-07-17 13:14:28 -04:00
## Send a command from the host
2024-06-29 15:00:22 -04:00
@client.exec(command)
2024-06-09 22:16:20 -04:00
end
2024-06-29 15:00:22 -04:00
def exec_sudo (command)
2024-07-17 13:14:28 -04:00
## Send a sudo command from the host
2024-06-29 15:00:22 -04:00
@client.exec_sudo(command)
2024-06-09 22:16:20 -04:00
end
def [](key)
2024-07-17 13:14:28 -04:00
## Provide Access to data (:ip, etc)
2024-06-09 22:16:20 -04:00
@data[key]
end
def name
if @data[:hostname] != ""
2024-12-11 11:25:13 -05:00
@data[:hostname]
elsif @resources['facts'].data['hostname'] != ""
@resources['facts'].data['hostname']
2024-06-09 22:16:20 -04:00
else
2024-12-11 11:25:13 -05:00
self.exec('hostname')
2024-06-09 22:16:20 -04:00
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