Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add chef_handler::minitest; flesh out vagrant config

  • Loading branch information...
commit 8c2160f48ebc3be8d184524fb605c8e0cf4d02c0 1 parent e9b42b7
@fujin fujin authored
View
1  .gitignore
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+.vagrant
View
7 Vagrantfile
@@ -1,3 +1,8 @@
Vagrant::Config.run do |config|
- config.vm.box = "lucid64"
+ config.vm.box = "natty64"
+ config.vm.provision :chef_solo do |chef|
+ chef.cookbooks_path = "examples/cookbooks"
+ chef.add_recipe "chef_handler::minitest"
+ chef.add_recipe "foo"
+ end
end
View
115 examples/cookbooks/chef_handler/README.md
@@ -0,0 +1,115 @@
+Description
+===========
+
+Creates a configured handler path for distributing [Chef report and exception handlers](http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers). Also exposes an LWRP for enabling Chef handlers from within recipe code (as opposed to hard coding in the client.rb file). This is useful for cookbook authors who may want to ship a product specific handler (see the `cloudkick` cookbook for an example) with their cookbook.
+
+Attributes
+==========
+
+`node["chef_handler"]["handler_path"]` - location to drop off handlers directory, default is `/var/chef/handlers`.
+
+Resource/Provider
+=================
+
+`chef_handler`
+--------------
+
+Requires, configures and enables handlers on the node for the current Chef run. Also has the ability to pass arguments to the handlers initializer. This allows initialization data to be pulled from a node's attribute data.
+
+It is best to declare `chef_handler` resources early on in the compile phase so they are available to fire for any exceptions during the Chef run. If you have a base role you would want any recipes that register Chef handlers to come first in the run_list.
+
+### Actions
+
+- :enable: Enables the Chef handler for the current Chef run on the current node
+- :disable: Disables the Chef handler for the current Chef run on the current node
+
+### Attribute Parameters
+
+- class_name: name attribute. The name of the handler class (can be module name-spaced).
+- source: full path to the handler file. can also be a gem path if the handler ships as part of a Ruby gem.
+- arguments: an array of arguments to pass the handler's class initializer
+- supports: type of Chef Handler to register as, ie :report, :exception or both. default is `:report => true, :exception => true`
+
+### Example
+
+ # register the Chef::Handler::JsonFile handler
+ # that ships with the Chef gem
+ chef_handler "Chef::Handler::JsonFile" do
+ source "chef/handler/json_file"
+ arguments :path => '/var/chef/reports'
+ action :enable
+ end
+
+ # do the same but during the compile phase
+ chef_handler "Chef::Handler::JsonFile" do
+ source "chef/handler/json_file"
+ arguments :path => '/var/chef/reports'
+ action :nothing
+ end.run_action(:enable)
+
+ # handle exceptions only
+ chef_handler "Chef::Handler::JsonFile" do
+ source "chef/handler/json_file"
+ arguments :path => '/var/chef/reports'
+ supports exception => true
+ action :enable
+ end
+
+
+ # enable the CloudkickHandler which was
+ # dropped off in the default handler path.
+ # passes the oauth key/secret to the handler's
+ # intializer.
+ chef_handler "CloudkickHandler" do
+ source "#{node['chef_handler']['handler_path']}/cloudkick_handler.rb"
+ arguments [node['cloudkick']['oauth_key'], node['cloudkick']['oauth_secret']]
+ action :enable
+ end
+
+
+Usage
+=====
+
+default
+-------
+
+Put the recipe `chef_handler` at the start of the node's run list to make sure that custom handlers are dropped off early on in the Chef run and available for later recipes.
+
+For information on how to write report and exception handlers for Chef, please see the Chef wiki pages:
+http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers
+
+json_file
+---------
+
+Leverages the `chef_handler` LWRP to automatically register the `Chef::Handler::JsonFile` handler that ships as part of Chef. This handler serializes the run status data to a JSON file located at `/var/chef/reports`.
+
+Changes/Roadmap
+===============
+
+## 1.0.4
+
+* [COOK-654] dont try and access a class before it has been loaded
+* fix bad boolean check (if vs unless)
+
+## 1.0.2:
+
+* [COOK-620] ensure handler code is reloaded during daemonized chef runs
+
+License and Author
+==================
+
+Author:: Seth Chisamore (<schisamo@opscode.com>)
+
+Copyright:: 2011, Opscode, Inc
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
View
21 examples/cookbooks/chef_handler/attributes/default.rb
@@ -0,0 +1,21 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Cookbook Name:: chef_handlers
+# Attribute:: default
+#
+# Copyright 2011, Opscode, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+default["chef_handler"]["handler_path"] = "#{File.expand_path(File.join(Chef::Config[:file_cache_path],'..'))}/handlers"
View
1  examples/cookbooks/chef_handler/files/default/handlers/README
@@ -0,0 +1 @@
+This directory contains Chef handlers to distribute to your nodes.
View
29 examples/cookbooks/chef_handler/metadata.json
@@ -0,0 +1,29 @@
+{
+ "name": "chef_handler",
+ "description": "Distribute and enable Chef Exception and Report handlers",
+ "long_description": "Description\n===========\n\nCreates a configured handler path for distributing [Chef report and exception handlers](http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers). Also exposes an LWRP for enabling Chef handlers from within recipe code (as opposed to hard coding in the client.rb file). This is useful for cookbook authors who may want to ship a product specific handler (see the `cloudkick` cookbook for an example) with their cookbook.\n\nAttributes\n==========\n\n`node[\"chef_handler\"][\"handler_path\"]` - location to drop off handlers directory, default is `/var/chef/handlers`.\n\nResource/Provider\n=================\n\n`chef_handler`\n--------------\n\nRequires, configures and enables handlers on the node for the current Chef run. Also has the ability to pass arguments to the handlers initializer. This allows initialization data to be pulled from a node's attribute data.\n\nIt is best to declare `chef_handler` resources early on in the compile phase so they are available to fire for any exceptions during the Chef run. If you have a base role you would want any recipes that register Chef handlers to come first in the run_list.\n\n### Actions\n\n- :enable: Enables the Chef handler for the current Chef run on the current node\n- :disable: Disables the Chef handler for the current Chef run on the current node\n\n### Attribute Parameters\n\n- class_name: name attribute. The name of the handler class (can be module name-spaced).\n- source: full path to the handler file. can also be a gem path if the handler ships as part of a Ruby gem.\n- arguments: an array of arguments to pass the handler's class initializer\n- supports: type of Chef Handler to register as, ie :report, :exception or both. default is `:report => true, :exception => true`\n\n### Example\n \n # register the Chef::Handler::JsonFile handler \n # that ships with the Chef gem\n chef_handler \"Chef::Handler::JsonFile\" do\n source \"chef/handler/json_file\"\n arguments :path => '/var/chef/reports'\n action :enable\n end\n \n # do the same but during the compile phase\n chef_handler \"Chef::Handler::JsonFile\" do\n source \"chef/handler/json_file\"\n arguments :path => '/var/chef/reports'\n action :nothing\n end.run_action(:enable)\n \n # handle exceptions only\n chef_handler \"Chef::Handler::JsonFile\" do\n source \"chef/handler/json_file\"\n arguments :path => '/var/chef/reports'\n supports exception => true\n action :enable\n end\n \n \n # enable the CloudkickHandler which was \n # dropped off in the default handler path.\n # passes the oauth key/secret to the handler's \n # intializer.\n chef_handler \"CloudkickHandler\" do\n source \"#{node['chef_handler']['handler_path']}/cloudkick_handler.rb\"\n arguments [node['cloudkick']['oauth_key'], node['cloudkick']['oauth_secret']]\n action :enable\n end\n\n\nUsage\n=====\n\ndefault\n-------\n\nPut the recipe `chef_handler` at the start of the node's run list to make sure that custom handlers are dropped off early on in the Chef run and available for later recipes.\n\nFor information on how to write report and exception handlers for Chef, please see the Chef wiki pages:\nhttp://wiki.opscode.com/display/chef/Exception+and+Report+Handlers\n\njson_file\n---------\n\nLeverages the `chef_handler` LWRP to automatically register the `Chef::Handler::JsonFile` handler that ships as part of Chef. This handler serializes the run status data to a JSON file located at `/var/chef/reports`.\n\nChanges/Roadmap\n===============\n\n## 1.0.4\n\n* [COOK-654] dont try and access a class before it has been loaded\n* fix bad boolean check (if vs unless)\n\n## 1.0.2:\n\n* [COOK-620] ensure handler code is reloaded during daemonized chef runs\n\nLicense and Author\n==================\n\nAuthor:: Seth Chisamore (<schisamo@opscode.com>)\n\nCopyright:: 2011, Opscode, Inc\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n",
+ "maintainer": "Opscode, Inc.",
+ "maintainer_email": "cookbooks@opscode.com",
+ "license": "Apache 2.0",
+ "platforms": {
+ },
+ "dependencies": {
+ },
+ "recommendations": {
+ },
+ "suggestions": {
+ },
+ "conflicting": {
+ },
+ "providing": {
+ },
+ "replacing": {
+ },
+ "attributes": {
+ },
+ "groupings": {
+ },
+ "recipes": {
+ },
+ "version": "1.0.4"
+}
View
6 examples/cookbooks/chef_handler/metadata.rb
@@ -0,0 +1,6 @@
+maintainer "Opscode, Inc."
+maintainer_email "cookbooks@opscode.com"
+license "Apache 2.0"
+description "Distribute and enable Chef Exception and Report handlers"
+long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
+version "1.0.4"
View
83 examples/cookbooks/chef_handler/providers/default.rb
@@ -0,0 +1,83 @@
+#
+# Author:: Seth Chisamore <schisamo@opscode.com>
+# Cookbook Name:: chef_handler
+# Provider:: default
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+action :enable do
+ # use load instead of require to ensure the handler file
+ # is reloaded into memory each chef run. fixes COOK-620
+ begin
+ Object.send(:remove_const, klass)
+ GC.start
+ rescue
+ Chef::Log.debug("#{@new_resource.class_name} has not been loaded.")
+ end
+ file_name = @new_resource.source
+ file_name << ".rb" unless file_name =~ /.*\.rb$/
+ load file_name
+ handler = klass.send(:new, *collect_args(@new_resource.arguments))
+ @new_resource.supports.each do |type, enable|
+ if enable
+ # we have to re-enable the handler every chef run
+ # to ensure daemonized Chef always has the latest
+ # handler code. TODO: add a :reload action
+ Chef::Log.info("Enabling #{@new_resource} as a #{type} handler")
+ Chef::Config.send("#{type.to_s}_handlers").delete_if {|v| v.class.to_s.include? @new_resource.class_name}
+ Chef::Config.send("#{type.to_s}_handlers") << handler
+ new_resource.updated_by_last_action(true)
+ end
+ end
+end
+
+action :disable do
+ @new_resource.supports.each_key do |type|
+ if enabled?(type)
+ Chef::Log.info("Disabling #{@new_resource} as a #{type} handler")
+ Chef::Config.send("#{type.to_s}_handlers").delete_if {|v| v.class.to_s.include? @new_resource.class_name}
+ new_resource.updated_by_last_action(true)
+ end
+ end
+end
+
+def load_current_resource
+ @current_resource = Chef::Resource::ChefHandler.new(@new_resource.name)
+ @current_resource.class_name(@new_resource.class_name)
+ @current_resource.source(@new_resource.source)
+ @current_resource
+end
+
+private
+def enabled?(type)
+ Chef::Config.send("#{type.to_s}_handlers").select do |handler|
+ handler.class.to_s.include? @new_resource.class_name
+ end.size >= 1
+end
+
+def collect_args(resource_args = [])
+ if resource_args.is_a? Array
+ resource_args
+ else
+ [resource_args]
+ end
+end
+
+def klass
+ @klass ||= begin
+ @new_resource.class_name.split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
+ end
+end
View
31 examples/cookbooks/chef_handler/recipes/default.rb
@@ -0,0 +1,31 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Cookbook Name:: chef_handlers
+# Recipe:: default
+#
+# Copyright 2011, Opscode, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+Chef::Log.info("Chef Handlers will be at: #{node['chef_handler']['handler_path']}")
+
+remote_directory node['chef_handler']['handler_path'] do
+ source 'handlers'
+ owner 'root'
+ group 'root'
+ mode "0755"
+ recursive true
+ action :nothing
+end.run_action(:create)
+
View
28 examples/cookbooks/chef_handler/recipes/json_file.rb
@@ -0,0 +1,28 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Cookbook Name:: chef_handlers
+# Recipe:: json_file
+#
+# Copyright 2011, Opscode, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# force resource actions in compile phase so exception handler
+# fires for compile phase exceptions
+
+chef_handler "Chef::Handler::JsonFile" do
+ source "chef/handler/json_file"
+ arguments :path => '/var/chef/reports'
+ action :nothing
+end.run_action(:enable)
View
24 examples/cookbooks/chef_handler/recipes/minitest.rb
@@ -0,0 +1,24 @@
+gem_package "minitest" do
+ action :nothing
+end.run_action(:upgrade)
+
+gem_package "minitest-chef-handler" do
+ action :nothing
+end.run_action(:upgrade)
+
+require 'rubygems'
+Gem.clear_paths
+require 'minitest-chef-handler'
+
+path = File.join(Chef::Config[:cookbook_path],
+ "**",
+ "test",
+ "test_*.rb")
+
+Chef::Log.info "path is #{path}, entries: #{Dir.glob(path).entries}"
+
+chef_handler "MiniTest::Chef::Handler" do
+ source "minitest-chef-handler"
+ arguments :path => path
+ action :nothing
+end.run_action(:enable)
View
34 examples/cookbooks/chef_handler/resources/default.rb
@@ -0,0 +1,34 @@
+#
+# Author:: Seth Chisamore <schisamo@opscode.com>
+# Cookbook Name:: chef_handler
+# Resource:: default
+#
+# Copyright:: 2011, Opscode, Inc <legal@opscode.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+actions :enable, :disable
+
+attribute :class_name, :kind_of => String, :name_attribute => true
+attribute :source, :default => nil, :kind_of => String
+attribute :arguments, :default => []
+attribute :supports, :kind_of => Hash, :default => {:report => true, :exception => true}
+
+# we have to set default for the supports attribute
+# in initializer since it is a 'reserved' attribute name
+def initialize(*args)
+ super
+ @action = :enable
+ @supports = {:report => true, :exception => true}
+end
View
9 examples/cookbooks/foo/test/test_foo.rb
@@ -0,0 +1,9 @@
+class FooTest < MiniTest::Chef::TestCase
+ def test_exist_file
+ assert File.exist?('/tmp/temporal_file')
+ end
+
+ def test_succeed
+ assert run_status.success?
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.