Class: TemplateConfigurator::CommandLine
- Inherits:
-
Object
- Object
- TemplateConfigurator::CommandLine
- Defined in:
- lib/template_configurator/command_line.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize ⇒ CommandLine
constructor
A new instance of CommandLine.
Constructor Details
#initialize ⇒ CommandLine
Returns a new instance of CommandLine.
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 |
# File 'lib/template_configurator/command_line.rb', line 27 def initialize @logger = Logger.new STDERR = {} [:log] = {} [:template] = {} [:service] = {} args = OptionParser.new do |opts| opts. = "Usage: #{$0}" # # Service options # [:service][:command] = '/sbin/service' opts.on("--service:command EXECUTABLE", "action to execute to command service (default: #{@options[:service][:command]})") do |executable| [:service][:command] = executable end [:service][:name] = nil opts.on("--service:name INITRC", "initrc used to control service (default: #{@options[:service][:name]})") do |initrc| [:service][:name] = initrc end [:service][:status] = 'status' opts.on("--service:status ACTION", "action to execute to get status of service (default: #{@options[:service][:status]})") do |action| [:service][:status] = action end [:service][:reload] = 'reload' opts.on("--service:reload ACTION", "action to execute to reload service (default: #{@options[:service][:reload]})") do |action| [:service][:reload] = action end [:service][:restart] = 'restart' opts.on("--service:restart ACTION", "action to execute to restart service (default: #{@options[:service][:restart]})") do |action| [:service][:restart] = action end [:service][:start] = 'start' opts.on("--service:start ACTION", "action to execute to start service (default: #{@options[:service][:start]})") do |action| [:service][:start] = action end [:service][:stop] = 'stop' opts.on("--service:stop ACTION", "action to execute to stop service (default: #{@options[:service][:stop]})") do |action| [:service][:stop] = action end [:service][:retries] = 5 opts.on("--service:retries NUMBER", "number of attempts to reload service (default: #{@options[:service][:retries]})") do |number| [:service][:retries] = number.to_i end [:service][:retry_delay] = 2 opts.on("--service:retry-delay SECS", "seconds to sleep between retries (default: #{@options[:service][:retry_delay]})") do |seconds| [:service][:retry_delay] = seconds.to_i end # # Template options # [:template][:input_file] = nil opts.on("--template:input-file FILE", "Where to read ERB template") do |file| [:template][:input_file] = file end [:template][:output_file] = nil opts.on("--template:output-file FILE", "Where to write the output of the template") do |file| [:template][:output_file] = file end [:template][:json_file] = nil opts.on("--template:json-file FILE", "Base port to initialize haproxy listening for mysql clusters") do |file| [:template][:json_file] = file end # # Logging # [:log][:level] = Logger::INFO opts.on( '--log:level LEVEL', 'Logging level' ) do|level| [:log][:level] = Logger.const_get level.upcase end [:log][:file] = STDERR opts.on( '--log:file FILE', 'Write logs to FILE (default: STDERR)' ) do|file| [:log][:file] = File.open(file, File::WRONLY | File::APPEND | File::CREAT) end [:log][:age] = 7 opts.on( '--log:age DAYS', "Rotate logs after DAYS pass (default: #{@options[:log][:age]})" ) do|days| [:log][:age] = days.to_i end [:log][:size] = 1024*1024*10 opts.on( '--log:size SIZE', 'Rotate logs after the grow past SIZE bytes' ) do |size| [:log][:size] = size.to_i end # # General options # [:dry_run] = false opts.on("--dry-run", "Dry run (do not commit changes to disk)") do [:dry_run] = true end opts.on( '-V', '--version', 'Display version information' ) do puts "Template Configurator #{TemplateConfigurator::VERSION}" puts "Copyright (C) 2012 Erik Osterman <[email protected]>" puts "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" puts "This is free software: you are free to change and redistribute it." puts "There is NO WARRANTY, to the extent permitted by law." exit end opts.on( '-h', '--help', 'Display this screen' ) do puts opts exit end end begin args.parse! raise OptionParser::MissingArgument.new("--template:input-file") if [:template][:input_file].nil? rescue OptionParser::MissingArgument => e puts e. puts args exit 1 rescue OptionParser::InvalidOption => e puts e. puts args exit 1 end end |
Instance Method Details
#execute ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/template_configurator/command_line.rb', line 171 def execute @processor = Processor.new() TemplateConfigurator.log = Logger.new([:log][:file], [:log][:age], [:log][:size]) TemplateConfigurator.log.level = [:log][:level] begin @processor.render rescue Interrupt => e TemplateConfigurator.log.info("Aborting") rescue NameError, ArgumentError => e TemplateConfigurator.log.fatal(e.) TemplateConfigurator.log.debug(e.backtrace.join("\n")) exit(1) rescue Exception => e TemplateConfigurator.log.fatal("#{e.class}: #{e.message}") TemplateConfigurator.log.debug(e.backtrace.join("\n")) exit(1) end exit(0) end |