Skip to content

Commit e00fad6

Browse files
committed
Adding basic handler support
1 parent 76bf963 commit e00fad6

4 files changed

Lines changed: 111 additions & 17 deletions

File tree

‎chef/lib/chef/client.rb‎

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Client
3737
include Chef::Mixin::GenerateURL
3838
include Chef::Mixin::Checksum
3939

40-
attr_accessor :node, :registration, :json_attribs, :validation_token, :node_name, :ohai, :rest
40+
attr_accessor :node, :registration, :json_attribs, :validation_token, :node_name, :ohai, :rest, :runner
4141

4242
# Creates a new Chef::Client.
4343
def initialize()
@@ -47,6 +47,7 @@ def initialize()
4747
@json_attribs = nil
4848
@node_name = nil
4949
@node_exists = true
50+
@runner = nil
5051
@ohai = Ohai::System.new
5152
Chef::Log.verbose = Chef::Config[:verbose_logging]
5253
Mixlib::Authentication::Log.logger = Ohai::Log.logger = Chef::Log.logger
@@ -77,21 +78,32 @@ def initialize()
7778
# === Returns
7879
# true:: Always returns true.
7980
def run
80-
start_time = Time.now
81-
Chef::Log.info("Starting Chef Run")
82-
83-
determine_node_name
84-
register
85-
build_node(@node_name)
86-
save_node
87-
sync_cookbooks
88-
save_node
89-
converge
90-
save_node
91-
92-
end_time = Time.now
93-
Chef::Log.info("Chef Run complete in #{end_time - start_time} seconds")
94-
true
81+
begin
82+
start_time = Time.now
83+
Chef::Log.info("Starting Chef Run")
84+
85+
determine_node_name
86+
register
87+
build_node(@node_name)
88+
save_node
89+
sync_cookbooks
90+
save_node
91+
converge
92+
save_node
93+
94+
end_time = Time.now
95+
total_time = end_time - start_time
96+
Chef::Log.info("Chef Run complete in #{total_time} seconds")
97+
Chef::Config[:report_handlers].each do |handler|
98+
handler.report(@node, @runner, total_time)
99+
end
100+
true
101+
rescue Exception => e
102+
Chef::Config[:exception_handlers].each do |handler|
103+
handler.exception(@node, @runner, e)
104+
end
105+
raise
106+
end
95107
end
96108

97109
# Similar to Chef::Client#run, but instead of talking to the Chef server,
@@ -316,7 +328,8 @@ def converge(solo=false)
316328
compile = Chef::Compile.new(@node)
317329

318330
Chef::Log.debug("Converging node #{@node_name}")
319-
Chef::Runner.new(@node, compile.collection, compile.definitions, compile.cookbook_loader).converge
331+
@runner = Chef::Runner.new(@node, compile.collection, compile.definitions, compile.cookbook_loader)
332+
@runner.converge
320333
true
321334
end
322335

‎chef/lib/chef/config.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ def self.manage_secret_key
192192
signing_ca_domain "opensource.opscode.com"
193193
signing_ca_email "opensource-cert@opscode.com"
194194

195+
# Report Handlers
196+
report_handlers = []
197+
198+
# Exception Handlers
199+
exception_handlers = []
200+
195201
# Checksum Cache
196202
# Uses Moneta on the back-end
197203
cache_type "BasicFile"

‎chef/lib/chef/handler.rb‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2010 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+
19+
class Chef
20+
class Handler
21+
22+
attr_accessor :config
23+
24+
def new(config={})
25+
@config = config
26+
end
27+
28+
end
29+
end

‎chef/lib/chef/handler/file.rb‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#
2+
# Author:: Adam Jacob (<adam@opscode.com>)
3+
# Copyright:: Copyright (c) 2010 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+
19+
class Chef
20+
class Handler
21+
class File < Chef::Handler
22+
23+
def new(config={})
24+
config[:path] ||= "/tmp"
25+
super(config)
26+
end
27+
28+
def report(node, runner, time)
29+
data = Hash.new
30+
data[:node] = node
31+
data[:resources] = {
32+
:all => runner.collection.all_resources,
33+
:updated => runner.collection.inject([]) { |m, r| m << r if r.updated; m }
34+
}
35+
data[:time] = time
36+
File.open(File.join(config[:path], "report.txt"), "w") do |f|
37+
f.print data.to_json
38+
end
39+
end
40+
41+
def exception(node, runner, exception=nil)
42+
end
43+
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)