Class: Nerve::ConfigurationManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/nerve/configuration_manager.rb', line 7

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/nerve/configuration_manager.rb', line 6

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



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 parse_options!
 @options = parse_options_from_argv!
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 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
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