Module | MCollective::Util |
In: |
lib/mcollective/util.rb
|
Some basic utility helper methods useful to clients, agents, runner etc.
Picks a config file defaults to ~/.mcollective else /etc/mcollective/client.cfg
# File lib/mcollective/util.rb, line 117 117: def self.config_file_for_user 118: # expand_path is pretty lame, it relies on HOME environment 119: # which isnt't always there so just handling all exceptions 120: # here as cant find reverting to default 121: begin 122: config = File.expand_path("~/.mcollective") 123: 124: unless File.readable?(config) && File.file?(config) 125: config = "/etc/mcollective/client.cfg" 126: end 127: rescue Exception => e 128: config = "/etc/mcollective/client.cfg" 129: end 130: 131: return config 132: end
Creates a standard options hash
# File lib/mcollective/util.rb, line 135 135: def self.default_options 136: {:verbose => false, 137: :disctimeout => 2, 138: :timeout => 5, 139: :config => config_file_for_user, 140: :filter => empty_filter} 141: end
Creates an empty filter
# File lib/mcollective/util.rb, line 108 108: def self.empty_filter 109: {"fact" => [], 110: "cf_class" => [], 111: "agent" => [], 112: "identity" => []} 113: end
Checks if the passed in filter is an empty one
# File lib/mcollective/util.rb, line 103 103: def self.empty_filter?(filter) 104: filter == empty_filter || filter == {} 105: end
Gets the value of a specific fact, mostly just a duplicate of MCollective::Facts.get_fact but it kind of goes with the other classes here
# File lib/mcollective/util.rb, line 49 49: def self.get_fact(fact) 50: Facts.get_fact(fact) 51: end
Finds out if this MCollective has an agent by the name passed
If the passed name starts with a / it‘s assumed to be regex and will use regex to match
# File lib/mcollective/util.rb, line 8 8: def self.has_agent?(agent) 9: agent = Regexp.new(agent.gsub("\/", "")) if agent.match("^/") 10: 11: if agent.is_a?(Regexp) 12: if Agents.agentlist.grep(agent).size > 0 13: return true 14: else 15: return false 16: end 17: else 18: return Agents.agentlist.include?(agent) 19: end 20: 21: false 22: end
Checks if this node has a configuration management class by parsing the a text file with just a list of classes, recipes, roles etc. This is ala the classes.txt from puppet.
If the passed name starts with a / it‘s assumed to be regex and will use regex to match
# File lib/mcollective/util.rb, line 30 30: def self.has_cf_class?(klass) 31: klass = Regexp.new(klass.gsub("\/", "")) if klass.match("^/") 32: cfile = Config.instance.classesfile 33: 34: Log.instance.debug("Looking for configuration management classes in #{cfile}") 35: 36: File.readlines(cfile).each do |k| 37: if klass.is_a?(Regexp) 38: return true if k.chomp.match(klass) 39: else 40: return true if k.chomp == klass 41: end 42: end 43: 44: false 45: end
Compares fact == value,
If the passed value starts with a / it‘s assumed to be regex and will use regex to match
# File lib/mcollective/util.rb, line 57 57: def self.has_fact?(fact, value, operator) 58: 59: Log.instance.debug("Comparing #{fact} #{operator} #{value}") 60: Log.instance.debug("where :fact = '#{fact}', :operator = '#{operator}', :value = '#{value}'") 61: 62: fact = Facts.get_fact(fact).clone 63: 64: if operator == '=~' 65: return true if fact.match(Regexp.new(value)) 66: 67: elsif operator == "==" 68: return true if fact == value 69: 70: elsif ['<=', '>=', '<', '>', '!='].include?(operator) 71: # Yuk - need to type cast, but to_i and to_f are overzealous 72: if value =~ /^[0-9]+$/ && fact =~ /^[0-9]+$/ 73: fact = Integer(fact) 74: value = Integer(value) 75: elsif value =~ /^[0-9]+.[0-9]+$/ && fact =~ /^[0-9]+.[0-9]+$/ 76: fact = Float(fact) 77: value = Float(value) 78: end 79: 80: return true if eval("fact #{operator} value") 81: end 82: 83: false 84: end
Checks if the configured identity matches the one supplied
If the passed name starts with a / it‘s assumed to be regex and will use regex to match
# File lib/mcollective/util.rb, line 90 90: def self.has_identity?(identity) 91: identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/") 92: 93: if identity.is_a?(Regexp) 94: return Config.instance.identity.match(identity) 95: else 96: return true if Config.instance.identity == identity 97: end 98: 99: false 100: end
Wrapper around PluginManager.loadclass
# File lib/mcollective/util.rb, line 153 153: def self.loadclass(klass) 154: PluginManager.loadclass(klass) 155: end
Constructs the full target name based on topicprefix and topicsep config options
# File lib/mcollective/util.rb, line 144 144: def self.make_target(agent, type) 145: config = Config.instance 146: 147: raise("Uknown target type #{type}") unless type == :command || type == :reply 148: 149: [config.topicprefix, agent, type].join(config.topicsep) 150: end
Parse a fact filter string like foo=bar into the tuple hash thats needed
# File lib/mcollective/util.rb, line 158 158: def self.parse_fact_string(fact) 159: if fact =~ /^([^ ]+?)[ ]*=>[ ]*(.+)/ 160: return {:fact => $1, :value => $2, :operator => '>=' } 161: elsif fact =~ /^([^ ]+?)[ ]*=<[ ]*(.+)/ 162: return {:fact => $1, :value => $2, :operator => '<=' } 163: elsif fact =~ /^([^ ]+?)[ ]*(<=|>=|<|>|!=|==|=~)[ ]*(.+)/ 164: return {:fact => $1, :value => $3, :operator => $2 } 165: elsif fact =~ /^(.+?)[ ]*=[ ]*\/(.+)\/$/ 166: return {:fact => $1, :value => $2, :operator => '=~' } 167: elsif fact =~ /^([^= ]+?)[ ]*=[ ]*(.+)/ 168: return {:fact => $1, :value => $2, :operator => '==' } 169: end 170: 171: return false 172: end