summaryrefslogtreecommitdiff
path: root/bin/custodian-dequeue
blob: 3072fbd46d51464ed811d1835c1b95de1203a5bf (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
#!/usr/bin/ruby -Ilib/ -I../lib/ -rubygems
#
# NAME
#  custodian-dequeue - Pull network tests from a queue and execute them in series.
#
# SYNOPSIS
#  custodian-dequeue  [ -h | --help ]
#                     [ -m | --manual]
#                     [ -f | --fail ]
#                     [ -l | --logfile FILE]
#                     [ -S | --server 1.2.3.4:123 ]
#                     [ -s | --single ]
#                     [ -v | --verbose ]
#
# OPTIONS
#
#  -h, --help          Show a help message, and exit.
#
#  -m, --manual        Show this manual, and exit.
#
#  -l, --logfile       Specify the path to the logfile to run.
#
#  -S, --server        Specify the host:port for the beanstalkd queue.
#
#  -s, --single        Run a single test and exit.  Don't poll the queue for more.
#
#  -f, --fail          Run tests but stop the first time we see a fail.
#
#  -v, --verbose       Be noisy.
#
#
# ABOUT
#
#  This tool is designed to pull network/protocol-tests from a beanstalkd server and execute
# them one by one.
#
#  The results of the testing will be sent to a notifier, where they can later be acted upon.
#
#
# AUTHOR
#
#  Steve Kemp  <steve@bytemark.co.uk>
#


#
# Standard modules
#
require 'getoptlong'


#
# Our code.
#
require 'custodian/settings'
require 'custodian/worker'




#
#  Entry-point to our code.
#
if __FILE__ == $0 then

  $help    = false
  $manual  = false

  #
  #  The beanstalkd server address, alerting method, and logfile.
  #
  settings = Custodian::Settings.instance()
  $SERVER  = settings.queue_server
  $QUEUE   = settings.queue_name
  $ALERTER = settings.alerter
  $LOGFILE = settings.log_file


  begin
    opts = GetoptLong.new(
                          [ "--help",    "-h", GetoptLong::NO_ARGUMENT ],
                          [ "--manual",  "-m", GetoptLong::NO_ARGUMENT ],
                          [ "--fail",    "-f", GetoptLong::NO_ARGUMENT  ],
                          [ "--logfile", "-l", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--queue",   "-q", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--server",  "-S", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--alerter", "-a", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--single",  "-s", GetoptLong::NO_ARGUMENT ],
                          [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ]
                          )
    opts.each do |opt, arg|
      case opt
      when "--verbose" then
          ENV["VERBOSE"] = "1"
      when "--logfile" then
          $LOGFILE = arg
      when "--server" then
          $SERVER = arg
      when "--queue" then
          $QUEUE = arg
      when "--alerter" then
          $ALERTER = arg
      when "--single" then
          ENV["SINGLE"] = "1"
      when "--fail" then
          ENV["FAIL"] = "1"
      when "--help" then
          $help = true
      when "--manual" then
          $manual = true
      end
    end
  rescue StandardError => ex
    puts "Option parsing failed: #{ex.to_s}"
    exit
  end



  #
  # CAUTION! Here be quality kode.
  #
  if $manual or $help

    # Open the file, stripping the shebang line
    lines = File.open(__FILE__){|fh| fh.readlines}[1..-1]

    found_synopsis = false

    lines.each do |line|

      line.chomp!
      break if line.empty?

      if $help and !found_synopsis
        found_synopsis = (line =~ /^#\s+SYNOPSIS\s*$/)
        next
      end

      puts line[2..-1].to_s

      break if $help and found_synopsis and line =~ /^#\s*$/

    end

    exit 0
  end

  #
  #  Create the object
  #
  worker = Custodian::Worker.new( $SERVER, $QUEUE, $ALERTER, $LOGFILE, settings )


  #
  #  Single step?
  #
  if ( ENV['SINGLE'] )
    worker.process_single_job()
    exit(0)
  end

  #
  #  Run until we see a failure?
  #
  if ( ENV['FAIL'] )
    worker.process_until_fail()
    exit(0)
  end

  #
  #  Otherwise loop indefinitely
  #
  worker.run!

end