Class: Puppet::Application

Inherits:
Object show all
Extended by:
Util
Includes:
Util
Defined in:
lib/vendor/puppet/application.rb

Defined Under Namespace

Classes: Agent, Apply, Ca, Catalog, Cert, Certificate, Certificate_request, Certificate_revocation_list, Config, Describe, Device, Doc, FaceBase, Facts, File, Filebucket, Help, IndirectionBase, Inspect, Instrumentation_data, Instrumentation_listener, Instrumentation_probe, Key, Kick, Man, Master, Module, Node, Parser, Plugin, Queue, Report, Resource, Resource_type, Secret_agent, Status

Constant Summary collapse

DOCPATTERN =
::File.expand_path(::File.dirname(__FILE__) + "/util/command_line/*" )

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, execfail, execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Constructor Details

#initialize(command_line = nil) ⇒ Application

Returns a new instance of Application.



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vendor/puppet/application.rb', line 265

def initialize(command_line = nil)

  require 'puppet/util/command_line'
  @command_line = command_line || Puppet::Util::CommandLine.new
  set_run_mode self.class.run_mode
  @options = {}

  require 'puppet'
  require 'puppet/util/instrumentation'
  Puppet::Util::Instrumentation.init
end

Class Attribute Details

.run_statusObject

Returns the value of attribute run_status.



127
128
129
# File 'lib/vendor/puppet/application.rb', line 127

def run_status
  @run_status
end

Instance Attribute Details

#command_lineObject (readonly)

Returns the value of attribute command_line.



243
244
245
# File 'lib/vendor/puppet/application.rb', line 243

def command_line
  @command_line
end

#optionsObject (readonly)

Returns the value of attribute options.



243
244
245
# File 'lib/vendor/puppet/application.rb', line 243

def options
  @options
end

Class Method Details

.[](name) ⇒ Object



228
229
230
# File 'lib/vendor/puppet/application.rb', line 228

def [](name)
  find(name).new
end


204
205
206
# File 'lib/vendor/puppet/application.rb', line 204

def banner(banner = nil)
  @banner ||= banner
end

.clear!Object



129
130
131
# File 'lib/vendor/puppet/application.rb', line 129

def clear!
  self.run_status = nil
end

.clear?Boolean

Indicates that Puppet::Application believes that it’s in usual running run_mode (no stop/restart request currently active).

Returns:

  • (Boolean)


161
162
163
# File 'lib/vendor/puppet/application.rb', line 161

def clear?
  run_status.nil?
end

.controlled_run(&block) ⇒ Object

Only executes the given block if the run status of Puppet::Application is clear (no restarts, stops, etc. requested). Upon block execution, checks the run status again; if a restart has been requested during the block’s execution, then controlled_run will send a new HUP signal to the current process. Thus, long-running background processes can potentially finish their work before a restart.



170
171
172
173
174
175
# File 'lib/vendor/puppet/application.rb', line 170

def controlled_run(&block)
  return unless clear?
  result = block.call
  Process.kill(:HUP, $PID) if restart_requested?
  result
end

.exit(code) ⇒ Object

this is used for testing



392
393
394
# File 'lib/vendor/puppet/application.rb', line 392

def self.exit(code)
  exit(code)
end

.find(name) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/vendor/puppet/application.rb', line 215

def find(name)
  klass = name.to_s.capitalize

  begin
    require ::File.join('puppet', 'application', name.to_s.downcase)
  rescue LoadError => e
    puts "Unable to find application '#{name}'.  #{e}"
    Kernel::exit(1)
  end

  self.const_get(klass)
end

.interrupted?Boolean

Indicates that one of stop! or start! was invoked on Puppet::Application, and some kind of process shutdown/short-circuit may be necessary.

Returns:

  • (Boolean)


155
156
157
# File 'lib/vendor/puppet/application.rb', line 155

def interrupted?
  [:restart_requested, :stop_requested].include? run_status
end

.option(*options, &block) ⇒ Object

used to declare code that handle an option



191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/vendor/puppet/application.rb', line 191

