7ce3a26714cdf4fc6ed08c224532ed4a923ca30c
[packages/precise/mcollective.git] / lib / mcollective / config.rb
1 module MCollective
2   # A pretty sucky config class, ripe for refactoring/improving
3   class Config
4     include Singleton
5
6     attr_accessor :mode
7
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
18
19     def initialize
20       @configured = false
21     end
22
23     def loadconfig(configfile)
24       set_config_defaults(configfile)
25
26       if File.exists?(configfile)
27         File.readlines(configfile).each do |line|
28
29           # strip blank spaces, tabs etc off the end of all lines
30           line.gsub!(/\s*$/, "")
31
32           unless line =~ /^#|^$/
33             if (line =~ /(.+?)\s*=\s*(.+)/)
34               key = $1
35               val = $2
36
37               case key
38                 when "registration"
39                   @registration = val.capitalize
40                 when "registration_collective"
41                   @registration_collective = val
42                 when "registerinterval"
43                   @registerinterval = val.to_i
44                 when "collectives"
45                   @collectives = val.split(",").map {|c| c.strip}
46                 when "main_collective"
47                   @main_collective = val
48                 when "logfile"
49                   @logfile = val
50                 when "keeplogs"
51                   @keeplogs = val.to_i
52                 when "max_log_size"
53                   @max_log_size = val.to_i
54                 when "loglevel"
55                   @loglevel = val
56                 when "logfacility"
57                   @logfacility = val
58                 when "libdir"
59                   paths = val.split(File::PATH_SEPARATOR)
60                   paths.each do |path|
61                     raise("libdir paths should be absolute paths but '%s' is relative" % path) unless Util.absolute_path?(path)
62
63                     @libdir << path
64                     unless $LOAD_PATH.include?(path)
65                       $LOAD_PATH << path
66                     end
67                   end
68                 when "identity"
69                   @identity = val
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
74                 when "color"
75                   val =~ /^1|y/i ? @color = true : @color = false
76                 when "daemonize"
77                   val =~ /^1|y/i ? @daemonize = true : @daemonize = false
78                 when "securityprovider"
79                   @securityprovider = val.capitalize
80                 when "factsource"
81                   @factsource = val.capitalize
82                 when "connector"
83                   @connector = val.capitalize
84                 when "classesfile"
85                   @classesfile = val
86                 when /^plugin.(.+)$/
87                   @pluginconf[$1] = val
88                 when "rpcaudit"
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
98                 when "rpclimitmethod"
99                   @rpclimitmethod = val.to_sym
100                 when "logger_type"
101                   @logger_type = val
102                 when "fact_cache_time"
103                   @fact_cache_time = val.to_i
104                 when "ssl_cipher"
105                   @ssl_cipher = val
106                 when "ttl"
107                   @ttl = 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
114                 else
115                   raise("Unknown config parameter #{key}")
116               end
117             end
118           end
119         end
120
121         raise('The %s config file does not specify a libdir setting, cannot continue' % configfile) if @libdir.empty?
122
123         I18n.load_path = Dir[File.expand_path(File.join(File.dirname(__FILE__), "locales", "*.yml"))]
124         I18n.locale = :en
125
126         read_plugin_config_dir("#{@configdir}/plugin.d")
127
128         raise 'Identities can only match /\w\.\-/' unless @identity.match(/^[\w\.\-]+$/)
129
130         @configured = true
131
132         @libdir.each {|dir| Log.warn("Cannot find libdir: #{dir}") unless File.directory?(dir)}
133
134         if @logger_type == "syslog"
135           raise "The sylog logger is not usable on the Windows platform" if Util.windows?
136         end
137
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}
144
145         Log.logmsg(:PLMC1, "The Marionette Collective version %{version} started by %{name} using config file %{config}", :info, :version => MCollective::VERSION, :name => $0, :config => configfile)
146       else
147         raise("Cannot find config file '#{configfile}'")
148       end
149     end
150
151     def set_config_defaults(configfile)
152       @stomp = Hash.new
153       @subscribe = Array.new
154       @pluginconf = Hash.new
155       @connector = "activemq"
156       @securityprovider = "Psk"
157       @factsource = "Yaml"
158       @identity = Socket.gethostname
159       @registration = "Agentlist"
160       @registerinterval = 0
161       @registration_collective = nil
162       @classesfile = "/var/lib/puppet/state/classes.txt"
163       @rpcaudit = false
164       @rpcauditprovider = ""
165       @rpcauthorization = false
166       @rpcauthprovider = ""
167       @configdir = File.dirname(configfile)
168       @color = !Util.windows?
169       @configfile = configfile
170       @logger_type = "file"
171       @keeplogs = 5
172       @max_log_size = 2097152
173       @rpclimitmethod = :first
174       @libdir = Array.new
175       @fact_cache_time = 300
176       @loglevel = "info"
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 = []
185       @ttl = 60
186       @mode = :client
187
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
190       # not exist
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)
194     end
195
196     def read_plugin_config_dir(dir)
197       return unless File.directory?(dir)
198
199       Dir.new(dir).each do |pluginconfigfile|
200         next unless pluginconfigfile =~ /^([\w]+).cfg$/
201
202         plugin = $1
203         File.open("#{dir}/#{pluginconfigfile}", "r").each do |line|
204           # strip blank lines
205           line.gsub!(/\s*$/, "")
206           next if line =~ /^#|^$/
207           if (line =~ /(.+?)\s*=\s*(.+)/)
208             key = $1
209             val = $2
210             @pluginconf["#{plugin}.#{key}"] = val
211           end
212         end
213       end
214     end
215   end
216 end