110 lines
3.5 KiB
Ruby
110 lines
3.5 KiB
Ruby
module RubyQA
|
|
class Resource
|
|
# This is responsible for data gathering & parsing to provide the test-writers direct access to
|
|
# resource information without needing to manually specify them in their code.
|
|
attr_reader :name, :data
|
|
|
|
# REQUIREMENTS
|
|
# This is a Hash that is meant to serve as a way of determining if a resource needs to be
|
|
# added to a host. If a key is defined here it will be checked against the information
|
|
# configured on the host. If it does not find matching keys it will not be added
|
|
#
|
|
# If empty it will just be assumed that this resource can be added to the host.
|
|
#
|
|
# This MUST be defined in each Resourced subclass, as otherwise manager will error when
|
|
# adding this resource to host.
|
|
REQUIREMENTS = {}
|
|
|
|
|
|
def initialize (host)
|
|
@host = host
|
|
@data = Hash.new
|
|
@gather_command = ""
|
|
end
|
|
|
|
def [](key)
|
|
@data[key]
|
|
end
|
|
|
|
|
|
# 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.
|
|
|
|
# TODO: Add a discovery method for the resource to find out if the necessary utilities or
|
|
# otherwise are on the host
|
|
|
|
def gather
|
|
# Handles gathering the information from the host. Provided the developer has provided a
|
|
# @gather_command it will run that command on the remote host & provide the output to the
|
|
# user/dev implemented parse command. This can be overwritten by the sublcass to make it all in one in cases
|
|
# where something needs to be done differently
|
|
if not @gather_command.empty?
|
|
output = @host.exec(@gather_command)
|
|
parse(output)
|
|
else
|
|
raise "@gather_command was not defined on Resource[#{@name}]"
|
|
end
|
|
end
|
|
|
|
def parse(output)
|
|
# Subclass Implemented method that is supposed to handle parsing the gathered data on the remote node.
|
|
raise "parse not yet implemented on Resource[#{@name}]"
|
|
end
|
|
|
|
def self.all_resources
|
|
## This will allow me to iterate through all subclasses without
|
|
## having to manually define them elsewhere
|
|
ObjectSpace.each_object(Class).select{|klass| klass < self}
|
|
end
|
|
end
|
|
|
|
|
|
## Gathers the facts from the remote machine via the facter utility
|
|
class Facts < Resource
|
|
require 'json'
|
|
REQUIREMENTS = {}
|
|
def initialize (host)
|
|
super host
|
|
@name = 'facts'
|
|
@gather_command = "sudo puppet facts"
|
|
end
|
|
|
|
def parse (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
|
|
|
|
class DRDB < Resource
|
|
# Handles gathering DRBD related information from the drbdadm utility.
|
|
|
|
REQUIREMENTS = {
|
|
:cluster => true
|
|
}
|
|
|
|
def initialize (host)
|
|
super host
|
|
@name = 'facts'
|
|
@gather_command = "sudo drbdadm"
|
|
end
|
|
|
|
# TODO: Add a XML parser to requires & have the parse method load the config from the `sudo drbdadm dump-xml`
|
|
# command
|
|
|
|
def parse (output)
|
|
@data = output
|
|
end
|
|
end
|
|
|
|
end
|