Class: MCollective::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mcollective/config.rb

Overview

A pretty sucky config class, ripe for refactoring/improving

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



22
23
24
# File 'lib/mcollective/config.rb', line 22

def initialize
  @configured = false
end

Instance Attribute Details

#activate_agentsObject (readonly)

Returns the value of attribute activate_agents.



18
19
20
# File 'lib/mcollective/config.rb', line 18

def activate_agents
  @activate_agents
end

#classesfileObject (readonly)

Returns the value of attribute classesfile.



11
12
13
# File 'lib/mcollective/config.rb', line 11

def classesfile
  @classesfile
end

#collectivesObject (readonly)

Returns the value of attribute collectives.



14
15
16
# File 'lib/mcollective/config.rb', line 14

def collectives
  @collectives
end

#colorObject (readonly)

Returns the value of attribute color.



13
14
15
# File 'lib/mcollective/config.rb', line 13

def color
  @color
end

#configdirObject (readonly)

Returns the value of attribute configdir.



12
13
14
# File 'lib/mcollective/config.rb', line 12

def configdir
  @configdir
end

#configfileObject (readonly)

Returns the value of attribute configfile.



13
14
15
# File 'lib/mcollective/config.rb', line 13

def configfile
  @configfile
end

#configuredObject (readonly)

Returns the value of attribute configured.



8
9
10
# File 'lib/mcollective/config.rb', line 8

def configured
  @configured
end

#connection_timeoutObject (readonly)

Returns the value of attribute connection_timeout.



20
21
22
# File 'lib/mcollective/config.rb', line 20

def connection_timeout
  @connection_timeout
end

#connectorObject (readonly)

Returns the value of attribute connector.



10
11
12
# File 'lib/mcollective/config.rb', line 10

def connector
  @connector
end

#daemonizeObject (readonly)

Returns the value of attribute daemonize.



8
9
10
# File 'lib/mcollective/config.rb', line 8

def daemonize
  @daemonize
end

#default_discovery_methodObject (readonly)

Returns the value of attribute default_discovery_method.



17
18
19
# File 'lib/mcollective/config.rb', line 17

def default_discovery_method
  @default_discovery_method
end

#default_discovery_optionsObject (readonly)

Returns the value of attribute default_discovery_options.



17
18
19
# File 'lib/mcollective/config.rb', line 17

def default_discovery_options
  @default_discovery_options
end

#direct_addressingObject (readonly)

Returns the value of attribute direct_addressing.



16
17
18
# File 'lib/mcollective/config.rb', line 16

def direct_addressing
  @direct_addressing
end

#direct_addressing_thresholdObject (readonly)

Returns the value of attribute direct_addressing_threshold.



16
17
18
# File 'lib/mcollective/config.rb', line 16

def direct_addressing_threshold
  @direct_addressing_threshold
end

#discovery_timeoutObject (readonly)

Returns the value of attribute discovery_timeout.



19
20
21
# File 'lib/mcollective/config.rb', line 19

def discovery_timeout
  @discovery_timeout
end

#fact_cache_timeObject (readonly)

Returns the value of attribute fact_cache_time.



14
15
16
# File 'lib/mcollective/config.rb', line 14

def fact_cache_time
  @fact_cache_time
end

#factsourceObject (readonly)

Returns the value of attribute factsource.



10
11
12
# File 'lib/mcollective/config.rb', line 10

def factsource
  @factsource
end

#identityObject (readonly)

Returns the value of attribute identity.



10
11
12
# File 'lib/mcollective/config.rb', line 10

def identity
  @identity
end

#keeplogsObject (readonly)

Returns the value of attribute keeplogs.



9
10
11
# File 'lib/mcollective/config.rb', line 9

def keeplogs
  @keeplogs
end

#logfacilityObject (readonly)

Returns the value of attribute logfacility.



9
10
11
# File 'lib/mcollective/config.rb', line 9

