Class: Puppet::Application
Direct Known Subclasses
Agent, Apply, Cert, Describe, Device, Doc, FaceBase, Filebucket, Inspect, Kick, Master, Queue, Resource, UploadFacts
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, Key, Kick, Man, Master, Node, Parser, Plugin, Queue, Report, Resource, Resource_type, Secret_agent, Status, UploadFacts
Constant Summary collapse
- DOCPATTERN =
File.(File.dirname(__FILE__) + "/util/command_line/*" )
Class Attribute Summary collapse
-
.run_status ⇒ Object
Returns the value of attribute run_status.
Instance Attribute Summary collapse
-
#command_line ⇒ Object
readonly
Returns the value of attribute command_line.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .[](name) ⇒ Object
- .banner(banner = nil) ⇒ Object
- .clear! ⇒ Object
-
.clear? ⇒ Boolean
Indicates that Puppet::Application believes that it’s in usual running run_mode (no stop/restart request currently active).
-
.controlled_run(&block) ⇒ Object
Only executes the given block if the run status of Puppet::Application is clear (no restarts, stops, etc. requested).
-
.exit(code) ⇒ Object
this is used for testing.
- .find(name) ⇒ Object
-
.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.
-
.option(*options, &block) ⇒ Object
used to declare code that handle an option.
- .option_parser_commands ⇒ Object
- .restart! ⇒ Object
-
.restart_requested? ⇒ Boolean
Indicates that Puppet::Application.restart! has been invoked and components should do what is necessary to facilitate a restart.
-
.run_mode(mode_name = nil) ⇒ Object
Sets or gets the run_mode name.
- .should_not_parse_config ⇒ Object
- .should_parse_config ⇒ Object
- .should_parse_config? ⇒ Boolean
- .stop! ⇒ Object
-
.stop_requested? ⇒ Boolean
Indicates that Puppet::Application.stop! has been invoked and components should do what is necessary for a clean stop.
Instance Method Summary collapse
- #configure_indirector_routes ⇒ Object
- #handlearg(opt, arg) ⇒ Object
- #help ⇒ Object
-
#initialize(command_line = nil) ⇒ Application
constructor
A new instance of Application.
- #main ⇒ Object
- #name ⇒ Object
- #parse_options ⇒ Object
-
#preinit ⇒ Object
override to execute code before running anything else.
-
#run ⇒ Object
This is the main application entry point.
- #run_command ⇒ Object
-
#set_run_mode(mode) ⇒ Object
WARNING: This is a totally scary, frightening, and nasty internal API.
- #setup ⇒ Object
- #should_parse_config? ⇒ Boolean
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, recmkdir, secure_open, 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 |
# File 'lib/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 = {} require 'puppet' end |
Class Attribute Details
.run_status ⇒ Object
Returns the value of attribute run_status.
127 128 129 |
# File 'lib/puppet/application.rb', line 127 def run_status @run_status end |
Instance Attribute Details
#command_line ⇒ Object (readonly)
Returns the value of attribute command_line.
243 244 245 |
# File 'lib/puppet/application.rb', line 243 def command_line @command_line end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
243 244 245 |
# File 'lib/puppet/application.rb', line 243 def end |
Class Method Details
.[](name) ⇒ Object
228 229 230 |
# File 'lib/puppet/application.rb', line 228 def [](name) find(name).new end |
.banner(banner = nil) ⇒ Object
204 205 206 |
# File 'lib/puppet/application.rb', line 204 def ( = nil) ||= end |
.clear! ⇒ Object
129 130 131 |
# File 'lib/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).
161 162 163 |
# File 'lib/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/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
386 387 388 |
# File 'lib/puppet/application.rb', line 386 def self.exit(code) exit(code) end |
.find(name) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/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.
155 156 157 |
# File 'lib/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/puppet/application.rb', line 191 def option(*, &block) long = .find { |opt| opt =~ /^--/ }.gsub(/^--(?:\[no-\])?([^ =]+).*$/, '\1' ).gsub('-','_') fname = symbolize("handle_#{long}") if (block_given?) define_method(fname, &block) else define_method(fname) do |value| self.["#{long}".to_sym] = value end end self.option_parser_commands << [, fname] end |
.option_parser_commands ⇒ Object
208 209 210 211 212 213 |
# File 'lib/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/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.
143 144 145 |
# File 'lib/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/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_config ⇒ Object
181 182 183 |
# File 'lib/puppet/application.rb', line 181 def should_not_parse_config @parse_config = false end |
.should_parse_config ⇒ Object
177 178 179 |
# File 'lib/puppet/application.rb', line 177 def should_parse_config @parse_config = true end |
.should_parse_config? ⇒ Boolean
185 186 187 188 |
# File 'lib/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/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.
149 150 151 |
# File 'lib/puppet/application.rb', line 149 def stop_requested? :stop_requested == run_status end |
Instance Method Details
#configure_indirector_routes ⇒ Object
331 332 333 334 335 336 337 338 |
# File 'lib/puppet/application.rb', line 331 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
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/puppet/application.rb', line 367 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 |
#help ⇒ Object
394 395 396 |
# File 'lib/puppet/application.rb', line 394 def help "No help available for puppet #{name}" end |
#main ⇒ Object
309 310 311 |
# File 'lib/puppet/application.rb', line 309 def main raise NotImplementedError, "No valid command or main" end |
#name ⇒ Object
390 391 392 |
# File 'lib/puppet/application.rb', line 390 def name self.class.to_s.sub(/.*::/,"").downcase.to_sym end |
#parse_options ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/puppet/application.rb', line 340 def # Create an option parser option_parser = OptionParser.new(self.class.) # 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 |, fname| option_parser.on(*) 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 |
#preinit ⇒ Object
override to execute code before running anything else
262 263 |
# File 'lib/puppet/application.rb', line 262 def preinit end |
#run ⇒ Object
This is the main application entry point
300 301 302 303 304 305 306 307 |
# File 'lib/puppet/application.rb', line 300 def run exit_on_fail("initialize") { hook('preinit') { preinit } } exit_on_fail("parse options") { hook('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_command ⇒ Object
313 314 315 |
# File 'lib/puppet/application.rb', line 313 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
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/puppet/application.rb', line 281 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 |
#setup ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/puppet/application.rb', line 317 def setup # Handle the logging settings if [:debug] or [:verbose] Puppet::Util::Log.newdestination(:console) if [:debug] Puppet::Util::Log.level = :debug else Puppet::Util::Log.level = :info end end Puppet::Util::Log.newdestination(:syslog) unless [:setdest] end |
#should_parse_config? ⇒ Boolean
257 258 259 |
# File 'lib/puppet/application.rb', line 257 def should_parse_config? self.class.should_parse_config? end |