summaryrefslogtreecommitdiff
path: root/bin/custodian-enqueue
blob: 6ca56ad1d99316fb660ef18dce08338502ae49aa (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
#!/usr/bin/ruby
#
# NAME
#  custodian-enqueue - Parse tests from a file and enqueue to beanstalkd.
#
# SYNOPSIS
#  custodian-enqueue  [ -h | --help ]
#                     [ -m | --manual]
#                     [ -q | --queue NAME]
#                     [ -f | --file FILE]
#                     [ -d | --dump ]
#                     [    | --test ]
#
# OPTIONS
#
#  -h, --help          Show a help message, and exit.
#
#  -m, --manual        Show this manual, and exit.
#
#  -d, --dump          Dump the parsed tests to the console; don't insert in the queue.
#
#  -q, --queue         Use the named beanstalkd tube.
#
#  --test              Test the parsing of the given file, alert on errors.
#
#  -f, --file FILE     Parse the given configuration file.
#
#
#
# ABOUT
#
#  This tool reads a single configuration file and parses it into a
# series of network & protocol tests.   These tests are then stored in
# a beanstalkd queue.
#
#  The intention is that the tests will be pulled from the queue and
# executed by the companion program custodian-dequeue.  The dequeing
# process may occur up numerous other hosts
#
# CONFIGURATION FILE
#
#  The configuration file is 99% compatible with that used in the tool
# custodian replaces.
#
#
# AUTHOR
#
#  Steve Kemp  <steve@bytemark.co.uk>
#



#
# Standard modules
#
require 'beanstalk-client'
require 'getoptlong'

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



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

  $help   = false
  $manual = false

  #
  #  The beanstalkd server address.
  #
  settings = Custodian::Settings.instance()
  $SERVER  = settings.queue_server
  $QUEUE   = settings.queue_name

  begin
    opts = GetoptLong.new(
                          [ "--dump",  "-d", GetoptLong::NO_ARGUMENT ],
                          [ "--test",   GetoptLong::NO_ARGUMENT ],
                          [ "--server",   GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--file",  "-f", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--help",  "-h", GetoptLong::NO_ARGUMENT ],
                          [ "--queue",  "-q", GetoptLong::REQUIRED_ARGUMENT ],
                          [ "--manual","-m", GetoptLong::NO_ARGUMENT ]
                          )
    opts.each do |opt, arg|
      case opt
      when "--dump" then
          ENV["DUMP"] = "1"
      when "--test" then
          ENV["TEST"] = "1"
      when "--server" then
          $SERVER = arg
      when "--queue" then
          $QUEUE= arg
       when "--file" then
          ENV["FILE"] = arg
      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


  #
  # Connected to the server
  #
  queue = Beanstalk::Pool.new([$SERVER], $QUEUE )
  if ( ! queue )
    puts "Failed to connect to beanstalk server: #{$SERVER}"
    exit 1
  end


  #
  # Create the parser object.
  #
  mon = Custodian::Parser.new()


  #
  # Parse our configuration file.  If there are errors then we'll
  # exit this script.
  #
  begin
    mon.parse_file( ENV['FILE'] )
  rescue => e
    puts "Failure in parsing the configuration file : #{ENV['FILE']}"
    puts e.to_s
    exit( 1 )
  end

  mon.jobs.each do |test|

    if ( ENV['TEST'] )
      # nop
    elsif ( ENV['DUMP'] )
      puts test
    else

      # priority of new job.
      priority = 1000

      # delay before putting into queue.
      delay    = 0

      # time-to-remove: how long job.reserve() will live for.
      ttr      = 300

      queue.put( test.to_s, priority, delay, ttr )
    end
  end

end