Class: Sensu::Settings::Loader
- Inherits:
-
Object
- Object
- Sensu::Settings::Loader
- Defined in:
- lib/sensu/settings/loader.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#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 setting object corresponding to a key, acting like a Hash object.
-
#client_defaults ⇒ Hash
Auto-detected defaults for client definition.
-
#default_settings ⇒ Hash
Default settings.
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
-
#load_directory(directory) ⇒ Object
Load settings from files in a directory.
-
#load_env ⇒ Object
Load settings from the environment.
-
#load_file(file, must_exist = true) ⇒ 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.
23 24 25 26 27 28 29 30 |
# File 'lib/sensu/settings/loader.rb', line 23 def initialize @warnings = [] @errors = [] @settings = default_settings @indifferent_access = false @loaded_files = [] self.class.create_category_methods end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
17 18 19 |
# File 'lib/sensu/settings/loader.rb', line 17 def errors @errors end |
#loaded_files ⇒ Object (readonly)
Returns the value of attribute loaded_files.
21 22 23 |
# File 'lib/sensu/settings/loader.rb', line 21 def loaded_files @loaded_files end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
13 14 15 |
# File 'lib/sensu/settings/loader.rb', line 13 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().
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sensu/settings/loader.rb', line 75 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.
102 103 104 |
# File 'lib/sensu/settings/loader.rb', line 102 def [](key) to_hash[key] end |
#client_defaults ⇒ Hash
Auto-detected defaults for client definition
Client name defaults to system hostname. Client address defaults to first detected non-loopback ipv4 address.
Client subscriptions are intentionally omitted here as sensu-client will provide defaults using client name after final settings are loaded.
42 43 44 45 46 47 |
# File 'lib/sensu/settings/loader.rb', line 42 def client_defaults { :name => system_hostname, :address => system_address } end |
#default_settings ⇒ Hash
Default settings.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sensu/settings/loader.rb', line 52 def default_settings default = { :sensu => { :spawn => { :limit => 12 } }, :transport => { :name => "rabbitmq", :reconnect_on_error => true } } CATEGORIES.each do |category| default[category] = {} end if ["client", "rspec"].include?(sensu_service_name) default[:client] = client_defaults end default end |
#load_directory(directory) ⇒ Object
Load settings from files in a directory. Files may be in nested directories.
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/sensu/settings/loader.rb', line 159 def load_directory(directory) warning("loading config files from directory", :directory => directory) path = directory.gsub(/\\(?=\S)/, "/") if File.readable?(path) && File.executable?(path) Dir.glob(File.join(path, "**{,/*/**}/*.json")).uniq.each do |file| load_file(file) end else load_error("insufficient permissions for loading", :directory => directory) end end |
#load_env ⇒ Object
Load settings from the environment.
Loads: SENSU_TRANSPORT_NAME, RABBITMQ_URL, REDIS_URL,
SENSU_CLIENT_NAME, SENSU_CLIENT_ADDRESS
SENSU_CLIENT_SUBSCRIPTIONS, SENSU_API_PORT
111 112 113 114 115 116 117 |
# File 'lib/sensu/settings/loader.rb', line 111 def load_env load_transport_env load_rabbitmq_env load_redis_env load_client_env load_api_env end |
#load_file(file, must_exist = true) ⇒ Object
Load settings from a JSON file.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/sensu/settings/loader.rb', line 124 def load_file(file, must_exist=true) if File.file?(file) && File.readable?(file) begin warning("loading config file", :file => file) contents = read_config_file(file) config = Sensu::JSON.load(contents) 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 Sensu::JSON::ParseError => error load_error("config file must be valid json", { :file => file, :error => error.to_s }) end elsif must_exist load_error("config file does not exist or is not readable", :file => file) 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 sets ‘SENSU_LOADED_TEMPFILE` to a new temporary file path, a file containing the colon delimited list of loaded configuration files (using `create_loaded_tempfile!()`. The environment variable `SENSU_CONFIG_FILES` has been removed, due to the exec ARG_MAX (E2BIG) error when spawning processes after loading many configuration files (e.g. > 2000).
178 179 180 |
# File 'lib/sensu/settings/loader.rb', line 178 def set_env! ENV["SENSU_LOADED_TEMPFILE"] = create_loaded_tempfile! end |
#to_hash ⇒ Hash
Access settings as an indifferent hash.
90 91 92 93 94 95 |
# File 'lib/sensu/settings/loader.rb', line 90 def to_hash unless @indifferent_access indifferent_access! end @settings end |
#validate ⇒ Array
Validate the loaded settings.
185 186 187 188 |
# File 'lib/sensu/settings/loader.rb', line 185 def validate validator = Validator.new @errors += validator.run(@settings, sensu_service_name) end |