Initial Chef repository

This commit is contained in:
Greg Karékinian
2015-07-21 19:45:23 +02:00
parent 7e5401fc71
commit ee4079fa85
1151 changed files with 185163 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
chef_handler cookbook CHANGELOG
===============================
v1.1.9 (2015-05-26)
-------------------
Bugfixes from 1.1.8 - loading without source is not allowed again.
Class unloading is performed more carefully.
Tests for resource providers.
v1.1.8 (2015-05-14)
-------------------
Updated Contribution and Readme docs.
Fix ChefSpec matchers.
Allow handler to load classes when no source is provided.
v1.1.6 (2014-04-09)
-------------------
[COOK-4494] - Add ChefSpec matchers
v1.1.5 (2014-02-25)
-------------------
- [COOK-4117] - use the correct scope when searching the children class name
v1.1.4
------
- [COOK-2146] - style updates
v1.1.2
---------
- [COOK-1989] - fix scope for handler local variable to the enable block
v1.1.0
------
- [COOK-1645] - properly delete old handlers
- [COOK-1322] - support platforms that use 'wheel' as root group'
v1.0.8
------
- [COOK-1177] - doesn't work on windows due to use of unix specific attributes
v1.0.6
------
- [COOK-1069] - typo in chef_handler readme
v1.0.4
------
- [COOK-654] dont try and access a class before it has been loaded
- fix bad boolean check (if vs unless)
v1.0.2
------
- [COOK-620] ensure handler code is reloaded during daemonized chef runs

View File

