Skip to content

Commit 4004bcb

Browse files
author
AJ Christensen
committed
CHEF-151: Initial work refactoring the binary logic into classes
Add 'Application' exception class. Add an Application class, which configures chef, the logger and options parser. Add placeholder specifications for application class and child classes.
1 parent 17de4f8 commit 4004bcb

13 files changed

Lines changed: 222 additions & 0 deletions

File tree

chef/lib/chef/application.rb

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'optparse'
19+
require 'chef'
20+
require 'chef/exceptions'
21+
require 'chef/logger'
22+
23+
class Chef::Application
24+
attr_accessor :argv, :config, :options, :opt_parser
25+
26+
def initialize()
27+
@argv = ARGV.dup
28+
@config = { :config_file => "/etc/chef/default.rb" }
29+
@options = nil
30+
@opt_parser = nil
31+
32+
trap("INT") do
33+
Chef.fatal!("SIGINT received, stopping", 2)
34+
end
35+
36+
trap("HUP") do
37+
Chef::Log.info("SIGHUP received, reconfiguring")
38+
reconfigure
39+
end
40+
41+
at_exit do
42+
# tear down the logger and shit
43+
end
44+
45+
end
46+
47+
def reconfigure
48+
configure_opt_parser
49+
configure_chef
50+
parse_arguments
51+
configure_logging
52+
end
53+
54+
def run
55+
reconfigure
56+
get_this_party_started
57+
end
58+
59+
def configure_opt_parser
60+
@opt_parser = OptionParser.new do |opts|
61+
opts.banner = "Usage: #{$0} (options)"
62+
63+
default_options = {
64+
:config_file => {
65+
:short => "-c CONFIG",
66+
:long => "--config CONFIG",
67+
:description => "The Chef Config file to use",
68+
:proc => nil },
69+
:log_level => {
70+
:short => "-l LEVEL",
71+
:long => "--loglevel LEVEL",
72+
:description => "Set the log level (debug, info, warn, error, fatal)",
73+
:proc => lambda { |p| p.to_sym} },
74+
:log_location => {
75+
:short => "-L LOGLOCATION",
76+
:long => "--logfile LOGLOCATION",
77+
:description => "Set the log file location, defaults to STDOUT - recommended for daemonizing",
78+
:proc => nil }
79+
}
80+
81+
# just a step to the left
82+
@options = default_options.merge(@options) if @options
83+
84+
# setup our instance config hash
85+
@options.each do |opt_key, opt_val|
86+
opts.on(opt_val[:short], opt_val[:long], opt_val[:description]) do |c|
87+
@config[opt_key] = (opt_val[:proc] && opt_val[:proc].call(c)) || c
88+
end
89+
end
90+
91+
opts.on_tail("-h", "--help", "Show this message") do
92+
puts opts
93+
exit 0
94+
end
95+
end
96+
end
97+
98+
# Parse the configuration file
99+
def configure_chef
100+
Chef::Config.from_file(@config[:config_file])
101+
Chef::Config.configure { |c| c.merge!(@config).rehash }
102+
end
103+
104+
# Initialize and configure the logger
105+
def configure_logging
106+
Chef::Log.init(Chef::Config[:log_location])
107+
Chef::Log.level(Chef::Config[:log_level])
108+
end
109+
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)
114+
end
115+
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+
end
121+
122+
private
123+
124+
def get_this_party_started
125+
raise Chef::Exceptions::Application, "#{self.to_s}: you must override get_this_party_started"
126+
end
127+
128+
end
129+
130+
131+
Chef::Application.new.run

chef/lib/chef/application/agent.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'chef/application'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'chef/application'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'chef/application'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'chef/application'

chef/lib/chef/application/solo.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
3+
# Copyright:: Copyright (c) 2008 Opscode, Inc.
4+
# License:: Apache License, Version 2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
require 'chef/application'

chef/lib/chef/exceptions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
class Chef
1919
class Exceptions
20+
class Application < RuntimeError; end
2021
class Cron < RuntimeError; end
2122
class Exec < RuntimeError; end
2223
class FileNotFound < RuntimeError; end

chef/spec/unit/application/agent_spec.rb

Whitespace-only changes.

chef/spec/unit/application/client_spec.rb

Whitespace-only changes.

chef/spec/unit/application/indexer_spec.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)