Module: Kurchatov::Plugins::Config

Defined in:
lib/kurchatov/plugin/config.rb

Class Method Summary collapse

Class Method Details

.find_plugin(name, array) ⇒ Object



16
17
18
# File 'lib/kurchatov/plugin/config.rb', line 16

def self.find_plugin(name, array)
  array.find { |p| p.name == name }
end

.load_plugins(plugins_path, config_file) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kurchatov/plugin/config.rb', line 20

def self.load_plugins(plugins_path, config_file)
  Log.error("Config file #{config_file} not found") and
      exit(Kurchatov::Config[:ERROR_CONFIG]) unless File.exists?(config_file)
  @all_plugins = Kurchatov::Plugins::DSL.load_riemann_plugins(plugins_path)
  @all_names = Array.new
  @plugins_to_run = Array.new
  config = YAML.load_file(config_file)
  config = {} and Log.info("Empty config file '#{config_file}'") if config == false
  config.each do |name, val|
    @all_names << name
    next if val.nil?
    #
    # dup plugins from array
    #
    if val.kind_of? Array
      parent = find_plugin(name, @all_plugins)
      Log.error("Unable to find parent plugin for #{name}") and next if parent.nil?
      val.each_with_index do |p_settings, i|
        child = parent.dup
        child.name = "#{name}_#{i}"
        child.plugin = parent.plugin.dup
        child.plugin.merge!(p_settings)
        @all_plugins << child
        @all_names << child.name
      end
      @all_plugins.delete(parent)
      next
    end
    #
    # dup plugins from 'parent'
    #
    if val.is_a?(Hash) && val.has_key?('parent')
      parent = find_plugin(val['parent'], @all_plugins)
      Log.error("Unable to find parent '#{parent}' for '#{name}'") and next if parent.nil?
      child = parent.dup
      child.name = name
      child.plugin = parent.plugin.dup
      child.plugin.merge!(val)
      @all_plugins << child
      @all_names << child.name
      next
    end
    @all_plugins.each { |p| p.plugin.merge!(val) if name == p.name }
  end
  @all_plugins.each do |p|
    unless p.always_start || @all_names.include?(p.name)
      Log.info("Plugin '#{p.name}' not started, because it " +
                   "not 'always_start' and not in config file")
      next
    end
    @plugins_to_run << p if p.runnable_by_config?
  end
  Log.info("Start plugins: #{ @plugins_to_run.map {|p| p.name} }")
  @plugins_to_run
end

.load_test_plugin(plugin_path, config_file) ⇒ Object



9
10
11
12
13
14
# File 'lib/kurchatov/plugin/config.rb', line 9

def self.load_test_plugin(plugin_path, config_file)
  config = File.exists?(config_file) ? YAML.load_file(config_file) : {}
  plugin = Kurchatov::Plugins::DSL.load_riemann_plugin(plugin_path)
  plugin.plugin.merge!(config[plugin.name]) unless config.empty?
  [plugin]
end