def option(*options, &block)
  long = options.find { |opt| opt =~ /^--/ }.gsub(/^--(?:\[no-\])?([^ =]+).*$/, '\1' ).gsub('-','_')
  fname = "handle_#{long}".intern
  if (block_given?)
    define_method(fname, &block)
  else
    define_method(fname) do |value|
      self.options["#{long}".to_sym] = value
    end
  end
  self.option_parser_commands << [options, fname]
end

.option_parser_commandsObject



208
209
210
211
212
213
# File 'lib/vendor/puppet/application.rb', line 208

def option_parser_commands
  @option_parser_commands ||= (
    superclass.respond_to?(:option_parser_commands) ? superclass.option_parser_commands.dup : []
  )
  @option_parser_commands
end

.restart!Object



137
138
139
# File 'lib/vendor/puppet/application.rb', line 137

def restart!
  self.run_status = :restart_requested
end

.restart_requested?Boolean

Indicates that Puppet::Application.restart! has been invoked and components should do what is necessary to facilitate a restart.

Returns:

  • (Boolean)


143
144
145
# File 'lib/vendor/puppet/application.rb', line 143

def restart_requested?
  :restart_requested == run_status
end

.run_mode(mode_name = nil) ⇒ Object

Sets or gets the run_mode name. Sets the run_mode name if a mode_name is passed. Otherwise, gets the run_mode or a default run_mode



235
236
237
238
239
240
# File 'lib/vendor/puppet/application.rb', line 235

def run_mode( mode_name = nil)
  return @run_mode if @run_mode and not mode_name

  require 'puppet/util/run_mode'
  @run_mode = Puppet::Util::RunMode[ mode_name || :user ]
end

.should_not_parse_configObject



181
182
183
# File 'lib/vendor/puppet/application.rb', line 181

def should_not_parse_config
  @parse_config = false
end

.should_parse_configObject



177
178
179
# File 'lib/vendor/puppet/application.rb', line 177

def should_parse_config
  @parse_config = true
end

.should_parse_config?Boolean

Returns:

  • (Boolean)


185
186
187
188
# File 'lib/vendor/puppet/application.rb', line 185

def should_parse_config?
  @parse_config = true if ! defined?(@parse_config)
  @parse_config
end

.stop!Object



133
134
135
# File 'lib/vendor/puppet/application.rb', line 133

def stop!
  self.run_status = :stop_requested
end

.stop_requested?Boolean

Indicates that Puppet::Application.stop! has been invoked and components should do what is necessary for a clean stop.

Returns:

  • (Boolean)


149
150
151
# File 'lib/vendor/puppet/application.rb', line 149

def stop_requested?
  :stop_requested == run_status
end

Instance Method Details

#configure_indirector_routesObject



337
338
339
340
341
342
343
344
# File 'lib/vendor/puppet/application.rb', line 337

def configure_indirector_routes
  route_file = Puppet[:route_file]
  if ::File.exists?(route_file)
    routes = YAML.load_file(route_file)
    application_routes = routes[name.to_s]
    Puppet::Indirector.configure_routes(application_routes) if application_routes
  end
end

#handlearg(opt, arg) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/vendor/puppet/application.rb', line 373

def handlearg(opt, arg)
  # rewrite --[no-]option to --no-option if that's what was given
  if opt =~ /\[no-\]/ and !arg
    opt = opt.gsub(/\[no-\]/,'no-')
  end
  # otherwise remove the [no-] prefix to not confuse everybody
  opt = opt.gsub(/\[no-\]/, '')
  unless respond_to?(:handle_unknown) and send(:handle_unknown, opt, arg)
    # Puppet.settings.handlearg doesn't handle direct true/false :-)
    if arg.is_a?(FalseClass)
      arg = "false"
    elsif arg.is_a?(TrueClass)
      arg = "true"
    end
    Puppet.settings.handlearg(opt, arg)
  end
end

#helpObject



400
401
402
# File 'lib/vendor/puppet/application.rb', line 400

def help
  "No help available for puppet #{name}"
end

#mainObject

Raises:

  • (NotImplementedError)


312
313
314
# File 'lib/vendor/puppet/application.rb', line 312

