X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=lib%2Fmcollective%2Futil.rb;h=707df666dd571642342bd79e134cdd84f0ddafb6;hb=d1f1649ba43c5cbc43c4beb2380096ba051d646a;hp=9095cc330bf0369d9bb769c2a6ec7c8d028fd6af;hpb=b87d2f4e68281062df1913440ca5753ae63314a9;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/util.rb b/lib/mcollective/util.rb index 9095cc3..707df66 100644 --- a/lib/mcollective/util.rb +++ b/lib/mcollective/util.rb @@ -1,6 +1,8 @@ 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 @@ -135,6 +137,12 @@ module MCollective "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 @@ -145,10 +153,18 @@ module MCollective 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 @@ -413,40 +429,41 @@ module MCollective # 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 @@ -454,6 +471,20 @@ module MCollective !!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) @@ -462,5 +493,15 @@ module MCollective 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