module PDO::Host
Public Class Methods
load_file(def_file=nil)
click to toggle source
# File lib/pdo/host.rb, line 20 def self.load_file(def_file=nil) if def_file then files = [ def_file ] else files = [ "/etc/pdo/pdo.yaml", "#{ENV['HOME']}/.pdo/pdo.yaml", ] end hosts = {} files.each do |f| begin hosts.update(YAML::load_file(f)) rescue => ex logger.warn { "#{ex.class}: #{ex.message}" } logger.debug { ex.backtrace.join "\n" } end end return @@host_hash = hosts end
recursive_level()
click to toggle source
# File lib/pdo/host.rb, line 16 def self.recursive_level @@recursive_level end
recursive_level=(level)
click to toggle source
# File lib/pdo/host.rb, line 13 def self.recursive_level=(level) @@recursive_level = level end
Public Instance Methods
get_hosts(group, step)
click to toggle source
# File lib/pdo/host.rb, line 107 def get_hosts(group, step) hosts = [] hosts += expand_group(group, 0) hosts.uniq! hosts = stepping hosts, step return hosts end
get_sub_groups(group)
click to toggle source
# File lib/pdo/host.rb, line 150 def get_sub_groups(group) keys = group.gsub(/^\//, '').split('/') pointer = @@host_hash keys.each do |k| begin if pointer.key? k then pointer = pointer[k] else # if any part of the given group name is not a key in the # host hash, then return an empty array. return [ ] end rescue => ex logger.warn { "failed to get hash value for #{group.inspect}. "\ 'maybe a wrong key is specifid?' } logger.debug { ex.backtrace.join "\n" } return [] end end # return empty array if the group is empty. return [] if pointer.nil? if pointer.is_a? Hash then return pointer.keys elsif pointer.is_a? Array then return pointer else logger.fatal "I don't know how to handle this. "\ 'possibly an error in the yaml file.' exit 1 end end
show_hosts()
click to toggle source
# File lib/pdo/host.rb, line 146 def show_hosts pp @@host_hash end
stepping(hosts, step)
click to toggle source
# File lib/pdo/host.rb, line 116 def stepping(hosts, step) unless hosts.is_a? Array or step.is_a? Array then logger.warn "both hosts or step should be array." return nil end start, stride = step[0].to_i, step[1].to_i # if stride > 0, stepping forward # if stride < 0, stepping backward # if stride == 0, no stepping # if start or stride makes no sense, no stepping either index = case when stride > 0 then (start-1...hosts.size).step(stride).to_a when stride < 0 then (-start+1..0).step(-stride).to_a.map {|x| -x} else [] end unless index.empty? then alist = [] index.each do |i| alist << hosts[i] end hosts = alist end return hosts end
Private Instance Methods
expand_group(group, level)
click to toggle source
# File lib/pdo/host.rb, line 40 def expand_group(group, level) # given a group, recursively expand it into a list of hosts. # if the group can't expand to a list of hosts, return that group. # if there are exception expanding the group, return an empty list. hosts = [] level += 1 if level > @@recursive_level then logger.warn "circle detected in the host definition file." return [] end # get the hash value keys = group.gsub(/^\//,'').split('/') list_of_hosts = @@host_hash keys.each do |k| begin if list_of_hosts.key? k then list_of_hosts = list_of_hosts[k] else # if any part of the group name is not a key in the # host hash, then return the group. return [ group ] end rescue => ex logger.warn { "failed to get hash value for #{group.inspect}. "\ 'possibly an error in the yaml file.' } logger.debug { ex.backtrace.join "\n" } return [] end end # return empty array if the group is empty. return [] if list_of_hosts.nil? if list_of_hosts.is_a? Hash then list_of_hosts.keys.each do |key| hosts += expand_group("#{group}/#{key}".squeeze('/'), level) end elsif list_of_hosts.is_a? Array then list_of_hosts.each do |h| if not h.is_a? String then logger.warn { "invalid host or group format: #{h.inspect}\n"\ 'possibly an error in the yaml file.' } next end if h.include? "/" then hosts += expand_group(h, level) else hosts << h end end else logger.fatal "I don't know how to handle this. "\ 'possibly an error in the yaml file.' exit 1 end return hosts end