def logfacility
  @logfacility
end

#logfileObject (readonly)

Returns the value of attribute logfile.



9
10
11
# File 'lib/mcollective/config.rb', line 9

def logfile
  @logfile
end

#logger_typeObject (readonly)

Returns the value of attribute logger_type.



14
15
16
# File 'lib/mcollective/config.rb', line 14

def logger_type
  @logger_type
end

#loglevelObject (readonly)

Returns the value of attribute loglevel.



9
10
11
# File 'lib/mcollective/config.rb', line 9

def loglevel
  @loglevel
end

#main_collectiveObject (readonly)

Returns the value of attribute main_collective.



15
16
17
# File 'lib/mcollective/config.rb', line 15

def main_collective
  @main_collective
end

#max_log_sizeObject (readonly)

Returns the value of attribute max_log_size.



9
10
11
# File 'lib/mcollective/config.rb', line 9

def max_log_size
  @max_log_size
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/mcollective/config.rb', line 6

def mode
  @mode
end

#pluginconfObject (readonly)

Returns the value of attribute pluginconf.



8
9
10
# File 'lib/mcollective/config.rb', line 8

def pluginconf
  @pluginconf
end

#publish_timeoutObject (readonly)

Returns the value of attribute publish_timeout.



18
19
20
# File 'lib/mcollective/config.rb', line 18

def publish_timeout
  @publish_timeout
end

#registerintervalObject (readonly)

Returns the value of attribute registerinterval.



11
12
13
# File 'lib/mcollective/config.rb', line 11

def registerinterval
  @registerinterval
end

#registrationObject (readonly)

Returns the value of attribute registration.



11
12
13
# File 'lib/mcollective/config.rb', line 11

def registration
  @registration
end

#registration_collectiveObject (readonly)

Returns the value of attribute registration_collective.



15
16
17
# File 'lib/mcollective/config.rb', line 15

def registration_collective
  @registration_collective
end

#registration_splayObject (readonly)

Returns the value of attribute registration_splay.



19
20
21
# File 'lib/mcollective/config.rb', line 19

def registration_splay
  @registration_splay
end

#rpcauditObject (readonly)

Returns the value of attribute rpcaudit.



12
13
14
# File 'lib/mcollective/config.rb', line 12

def rpcaudit
  @rpcaudit
end

#rpcauditproviderObject (readonly)

Returns the value of attribute rpcauditprovider.



12
13
14
# File 'lib/mcollective/config.rb', line 12

def rpcauditprovider
  @rpcauditprovider
end

#rpcauthorizationObject (readonly)

Returns the value of attribute rpcauthorization.



13
14
15
# File 'lib/mcollective/config.rb', line 13

def rpcauthorization
  @rpcauthorization
end

#rpcauthproviderObject (readonly)

Returns the value of attribute rpcauthprovider.



12
13
14
# File 'lib/mcollective/config.rb', line 12

def rpcauthprovider
  @rpcauthprovider
end

#rpclimitmethodObject (readonly)

Returns the value of attribute rpclimitmethod.



14
15
16
# File 'lib/mcollective/config.rb', line 14

def rpclimitmethod
  @rpclimitmethod
end

#securityproviderObject (readonly)

Returns the value of attribute securityprovider.



10
11
12
# File 'lib/mcollective/config.rb', line 10

def securityprovider
  @securityprovider
end

#soft_shutdownObject (readonly)

Returns the value of attribute soft_shutdown.



18
19
20
# File 'lib/mcollective/config.rb', line 18

def soft_shutdown
  @soft_shutdown
end

#soft_shutdown_timeoutObject (readonly)

Returns the value of attribute soft_shutdown_timeout.



19
20
21
# File 'lib/mcollective/config.rb', line 19

def soft_shutdown_timeout
  @soft_shutdown_timeout
end

#ssl_cipherObject (readonly)

