module MCollective
# Some basic utility helper methods useful to clients, agents, runner etc.
module Util
+ extend Translatable
+
# 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
"compound" => []}
end
+ # Returns the PuppetLabs mcollective path for windows
+ def self.windows_prefix
+ require 'win32/dir'
+ prefix = File.join(Dir::COMMON_APPDATA, "PuppetLabs", "mcollective")
+ end
+
# Picks a config file defaults to ~/.mcollective
# else /etc/mcollective/client.cfg
def self.config_file_for_user
config = File.expand_path("~/.mcollective")
unless File.readable?(config) && File.file?(config)
- config = "/etc/mcollective/client.cfg"
+ if self.windows?
+ config = File.join(self.windows_prefix, "etc", "client.cfg")
+ else
+ config = "/etc/mcollective/client.cfg"
+ end
end
rescue Exception => e
- config = "/etc/mcollective/client.cfg"
+ if self.windows?
+ config = File.join(self.windows_prefix, "etc", "client.cfg")
+ else
+ config = "/etc/mcollective/client.cfg"
+ end
end
return config
# returns -1 if a < b
# returns 1 if a > b
#
- # Code originally from Puppet but refactored to a more
- # ruby style that fits in better with this code base
+ # Code originally from Puppet
def self.versioncmp(version_a, version_b)
vre = /[-.]|\d+|[^-.\d]+/
ax = version_a.scan(vre)
bx = version_b.scan(vre)
- until ax.empty? || bx.empty?
+ while (ax.length>0 && bx.length>0)
a = ax.shift
b = bx.shift
- next if a == b
- next if a == '-' && b == '-'
- return -1 if a == '-'
- return 1 if b == '-'
- next if a == '.' && b == '.'
- return -1 if a == '.'
- return 1 if b == '.'
-
- if a =~ /^[^0]\d+$/ && b =~ /^[^0]\d+$/
- return Integer(a) <=> Integer(b)
+ if( a == b ) then next
+ elsif (a == '-' && b == '-') then next
+ elsif (a == '-') then return -1
+ elsif (b == '-') then return 1
+ elsif (a == '.' && b == '.') then next
+ elsif (a == '.' ) then return -1
+ elsif (b == '.' ) then return 1
+ elsif (a =~ /^\d+$/ && b =~ /^\d+$/) then
+ if( a =~ /^0/ or b =~ /^0/ ) then
+ return a.to_s.upcase <=> b.to_s.upcase
+ end
+ return a.to_i <=> b.to_i
else
return a.upcase <=> b.upcase
end
end
- version_a <=> version_b
+ version_a <=> version_b;
end
# we should really use Pathname#absolute? but it's not in all the
# ruby versions we support and it comes down to roughly this
def self.absolute_path?(path, separator=File::SEPARATOR, alt_separator=File::ALT_SEPARATOR)
if alt_separator
- path_matcher = /^[#{Regexp.quote alt_separator}#{Regexp.quote separator}]/
+ path_matcher = /^([a-zA-Z]:){0,1}[#{Regexp.quote alt_separator}#{Regexp.quote separator}]/
else
path_matcher = /^#{Regexp.quote separator}/
end
!!path.match(path_matcher)
end
+ # Converts a string into a boolean value
+ # Strings matching 1,y,yes,true or t will return TrueClass
+ # Any other value will return FalseClass
+ def self.str_to_bool(val)
+ clean_val = val.to_s.strip
+ if clean_val =~ /^(1|yes|true|y|t)$/i
+ return true
+ elsif clean_val =~ /^(0|no|false|n|f)$/i
+ return false
+ else
+ raise_code(:PLMC42, "Cannot convert string value '%{value}' into a boolean.", :error, :value => clean_val)
+ end
+ end
+
# Looks up and interprolate the hash values into a i18n string
def self.t(msgid, args={})
if msgid.is_a?(Symbol)
I18n.t(msgid, args)
end
end
+
+ # Looks up the template directory and returns its full path
+ def self.templatepath(template_file)
+ config_dir = File.dirname(Config.instance.configfile)
+ template_path = File.join(config_dir, template_file)
+ return template_path if File.exists?(template_path)
+
+ template_path = File.join("/etc/mcollective", template_file)
+ return template_path
+ end
end
end