summaryrefslogtreecommitdiff
path: root/lib/custodian/settings.rb
blob: 60e5a2e03d8dd022ab7906c8b12c739539b44016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
119
120
121
122
123
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
require 'singleton'


#
# A class which encapsulates some global-settings which are read from the
# global configuration file.
#
# The configuration file is optional, and we have defaults for every value.
#
# This class is a singleton to avoid having to re-parse the configuration
# file more than once per program-launch.
#
module Custodian

  class Settings

   include Singleton


    #
    # The parsed key=values store
    #
    attr_reader :settings


    #
    # Holder to mark whether we've loaded our file.
    #
    attr_reader :parsed


    #
    # Constructor.  Only called once, as we're a singleton.
    #
    def initialize
      @parsed = false
    end


    #
    # Load the configuration file; called only once.
    #
    def _load(file = '/etc/custodian/custodian.cfg')

      @parsed   = true
      @settings = {}

      #
      # The global configuration file.
      #
      return unless File.exist?(file)

      #
      # Load and "parse" the key=value content.
      #
      File.open(file, 'r').each_line do |line|
        next if line.nil? || line =~ /^#/
        if line =~ /^(.*)=(.*)$/
          key = $1.dup
          val = $2.dup
          key.strip!
          val.strip!
          @settings[key] = val
        end
      end
    end


    #
    #  Privately set the value for a named key.
    #
    #  Used by the test-suite.
    #
    def _store(key, val)
      @settings[key] = val
      @parsed = true
    end


    #
    # Have we loaded our data?
    #
    def _loaded?
      @parsed
    end


    #
    # Retrieve an arbitrary key
    #
    def key(name)
      _load unless _loaded?
      @settings[name]
    end


    #
    # The timeout period for each individual test.
    #
    def timeout
      _load unless _loaded?

      if @settings['timeout']
        @settings['timeout'].to_i
      else
        30
      end
    end



    #
    # The number of times to re-execute a failing test
    # before raising an alert.
    #
    def retries
      _load unless _loaded?

      if @settings['retries']
        @settings['retries'].to_i
      else
        5
      end
    end


    #
    # When a test fails we repeat it up to five times.
    #
    # (The retries() method will return the number of repeats, but we default to five.)
    #
    # Here we configure a delay between those repeats.
    #
    # A delay of zero is permissable.
    #
    def retry_delay
      _load unless _loaded?

      if @settings['retry_delay']
        @settings['retry_delay'].to_i
      else
        0
      end
    end


    #
    #  The type of queue to use.
    #
    #  The hostname/IP address of the queue should be set in the
    # environmental variable 'QUEUE_ADDRESS'.
    #
    def queue_type
      _load unless _loaded?

      @settings['queue_type'] || 'redis'
    end



    #
    # The filename for the logfile.
    #
    def log_file
      _load unless _loaded?

      @settings['log_file'] || 'custodian-dequeue.log'
    end


    #
    # The alerter to use
    #
    def alerter
      _load unless _loaded?

      @settings['alerter'] || 'file'
    end


    #
    # The alert-source we send.  Only used when the notifier is set to mauve.
    #
    def alert_source
      _load unless _loaded?

      @settings['alert_source'] || 'Custodian'
    end


    #
    # The target for the alert.
    #
    # When the alerter is "smtp" the target is the mail address.
    #
    # When the alerter is "file" the target is the logfile.
    #
    # When the alerter is "mauve" the target is the destination for the alerts.
    #
    # When the alerter is "redis" the target is the redis-server address.
    #
    def alerter_target(alert)
      _load unless _loaded?


      #
      # Find the alerting method.
      #
      # if we have something setup then use it.
      if @settings["#{alert}_target"]
        return(@settings["#{alert}_target"])
      end

      # otherwise per-test defaults.
      case alert
        when 'smtp'
          'root'
        when 'mauve'
          'alert.bytemark.co.uk'
        when 'file'
          'alerts.log'
        when 'redis'
          '127.0.0.1:6379'
        else
          nil
      end
    end


  end
end