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

Returns a new instance of Loader.



17
18
19
20
21
22
23
# File 'lib/sensu/settings/loader.rb', line 17

def initialize
  @warnings = []
  @settings = Hash[CATEGORIES.map {|category| [category, {}]}]
  @indifferent_access = false
  @loaded_files = []
  self.class.create_category_methods
end

Instance Attribute Details

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



15
16
17
# File 'lib/sensu/settings/loader.rb', line 15

def loaded_files
  @loaded_files
end

#warningsObject (readonly)

Returns the value of attribute warnings.



11
12
13
# File 'lib/sensu/settings/loader.rb', line 11

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().



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sensu/settings/loader.rb', line 27

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 value object corresponding to a key, acting like a Hash object.

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    value for key.



54
55
56
# File 'lib/sensu/settings/loader.rb', line 54

def [](key)
  to_hash[key]
end

#load(options = {}) ⇒ Hash

Load settings from the environment and the paths provided, set appropriate environment variables.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :config_file (String)

    to load.

  • :config_dir (String)

    to load.

Returns:

  • (Hash)

    loaded settings.



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/sensu/settings/loader.rb', line 132

def load(options={})
  load_env
  if options[:config_file]
    load_file(options[:config_file])
  end
  if options[:config_dir]
    load_directory(options[:config_dir])
  end
  set_env
  to_hash
end

#load_directory(directory) ⇒ Object

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

Parameters:

  • directory (String)

    path.



110
111
112
113
114
115
116
# File 'lib/sensu/settings/loader.rb', line 110

def load_directory(directory)
  warning(directory, "loading config files from 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



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

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

#load_file(file) ⇒ Object

Load settings from a JSON file.

Parameters:

  • file (String)

    path.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sensu/settings/loader.rb', line 82

def load_file(file)
  if File.file?(file) && File.readable?(file)
    begin
      warning(file, "loading config 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(changes, "config file applied changes")
      end
      @settings = merged
      @indifferent_access = false
      @loaded_files << file
    rescue MultiJson::ParseError => error
      warning(file, "config file must be valid json")
      warning(file, "ignoring config file")
    end
  else
    warning(file, "config file does not exist or is not readable")
    warning(file, "ignoring config file")
  end
end

#set_envObject

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



121
122
123
# File 'lib/sensu/settings/loader.rb', line 121

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

#to_hashHash

Access settings as an indifferent hash.

Returns:

  • (Hash)

    settings.



42
43
44
45
46
47
# File 'lib/sensu/settings/loader.rb', line 42

def to_hash
  unless @indifferent_access
    indifferent_access!
  end
  @settings
end

#validate!Array

Validate the loaded settings.

Returns:

  • (Array)

    validation failures.



147
148
149
150
151
# File 'lib/sensu/settings/loader.rb', line 147

def validate!
  service = ::File.basename($0).split("-").last
  validator = Validator.new
  validator.run(@settings, service)
end