Class: Nerve::ConfigurationManager
- Inherits:
-
Object
- Object
- Nerve::ConfigurationManager
- Includes:
- Logging
- Defined in:
- lib/nerve/configuration_manager.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #generate_nerve_config(options) ⇒ Object
- #parse_config_file(filename) ⇒ Object
- #parse_options! ⇒ Object
- #parse_options_from_argv! ⇒ Object
- #reload! ⇒ Object
- #setConfig(configuration) ⇒ Object
Methods included from Logging
configure_logger_for, #log, logger_for
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/nerve/configuration_manager.rb', line 12 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/nerve/configuration_manager.rb', line 11 def @options end |
Instance Method Details
#generate_nerve_config(options) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nerve/configuration_manager.rb', line 58 def generate_nerve_config() config = parse_config_file([:config]) config['services'] ||= {} if config.has_key?('service_conf_dir') cdir = File.(config['service_conf_dir']) if ! Dir.exists?(cdir) then raise "service conf dir does not exist:#{cdir}" end cfiles = Dir.glob(File.join(cdir, '*.{yaml,json}')) cfiles.each { |x| config['services'][File.basename(x[/(.*)\.(yaml|json)$/, 1])] = parse_config_file(x) } end if [:instance_id] && ![:instance_id].empty? config['instance_id'] = [:instance_id] end config end |
#parse_config_file(filename) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/nerve/configuration_manager.rb', line 79 def parse_config_file(filename) # parse nerve config file begin c = YAML::parse(File.read(filename)) rescue Errno::ENOENT => e raise ArgumentError, "config file does not exist:\n#{e.inspect}" rescue Errno::EACCES => e raise ArgumentError, "could not open config file:\n#{e.inspect}" rescue YAML::SyntaxError => e raise "config file #{filename} is not proper yaml:\n#{e.inspect}" end return c.to_ruby end |
#parse_options! ⇒ Object
54 55 56 |
# File 'lib/nerve/configuration_manager.rb', line 54 def @options = end |
#parse_options_from_argv! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 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 |
# File 'lib/nerve/configuration_manager.rb', line 14 def = {} # set command line options optparse = OptionParser.new do |opts| opts. =<<EOB Welcome to nerve Usage: nerve --config /path/to/nerve/config EOB [:config] = ENV['NERVE_CONFIG'] opts.on('-c config','--config config', String, 'path to nerve config') do |key,value| [:config] = key end [:instance_id] = ENV['NERVE_INSTANCE_ID'] opts.on('-i instance_id','--instance_id instance_id', String, 'reported as `name` to ZK; overrides instance id from config file') do |key,value| [:instance_id] = key end [:check_config] = ENV['NERVE_CHECK_CONFIG'] opts.on('-k', '--check-config', 'Validate the nerve config ONLY and exit 0 if valid (non zero otherwise)') do |_| [:check_config] = true end opts.on('-h', '--help', 'Display this screen') do puts opts exit end end optparse.parse! .each do|key, value| log.info key log.info value end end |
#reload! ⇒ Object
93 94 95 96 |
# File 'lib/nerve/configuration_manager.rb', line 93 def reload! raise "You must parse command line options before reloading config" if @options.nil? @config = generate_nerve_config(@options) end |
#setConfig(configuration) ⇒ Object
98 99 100 101 102 |
# File 'lib/nerve/configuration_manager.rb', line 98 def setConfig(configuration) @config = configuration @options = {} @options[:check_config]=false end |