2 # A pretty sucky config class, ripe for refactoring/improving
8 attr_reader :daemonize, :pluginconf, :libdir, :configured
9 attr_reader :logfile, :keeplogs, :max_log_size, :loglevel, :logfacility
10 attr_reader :identity, :daemonize, :connector, :securityprovider, :factsource
11 attr_reader :registration, :registerinterval, :classesfile
12 attr_reader :rpcauditprovider, :rpcaudit, :configdir, :rpcauthprovider
13 attr_reader :rpcauthorization, :color, :configfile, :rpchelptemplate
14 attr_reader :rpclimitmethod, :logger_type, :fact_cache_time, :collectives
15 attr_reader :main_collective, :ssl_cipher, :registration_collective
16 attr_reader :direct_addressing, :direct_addressing_threshold, :ttl, :helptemplatedir
17 attr_reader :default_discovery_method, :default_discovery_options
23 def loadconfig(configfile)
24 set_config_defaults(configfile)
26 if File.exists?(configfile)
27 File.readlines(configfile).each do |line|
29 # strip blank spaces, tabs etc off the end of all lines
30 line.gsub!(/\s*$/, "")
32 unless line =~ /^#|^$/
33 if (line =~ /(.+?)\s*=\s*(.+)/)
39 @registration = val.capitalize
40 when "registration_collective"
41 @registration_collective = val
42 when "registerinterval"
43 @registerinterval = val.to_i
45 @collectives = val.split(",").map {|c| c.strip}
46 when "main_collective"
47 @main_collective = val
53 @max_log_size = val.to_i
59 paths = val.split(File::PATH_SEPARATOR)
61 raise("libdir paths should be absolute paths but '%s' is relative" % path) unless Util.absolute_path?(path)
64 unless $LOAD_PATH.include?(path)
70 when "direct_addressing"
71 @direct_addressing = false unless val =~ /^1|y/i
72 when "direct_addressing_threshold"
73 @direct_addressing_threshold = val.to_i
75 val =~ /^1|y/i ? @color = true : @color = false
77 val =~ /^1|y/i ? @daemonize = true : @daemonize = false
78 when "securityprovider"
79 @securityprovider = val.capitalize
81 @factsource = val.capitalize
83 @connector = val.capitalize
89 val =~ /^1|y/i ? @rpcaudit = true : @rpcaudit = false
90 when "rpcauditprovider"
91 @rpcauditprovider = val.capitalize
92 when "rpcauthorization"
93 val =~ /^1|y/i ? @rpcauthorization = true : @rpcauthorization = false
94 when "rpcauthprovider"
95 @rpcauthprovider = val.capitalize
96 when "rpchelptemplate"
97 @rpchelptemplate = val
99 @rpclimitmethod = val.to_sym
102 when "fact_cache_time"
103 @fact_cache_time = val.to_i
108 when "helptemplatedir"
109 @helptemplatedir = val
110 when "default_discovery_options"
111 @default_discovery_options << val
112 when "default_discovery_method"
113 @default_discovery_method = val
115 raise("Unknown config parameter #{key}")
121 raise('The %s config file does not specify a libdir setting, cannot continue' % configfile) if @libdir.empty?
123 I18n.load_path = Dir[File.expand_path(File.join(File.dirname(__FILE__), "locales", "*.yml"))]
126 read_plugin_config_dir("#{@configdir}/plugin.d")
128 raise 'Identities can only match /\w\.\-/' unless @identity.match(/^[\w\.\-]+$/)
132 @libdir.each {|dir| Log.warn("Cannot find libdir: #{dir}") unless File.directory?(dir)}
134 if @logger_type == "syslog"
135 raise "The sylog logger is not usable on the Windows platform" if Util.windows?
138 PluginManager.loadclass("Mcollective::Facts::#{@factsource}_facts")
139 PluginManager.loadclass("Mcollective::Connector::#{@connector}")
140 PluginManager.loadclass("Mcollective::Security::#{@securityprovider}")
141 PluginManager.loadclass("Mcollective::Registration::#{@registration}")
142 PluginManager.loadclass("Mcollective::Audit::#{@rpcauditprovider}") if @rpcaudit
143 PluginManager << {:type => "global_stats", :class => RunnerStats.new}
145 Log.logmsg(:PLMC1, "The Marionette Collective version %{version} started by %{name} using config file %{config}", :info, :version => MCollective::VERSION, :name => $0, :config => configfile)
147 raise("Cannot find config file '#{configfile}'")
151 def set_config_defaults(configfile)
153 @subscribe = Array.new
154 @pluginconf = Hash.new
155 @connector = "activemq"
156 @securityprovider = "Psk"
158 @identity = Socket.gethostname
159 @registration = "Agentlist"
160 @registerinterval = 0
161 @registration_collective = nil
162 @classesfile = "/var/lib/puppet/state/classes.txt"
164 @rpcauditprovider = ""
165 @rpcauthorization = false
166 @rpcauthprovider = ""
167 @configdir = File.dirname(configfile)
168 @color = !Util.windows?
169 @configfile = configfile
170 @logger_type = "file"
172 @max_log_size = 2097152
173 @rpclimitmethod = :first
175 @fact_cache_time = 300
177 @logfacility = "user"
178 @collectives = ["mcollective"]
179 @main_collective = @collectives.first
180 @ssl_cipher = "aes-256-cbc"
181 @direct_addressing = true
182 @direct_addressing_threshold = 10
183 @default_discovery_method = "mc"
184 @default_discovery_options = []
188 # look in the config dir for the template so users can provide their own and windows
189 # with odd paths will just work more often, but fall back to old behavior if it does
191 @rpchelptemplate = File.join(File.dirname(configfile), "rpc-help.erb")
192 @rpchelptemplate = "/etc/mcollective/rpc-help.erb" unless File.exists?(@rpchelptemplate)
193 @helptemplatedir = File.dirname(@rpchelptemplate)
196 def read_plugin_config_dir(dir)
197 return unless File.directory?(dir)
199 Dir.new(dir).each do |pluginconfigfile|
200 next unless pluginconfigfile =~ /^([\w]+).cfg$/
203 File.open("#{dir}/#{pluginconfigfile}", "r").each do |line|
205 line.gsub!(/\s*$/, "")
206 next if line =~ /^#|^$/
207 if (line =~ /(.+?)\s*=\s*(.+)/)
210 @pluginconf["#{plugin}.#{key}"] = val