@@ -0,0 +1,126 @@
Description
===========
Creates a configured handler path for distributing [Chef report and exception handlers](http://docs.chef.io/handlers.html). 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.
Requirements
============
* Ruby >= 1.9
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, i.e. :report, :exception or both. default is `:report => true, :exception => true`
### Example
```ruby
# 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.chef.io/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`.
Unit Testing
==================
chef_handler provides built in [chefspec](https://github.com/sethvargo/chefspec) matchers for assisting unit tests. These matchers will only be loaded if chefspec is already loaded. Following is an example of asserting against the jsonfile handler:
```ruby
expect(runner).to enable_chef_handler("Chef::Handler::JsonFile").with(
source: "chef/handler/json_file",
arguments: { :path => '/var/chef/reports'},
supports: {:exception => true}
)
end
```
License and Author
==================
Author:: Seth Chisamore (<schisamo@chef.io>)
Copyright:: 2011, Chef Software, 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 File

@@ -0,0 +1,30 @@
#
# Author:: Seth Chisamore (<schisamo@chef.io>)
# Cookbook Name:: chef_handlers
# Attribute:: default
#
# Copyright 2011-2013, Chef Software, 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"]["root_user"] = "root"
case platform
when "openbsd", "freebsd", "mac_os_x", "mac_os_x_server"
default["chef_handler"]["root_group"] = "wheel"
else
default["chef_handler"]["root_group"] = "root"
end
default["chef_handler"]["handler_path"] = "#{File.expand_path(File.join(Chef::Config[:file_cache_path], '..'))}/handlers"

View File

@@ -0,0 +1 @@
This directory contains Chef handlers to distribute to your nodes.

View File

@@ -0,0 +1,88 @@
#
# Author:: Kartik Cating-Subramanian (<ksubramanian@chef.io>)
# Copyright:: Copyright (c) 2015 Chef, Inc.
# License:: Apache License, Version 2.0
#
# 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.
module ChefHandler
module Helpers
# Registers a handler in Chef::Config.
#
# @param handler_type [Symbol] such as :report or :exception.
# @param handler [Chef::Handler] handler to register.
def register_handler(handler_type, handler)
Chef::Log.info("Enabling #{handler.class.name} as a #{handler_type} handler.")
Chef::Config.send("#{handler_type.to_s}_handlers") << handler
end
# Removes all handlers that match the given class name in Chef::Config.
#
# @param handler_type [Symbol] such as :report or :exception.
# @param class_full_name [String] such as 'Chef::Handler::ErrorReport'.
def unregister_handler(handler_type, class_full_name)
Chef::Log.info("Disabling #{class_full_name} as a #{handler_type} handler.")
Chef::Config.send("#{handler_type.to_s}_handlers").delete_if { |v| v.class.name == class_full_name }
end
# Walks down the namespace heirarchy to return the class object for the given class name.
# If the class is not available, NameError is thrown.
#
# @param class_full_name [String] full class name such as 'Chef::Handler::Foo' or 'MyHandler'.
# @return [Array] parent class and child class.
def get_class(class_full_name)
ancestors = class_full_name.split('::')
class_name = ancestors.pop
# We need to search the ancestors only for the first/uppermost namespace of the class, so we
# need to enable the #const_get inherit paramenter only when we are searching in Kernel scope
# (see COOK-4117).
parent = ancestors.inject(Kernel) { |scope, const_name| scope.const_get(const_name, scope === Kernel) }
child = parent.const_get(class_name, parent === Kernel)
return parent, child
end
# Unloads a given class and reloads it from the file provided.
#
# @param class_full_name [String] full class name such as 'Chef::Handler::Foo'. If a class
# with that name currently exists, its definition is deleted from the enclosing module.
# @param file_name [String] full path to the ruby file to be loaded. If path doesn't end with
# .rb, that extension is appended.
# @return [Class] definition for the freshly loaded class.
def reload_class(class_full_name, file_name)
begin
parent, child = get_class(class_full_name)
rescue
Chef::Log.debug("#{class_full_name} was not previously loaded.")
end
if child then
class_name = class_full_name.split('::').last
child = nil
parent = Object if parent === Kernel
parent.send(:remove_const, class_name)
GC.start
end
# Use load instead of require because we need to explicitly avoid any caching that 'require'
# performs. If the file has changed, we want to get the changes.
file_name << '.rb' unless file_name =~ /.*\.rb$/
load file_name
parent, child = get_class(class_full_name)
return child
end
end
end

View File

@@ -0,0 +1,38 @@
#
# Author:: Douglas Thrift (<douglas.thrift@rightscale.com>)
# Cookbook Name:: chef_handler
# Library:: matchers
#
# Copyright 2014, Chef Software, 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.
#
if defined?(ChefSpec)
chefspec_version = Gem.loaded_specs["chefspec"].version
if chefspec_version < Gem::Version.new('4.1.0')
define_method = ChefSpec::Runner.method(:define_runner_method)
else
define_method = ChefSpec.method(:define_matcher)
end
define_method.call :chef_handler
def enable_chef_handler(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:chef_handler, :enable, resource_name)
end
def disable_chef_handler(resource_name)
ChefSpec::Matchers::ResourceMatcher.new(:chef_handler, :disable, resource_name)
end
end

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,86 @@
#
# Author:: Seth Chisamore <schisamo@chef.io>
# Cookbook Name:: chef_handler
# Provider:: default
#
# Copyright:: 2011-2013, Chef Software, Inc <legal@chef.io>
#
# 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.
#
include ::ChefHandler::Helpers
def whyrun_supported?
true
end
# This action needs to find an rb file that presumably contains the indicated class in it and the
# load that file. It needs to do this keeping in mind that the same handler class can get enabled
# and disabled multiple times and there may be multiple instances of them running around. The
# handler code may also have changed between actions. To handle all this, we parse the full class
# name and attempt to find its class object, in case it has already been loaded. If such a class
# is found, we then attempt to unload that class before we load the file requested. We use "load"
# instead of "require" because we want to reload the handler class in case it has changed and
# don't want the caching behavior of "require".
#
# Note that during this process, we also need to keep track of the current handler configuration.
# Any of the above steps might fail - in which case we would not want to be in a situation where
# we have a registered handler that has been unloaded or mangled.
action :enable do
class_name = new_resource.class_name
new_resource.supports.each do |type, enable|
if enable
converge_by("disable #{class_name} as a #{type} handler") do
unregister_handler(type, class_name)
end
end
end
handler = nil
converge_by("load #{class_name} from #{new_resource.source}") do
klass = reload_class(class_name, new_resource.source)
handler = klass.send(:new, *collect_args(new_resource.arguments))
end
new_resource.supports.each do |type, enable|
if enable
converge_by("enable #{new_resource} as a #{type} handler") do
register_handler(type, handler)
end
end
end
end
action :disable do
new_resource.supports.each_key do |type|
converge_by("disable #{new_resource} as a #{type} handler") do
unregister_handler(type, new_resource.class_name)
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 collect_args(resource_args = [])
if resource_args.is_a? Array
resource_args
else
[resource_args]
end
end

View File

@@ -0,0 +1,33 @@
#
# Author:: Seth Chisamore (<schisamo@chef.io>)
# Cookbook Name:: chef_handlers
# Recipe:: default
#
# Copyright 2011, Chef Software, 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'
# Just inherit permissions on Windows, don't try to set POSIX perms
if node["platform"] != "windows"
owner node['chef_handler']['root_user']
group node['chef_handler']['root_group']
mode "0755"
recursive true
end
action :nothing
end.run_action(:create)

View File

@@ -0,0 +1,28 @@
#
# Author:: Seth Chisamore (<schisamo@chef.io>)
# Cookbook Name:: chef_handlers
# Recipe:: json_file
#
# Copyright 2011, Chef Software, 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 File

@@ -0,0 +1,34 @@
#
# Author:: Seth Chisamore <schisamo@chef.io>
# Cookbook Name:: chef_handler
# Resource:: default
#
# Copyright:: 2011-2013, Chef Software, Inc <legal@chef.io>
#
# 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