Class: Main
Overview
The Main class ties together the core Reckoner functionality with the command line arguments, configuration file and email.
Constant Summary collapse
- VERSION =
'0.9.4.1'
- DEFAULT_ARGUMENTS =
{ 'debug' => false, 'quiet' => false, 'extra_checks' => [], 'sample' => nil, 'email' => true }
Constants included from SampleFile
SampleFile::SAMPLE_CONFIGURATION_FILE
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(argv) ⇒ Main
constructor
A new instance of Main.
- #make_sample_file(file_name) ⇒ Object
- #output_results(cm) ⇒ Object
- #run_config(config_file) ⇒ Object
- #run_reckoner ⇒ Object
- #send_email(cm, mail_config) ⇒ Object
Constructor Details
#initialize(argv) ⇒ Main
Returns a new instance of Main.
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 |
# File 'lib/main.rb', line 37 def initialize(argv) @argv = argv @arguments = DEFAULT_ARGUMENTS.dup @opt_parser = OptionParser.new do |o| o. = "Usage: #{$0} [options] [config_file]" o.on('-d', '--debug', 'Output debugging information') do |d| @arguments['debug'] = d end o.on('-q', '--quiet', 'Do not print final report') do |q| @arguments['quiet'] = q end o.on('-e', '--email-off', 'Do not send email, even if configured') do |q| @arguments['email'] = !q end o.on('-c','--checks=FILE', 'Additional checks file') do |c| @arguments['extra_checks'] << c end o.on('-s FILE','--sample-config FILE', 'Create a sample config named FILE') do |sample| @arguments['sample'] = sample end o.on('-v', '--version', 'Display version string') do |v| puts "Ruby Reckoner version #{Main::VERSION}" exit end end @opt_parser.parse!(@argv) end |
Class Method Details
.error_out(message) ⇒ Object
31 32 33 34 35 |
# File 'lib/main.rb', line 31 def self.error_out() STDERR.puts "Aborting Reckoner operations!" STDERR.puts exit 1 end |
Instance Method Details
#make_sample_file(file_name) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/main.rb', line 144 def make_sample_file(file_name) out = File.open(file_name,'w') out.write SAMPLE_CONFIGURATION_FILE out.close "Generated file #{file_name}" end |
#output_results(cm) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/main.rb', line 131 def output_results(cm) s = String.new if cm.errors.empty? s = "No failed checks" else s = "#{cm.errors.length} problem(s) found!\n" cm.errors.each do |err| s << err + "\n" end end return s end |
#run_config(config_file) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/main.rb', line 83 def run_config(config_file) begin yaml_tree = YAML::load(config_file) rescue ArgumentError => e Main.error_out "There was an error parsing the YAML configuration file." end cm = Reckoner.new(@arguments['extra_checks'],@arguments['debug']) cm.check(yaml_tree['check']) if yaml_tree['mail'] && @arguments['email'] send_email( cm, yaml_tree['mail']) end if @arguments['quiet'] return nil else return output_results(cm) end end |
#run_reckoner ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/main.rb', line 65 def run_reckoner if @arguments['sample'] return(make_sample_file(@arguments['sample'])) end if @argv.length > 1 Main.error_out "Aborting Reckoner: Too many arguments\n" + @opt_parser.help end if @argv.empty? config_file = STDIN.read else config_file = File.read(@argv[0]) end run_config(config_file) end |
#send_email(cm, mail_config) ⇒ Object
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 |
# File 'lib/main.rb', line 104 def send_email(cm,mail_config) raise "Mail section must have a 'to' parameter" unless mail_config['to'] always_mail = mail_config['always_mail'] || true mail = Mail.new mail[:to] = mail_config['to'] mail_user = ENV['USER'] || 'reckoner' mail_host = Socket.gethostname || ENV['HOSTNAME'] || 'unknown' mail[:from] = mail_user + '@' + mail_host.downcase prefix = mail_config['subject_prefix'] || 'Reckoner:' prefix.strip if !cm.errors.empty? mail[:subject] = "#{prefix} Found #{cm.errors.length} problem(s)" mail[:body] = cm.errors.join("\n") elsif always_mail mail[:subject] = "#{prefix} No problems found" mail[:body] = "No problems found" end mail.deliver! end |