Class: Nerve::ConfigurationManager

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/nerve/configuration_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/nerve/configuration_manager.rb', line 12

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/nerve/configuration_manager.rb', line 11

def options
  @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(options)

  config = parse_config_file(options[:config])
  config['services'] ||= {}

  if config.has_key?('service_conf_dir')
    cdir = File.expand_path(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 options[:instance_id] && !options[:instance_id].empty?
    config['instance_id'] = options[: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 parse_options!
 @options = parse_options_from_argv!
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 parse_options_from_argv!
  options = {}
  # set command line options
  optparse = OptionParser.new do |opts|
    opts.banner =<<EOB
Welcome to nerve

Usage: nerve --config /path/to/nerve/config
EOB

    options[:config] = ENV['NERVE_CONFIG']
    opts.on('-c config','--config config', String, 'path to nerve config') do |key,value|
      options[:config] = key
    end

    options[: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|
      options[:instance_id] = key
    end

    options[: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 |_|
      options[:check_config] = true
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end
  optparse.parse!
  options.each do|key, value|
    log.info key
    log.info value
  end
  options
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