Class: Sensu::Settings::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/sensu/settings/loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoader


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sensu/settings/loader.rb', line 15

def initialize
  @warnings = []
  @settings = {
    :transport => {}
  }
  CATEGORIES.each do |category|
    @settings[category] = {}
  end
  @indifferent_access = false
  @loaded_files = []
  self.class.create_category_methods
end

Instance Attribute Details

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files


13
14
15
# File 'lib/sensu/settings/loader.rb', line 13

def loaded_files
  @loaded_files
end

#warningsObject (readonly)

Returns the value of attribute warnings


9
10
11
# File 'lib/sensu/settings/loader.rb', line 9

def warnings
  @warnings
end

Class Method Details

.create_category_methodsObject

Create setting category accessors and methods to test the existence of definitions. Called in initialize().


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sensu/settings/loader.rb', line 30

def self.create_category_methods
  CATEGORIES.each do |category|
    define_method(category) do
      setting_category(category)
    end
    method_name = category.to_s.chop + "_exists?"
    define_method(method_name.to_sym) do |name|
      definition_exists?(category, name)
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the setting object corresponding to a key, acting like a Hash object.


57
58
59
# File 'lib/sensu/settings/loader.rb', line 57

def [](key)
  to_hash[key]
end

#load_directory(directory) ⇒ Object

Load settings from files in a directory. Files may be in nested directories.


119
120
121
122
123
124
125
# File 'lib/sensu/settings/loader.rb', line 119

def load_directory(directory)
  warning("loading config files from directory", :directory => directory)
  path = directory.gsub(/\\(?=\S)/, "/")
  Dir.glob(File.join(path, "**/*.json")).each do |file|
    load_file(file)
  end
end

#load_envObject

Load settings from the environment. Loads: RABBITMQ_URL, REDIS_URL, REDISTOGO_URL, API_PORT, PORT


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sensu/settings/loader.rb', line 63

def load_env
  if ENV["RABBITMQ_URL"]
    @settings[:rabbitmq] = ENV["RABBITMQ_URL"]
    warning("using rabbitmq url environment variable", :rabbitmq => @settings[:rabbitmq])
  end
  ENV["REDIS_URL"] ||= ENV["REDISTOGO_URL"]
  if ENV["REDIS_URL"]
    @settings[:redis] = ENV["REDIS_URL"]
    warning("using redis url environment variable", :redis => @settings[:redis])
  end
  ENV["API_PORT"] ||= ENV["PORT"]
  if ENV["API_PORT"]
    @settings[:api] ||= {}
    @settings[:api][:port] = ENV["API_PORT"].to_i
    warning("using api port environment variable", :api => @settings[:api])
  end
  @indifferent_access = false
end

#load_file(file) ⇒ Object

Load settings from a JSON file.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sensu/settings/loader.rb', line 85

def load_file(file)
  if File.file?(file) && File.readable?(file)
    begin
      warning("loading config file", :file => file)
      contents = IO.read(file)
      config = MultiJson.load(contents, :symbolize_keys => true)
      merged = deep_merge(@settings, config)
      unless @loaded_files.empty?
        changes = deep_diff(@settings, merged)
        warning("config file applied changes", {
          :file => file,
          :changes => changes
        })
      end
      @settings = merged
      @indifferent_access = false
      @loaded_files << file
    rescue MultiJson::ParseError => error
      warning("config file must be valid json", {
        :file => file,
        :error => error.to_s
      })
      warning("ignoring config file", :file => file)
    end
  else
    warning("config file does not exist or is not readable", :file => file)
    warning("ignoring config file", :file => file)
  end
end

#set_env!Object

Set Sensu settings related environment variables. This method currently sets SENSU_CONFIG_FILES, a colon delimited list of loaded config files.


130
131
132
# File 'lib/sensu/settings/loader.rb', line 130

def set_env!
  ENV["SENSU_CONFIG_FILES"] = @loaded_files.join(":")
end

#to_hashHash

Access settings as an indifferent hash.


45
46
47
48
49
50
# File 'lib/sensu/settings/loader.rb', line 45

def to_hash
  unless @indifferent_access
    indifferent_access!
  end
  @settings
end

#validateArray

Validate the loaded settings.


137
138
139
140
# File 'lib/sensu/settings/loader.rb', line 137

def validate
  validator = Validator.new
  validator.run(@settings, sensu_service_name)
end