2 # A pretty sucky config class, ripe for refactoring/improving
8 attr_reader :topicprefix, :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, :topicsep, :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 :queueprefix, :default_discovery_method, :default_discovery_options
23 def loadconfig(configfile)
24 set_config_defaults(configfile)
26 if File.exists?(configfile)
27 File.open(configfile, "r").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*(.+)/)
41 @registration = val.capitalize
42 when "registration_collective"
43 @registration_collective = val
44 when "registerinterval"
45 @registerinterval = val.to_i
47 @collectives = val.split(",").map {|c| c.strip}
48 when "main_collective"
49 @main_collective = val
59 @max_log_size = val.to_i
65 paths = val.split(File::PATH_SEPARATOR)
67 raise("libdir paths should be absolute paths but '%s' is relative" % path) unless Util.absolute_path?(path)
70 unless $LOAD_PATH.include?(path)
76 when "direct_addressing"
77 val =~ /^1|y/i ? @direct_addressing = true : @direct_addressing = false
78 when "direct_addressing_threshold"
79 @direct_addressing_threshold = val.to_i
81 val =~ /^1|y/i ? @color = true : @color = false
83 val =~ /^1|y/i ? @daemonize = true : @daemonize = false
84 when "securityprovider"
85 @securityprovider = val.capitalize
87 @factsource = val.capitalize
89 @connector = val.capitalize
95 val =~ /^1|y/i ? @rpcaudit = true : @rpcaudit = false
96 when "rpcauditprovider"
97 @rpcauditprovider = val.capitalize
98 when "rpcauthorization"
99 val =~ /^1|y/i ? @rpcauthorization = true : @rpcauthorization = false
100 when "rpcauthprovider"
101 @rpcauthprovider = val.capitalize
102 when "rpchelptemplate"
103 @rpchelptemplate = val
104 when "rpclimitmethod"
105 @rpclimitmethod = val.to_sym
108 when "fact_cache_time"
109 @fact_cache_time = val.to_i
114 when "helptemplatedir"
115 @helptemplatedir = val
116 when "default_discovery_options"
117 @default_discovery_options << val
118 when "default_discovery_method"
119 @default_discovery_method = val
121 raise("Unknown config parameter #{key}")
127 I18n.load_path = Dir[File.expand_path(File.join(File.dirname(__FILE__), "locales", "*.yml"))]
130 read_plugin_config_dir("#{@configdir}/plugin.d")
132 raise 'Identities can only match /\w\.\-/' unless @identity.match(/^[\w\.\-]+$/)
136 @libdir.each {|dir| Log.warn("Cannot find libdir: #{dir}") unless File.directory?(dir)}
138 if @logger_type == "syslog"
139 raise "The sylog logger is not usable on the Windows platform" if Util.windows?
142 PluginManager.loadclass("Mcollective::Facts::#{@factsource}_facts")
143 PluginManager.loadclass("Mcollective::Connector::#{@connector}")
144 PluginManager.loadclass("Mcollective::Security::#{@securityprovider}")
145 PluginManager.loadclass("Mcollective::Registration::#{@registration}")
146 PluginManager.loadclass("Mcollective::Audit::#{@rpcauditprovider}") if @rpcaudit
147 PluginManager << {:type => "global_stats", :class => RunnerStats.new}
149 Log.logmsg(:PLMC1, "The Marionette Collective version %{version} started by %{name} using config file %{config}", :info, :version => MCollective::VERSION, :name => $0, :config => configfile)
151 raise("Cannot find config file '#{configfile}'")
155 def set_config_defaults(configfile)
157 @subscribe = Array.new
158 @pluginconf = Hash.new
159 @connector = "activemq"
160 @securityprovider = "Psk"
162 @identity = Socket.gethostname
163 @registration = "Agentlist"
164 @registerinterval = 0
165 @registration_collective = nil
167 @topicprefix = "/topic/"
168 @queueprefix = "/queue/"
169 @classesfile = "/var/lib/puppet/state/classes.txt"
171 @rpcauditprovider = ""
172 @rpcauthorization = false
173 @rpcauthprovider = ""
174 @configdir = File.dirname(configfile)
175 @color = !Util.windows?
176 @configfile = configfile
177 @logger_type = "file"
179 @max_log_size = 2097152
180 @rpclimitmethod = :first
182 @fact_cache_time = 300
184 @logfacility = "user"
185 @collectives = ["mcollective"]
186 @main_collective = @collectives.first
187 @ssl_cipher = "aes-256-cbc"
188 @direct_addressing = false
189 @direct_addressing_threshold = 10
190 @default_discovery_method = "mc"
191 @default_discovery_options = []
195 # look in the config dir for the template so users can provide their own and windows
196 # with odd paths will just work more often, but fall back to old behavior if it does
198 @rpchelptemplate = File.join(File.dirname(configfile), "rpc-help.erb")
199 @rpchelptemplate = "/etc/mcollective/rpc-help.erb" unless File.exists?(@rpchelptemplate)
200 @helptemplatedir = File.dirname(@rpchelptemplate)
203 def read_plugin_config_dir(dir)
204 return unless File.directory?(dir)
206 Dir.new(dir).each do |pluginconfigfile|
207 next unless pluginconfigfile =~ /^([\w]+).cfg$/
210 File.open("#{dir}/#{pluginconfigfile}", "r").each do |line|
212 line.gsub!(/\s*$/, "")
213 next if line =~ /^#|^$/
214 if (line =~ /(.+?)\s*=\s*(.+)/)
217 @pluginconf["#{plugin}.#{key}"] = val