summaryrefslogtreecommitdiff
path: root/lib/custodian/settings.rb
blob: cdc1b6d1675770dddc907018f05517205c81e7db (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
require 'singleton'


#
# A class which encapsulates some global-settings from the custodian 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




    #
    # Load the configuration file; called only once.
    #
    def _load
      @parsed   = true
      @settings = Hash.new()

      #
      # The global configuration file.
      #
      file = "/etc/custodian/custodian.cfg"
      return unless( File.exists?( 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




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




    #
    # The timeout period for all tests
    #
    def timeout
      _load() unless( _loaded? )

      @settings['timeout'] || 30
    end




    #
    # The beanstalkd server address
    #
    def queue_server
      _load() unless( _loaded? )

      @settings['queue_server'] || "127.0.0.1:11300"
    end




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

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


  end
end