def main
  raise NotImplementedError, "No valid command or main"
end

#nameObject



396
397
398
# File 'lib/vendor/puppet/application.rb', line 396

def name
  self.class.to_s.sub(/.*::/,"").downcase.to_sym
end

#parse_optionsObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/vendor/puppet/application.rb', line 346

def parse_options
  # Create an option parser
  option_parser = OptionParser.new(self.class.banner)

  # Add all global options to it.
  Puppet.settings.optparse_addargs([]).each do |option|
    option_parser.on(*option) do |arg|
      handlearg(option[0], arg)
    end
  end

  # Add options that are local to this application, which were
  # created using the "option()" metaprogramming method.  If there
  # are any conflicts, this application's options will be favored.
  self.class.option_parser_commands.each do |options, fname|
    option_parser.on(*options) do |value|
      # Call the method that "option()" created.
      self.send(fname, value)
    end
  end

  # Scan command line.  We just hand any exceptions to our upper levels,
  # rather than printing help and exiting, so that we can meaningfully
  # respond with context-sensitive help if we want to. --daniel 2011-04-12
  option_parser.parse!(self.command_line.args)
end

#preinitObject

override to execute code before running anything else



262
263
# File 'lib/vendor/puppet/application.rb', line 262

def preinit
end

#runObject

This is the main application entry point



303
304
305
306
307
308
309
310
# File 'lib/vendor/puppet/application.rb', line 303

def run
  exit_on_fail("initialize")                                   { hook('preinit')       { preinit } }
  exit_on_fail("parse options")                                { hook('parse_options') { parse_options } }
  exit_on_fail("parse configuration file")                     { Puppet.settings.parse } if should_parse_config?
  exit_on_fail("prepare for execution")                        { hook('setup')         { setup } }
  exit_on_fail("configure routes from #{Puppet[:route_file]}") { configure_indirector_routes }
  exit_on_fail("run")                                          { hook('run_command')   { run_command } }
end

#run_commandObject



316
317
318
# File 'lib/vendor/puppet/application.rb', line 316

def run_command
  main
end

#set_run_mode(mode) ⇒ Object

WARNING: This is a totally scary, frightening, and nasty internal API. We strongly advise that you do not use this, and if you insist, we will politely allow you to keep both pieces of your broken code.

We plan to provide a supported, long-term API to deliver this in a way that you can use. Please make sure that you let us know if you do require this, and this message is still present in the code. –daniel 2011-02-03



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/vendor/puppet/application.rb', line 284

def set_run_mode(mode)
  @run_mode = mode
  $puppet_application_mode = @run_mode
  $puppet_application_name = name

  if Puppet.respond_to? :settings
    # This is to reduce the amount of confusion in rspec
    # because it might have loaded defaults.rb before the globals were set
    # and thus have the wrong defaults for the current application
    Puppet.settings.set_value(:confdir, Puppet.run_mode.conf_dir, :mutable_defaults)
    Puppet.settings.set_value(:vardir, Puppet.run_mode.var_dir, :mutable_defaults)
    Puppet.settings.set_value(:name, Puppet.application_name.to_s, :mutable_defaults)
    Puppet.settings.set_value(:logdir, Puppet.run_mode.logopts, :mutable_defaults)
    Puppet.settings.set_value(:rundir, Puppet.run_mode.run_dir, :mutable_defaults)
    Puppet.settings.set_value(:run_mode, Puppet.run_mode.name.to_s, :mutable_defaults)
  end
end

#setupObject



320
321
322
# File 'lib/vendor/puppet/application.rb', line 320

def setup
  setup_logs
end

#setup_logsObject



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/vendor/puppet/application.rb', line 324

def setup_logs
  if options[:debug] or options[:verbose]
    Puppet::Util::Log.newdestination(:console)
    if options[:debug]
      Puppet::Util::Log.level = :debug
    else
      Puppet::Util::Log.level = :info
    end
  end

  Puppet::Util::Log.setup_default unless options[:setdest]
end

#should_parse_config?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'lib/vendor/puppet/application.rb', line 257

def should_parse_config?
  self.class.should_parse_config?
end