Returns the value of attribute ssl_cipher.



15
16
17
# File 'lib/mcollective/config.rb', line 15

def ssl_cipher
  @ssl_cipher
end

#threadedObject (readonly)

Returns the value of attribute threaded.



18
19
20
# File 'lib/mcollective/config.rb', line 18

def threaded
  @threaded
end

#ttlObject (readonly)

Returns the value of attribute ttl.



16
17
18
# File 'lib/mcollective/config.rb', line 16

def ttl
  @ttl
end

Instance Method Details

#libdirObject



217
218
219
# File 'lib/mcollective/config.rb', line 217

def libdir
  $LOAD_PATH
end

#loadconfig(configfile) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mcollective/config.rb', line 26

def loadconfig(configfile)
  set_config_defaults(configfile)

  if File.exists?(configfile)
    libdirs = []
    File.readlines(configfile).each do |line|

      # strip blank spaces, tabs etc off the end of all lines
      line.gsub!(/\s*$/, "")

      unless line =~ /^#|^$/
        if (line =~ /(.+?)\s*=\s*(.+)/)
          key = $1.strip
          val = $2

          begin
            case key
            when "registration"
              @registration = val.capitalize
            when "registration_collective"
              @registration_collective = val
            when "registerinterval"
              @registerinterval = Integer(val)
            when "registration_splay"
              @registration_splay = Util.str_to_bool(val)
            when "collectives"
              @collectives = val.split(",").map {|c| c.strip}
            when "main_collective"
              @main_collective = val
            when "logfile"
              @logfile = val
            when "keeplogs"
              @keeplogs = Integer(val)
            when "max_log_size"
              @max_log_size = Integer(val)
            when "loglevel"
              @loglevel = val
            when "logfacility"
              @logfacility = val
            when "libdir"
              paths = val.split(File::PATH_SEPARATOR)
              paths.each do |path|
                raise("libdir paths should be absolute paths but '%s' is relative" % path) unless Util.absolute_path?(path)

                libdirs << path
              end
            when "identity"
              @identity = val
            when "direct_addressing"
              @direct_addressing = Util.str_to_bool(val)
            when "direct_addressing_threshold"
              @direct_addressing_threshold = Integer(val)
            when "color"
              @color = Util.str_to_bool(val)
            when "daemonize"
              @daemonize = Util.str_to_bool(val)
            when "securityprovider"
              @securityprovider = val.capitalize
            when "factsource"
              @factsource = val.capitalize
            when "connector"
              @connector = val.capitalize
            when "classesfile"
              @classesfile = val
            when /^plugin.(.+)$/
              @pluginconf[$1] = val
            when "discovery_timeout"
              @discovery_timeout = Integer(val)
            when "publish_timeout"
              @publish_timeout = Integer(val)
            when "connection_timeout"
              @connection_timeout = Integer(val)
            when "rpcaudit"
              @rpcaudit = Util.str_to_bool(val)
            when "rpcauditprovider"
              @rpcauditprovider = val.capitalize
            when "rpcauthorization"
              @rpcauthorization = Util.str_to_bool(val)
            when "rpcauthprovider"
              @rpcauthprovider = val.capitalize
            when "rpclimitmethod"
              @rpclimitmethod = val.to_sym
            when "logger_type"
              @logger_type = val
            when "fact_cache_time"
              @fact_cache_time = Integer(val)
            when "ssl_cipher"
              @ssl_cipher = val
            when "threaded"
              @threaded = Util.str_to_bool(val)
            when "ttl"
              @ttl = Integer(val)
            when "default_discovery_options"
              @default_discovery_options << val
            when "default_discovery_method"
              @default_discovery_method = val
            when "soft_shutdown"
              @soft_shutdown = Util.str_to_bool(val)
            when "soft_shutdown_timeout"
              @soft_shutdown_timeout = Integer(val)
            when "activate_agents"
              @activate_agents = Util.str_to_bool(val)
            when "topicprefix", "topicsep", "queueprefix", "rpchelptemplate", "helptemplatedir"
              Log.warn("Use of deprecated '#{key}' option.  This option is ignored and should be removed from '#{configfile}'")
            else
              raise("Unknown config parameter '#{key}'")
            end
          rescue ArgumentError => e
            raise "Could not parse value for configuration option '#{key}' with value '#{val}'"
          end
        end
      end
    end

    read_plugin_config_dir("#{@configdir}/plugin.d")

    raise 'Identities can only match /\w\.\-/' unless @identity.match(/^[\w\.\-]+$/)

    @configured = true

    libdirs.each do |dir|
      unless File.directory?(dir)
        Log.debug("Cannot find libdir: #{dir}")
      end

      # remove the old one if it exists, we're moving it to the front
      $LOAD_PATH.reject! { |elem| elem == dir }
      $LOAD_PATH.unshift dir
    end

    if @logger_type == "syslog"
      raise "The sylog logger is not usable on the Windows platform" if Util.windows?
    end

    PluginManager.loadclass("Mcollective::Facts::#{@factsource}_facts")
    PluginManager.loadclass("Mcollective::Connector::#{@connector}")
    PluginManager.loadclass("Mcollective::Security::#{@securityprovider}")
    PluginManager.loadclass("Mcollective::Registration::#{@registration}")
    PluginManager.loadclass("Mcollective::Audit::#{@rpcauditprovider}") if @rpcaudit
    PluginManager << {:type => "global_stats", :class => RunnerStats.new}

    Log.info("The Marionette Collective version #{MCollective::VERSION} started by #{$0} using config file #{configfile}")
  else
    raise("Cannot find config file '#{configfile}'")
  end
