Class: Notifu::Config

Inherits:
Object show all
Defined in:
lib/notifu/config.rb

Constant Summary collapse

@@config_path =

FIXME: should be parametrized

"/etc/notifu/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



8
9
10
11
12
13
14
# File 'lib/notifu/config.rb', line 8

def initialize
  begin
    @config = YAML.load_file(@@config_path + 'notifu.yaml')
  rescue
    raise "Failed to load main config file!"
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/notifu/config.rb', line 4

def config
  @config
end

Instance Method Details

#contacts_initObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/notifu/config.rb', line 26

def contacts_init
  Dir[@@config_path + 'contacts/*.yaml'].each do |path|
    cfg = YAML.load_file(path).deep_symbolize_keys
    begin
        Notifu::Model::Contact.with(:name, cfg[:name]).update(cfg)
        puts "Updated contact '#{cfg[:name]}'."
    rescue
        Notifu::Model::Contact.create(cfg).save
        puts "Created contact '#{cfg[:name]}'."
    end
  end
end

#getObject



16
17
18
# File 'lib/notifu/config.rb', line 16

def get
  @get ||= self.config.deep_symbolize_keys
end

#groups_initObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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/notifu/config.rb', line 52

def groups_init
  Dir[@@config_path + 'groups/*.yaml'].each do |path|
    cfg = YAML.load_file(path).deep_symbolize_keys
    begin
      group = Notifu::Model::Group.create(name: cfg[:name])
      puts "Created group '#{cfg[:name]}'."
    rescue
      group = Notifu::Model::Group.with(:name, cfg[:name])
      puts "Found group '#{cfg[:name]}'."
    end

    contacts = Array.new

    cfg[:primary].each do |contact_id|
      begin
        contacts << Notifu::Model::Contact.with(:name, contact_id)
        puts "Contact '#{contact_id}' accepted as primary."
      rescue
        puts "Failed to load primary contact '#{contact_id}'."
      end
    end

    group.primary.replace(contacts)
    group.save
    puts "Primary contacts for group '#{cfg[:name]}' updated."

    if cfg[:secondary].is_a? Array
      contacts = Array.new

      cfg[:secondary].each do |contact_id|
        begin
          contacts << Notifu::Model::Contact.with(:name, contact_id)
          puts "Contact '#{contact_id}' accepted as secondary."
        rescue
          puts "Failed to load secondary contact '#{contact_id}'."
          exit 1
        end
      end

      group.secondary.replace(contacts)
      group.save
      puts "Secondary contacts for group '#{cfg[:name]}' updated."

      if cfg[:tertiary].is_a? Array
        contacts = Array.new

        cfg[:tertiary].each do |contact_id|
          begin
            contacts << Notifu::Model::Contact.with(:name, contact_id)
            puts "Contact '#{contact_id}' accepted as tertiary."
          rescue
            puts "Failed to load tertiary contact '#{contact_id}'."
            exit 1
          end
        end

        group.tertiary.replace(contacts)
        group.save
        puts "Tertiary contacts for group '#{cfg[:name]}' updated."
      else
        group.tertiary.replace([])
      end
    else
      group.secondary.replace([])
    end
  end
end

#ohm_initObject



20
21
22
23
24
# File 'lib/notifu/config.rb', line 20

def ohm_init
  contacts_init
  slas_init
  groups_init
end

#slas_initObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/notifu/config.rb', line 39

def slas_init
  Dir[@@config_path + 'slas/*.yaml'].each do |path|
    cfg = YAML.load_file(path).deep_symbolize_keys
    begin
        Notifu::Model::Sla.with(:name, cfg[:name]).update(cfg)
        puts "Updated SLA '#{cfg[:name]}'."
    rescue
        Notifu::Model::Sla.create(cfg).save
        puts "Created SLA '#{cfg[:name]}'."
    end
  end
end