Class: Sensu::Settings::Loader
- Inherits:
-
Object
- Object
- Sensu::Settings::Loader
- Defined in:
- lib/sensu/settings/loader.rb
Instance Attribute Summary collapse
-
#loaded_files ⇒ Object
readonly
Returns the value of attribute loaded_files.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Class Method Summary collapse
-
.create_category_methods ⇒ Object
Create setting category accessors and methods to test the existence of definitions.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve the value object corresponding to a key, acting like a Hash object.
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
-
#load(options = {}) ⇒ Hash
Load settings from the environment and the paths provided, set appropriate environment variables.
-
#load_directory(directory) ⇒ Object
Load settings from files in a directory.
-
#load_env ⇒ Object
Load settings from the environment.
-
#load_file(file) ⇒ Object
Load settings from a JSON file.
-
#set_env ⇒ Object
Set Sensu settings related environment variables.
-
#to_hash ⇒ Hash
Access settings as an indifferent hash.
-
#validate! ⇒ Array
Validate the loaded settings.
Constructor Details
#initialize ⇒ Loader
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_files ⇒ Object (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 |
#warnings ⇒ Object (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_methods ⇒ Object
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.
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.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sensu/settings/loader.rb', line 132 def load(={}) load_env if [:config_file] load_file([:config_file]) end if [:config_dir] load_directory([: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.
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_env ⇒ Object
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.
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_env ⇒ Object
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_hash ⇒ Hash
Access settings as an indifferent hash.
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 |