Skip to content

Commit

Permalink
Merge pull request #131 from ripienaar/130
Browse files Browse the repository at this point in the history
(#130) support project configurations
  • Loading branch information
ripienaar authored Jan 22, 2021
2 parents 2f8aacf + 3b64ba8 commit 191bb13
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 103 deletions.
238 changes: 135 additions & 103 deletions lib/mcollective/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,123 +17,130 @@ def initialize
@configured = false
end

def loadconfig(configfile) # rubocop:disable Metrics/MethodLength
set_config_defaults(configfile)
def parse_config_file(configfile, libdirs) # rubocop:disable Metrics/MethodLength
File.readlines(configfile).each do |line|
# strip blank spaces, tabs etc off the end of all lines
line.gsub!(/\s*$/, "")

if File.exist?(configfile)
libdirs = []
File.readlines(configfile).each do |line|
# strip blank spaces, tabs etc off the end of all lines
line.gsub!(/\s*$/, "")
next if line =~ /^#|^$/
next unless line =~ /(.+?)\s*=\s*(.+)/

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

key = $1.strip
val = $2
begin
case key
when "collectives"
@collectives = val.split(",").map(&: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)

begin
case key
when "collectives"
@collectives = val.split(",").map(&: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 "default_batch_size"
@default_batch_size = Integer(val)
when "default_batch_sleep_time"
@default_batch_sleep_time = Float(val)
else
# server config might now be choria config which will divirge from mcollective
# in time, so we only raise this error when it looks like we aren't loading
# a server config else we try our best to load as much as we can
raise("Unknown config parameter '#{key}'") unless configfile =~ /server/
libdirs << path
end
rescue ArgumentError
raise("Could not parse value for configuration option '%s' with value '%s'" % [key, val])
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 "default_batch_size"
@default_batch_size = Integer(val)
when "default_batch_sleep_time"
@default_batch_sleep_time = Float(val)
else
# server config might now be choria config which will divirge from mcollective
# in time, so we only raise this error when it looks like we aren't loading
# a server config else we try our best to load as much as we can
raise("Unknown config parameter '#{key}'") unless configfile =~ /server/
end
rescue ArgumentError
raise("Could not parse value for configuration option '%s' with value '%s'" % [key, val])
end
end
end

def loadconfig(configfile)
set_config_defaults(configfile)

if File.exist?(configfile)
libdirs = []

parse_config_file(configfile, libdirs)

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

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

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

@configured = true

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

# remove the old one if it exists, we're moving it to the front
$LOAD_PATH.reject! { |elem| elem == dir }
Expand All @@ -154,6 +161,31 @@ def loadconfig(configfile) # rubocop:disable Metrics/MethodLength
end
end

def project_root
Dir.pwd
end

def parse_project_config(libdirs)
project_config_files(project_root).each do |pfile|
parse_config_file(pfile, libdirs)
end
end

def project_config_files(path)
paths = []

path = File.expand_path(path)
parent = File.expand_path("..", path)

paths << project_config_files(parent) if parent != path

config = File.join(path, "choria.conf")

paths << config if File.exist?(config)

paths.flatten
end

def set_config_defaults(configfile) # rubocop:disable Naming/AccessorMethodName
@subscribe = []
@pluginconf = {}
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/config/client.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugin.project = 0

1 change: 1 addition & 0 deletions spec/fixtures/config/project/choria.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plugin.project = 1
3 changes: 3 additions & 0 deletions spec/fixtures/config/project/project2/choria.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin.project = 2
loglevel = debug

Loading

0 comments on commit 191bb13

Please sign in to comment.