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 = default_settings
  @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().



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sensu/settings/loader.rb', line 43

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.

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    value for key.



70
71
72
# File 'lib/sensu/settings/loader.rb', line 70

def [](key)
  to_hash[key]
end

#default_settingsHash

Default settings.

Returns:

  • (Hash)

    settings.



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

def default_settings
  default = {
    :transport => {
      :name => "rabbitmq",
      :reconnect_on_error => true
    }
  }
  CATEGORIES.each do |category|
    default[category] = {}
  end
  default
end

#load_directory(directory) ⇒ Object

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

Parameters:

  • directory (String)

    path.



124
125
126
127
128
129
130
# File 'lib/sensu/settings/loader.rb', line 124

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

#load_envObject

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


79
80
81
82
83
84
85
# File 'lib/sensu/settings/loader.rb', line 79

def load_env
  load_transport_env
  load_rabbitmq_env
  load_redis_env
  load_client_env
  load_api_env
end

#load_file(file) ⇒ Object

Load settings from a JSON file.

Parameters:

  • file (String)

    path.



90
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
116
117
118
# File 'lib/sensu/settings/loader.rb', line 90

def load_file(file)
  if File.file?(file) && File.readable?(file)
    begin
      warning("loading config file", :file => file)
      contents = read_config_file(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 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).



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

def set_env!
  ENV["SENSU_LOADED_TEMPFILE"] = create_loaded_tempfile!
end

#to_hashHash

Access settings as an indifferent hash.

Returns:

  • (Hash)

    settings.



58
59
60
61
62
63
# File 'lib/sensu/settings/loader.rb', line 58

def to_hash
  unless @indifferent_access
    indifferent_access!
  end
  @settings
end

#validateArray

Validate the loaded settings.

Returns:

  • (Array)

    validation failures.



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

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