Class: TemplateConfigurator::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/template_configurator/command_line.rb

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

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

  @options = {}
  @options[:log] = {}
  @options[:template] = {}
  @options[:service] = {}

  args = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0}"

    #
    # Service options
    #
    @options[:service][:command] = '/sbin/service'
    opts.on("--service:command EXECUTABLE", "action to execute to command service (default: #{@options[:service][:command]})") do |executable|
      @options[:service][:command] = executable
    end

    @options[:service][:name] = nil
    opts.on("--service:name INITRC", "initrc used to control service (default: #{@options[:service][:name]})") do |initrc|
      @options[:service][:name] = initrc
    end

    @options[:service][:status] = 'status'
    opts.on("--service:status ACTION", "action to execute to get status of service (default: #{@options[:service][:status]})") do |action|
      @options[:service][:status] = action
    end

    @options[:service][:reload] = 'reload'
    opts.on("--service:reload ACTION", "action to execute to reload service (default: #{@options[:service][:reload]})") do |action|
      @options[:service][:reload] = action
    end

    @options[:service][:restart] = 'restart'
    opts.on("--service:restart ACTION", "action to execute to restart service (default: #{@options[:service][:restart]})") do |action|
      @options[:service][:restart] = action
    end

    @options[:service][:start] = 'start'
    opts.on("--service:start ACTION", "action to execute to start service (default: #{@options[:service][:start]})") do |action|
      @options[:service][:start] = action
    end

    @options[:service][:stop] = 'stop'
    opts.on("--service:stop ACTION", "action to execute to stop service (default: #{@options[:service][:stop]})") do |action|
      @options[:service][:stop] = action
    end


    @options[:service][:retries] = 5
    opts.on("--service:retries NUMBER", "number of attempts to reload service (default: #{@options[:service][:retries]})") do |number|
      @options[:service][:retries] = number.to_i
    end

    @options[:service][:retry_delay] = 2
    opts.on("--service:retry-delay SECS", "seconds to sleep between retries (default: #{@options[:service][:retry_delay]})") do |seconds|
      @options[:service][:retry_delay] = seconds.to_i
    end

    #
    # Template options
    #

    @options[:template][:input_file] = nil
    opts.on("--template:input-file FILE", "Where to read ERB template") do |file|
      @options[:template][:input_file] = file
    end

    @options[:template][:output_file] = nil
    opts.on("--template:output-file FILE", "Where to write the output of the template") do |file|
      @options[:template][:output_file] = file
    end

    @options[:template][:json_file] = nil
    opts.on("--template:json-file FILE", "Base port to initialize haproxy listening for mysql clusters") do |file|
      @options[:template][:json_file] = file
    end

    #
    # Logging
    #

    @options[:log][:level] = Logger::INFO
    opts.on( '--log:level LEVEL', 'Logging level' ) do|level|
      @options[:log][:level] = Logger.const_get level.upcase
    end

    @options[:log][:file] = STDERR
    opts.on( '--log:file FILE', 'Write logs to FILE (default: STDERR)' ) do|file|
      @options[:log][:file] = File.open(file, File::WRONLY | File::APPEND | File::CREAT)
    end

    @options[:log][:age] = 7
    opts.on( '--log:age DAYS', "Rotate logs after DAYS pass (default: #{@options[:log][:age]})" ) do|days|
      @options[:log][:age] = days.to_i
    end

    @options[:log][:size] = 1024*1024*10
    opts.on( '--log:size SIZE', 'Rotate logs after the grow past SIZE bytes' ) do |size|
      @options[:log][:size] = size.to_i
    end


    #
    # General options
    #

    @options[:dry_run] = false
    opts.on("--dry-run", "Dry run (do not commit changes to disk)") do 
      @options[: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 @options[:template][:input_file].nil?
  rescue OptionParser::MissingArgument => e
    puts e.message
    puts args
    exit 1
  rescue OptionParser::InvalidOption => e
    puts e.message
    puts args
    exit 1
  end
end

Instance Method Details

#executeObject



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(@options)
  TemplateConfigurator.log = Logger.new(@options[:log][:file], @options[:log][:age], @options[:log][:size])
  TemplateConfigurator.log.level = @options[:log][:level]
  begin
    @processor.render
  rescue Interrupt => e
    TemplateConfigurator.log.info("Aborting")
  rescue NameError, ArgumentError => e
    TemplateConfigurator.log.fatal(e.message)
    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