Module: Sensu::Utilities

Included in:
Daemon
Defined in:
lib/sensu/utilities.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge(hash_one, hash_two) ⇒ Hash

Deep merge two hashes. Nested hashes are deep merged, arrays are concatenated and duplicate array items are removed.

Parameters:

  • hash_one (Hash)
  • hash_two (Hash)

Returns:

  • (Hash)

    deep merged hash.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sensu/utilities.rb', line 34

def deep_merge(hash_one, hash_two)
  merged = hash_one.dup
  hash_two.each do |key, value|
    merged[key] = case
    when hash_one[key].is_a?(Hash) && value.is_a?(Hash)
      deep_merge(hash_one[key], value)
    when hash_one[key].is_a?(Array) && value.is_a?(Array)
      hash_one[key].concat(value).uniq
    else
      value
    end
  end
  merged
end

#random_uuidString

Generate a random universally unique identifier.

Returns:

  • (String)

    random UUID.



52
53
54
# File 'lib/sensu/utilities.rb', line 52

def random_uuid
  UUIDTools::UUID.random_create.to_s
end

#redact_sensitive(hash, keys = nil) ⇒ Hash

Remove sensitive information from a hash (eg. passwords). By default, hash values will be redacted for the following keys: password, passwd, pass, api_key, api_token, access_key, secret_key, private_key, secret

Parameters:

  • hash (Hash)

    to redact sensitive value from.

  • keys (Array) (defaults to: nil)

    that indicate sensitive values.

Returns:

  • (Hash)

    hash with redacted sensitive values.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sensu/utilities.rb', line 64

def redact_sensitive(hash, keys=nil)
  keys ||= %w[
    password passwd pass
    api_key api_token
    access_key secret_key private_key
    secret
  ]
  hash = hash.dup
  hash.each do |key, value|
    if keys.include?(key.to_s)
      hash[key] = "REDACTED"
    elsif value.is_a?(Hash)
      hash[key] = redact_sensitive(value, keys)
    elsif value.is_a?(Array)
      hash[key] = value.map do |item|
        item.is_a?(Hash) ? redact_sensitive(item, keys) : item
      end
    end
  end
  hash
end

#retry_until_true(wait = 0.5, &block) ⇒ Object

Retry a code block until it retures true. The first attempt and following retries are delayed.

Parameters:

  • wait (Numeric) (defaults to: 0.5)

    time to delay block calls.

  • block (Proc)

    to call that needs to return true.



20
21
22
23
24
25
26
# File 'lib/sensu/utilities.rb', line 20

def retry_until_true(wait=0.5, &block)
  EM::Timer.new(wait) do
    unless block.call
      retry_until_true(wait, &block)
    end
  end
end

#testing?TrueClass, FalseClass

Determine if Sensu is being tested, using the process name. Sensu is being test if the process name is “rspec”,

Returns:

  • (TrueClass, FalseClass)


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

def testing?
  File.basename($0) == "rspec"
end