Class: LitmusPaper::ConfigurationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/configuration_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path) ⇒ ConfigurationFile

Returns a new instance of ConfigurationFile.



5
6
7
8
9
10
11
12
# File 'lib/litmus_paper/configuration_file.rb', line 5

def initialize(config_file_path)
  @config_file_path = config_file_path
  @services = {}
  @port = 9292
  @data_directory = "/etc/litmus"
  @cache_location = "/run/shm"
  @cache_ttl = -1
end

Instance Method Details

#cache_location(location) ⇒ Object



117
118
119
# File 'lib/litmus_paper/configuration_file.rb', line 117

def cache_location(location)
  @cache_location = location
end

#cache_ttl(ttl) ⇒ Object



121
122
123
# File 'lib/litmus_paper/configuration_file.rb', line 121

def cache_ttl(ttl)
  @cache_ttl = ttl
end

#data_directory(directory) ⇒ Object



44
45
46
# File 'lib/litmus_paper/configuration_file.rb', line 44

def data_directory(directory)
  @data_directory = directory
end

#evaluate(file = @config_file_path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/litmus_paper/configuration_file.rb', line 14

def evaluate(file = @config_file_path)
  LitmusPaper.logger.info "Loading file #{file}"
  config_contents = File.read(file)
  if file =~ /\.(yaml|yml)/
    load_yaml(config_contents)
  else
    instance_eval(config_contents)
  end
  LitmusPaper::Configuration.new(
    @port,
    @data_directory,
    @services,
    @cache_location,
    @cache_ttl
  )
end

#include_files(glob_pattern) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/litmus_paper/configuration_file.rb', line 31

def include_files(glob_pattern)
  full_glob_pattern = File.expand_path(glob_pattern, File.dirname(@config_file_path))
  LitmusPaper.logger.info "Searching for files matching: #{full_glob_pattern}"

  Dir.glob(full_glob_pattern).each do |file|
    evaluate(file)
  end
end

#load_yaml(contents) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/litmus_paper/configuration_file.rb', line 125

def load_yaml(contents)
  config = Util.symbolize_keys(YAML.load(contents))

  config.each do |key, value|
    case key
    when :include_files, :data_directory, :cache_location, :cache_ttl, :port
      send(key, value)
    when :services
      yaml_service(value)
    end
  end
end

#parse_yaml_checks(config) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/litmus_paper/configuration_file.rb', line 64

def parse_yaml_checks(config)
  config.map do |check|
    check_config = Util.symbolize_keys(check)
    case check[:type].to_sym
      when :big_brother_service
        Metric::BigBrotherService.new(check_config.delete(:service))
      when :cpu_load
        Metric::CPULoad.new(check_config.delete(:weight))
      when :constant_metric
        Metric::ConstantMetric.new(check_config.delete(:weight))
      when :internet_health
        weight = check_config.delete(:weight)
        hosts = check_config.delete(:hosts)
        Metric::InternetHealth.new(weight, hosts, check_config)
      when :script
        command = check_config.delete(:command)
        weight = check_config.delete(:weight)
        Metric::Script.new(command, weight, check_config)
      when :haproxy_backends_health
        weight = check_config.delete(:weight)
        domain_socket = dep_config.delete(:domain_socket)
        cluster = dep_config.delete(:cluster)
        Metric::HaproxyBackendsHealth.new(weight, domain_socket, cluster, check_config)
    end
  end
end

#parse_yaml_dependencies(config) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/litmus_paper/configuration_file.rb', line 91

def parse_yaml_dependencies(config)
  config.map do |dep|
    dep_config = Util.symbolize_keys(dep)
    case dep[:type].to_sym
      when :file_contents
        path = dep_config.delete(:path)
        regex = dep_config.delete(:regex)
        Dependency::FileContents.new(path, regex, dep_config)
      when :haproxy_backends
        domain_socket = dep_config.delete(:domain_socket)
        cluster = dep_config.delete(:cluster)
        Dependency::HaproxyBackends.new(domain_socket, cluster, dep_config)
      when :http
        uri = dep_config.delete(:uri)
        Dependency::HTTP.new(uri, dep_config)
      when "script"
        command = dep_config.delete(:command)
        Dependency::Script.new(command, options)
      when "tcp"
        ip = dep_config.delete(:ip)
        port = dep_config.delete(:port)
        Dependency::TCP.new(ip, port, dep_config)
    end
  end
end

#port(port) ⇒ Object



40
41
42
# File 'lib/litmus_paper/configuration_file.rb', line 40

def port(port)
  @port = port
end

#service(name, &block) ⇒ Object



48
49
50
51
52
# File 'lib/litmus_paper/configuration_file.rb', line 48

def service(name, &block)
  service = Service.new(name.to_s)
  block.call(service)
  @services[name.to_s] = service
end

#yaml_service(value) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/litmus_paper/configuration_file.rb', line 54

def yaml_service(value)
  value.each do |name, config|
    config = Util.symbolize_keys(config)
    dependencies = parse_yaml_dependencies(config.fetch(:dependencies, []))
    checks = parse_yaml_checks(config.fetch(:checks, []))
    service = Service.new(name.to_s, dependencies, checks)
    @services[name.to_s] = service
  end
end