Module | MCollective::Util |
In: |
lib/mcollective/util.rb
|
Some basic utility helper methods useful to clients, agents, runner etc.
Creates an empty filter
# File lib/mcollective/util.rb, line 91 91: def self.empty_filter 92: {"fact" => [], 93: "cf_class" => [], 94: "agent" => [], 95: "identity" => []} 96: end
Checks if the passed in filter is an empty one
# File lib/mcollective/util.rb, line 86 86: def self.empty_filter?(filter) 87: filter == empty_filter || filter == {} 88: 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) 58: value = Regexp.new(value.gsub("\/", "")) if value.match("^/") 59: 60: if value.is_a?(Regexp) 61: return true if Facts.get_fact(fact).match(value) 62: else 63: return true if Facts.get_fact(fact) == value 64: end 65: 66: false 67: 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 73 73: def self.has_identity?(identity) 74: identity = Regexp.new(identity.gsub("\/", "")) if identity.match("^/") 75: 76: if identity.is_a?(Regexp) 77: return Config.instance.identity.match(identity) 78: else 79: return true if Config.instance.identity == identity 80: end 81: 82: false 83: end
Wrapper around PluginManager.loadclass
# File lib/mcollective/util.rb, line 108 108: def self.loadclass(klass) 109: PluginManager.loadclass(klass) 110: end
Constructs the full target name based on topicprefix and topicsep config options
# File lib/mcollective/util.rb, line 99 99: def self.make_target(agent, type) 100: config = Config.instance 101: 102: raise("Uknown target type #{type}") unless type == :command || type == :reply 103: 104: [config.topicprefix, agent, type].join(config.topicsep) 105: end