end

#read_plugin_config_dir(dir) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/mcollective/config.rb', line 221

def read_plugin_config_dir(dir)
  return unless File.directory?(dir)

  Dir.new(dir).each do |pluginconfigfile|
    next unless pluginconfigfile =~ /^([\w]+).cfg$/

    plugin = $1
    File.open("#{dir}/#{pluginconfigfile}", "r").each do |line|
      # strip blank lines
      line.gsub!(/\s*$/, "")
      next if line =~ /^#|^$/
      if (line =~ /(.+?)\s*=\s*(.+)/)
        key = $1.strip
        val = $2
        @pluginconf["#{plugin}.#{key}"] = val
      end
    end
  end
end

#set_config_defaults(configfile) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mcollective/config.rb', line 173

def set_config_defaults(configfile)
  @stomp = Hash.new
  @subscribe = Array.new
  @pluginconf = Hash.new
  @connector = "activemq"
  @securityprovider = "Psk"
  @factsource = "Yaml"
  @identity = Socket.gethostname
  @registration = "Agentlist"
  @registerinterval = 0
  @registration_collective = nil
  @registration_splay = false
  @classesfile = "/var/lib/puppet/state/classes.txt"
  @rpcaudit = false
  @rpcauditprovider = ""
  @rpcauthorization = false
  @rpcauthprovider = ""
  @configdir = File.dirname(configfile)
  @color = !Util.windows?
  @configfile = configfile
  @logger_type = "file"
  @keeplogs = 5
  @max_log_size = 2097152
  @rpclimitmethod = :first
  @fact_cache_time = 300
  @loglevel = "info"
  @logfacility = "user"
  @collectives = ["mcollective"]
  @main_collective = @collectives.first
  @ssl_cipher = "aes-256-cbc"
  @direct_addressing = true
  @direct_addressing_threshold = 10
  @default_discovery_method = "mc"
  @default_discovery_options = []
  @ttl = 60
  @mode = :client
  @publish_timeout = 2
  @threaded = false
  @soft_shutdown = false
  @soft_shutdown_timeout = nil
  @activate_agents = true
  @connection_timeout = nil
end