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, Wakeup

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



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
# File 'lib/chef/application.rb', line 31

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("QUIT") do
      Chef::Log.info("SIGQUIT received, call stack:\n  " + caller.join("\n  "))
    end

    trap("HUP") do
      Chef::Log.info("SIGHUP received, reconfiguring")
      reconfigure
    end
  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

.debug_stacktrace(e) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/chef/application.rb', line 129

def debug_stacktrace(e)
  message = "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  chef_stacktrace_out = "Generated at #{Time.now.to_s}\n"
  chef_stacktrace_out += message

  Chef::FileCache.store("chef-stacktrace.out", chef_stacktrace_out)
  Chef::Log.fatal("Stacktrace dumped to #{Chef::FileCache.load("chef-stacktrace.out", false)}")
  Chef::Log.debug(message)
  true
end

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



146
147
148
149
# File 'lib/chef/application.rb', line 146

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



141
142
143
144
# File 'lib/chef/application.rb', line 141

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

Instance Method Details

#configure_chefObject

Parse the configuration file



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chef/application.rb', line 71

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. If the configured log location is not STDOUT, but stdout is a TTY and we’re not daemonizing, we set up a secondary logger with output to stdout. This way, we magically do the right thing when the user has configured logging to a file but they’re running chef in the shell to debug something.



99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/application.rb', line 99

def configure_logging
  Chef::Log.init(Chef::Config[:log_location])
  if ( Chef::Config[:log_location] != STDOUT ) && STDOUT.tty? && (!Chef::Config[:daemonize])
    stdout_logger = Logger.new(STDOUT)
    STDOUT.sync = true
    stdout_logger.formatter = Chef::Log.logger.formatter
    Chef::Log.loggers <<  stdout_logger
  end
  Chef::Log.level = Chef::Config[:log_level]
end

#reconfigureObject

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



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

def reconfigure
  configure_chef
  configure_logging
end

#runObject

Get this party started



64
65
66
67
68
# File 'lib/chef/application.rb', line 64

def run
  reconfigure
  setup_application
  run_application
end

#run_applicationObject

Actually run the application



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

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



111
112
113
# File 'lib/chef/application.rb', line 111

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