Skip to content

Commit 3df8a59

Browse files
author
AJ Christensen
committed
CHEF-151: Additional changes to the Application class, specifications
Moved Chef.fatal! into the Chef::Application class, updated other libraries and specs. Added a little consistency to the use of Chef::Config[:log_level] in the log#init, so it can be stubbed appropriately
1 parent 4004bcb commit 3df8a59

8 files changed

Lines changed: 317 additions & 64 deletions

File tree

chef-server/bin/chef-indexer

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require 'chef/queue'
2525
require 'chef/search_index'
2626
require 'chef/daemon'
2727

28-
trap("INT") { Chef.fatal!("SIGINT received, stopping", 2) }
28+
trap("INT") { Chef::Application.fatal!("SIGINT received, stopping", 2) }
2929

3030
config = {
3131
:config_file => "/etc/chef/server.rb",
@@ -58,7 +58,7 @@ end
5858
opts.parse!(ARGV)
5959

6060
unless File.exists?(config[:config_file]) and File.readable?(config[:config_file])
61-
Chef.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
61+
Chef::Application.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
6262
end
6363

6464
Chef::Config.from_file(config[:config_file])

chef/bin/chef-client

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ opts = OptionParser.new do |opts|
5959
end
6060
opts.parse!(ARGV)
6161

62-
trap("INT") { Chef.fatal!("SIGINT received, stopping", 2) }
62+
trap("INT") { Chef::Application.fatal!("SIGINT received, stopping", 2) }
6363
trap("HUP") {
6464
Chef::Log.info("SIGHUP received, reloading configuration")
6565
Chef::Config.from_file(config[:config_file])
6666
Chef::Config.configure { |c| c.merge!(config) }
6767
}
6868

6969
unless File.exists?(config[:config_file]) and File.readable?(config[:config_file])
70-
Chef.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
70+
Chef::Application.fatal!("I cannot find or read the config file: #{config[:config_file]}", 1)
7171
end
7272

7373
if config[:json_attribs]
@@ -78,21 +78,21 @@ if config[:json_attribs]
7878
begin
7979
json_io = open(config[:json_attribs])
8080
rescue SocketError => error
81-
Chef.fatal!("I cannot connect to #{config[:json_attribs]}", 2)
81+
Chef::Application.fatal!("I cannot connect to #{config[:json_attribs]}", 2)
8282
rescue Errno::ENOENT => error
83-
Chef.fatal!("I cannot find #{config[:json_attribs]}", 2)
83+
Chef::Application.fatal!("I cannot find #{config[:json_attribs]}", 2)
8484
rescue Errno::EACCES => error
85-
Chef.fatal!("Permissions are incorrect on #{config[:json_attribs]}. Please chmod a+r #{config[:json_attribs]}", 2)
85+
Chef::Application.fatal!("Permissions are incorrect on #{config[:json_attribs]}. Please chmod a+r #{config[:json_attribs]}", 2)
8686
rescue Exception => error
87-
Chef.fatal!("Got an unexpected error reading #{config[:json_attribs]}: #{error.message}", 2)
87+
Chef::Application.fatal!("Got an unexpected error reading #{config[:json_attribs]}: #{error.message}", 2)
8888
end
8989

9090
json_contents = json_io.read
9191

9292
begin
9393
config[:json_attribs] = JSON.parse(json_contents)
9494
rescue JSON::ParserError => error
95-
Chef.fatal!("Could not parse the provided JSON file (#{config[:json_attribs]})!: " + error.message, 2)
95+
Chef::Application.fatal!("Could not parse the provided JSON file (#{config[:json_attribs]})!: " + error.message, 2)
9696
exit 2
9797
end
9898
end

chef/lib/chef.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@
2828

2929
class Chef
3030
VERSION = '0.6.2'
31-
32-
class << self
33-
def fatal!(msg, err = -1)
34-
Chef::Log.fatal(msg)
35-
exit err
36-
end
37-
end
3831
end
3932

4033
# Adds a Dir.glob to Ruby 1.8.5, for compat

chef/lib/chef/application.rb

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818
require 'optparse'
1919
require 'chef'
2020
require 'chef/exceptions'
21-
require 'chef/logger'
21+
require 'chef/log'
2222

2323
class Chef::Application
24-
attr_accessor :argv, :config, :options, :opt_parser
2524

26-
def initialize()
25+
attr_accessor :argv, :config, :options
26+
27+
def initialize
2728
@argv = ARGV.dup
28-
@config = { :config_file => "/etc/chef/default.rb" }
29-
@options = nil
30-
@opt_parser = nil
29+
@config = {}
30+
@options = {}
3131

3232
trap("INT") do
33-
Chef.fatal!("SIGINT received, stopping", 2)
33+
Chef::Application.fatal!("SIGINT received, stopping", 2)
3434
end
3535

3636
trap("HUP") do
@@ -40,24 +40,26 @@ def initialize()
4040

4141
at_exit do
4242
# tear down the logger and shit
43-
end
44-
43+
end
4544
end
4645

46+
# Reconfigure the application. You'll want to override this.
4747
def reconfigure
4848
configure_opt_parser
4949
configure_chef
50-
parse_arguments
5150
configure_logging
5251
end
5352

53+
# Kick off the application
5454
def run
5555
reconfigure
56-
get_this_party_started
56+
setup_application
57+
run_application
5758
end
5859

60+
# Build an Option Parser, merge the default options and then parse the command line arguments into the config hash
5961
def configure_opt_parser
60-
@opt_parser = OptionParser.new do |opts|
62+
OptionParser.new do |opts|
6163
opts.banner = "Usage: #{$0} (options)"
6264

6365
default_options = {
@@ -79,53 +81,49 @@ def configure_opt_parser
7981
}
8082

8183
# just a step to the left
82-
@options = default_options.merge(@options) if @options
83-
84-
# setup our instance config hash
84+
@options = default_options.merge(@options)
85+
86+
# Add the CLI options into OptionParser
8587
@options.each do |opt_key, opt_val|
8688
opts.on(opt_val[:short], opt_val[:long], opt_val[:description]) do |c|
89+
# Update our internal Chef::Config hash, to be merged later.
8790
@config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c
8891
end
8992
end
9093

9194
opts.on_tail("-h", "--help", "Show this message") do
9295
puts opts
93-
exit 0
96+
self.fatal!("Exiting", 0)
9497
end
95-
end
98+
end.parse!(@argv)
9699
end
97100

98101
# Parse the configuration file
99102
def configure_chef
100-
Chef::Config.from_file(@config[:config_file])
103+
Chef::Config.from_file(@config[:config_file]) if @config[:config_file]
101104
Chef::Config.configure { |c| c.merge!(@config).rehash }
102105
end
103106

104107
# Initialize and configure the logger
105108
def configure_logging
106109
Chef::Log.init(Chef::Config[:log_location])
107-
Chef::Log.level(Chef::Config[:log_level])
108110
end
109111

110-
# Parse ARGV for arguments
111-
def parse_arguments
112-
raise Chef::Exceptions::Application, "#{self.to_s}: option parser has not been configured" unless @opt_parser
113-
@opt_parser.parse!(@argv)
112+
class << self
113+
# Log a fatal error message and exit the application
114+
def fatal!(msg, err = -1)
115+
Chef::Log.fatal(msg)
116+
Process.exit err
117+
end
114118
end
115119

116-
# Log a fatal error message and exit the process
117-
def fatal!(msg, err = -1)
118-
Chef::Log.fatal(msg)
119-
exit err
120+
# Called prior to starting the application, by the run method
121+
def setup_application
122+
raise Chef::Exceptions::Application, "#{self.to_s}: you must override setup_application"
120123
end
121124

122-
private
123-
124-
def get_this_party_started
125-
raise Chef::Exceptions::Application, "#{self.to_s}: you must override get_this_party_started"
125+
# Actually run the application
126+
def run_application
127+
raise Chef::Exceptions::Application, "#{self.to_s}: you must override run_application"
126128
end
127-
128-
end
129-
130-
131-
Chef::Application.new.run
129+
end

chef/lib/chef/daemon.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ def daemonize(name)
4848
save_pid_file
4949
at_exit { remove_pid_file }
5050
rescue NotImplementedError => e
51-
Chef.fatal!("There is no fork: #{e.message}")
51+
Chef::Application.fatal!("There is no fork: #{e.message}")
5252
end
5353
else
54-
Chef.fatal!("Chef is already running pid #{pid}")
54+
Chef::Application.fatal!("Chef is already running pid #{pid}")
5555
end
5656
end
5757

@@ -71,7 +71,7 @@ def running?
7171
rescue Errno::ESRCH, Errno::ENOENT
7272
false
7373
rescue Errno::EACCES => e
74-
Chef.fatal!("You don't have access to the PID file at #{pid_file}: #{e.message}")
74+
Chef::Application.fatal!("You don't have access to the PID file at #{pid_file}: #{e.message}")
7575
end
7676

7777
# Gets the pid file for @name
@@ -103,13 +103,13 @@ def save_pid_file
103103
begin
104104
FileUtils.mkdir_p(File.dirname(file))
105105
rescue Errno::EACCES => e
106-
Chef.fatal!("Failed store pid in #{File.dirname(file)}, permission denied: #{e.message}")
106+
Chef::Application.fatal!("Failed store pid in #{File.dirname(file)}, permission denied: #{e.message}")
107107
end
108108

109109
begin
110110
File.open(file, "w") { |f| f.write(Process.pid.to_s) }
111111
rescue Errno::EACCES => e
112-
Chef.fatal!("Couldn't write to pidfile #{file}, permission denied: #{e.message}")
112+
Chef::Application.fatal!("Couldn't write to pidfile #{file}, permission denied: #{e.message}")
113113
end
114114
end
115115

@@ -145,14 +145,14 @@ def _change_privilege(user, group=user)
145145
begin
146146
target_uid = Etc.getpwnam(user).uid
147147
rescue ArgumentError => e
148-
Chef.fatal!("Failed to get UID for user #{user}, does it exist? #{e.message}")
148+
Chef::Application.fatal!("Failed to get UID for user #{user}, does it exist? #{e.message}")
149149
return false
150150
end
151151

152152
begin
153153
target_gid = Etc.getgrnam(group).gid
154154
rescue ArgumentError => e
155-
Chef.fatal!("Failed to get GID for group #{group}, does it exist? #{e.message}")
155+
Chef::Application.fatal!("Failed to get GID for group #{group}, does it exist? #{e.message}")
156156
return false
157157
end
158158

@@ -163,7 +163,7 @@ def _change_privilege(user, group=user)
163163
end
164164
true
165165
rescue Errno::EPERM => e
166-
Chef.fatal!("Permission denied when trying to change #{uid}:#{gid} to #{target_uid}:#{target_gid}. #{e.message}")
166+
Chef::Application.fatal!("Permission denied when trying to change #{uid}:#{gid} to #{target_uid}:#{target_gid}. #{e.message}")
167167
end
168168
end
169169
end

chef/lib/chef/log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def init(*opts)
4444
@logger = Logger.new(*opts)
4545
end
4646
@logger.formatter = Chef::Log::Formatter.new()
47-
level(Chef::Config.log_level)
47+
level(Chef::Config[:log_level])
4848
end
4949

5050
# Sets the level for the Logger object by symbol. Valid arguments are:

0 commit comments

Comments
 (0)