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
233
234
235
236
237
238
|
# File 'lib/wcc.rb', line 111
def initialize
@options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ruby wcc.rb [options] [config-yaml-file]"
opts.banner += "\nOptions:\n"
opts.on('-v', '--verbose', 'Output more information') do self[:verbose] = true end
opts.on('-d', '--debug', 'Enable debug mode') do self[:debug] = true end
opts.on('--cache-dir DIR', 'Save hash and diff files to DIR') do |dir| self[:cache_dir] = dir end
opts.on('-s', '--simulate', 'Check for update but do not save hash or diff files') do self[:simulate] = true end
opts.on('--clean', 'Remove all saved hash and diff files') do self[:clean] = true end
opts.on('-t', '--tag TAG', 'Set TAG used in output') do |t| self[:tag] = t end
opts.on('-n', '--no-mails', 'Do not notify users in any way') do self[:nomails] = true end
opts.on('--show-config', 'Show config after loading config file (debug purposes)') do self[:show_config] = true end
opts.on('-h', '-?', '--help', 'Display this screen') do
puts opts
exit
end
end.parse!
WCC.logger.progname = 'wcc'
WCC.logger.level = Logger::ERROR
WCC.logger.level = Logger::INFO if self[:verbose]
WCC.logger.level = Logger::DEBUG if self[:debug]
WCC.logger.formatter = LogFormatter.new((self[:verbose] or self[:debug]))
WCC.logger.info "web change checker (aka wcc) #{WCC::VERSION}"
WCC.logger.info "Licensed under Apache License Version 2.0"
WCC.logger.info "No config file given, using default 'conf.yml' file" if ARGV.length == 0
self[:conf] = ARGV[0] || 'conf.yml'
if !File.exists?(self[:conf])
WCC.logger.fatal "Config file '#{self[:conf]}' does not exist!"
Prog.exit 1
end
Notificators.map 'email', MailNotificator
Notificators.map 'syslog', SyslogNotificator
WCC.logger.debug "Load config from '#{self[:conf]}'"
yaml = YAML.load_file(self[:conf])
yaml = {'conf' => {'email' => {}}}.recursive_merge(yaml) if yaml.is_a?(Hash)
if yaml.is_a?(Hash) and yaml['conf'].is_a?(Hash)
yaml['conf'].each do |key,val|
case key
when 'cache_dir'
@options[:cache_dir] ||= val
when 'tag'
@options[:tag] ||= val
when 'filterd'
@options[:filter_dir] ||= val
when 'templated'
@options[:template_dir] ||= val
else
if not Notificators.mappings.include?(key)
plugin_name = "wcc-#{key}-notificator"
WCC.logger.info "Trying to load plugin #{plugin_name}..."
begin
require plugin_name
rescue LoadError
WCC.logger.error "Plugin #{plugin_name} not found - maybe try `gem install #{plugin_name}`"
next
end
end
Notificators.mappings[key].parse_conf(val).each { |k,v| @options[k] ||= v }
end
end
end
if self[:show_config]
Conf.default.merge(@options).each do |k,v|
puts " #{k.to_s} => #{self[k]}"
end
Prog.exit 0
end
@recipients = {}
WCC.logger.debug "Load recipients from '#{self[:conf]}'"
yaml = YAML.load_file(self[:conf])
if not yaml
WCC.logger.info "No recipients loaded"
else
yaml['recipients'].to_a.each do |yaml_rec|
name = yaml_rec.keys.first
rec = []
yaml_rec[name].to_a.each do |yaml_way|
if yaml_way.is_a?(Hash)
prim_key = yaml_way.keys.first klass = Notificators.mappings[prim_key]
if klass.nil?
WCC.logger.error "Referenced notificator '#{prim_key}' not found!"
else
rec << klass.new(yaml_way[prim_key])
end
else
klass = Notificators.mappings[yaml_way]
if klass.nil?
WCC.logger.error "Referenced notificator '#{yaml_way}' not found!"
else
rec << klass.new
end
end
end
@recipients[name] = rec
end
end
WCC::Filters.add '--no-mails' do |data|
!self[:nomails]
end
end
|