chef-handler-zookeeper 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d3f5a10dbeae85b908d204ef9b02f2f524899bd
4
+ data.tar.gz: 1f8ec4afa53f87d2b477d5a9759fba6ac0aa4ff0
5
+ SHA512:
6
+ metadata.gz: 413a42ca2f042ca0cff07fdd26813f4fd6def7b0835506a50e6764ecc992ca3265dafb76cc1cb8a01455d68ef59c58b1068a59b5c67d72eb23401cde22457a8b
7
+ data.tar.gz: c079075e75cb5c5ca693e22fa60b36a331af49f776463f520cd9cd9a373b3d53abf15289b35a1fa10b268017f712f7d25093d782cdaf62f0f588c8bcb78e06e7
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # Chef Handler Zookeeper
2
+
3
+ A simple Chef report handler to send notifications to Zookeeper about Chef runs.
4
+
5
+ This Chef Handler is heavily based on [Chef Handler SNS](https://github.com/onddo/chef-handler-sns) code.
6
+
7
+ * http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers
8
+
9
+ ## Requirements
10
+
11
+ * A Zookeeper server.
12
+ * Uses the `zk` Ruby gem.
13
+
14
+ ## Usage
15
+
16
+ You can install this handler in two ways:
17
+
18
+ ### Method 1: In the Chef config file
19
+
20
+ You can install the RubyGem and configure Chef to use it:
21
+
22
+ gem install chef-handler-zookeeper
23
+
24
+ Then add to the configuration (`/etc/chef/solo.rb` for chef-solo or `/etc/chef/client.rb` for chef-client):
25
+
26
+ ```ruby
27
+ require "chef/handler/zookeeper"
28
+
29
+ # Create the handler
30
+ zookeeper_handler = Chef::Handler::ZookeeperHandler.new
31
+
32
+ # Some Zookeeper configurations
33
+ zookeeper_handler.server "zookeeper.mydomain.com"
34
+ zookeeper_handler.znode "/chef/#{`hostname`.chomp}/chef_status"
35
+
36
+ # Add your handler
37
+ start_handlers << zookeeper_handler
38
+ exception_handlers << zookeeper_handler
39
+ report_handlers << zookeeper_handler
40
+ ```
41
+
42
+ ### Method 2: In a recipe with the chef_handler LWRP
43
+
44
+ Use the [chef_handler LWRP](http://community.opscode.com/cookbooks/chef_handler), creating a recipe with the following:
45
+
46
+ ```ruby
47
+ # A compiler is required for the `zk` gem
48
+ node.default['build_essential']['compiletime'] = true
49
+ include_recipe 'build-essential'
50
+
51
+ # Handler configuration options
52
+ argument_array = [
53
+ :server => "zookeeper.mydomain.com:2181",
54
+ :znode => "/chef/somepath/chef_status",
55
+ ]
56
+
57
+ # Install the `chef-handler-zookeeper` RubyGem during the compile phase
58
+ chef_gem "chef-handler-zookeeper"
59
+
60
+ # Then activate the handler with the `chef_handler` LWRP
61
+ chef_handler "Chef::Handler::ZookeeperHandler" do
62
+ source "#{Gem::Specification.find_by_name("chef-handler-zookeeper").lib_dirs_glob}/chef/handler/zookeeper"
63
+ arguments argument_array
64
+ supports :report => true, :exception => true
65
+ action :enable
66
+ end
67
+ ```
68
+
69
+ If you have an old version of gem package (< 1.8.6) without `find_by_name` or old chef-client (< 0.10.10) without `chef_gem`, you can try creating a recipe similar to the following:
70
+
71
+ ```ruby
72
+ # A compiler is required for the `zk` gem
73
+ node.default['build_essential']['compiletime'] = true
74
+ include_recipe 'build-essential'
75
+
76
+ # Handler configuration options
77
+ argument_array = [
78
+ :server => "zookeeper.mydomain.com:2181",
79
+ :znode => "/chef/somepath/chef_status",
80
+ ]
81
+
82
+ # Install the `chef-handler-zookeeper` RubyGem during the compile phase
83
+ if defined?(Chef::Resource::ChefGem)
84
+ chef_gem "chef-handler-zookeeper"
85
+ else
86
+ gem_package("chef-handler-zookeeper") do
87
+ action :nothing
88
+ end.run_action(:install)
89
+ end
90
+
91
+ # Get the installed `chef-handler-zookeeper` gem path
92
+ zookeeper_handler_path = Gem::Specification.respond_to?("find_by_name") ?
93
+ Gem::Specification.find_by_name("chef-handler-zookeeper").lib_dirs_glob :
94
+ Gem.all_load_paths.grep(/chef-handler-zookeeper/).first
95
+
96
+ # Then activate the handler with the `chef_handler` LWRP
97
+ chef_handler "Chef::Handler::ZookeeperHandler" do
98
+ source "#{zookeeper_handler_path}/chef/handler/zookeeper"
99
+ arguments argument_array
100
+ supports :report => true, :exception => true
101
+ action :enable
102
+ end
103
+ ```
104
+
105
+ #### start_handler
106
+
107
+ If you want to run also as a *start handler* using `chef_handler` cookbook, you can use a recipe similar to the following:
108
+
109
+ ```ruby
110
+ # [...]
111
+
112
+ # We will need to install the chef handler at compile time
113
+ chef_handler "Chef::Handler::ZookeeperHandler" do
114
+ # [...]
115
+ action :nothing
116
+ end.run_action(:enable)
117
+
118
+ # based on code from chef-sensu-handler cookbook: https://github.com/needle-cookbooks/chef-sensu-handler/blob/master/recipes/default.rb
119
+ ruby_block 'trigger_start_handlers' do
120
+ block do
121
+ require 'chef/run_status'
122
+ require 'chef/handler'
123
+
124
+ # a bit tricky, required by the default start.json.erb template to have access to node
125
+ Chef::Handler.run_start_handlers(self)
126
+ end
127
+ action :nothing
128
+ end.run_action(:create)
129
+ ```
130
+
131
+ ## Handler Configuration Options
132
+
133
+ The following options are available to configure the handler:
134
+
135
+ * `server` - The Zookeeper server hostname and port (required).
136
+ * `znode` - Path of the znode to write to (required). **The znode must already exist**.
137
+ * `start_template` - Full path of an erubis template file to use for the znode body on Chef run start (optional).
138
+ * `end_template` - Full path of an erubis template file to use for the znode body when Chef run ended (optional).
139
+
140
+ ### start_template and end_template
141
+
142
+ This configuration options need to contain the full path of an erubis template. For example:
143
+
144
+ ```ruby
145
+ # recipe "myapp::zookeeper_handler"
146
+
147
+ cookbook_file "chef_handler_zookeeper_body.erb" do
148
+ path "/tmp/chef_handler_zookeeper_body.erb"
149
+ # [...]
150
+ end
151
+
152
+ argument_array = [
153
+ :server => "zookeeper.mydomain.com:2181",
154
+ :znode => "/chef/somepath/chef_status",
155
+ :end_template => "/tmp/chef_handler_zookeeper_body.erb",
156
+ ]
157
+ chef_handler "Chef::Handler::ZookeeperHandler" do
158
+ # [...]
159
+ arguments argument_array
160
+ end
161
+ ```
162
+
163
+ ```erb
164
+ <%# file "myapp/files/default/chef_handler_zookeeper_body.erb" %>
165
+
166
+ Node Name: <%= node.name %>
167
+ <% if node.attribute?("fqdn") -%>
168
+ Hostname: <%= node.fqdn %>
169
+ <% end -%>
170
+
171
+ Chef Run List: <%= node.run_list.to_s %>
172
+ Chef Environment: <%= node.chef_environment %>
173
+
174
+ <% if node.attribute?("ec2") -%>
175
+ Instance Id: <%= node.ec2.instance_id %>
176
+ Instance Public Hostname: <%= node.ec2.public_hostname %>
177
+ Instance Hostname: <%= node.ec2.hostname %>
178
+ Instance Public IPv4: <%= node.ec2.public_ipv4 %>
179
+ Instance Local IPv4: <%= node.ec2.local_ipv4 %>
180
+ <% end -%>
181
+
182
+ Chef Client Elapsed Time: <%= elapsed_time.to_s %>
183
+ Chef Client Start Time: <%= start_time.to_s %>
184
+ Chef Client Start Time: <%= end_time.to_s %>
185
+
186
+ <% if exception -%>
187
+ Exception: <%= run_status.formatted_exception %>
188
+ Stacktrace:
189
+ <%= Array(backtrace).join("\n") %>
190
+
191
+ <% end -%>
192
+ ```
193
+
194
+ The following variables are accessible inside the template:
195
+
196
+ * `start_time` - The time the chef run started.
197
+ * `end_time` - The time the chef run ended.
198
+ * `elapsed_time` - The time elapsed between the start and finish of the chef run.
199
+ * `run_context` - The Chef::RunContext object used by the chef run.
200
+ * `exception` - The uncaught Exception that terminated the chef run, or nil if the run completed successfully.
201
+ * `backtrace` - The backtrace captured by the uncaught exception that terminated the chef run, or nil if the run completed successfully.
202
+ * `node` - The Chef::Node for this client run.
203
+ * `all_resources` - An Array containing all resources in the chef run's resource_collection.
204
+ * `updated_resources` - An Array containing all resources that were updated during the chef run.
205
+ * `success?` - Was the chef run successful? True if the chef run did not raise an uncaught exception.
206
+ * `failed?` - Did the chef run fail? True if the chef run raised an uncaught exception.
207
+
208
+ Default templates are in the [templates](https://github.com/onddo/chef-handler-zookeeper/tree/master/lib/chef/handler/zookeeper/templates) directory.
209
+
210
+ **Note:** When using `start_template` with the **chef_handler cookbook**, only the `node` variable will be accesible from the template.
211
+
212
+ ## Running the tests
213
+
214
+ Minitest tests can be run as usual:
215
+
216
+ rake test
217
+
218
+ # TODO
219
+
220
+ * Support for znode creation:
221
+ * Including *:create* boolean, *:acl* for the ACL and *:recursive*.
222
+ * Add digest authentication support.
223
+ * Add tests.
224
+
225
+ ## Contributing
226
+
227
+ [Pull Requests](http://github.com/onddo/chef-handler-zookeeper/pulls) are welcome.
228
+
229
+ ## License and Author
230
+
231
+ | | |
232
+ |:---------------------|:-----------------------------------------|
233
+ | **Author:** | Xabier de Zuazo (<xabier@onddo.com>)
234
+ | **Copyright:** | Copyright (c) 2013 Onddo Labs, SL. (www.onddo.com)
235
+ | **License:** | Apache License, Version 2.0
236
+
237
+ Licensed under the Apache License, Version 2.0 (the "License");
238
+ you may not use this file except in compliance with the License.
239
+ You may obtain a copy of the License at
240
+
241
+ http://www.apache.org/licenses/LICENSE-2.0
242
+
243
+ Unless required by applicable law or agreed to in writing, software
244
+ distributed under the License is distributed on an "AS IS" BASIS,
245
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
246
+ See the License for the specific language governing permissions and
247
+ limitations under the License.
248
+
@@ -0,0 +1,92 @@
1
+ #
2
+ # Author:: Xabier de Zuazo (<xabier@onddo.com>)
3
+ # Copyright:: Copyright (c) 2013 Onddo Labs, SL.
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
+ require 'chef/mixin/params_validate'
20
+ require 'chef/exceptions'
21
+
22
+ class Chef
23
+ class Handler
24
+ class ZookeeperHandler < ::Chef::Handler
25
+ module Config
26
+ Config.extend Config # let Config use the methods it contains as instance methods
27
+ include ::Chef::Mixin::ParamsValidate
28
+
29
+ REQUIRED = [ 'server', 'znode' ]
30
+
31
+ def config_init(config={})
32
+ config.each do |key, value|
33
+ if Config.respond_to?(key) and not /^config_/ =~ key.to_s
34
+ self.send(key, value)
35
+ else
36
+ Chef::Log.warn("#{self.class.to_s}: configuration method not found: #{key}.")
37
+ end
38
+ end
39
+ end
40
+
41
+ def config_check
42
+ REQUIRED.each do |key|
43
+ if self.send(key).nil?
44
+ raise Exceptions::ValidationFailed,
45
+ "Required argument #{key.to_s} is missing!"
46
+ end
47
+ end
48
+
49
+ [ start_template, end_template ].each do |template|
50
+ if template and not ::File.exists?(template)
51
+ raise Exceptions::ValidationFailed,
52
+ "Template file not found: #{template}."
53
+ end
54
+ end
55
+ end
56
+
57
+ def server(arg=nil)
58
+ set_or_return(
59
+ :server,
60
+ arg,
61
+ :kind_of => String
62
+ )
63
+ end
64
+
65
+ def znode(arg=nil)
66
+ set_or_return(
67
+ :znode,
68
+ arg,
69
+ :kind_of => String
70
+ )
71
+ end
72
+
73
+ def start_template(arg=nil)
74
+ set_or_return(
75
+ :start_template,
76
+ arg,
77
+ :kind_of => String
78
+ )
79
+ end
80
+
81
+ def end_template(arg=nil)
82
+ set_or_return(
83
+ :end_template,
84
+ arg,
85
+ :kind_of => String
86
+ )
87
+ end
88
+
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,20 @@
1
+ <%= require 'json'
2
+ data = {
3
+ "chef_version" => Chef::VERSION,
4
+ "ohai_version" => Ohai::VERSION,
5
+ "run_list" => node.run_list.to_s,
6
+ "environment" => node.chef_environment,
7
+ "start_time" => start_time.to_s,
8
+ "end_time" => end_time.to_s,
9
+ "elapsed_time" => elapsed_time.to_s,
10
+ "updated_resources" => updated_resources.kind_of?(Array) ? updated_resources.count : 0,
11
+ "status" => success? ? "success" : "failure",
12
+ }
13
+
14
+ if exception
15
+ data['exception'] = run_status.formatted_exception
16
+ data['backtrace'] = Array(backtrace).join("\n")
17
+ end
18
+
19
+ data.to_json
20
+ %>
@@ -0,0 +1,14 @@
1
+ <%= require 'json'
2
+ data = {
3
+ "chef_version" => Chef::VERSION,
4
+ "ohai_version" => Ohai::VERSION,
5
+ "start_time" => run_status.kind_of?(Chef::RunStatus) ? start_time.to_s : Time.now.to_s,
6
+ "status" => "running",
7
+ }
8
+
9
+ if self.respond_to?(:node)
10
+ data['environment'] = self.node.chef_environment
11
+ end
12
+
13
+ data.to_json
14
+ %>
@@ -0,0 +1,39 @@
1
+ Node Name: <%= node.name %>
2
+ <% if node.attribute?('fqdn') -%>
3
+ Hostname: <%= node.fqdn %>
4
+ <% end -%>
5
+
6
+ Chef Version: <%= Chef::VERSION %>
7
+ Ohai Version: <%= Ohai::VERSION %>
8
+
9
+ Chef Run List: <%= node.run_list.to_s %>
10
+ Chef Environment: <%= node.chef_environment %>
11
+
12
+ <% if node.attribute?('ec2') -%>
13
+ <% if node.ec2.attribute?('instance_id') -%>
14
+ Instance Id: <%= node.ec2.instance_id %>
15
+ <% end -%>
16
+ <% if node.ec2.attribute?('public_hostname') -%>
17
+ Instance Public Hostname: <%= node.ec2.public_hostname %>
18
+ <% end -%>
19
+ <% if node.ec2.attribute?('hostname') -%>
20
+ Instance Hostname: <%= node.ec2.hostname %>
21
+ <% end -%>
22
+ <% if node.ec2.attribute?('public_ipv4') -%>
23
+ Instance Public IPv4: <%= node.ec2.public_ipv4 %>
24
+ <% end -%>
25
+ <% if node.ec2.attribute?('local_ipv4') -%>
26
+ Instance Local IPv4: <%= node.ec2.local_ipv4 %>
27
+ <% end -%>
28
+ <% end -%>
29
+
30
+ Chef Client Elapsed Time: <%= elapsed_time.to_s %>
31
+ Chef Client Start Time: <%= start_time.to_s %>
32
+ Chef Client Start Time: <%= end_time.to_s %>
33
+
34
+ <% if exception -%>
35
+ Exception: <%= run_status.formatted_exception %>
36
+ Stacktrace:
37
+ <%= Array(backtrace).join("\n") %>
38
+
39
+ <% end -%>
@@ -0,0 +1,9 @@
1
+
2
+ class Chef
3
+ class Handler
4
+ class ZookeeperHandler < ::Chef::Handler
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,69 @@
1
+ #
2
+ # Author:: Xabier de Zuazo (<xabier@onddo.com>)
3
+ # Copyright:: Copyright (c) 2013 Onddo Labs, SL.
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
+ require 'chef/handler'
20
+ require 'chef/handler/zookeeper/config'
21
+ require 'zk'
22
+ require 'erubis'
23
+
24
+ class Chef
25
+ class Handler
26
+ class ZookeeperHandler < ::Chef::Handler
27
+ include ::Chef::Handler::ZookeeperHandler::Config
28
+
29
+ def initialize(config={})
30
+ Chef::Log.debug("#{self.class.to_s} initialized.")
31
+ config_init(config)
32
+ end
33
+
34
+ def report
35
+ if !run_status.kind_of?(Chef::RunStatus) or elapsed_time.nil?
36
+ zk.set(znode, start_template_body)
37
+ else
38
+ zk.set(znode, end_template_body)
39
+ end
40
+ end
41
+
42
+ protected
43
+
44
+ def zk
45
+ @zk ||= begin
46
+ ZK.logger = Chef::Log
47
+ ZK.new(server)
48
+ end
49
+ end
50
+
51
+ def start_template_body
52
+ znode_body(start_template || "#{File.dirname(__FILE__)}/zookeeper/templates/start.json.erb")
53
+ end
54
+
55
+ def end_template_body
56
+ znode_body(end_template || "#{File.dirname(__FILE__)}/zookeeper/templates/end.json.erb")
57
+ end
58
+
59
+ def znode_body(body_template)
60
+ config_check
61
+ template = IO.read(body_template)
62
+ context = self
63
+ eruby = Erubis::Eruby.new(template)
64
+ eruby.evaluate(context)
65
+ end
66
+
67
+ end
68
+ end
69
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'simplecov'
5
+ if ENV['TRAVIS']
6
+ require 'coveralls'
7
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
8
+ end
9
+ SimpleCov.start
10
+
11
+ gem 'minitest' # ensures you're using the gem, and not the built in MT
12
+ require 'minitest/autorun'
13
+ require 'mocha/setup'
14
+ require 'chef/handler/zookeeper'
@@ -0,0 +1,193 @@
1
+ require 'helper'
2
+ require 'chef/node'
3
+ require 'chef/run_status'
4
+
5
+ class ZK::Client::Fake
6
+ attr_reader :zk_client_new, :server, :znode
7
+
8
+ def initialize(host, opts={}, &b)
9
+ end
10
+
11
+ def set(path, data, opts={})
12
+ return true
13
+ end
14
+
15
+ def fake_new
16
+ @zk_client_new = true
17
+ return self
18
+ end
19
+
20
+ end
21
+
22
+ class Chef::Handler::FakeZookeeperHandler < Chef::Handler::ZookeeperHandler
23
+
24
+ def get_zookeeper_start_template_body
25
+ start_template_body
26
+ end
27
+
28
+ def get_zookeeper_end_template_body
29
+ end_template_body
30
+ end
31
+
32
+ end
33
+
34
+ def run_status_new
35
+ @run_status = if Gem.loaded_specs['chef'].version > Gem::Version.new('0.12.0')
36
+ Chef::RunStatus.new(@node, {})
37
+ else
38
+ Chef::RunStatus.new(@node)
39
+ end
40
+ end
41
+
42
+ describe Chef::Handler::ZookeeperHandler do
43
+ before do
44
+ @config = {
45
+ :server => '127.0.0.1:2181',
46
+ :znode => '/chef/1.2.3.4',
47
+ }
48
+
49
+ zk = ZK::Client::Threaded.new(@config[:server], { :connect => false })
50
+ ZK::Client::Threaded.stubs(:new).returns(zk) # return a no connecting instance
51
+ ZK::Client::Threaded.any_instance.stubs(:set).returns(true)
52
+
53
+ @node = Chef::Node.new
54
+ @node.name('test')
55
+ Chef::Handler::ZookeeperHandler.any_instance.stubs(:node).returns(@node)
56
+
57
+ @run_status = run_status_new
58
+ @run_status.start_clock
59
+ @run_status.stop_clock
60
+ end
61
+
62
+ it 'should read the configuration options on initialization' do
63
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
64
+ assert_equal @zookeeper_handler.server, @config[:server]
65
+ assert_equal @zookeeper_handler.znode, @config[:znode]
66
+ end
67
+
68
+ it 'should be able to change configuration options using method calls' do
69
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new
70
+ @zookeeper_handler.server(@config[:server])
71
+ @zookeeper_handler.znode(@config[:znode])
72
+ assert_equal @zookeeper_handler.server, @config[:server]
73
+ assert_equal @zookeeper_handler.znode, @config[:znode]
74
+ end
75
+
76
+ it 'should try to set the znode message at the beginning and end when properly configured' do
77
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
78
+ ZK::Client::Threaded.any_instance.expects(:set).once
79
+
80
+ @zookeeper_handler.run_report_safely(@run_status)
81
+ end
82
+
83
+ it 'should create a ZK::Client::Threaded object' do
84
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
85
+ fake_zookeeper = ZK::Client::Fake.new(@config[:server])
86
+ ZK::Client::Threaded.any_instance.stubs(:new).returns(fake_zookeeper.fake_new)
87
+
88
+ @zookeeper_handler.run_report_safely(@run_status)
89
+
90
+ assert_equal fake_zookeeper.zk_client_new, true
91
+ end
92
+
93
+ describe '#report' do
94
+ before do
95
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
96
+ @run_status = run_status_new
97
+ end
98
+
99
+ describe 'when chef run is not over' do
100
+ before do
101
+ @run_status.start_clock
102
+ end
103
+
104
+ it 'should use the start_template when the run is not over' do
105
+ @zookeeper_handler.stubs(:start_template_body).once
106
+ @zookeeper_handler.stubs(:end_template_body).never
107
+
108
+ @zookeeper_handler.run_report_unsafe(@run_status)
109
+ end
110
+
111
+ it 'should be able to generate the default start_template' do
112
+ @fake_zookeeper_handler = Chef::Handler::FakeZookeeperHandler.new(@config)
113
+ Chef::Handler::FakeZookeeperHandler.any_instance.stubs(:node).returns(@node)
114
+ @fake_zookeeper_handler.run_report_unsafe(@run_status)
115
+
116
+ @fake_zookeeper_handler.get_zookeeper_start_template_body.must_match Regexp.new('"start_time":')
117
+ end
118
+
119
+ it 'should throw an exception when the start_template file does not exist' do
120
+ @config[:start_template] = '/tmp/nonexistent-template.erb'
121
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
122
+
123
+ assert_raises(Chef::Exceptions::ValidationFailed) { @zookeeper_handler.run_report_unsafe(@run_status) }
124
+ end
125
+
126
+ it 'should be able to generate the start_template when configured as an option' do
127
+ body_msg = 'My Template'
128
+ @config[:start_template] = '/tmp/existing-template.erb'
129
+ ::File.stubs(:exists?).with(@config[:start_template]).returns(true)
130
+ IO.stubs(:read).with(@config[:start_template]).returns(body_msg)
131
+ @fake_zookeeper_handler = Chef::Handler::FakeZookeeperHandler.new(@config)
132
+ Chef::Handler::FakeZookeeperHandler.any_instance.stubs(:node).returns(@node)
133
+ @fake_zookeeper_handler.run_report_unsafe(@run_status)
134
+
135
+ assert_equal @fake_zookeeper_handler.get_zookeeper_start_template_body, body_msg
136
+ end
137
+
138
+ end # describe when chef run is not over
139
+
140
+ describe 'when chef run is over' do
141
+ before do
142
+ @run_status.start_clock
143
+ @run_status.stop_clock
144
+ end
145
+
146
+ it 'should use the end_template when the run is over' do
147
+ @zookeeper_handler.stubs(:end_template_body).once
148
+ @zookeeper_handler.stubs(:start_template_body).never
149
+
150
+ @zookeeper_handler.run_report_unsafe(@run_status)
151
+ end
152
+
153
+ it 'should be able to generate the default end_template' do
154
+ @fake_zookeeper_handler = Chef::Handler::FakeZookeeperHandler.new(@config)
155
+ Chef::Handler::FakeZookeeperHandler.any_instance.stubs(:node).returns(@node)
156
+ @fake_zookeeper_handler.run_report_unsafe(@run_status)
157
+
158
+ @fake_zookeeper_handler.get_zookeeper_end_template_body.must_match Regexp.new('"end_time":')
159
+ end
160
+
161
+ it 'should throw an exception when the end_template file does not exist' do
162
+ @config[:end_template] = '/tmp/nonexistent-template.erb'
163
+ @zookeeper_handler = Chef::Handler::ZookeeperHandler.new(@config)
164
+
165
+ assert_raises(Chef::Exceptions::ValidationFailed) { @zookeeper_handler.run_report_unsafe(@run_status) }
166
+ end
167
+
168
+ it 'should be able to generate the end_template when configured as an option' do
169
+ body_msg = 'My Template'
170
+ @config[:end_template] = '/tmp/existing-template.erb'
171
+ ::File.stubs(:exists?).with(@config[:end_template]).returns(true)
172
+ IO.stubs(:read).with(@config[:end_template]).returns(body_msg)
173
+ @fake_zookeeper_handler = Chef::Handler::FakeZookeeperHandler.new(@config)
174
+ Chef::Handler::FakeZookeeperHandler.any_instance.stubs(:node).returns(@node)
175
+ @fake_zookeeper_handler.run_report_unsafe(@run_status)
176
+
177
+ assert_equal @fake_zookeeper_handler.get_zookeeper_end_template_body, body_msg
178
+ end
179
+
180
+ end # describe when chef run is over
181
+
182
+ describe 'when called without run_status (chef_handler LWRP)' do
183
+ it 'should use the start_template when the run is not over' do
184
+ @zookeeper_handler.stubs(:start_template_body).once
185
+ @zookeeper_handler.stubs(:end_template_body).never
186
+
187
+ @zookeeper_handler.run_report_unsafe(Object.new)
188
+ end
189
+ end
190
+
191
+ end # describe #report
192
+
193
+ end
@@ -0,0 +1,90 @@
1
+ require 'helper'
2
+ require 'chef/exceptions'
3
+
4
+ class ZookeeperConfig
5
+ include Chef::Handler::ZookeeperHandler::Config
6
+ end
7
+
8
+ describe Chef::Handler::ZookeeperHandler::Config do
9
+ before do
10
+ @config_params = {
11
+ :server => '127.0.0.1:2181',
12
+ :znode => '/chef/1.2.3.4',
13
+ }
14
+ @zookeeper_config = ZookeeperConfig.new
15
+ end
16
+
17
+ it 'should read the configuration options on config initialization' do
18
+ @zookeeper_config.config_init(@config_params)
19
+
20
+ assert_equal @zookeeper_config.server, @config_params[:server]
21
+ assert_equal @zookeeper_config.znode, @config_params[:znode]
22
+ end
23
+
24
+ it 'should be able to change configuration options using method calls' do
25
+ @zookeeper_config.server(@config_params[:server])
26
+ @zookeeper_config.znode(@config_params[:znode])
27
+
28
+ assert_equal @zookeeper_config.server, @config_params[:server]
29
+ assert_equal @zookeeper_config.znode, @config_params[:znode]
30
+ end
31
+
32
+ [ :server, :znode ].each do |required|
33
+ it "should throw an exception when '#{required}' required field is not set" do
34
+ @config_params.delete(required)
35
+ @config_params.each { |key, value| @zookeeper_config.send(key, value) }
36
+
37
+ assert_raises(Chef::Exceptions::ValidationFailed) { @zookeeper_config.config_check }
38
+ end
39
+ end
40
+
41
+ [ :server, :znode, :start_template, :end_template ].each do |option|
42
+
43
+ it "should accept string values in '#{option}' option" do
44
+ @zookeeper_config.send(option, 'test')
45
+ end
46
+
47
+ [ true, false, 25, Object.new ].each do |bad_value|
48
+ it "should throw and exception wen '#{option}' option is set to #{bad_value.to_s}" do
49
+ assert_raises(Chef::Exceptions::ValidationFailed) { @zookeeper_config.send(option, bad_value) }
50
+ end
51
+ end
52
+ end
53
+
54
+ [ :start_template, :end_template ].each do |template|
55
+ it "should throw an exception when the #{template} file does not exist" do
56
+ @zookeeper_config.send(template, '/tmp/nonexistent-template.erb')
57
+ ::File.stubs(:exists?).with(@zookeeper_config.send(template)).returns(false)
58
+
59
+ assert_raises(Chef::Exceptions::ValidationFailed) { @zookeeper_config.config_check }
60
+ end
61
+ end
62
+
63
+ describe 'config_init' do
64
+
65
+ it 'should accept valid config options' do
66
+ option = :server
67
+ Chef::Log.expects(:warn).never
68
+
69
+ @zookeeper_config.config_init({ option => 'valid' })
70
+ end
71
+
72
+ it 'should not accept invalid config options' do
73
+ option = :invalid_option
74
+ assert !@zookeeper_config.respond_to?(option)
75
+ Chef::Log.expects(:warn).once
76
+
77
+ @zookeeper_config.config_init({ option => 'none' })
78
+ end
79
+
80
+ it 'should not accept config options starting by "config_"' do
81
+ option = :config_check
82
+ assert @zookeeper_config.respond_to?(option)
83
+ Chef::Log.expects(:warn).once
84
+
85
+ @zookeeper_config.config_init({ option => 'exists but not configurable' })
86
+ end
87
+
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chef-handler-zookeeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Onddo Labs, SL.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: erubis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: chef
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: mocha
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: coveralls
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Chef report handler to send notifications to Zookeeper about Chef runs.
126
+ email: team@onddo.com
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - LICENSE
132
+ - README.md
133
+ - lib/chef/handler/zookeeper/templates/end.json.erb
134
+ - lib/chef/handler/zookeeper/templates/txt.erb
135
+ - lib/chef/handler/zookeeper/templates/start.json.erb
136
+ - lib/chef/handler/zookeeper/config.rb
137
+ - lib/chef/handler/zookeeper/version.rb
138
+ - lib/chef/handler/zookeeper.rb
139
+ - test/test_chef_handler_zookeeper_config.rb
140
+ - test/helper.rb
141
+ - test/test_chef_handler_zookeeper.rb
142
+ homepage: http://github.com/onddo/chef-handler-zookeeper
143
+ licenses: []
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - '>='
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.0.6
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Send Chef reports to Zookeeper
165
+ test_files:
166
+ - test/test_chef_handler_zookeeper_config.rb
167
+ - test/helper.rb
168
+ - test/test_chef_handler_zookeeper.rb