Class: Chef::Application

Inherits:
Object show all
Includes:
Mixlib::CLI
Defined in:
lib/chef/application.rb

Direct Known Subclasses

Client, Knife, Solo

Defined Under Namespace

Classes: Client, Knife, Solo

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/application.rb', line 26

def initialize   
  super
  
  trap("TERM") do
    Chef::Application.fatal!("SIGTERM received, stopping", 1)
  end

  trap("INT") do
    Chef::Application.fatal!("SIGINT received, stopping", 2)
  end
  
  trap("HUP") do 
    Chef::Log.info("SIGHUP received, reconfiguring")
    reconfigure
  end
  
  at_exit do
    # tear down the logger 
  end  
end

Class Method Details

.exit!(msg, err = -1)) ⇒ Object



92
93
94
95
# File 'lib/chef/application.rb', line 92

def exit!(msg, err = -1)
  Chef::Log.debug(msg)
  Process.exit err
end

.fatal!(msg, err = -1)) ⇒ Object

Log a fatal error message to both STDERR and the Logger, exit the application



86
87
88
89
90
# File 'lib/chef/application.rb', line 86

def fatal!(msg, err = -1)
  STDERR.puts("FATAL: #{msg}")
  Chef::Log.fatal(msg)
  Process.exit err
end

Instance Method Details

#configure_chefObject

Parse the configuration file



61
62
63
64
65
66
# File 'lib/chef/application.rb', line 61

def configure_chef
  parse_options
  
  Chef::Config.from_file(config[:config_file]) if !config[:config_file].nil? && File.exists?(config[:config_file]) && File.readable?(config[:config_file])
  Chef::Config.merge!(config)
end

#configure_loggingObject

Initialize and configure the logger



69
70
71
72
# File 'lib/chef/application.rb', line 69

def configure_logging
  Chef::Log.init(Chef::Config[:log_location])
  Chef::Log.level = Chef::Config[:log_level]
end

#reconfigureObject

Reconfigure the application. You’ll want to override and super this method.



48
49
50
51
# File 'lib/chef/application.rb', line 48

def reconfigure
  configure_chef
  configure_logging
end

#runObject

Get this party started



54
55
56
57
58
# File 'lib/chef/application.rb', line 54

def run
  reconfigure
  setup_application
  run_application
end

#run_applicationObject

Actually run the application



80
81
82
# File 'lib/chef/application.rb', line 80

def run_application
  raise Chef::Exceptions::Application, "#{self.to_s}: you must override run_application"  
end

#setup_applicationObject

Called prior to starting the application, by the run method



75
76
77
# File 'lib/chef/application.rb', line 75

def setup_application
  raise Chef::Exceptions::Application, "#{self.to_s}: you must override setup_application"      
end