Class: Chef::Application

Inherits:
Object
  • 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
46
47
48
49
50
# 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

  unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
    trap("HUP") do
      Chef::Log.info("SIGHUP received, reconfiguring")
      reconfigure
    end
  end

  at_exit do
    # tear down the logger
  end

  # Always switch to a readable directory. Keeps subsequent Dir.chdir() {}
  # from failing due to permissions when launched as a less privileged user.
end

Class Method Details

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



121
122
123
124
# File 'lib/chef/application.rb', line 121

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



115
116
117
118
119
# File 'lib/chef/application.rb', line 115

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/application.rb', line 66

def configure_chef
  parse_options

  begin
    case config[:config_file]
    when /^(http|https):\/\//
      Chef::REST.new("", nil, nil).fetch(config[:config_file]) { |f| apply_config(f.path) }
    else
      ::File::open(config[:config_file]) { |f| apply_config(f.path) }
    end
  rescue SocketError => error
    Chef::Application.fatal!("Error getting config file #{Chef::Config[:config_file]}", 2)
  rescue Exception => error
    Chef::Log.warn("*****************************************")
    Chef::Log.warn("Can not find config file: #{config[:config_file]}, using defaults.")
    Chef::Log.warn("#{error.message}")
    Chef::Log.warn("*****************************************")

    Chef::Config.merge!(config)
  end

end

#configure_loggingObject

Initialize and configure the logger



90
91
92
93
# File 'lib/chef/application.rb', line 90

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.



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

def reconfigure
  configure_chef
  configure_logging
end

#runObject

Get this party started



59
60
61
62
63
# File 'lib/chef/application.rb', line 59

def run
  reconfigure
  setup_application
  run_application
end

#run_applicationObject

Actually run the application



101
102
103
# File 'lib/chef/application.rb', line 101

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



96
97
98
# File 'lib/chef/application.rb', line 96

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