Class: Nerve::ConfigurationManager
- Inherits:
-
Object
- Object
- Nerve::ConfigurationManager
- 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
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/nerve/configuration_manager.rb', line 7 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/nerve/configuration_manager.rb', line 6 def @options end |
Instance Method Details
#generate_nerve_config(options) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nerve/configuration_manager.rb', line 49 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
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nerve/configuration_manager.rb', line 69 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
45 46 47 |
# File 'lib/nerve/configuration_manager.rb', line 45 def @options = end |
#parse_options_from_argv! ⇒ Object
9 10 11 12 13 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 |
# File 'lib/nerve/configuration_manager.rb', line 9 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! end |
#reload! ⇒ Object
83 84 85 86 |
# File 'lib/nerve/configuration_manager.rb', line 83 def reload! raise "You must parse command line options before reloading config" if @options.nil? @config = generate_nerve_config(@options) end |