minitest-chef-handler 0.6.9 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +1 -1
  2. data/.travis.yml +4 -0
  3. data/Appraisals +7 -0
  4. data/Berksfile +4 -0
  5. data/Gemfile +3 -0
  6. data/History.txt +6 -0
  7. data/README.md +23 -17
  8. data/Rakefile +7 -3
  9. data/Vagrantfile +17 -4
  10. data/examples/spec_examples/files/default/tests/minitest/{example_test.rb → default_test.rb} +15 -21
  11. data/examples/spec_examples/metadata.rb +1 -0
  12. data/gemfiles/chef_10.gemfile +8 -0
  13. data/gemfiles/chef_11.gemfile +8 -0
  14. data/lib/minitest-chef-handler/assertions.rb +41 -0
  15. data/lib/minitest-chef-handler/ci_runner.rb +1 -0
  16. data/lib/minitest-chef-handler/resources.rb +10 -4
  17. data/minitest-chef-handler.gemspec +8 -3
  18. data/spec/minitest-chef-handler/assertions_spec.rb +158 -4
  19. data/spec/minitest-chef-handler/resources_spec.rb +25 -4
  20. data/spec/spec_helper.rb +7 -3
  21. metadata +95 -23
  22. data/examples/chef-handler-cookbook/cookbooks/chef_handler/README.md +0 -115
  23. data/examples/chef-handler-cookbook/cookbooks/chef_handler/attributes/default.rb +0 -21
  24. data/examples/chef-handler-cookbook/cookbooks/chef_handler/files/default/handlers/README +0 -1
  25. data/examples/chef-handler-cookbook/cookbooks/chef_handler/metadata.json +0 -29
  26. data/examples/chef-handler-cookbook/cookbooks/chef_handler/metadata.rb +0 -6
  27. data/examples/chef-handler-cookbook/cookbooks/chef_handler/providers/default.rb +0 -83
  28. data/examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/default.rb +0 -31
  29. data/examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/json_file.rb +0 -28
  30. data/examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/minitest.rb +0 -24
  31. data/examples/chef-handler-cookbook/cookbooks/chef_handler/resources/default.rb +0 -34
  32. data/examples/simple-solo/cookbooks/foo/recipes/default.rb +0 -1
  33. data/examples/simple-solo/cookbooks/foo/test/test_foo.rb +0 -9
  34. data/examples/simple-solo/dna.json +0 -3
  35. data/examples/simple-solo/solo.rb +0 -10
  36. data/examples/simple-solo/spec/foo_spec.rb +0 -9
  37. data/examples/simple-solo/test/test_foo.rb +0 -9
  38. data/gemfile_chef_10 +0 -3
  39. data/gemfile_chef_11 +0 -4
  40. data/script/bootstrap +0 -11
  41. data/script/test +0 -28
@@ -1,7 +1,6 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe MiniTest::Chef::Resources do
4
-
5
4
  let(:resources) { Class.new{ include MiniTest::Chef::Resources }.new }
6
5
 
7
6
  it "provides convenient access to current resource state" do
@@ -36,13 +35,13 @@ describe MiniTest::Chef::Resources do
36
35
  and(:backup, 5).must_equal(file)
37
36
  end
38
37
  it "fails if the assertion is not met" do
39
- assert_triggered(/The file does not have the expected name/) do
38
+ assert_triggered("The file /etc/foo does not have the expected name") do
40
39
  file.must_have(:name, '/etc/bar')
41
40
  end
42
- assert_triggered(/The file does not have the expected action/) do
41
+ assert_triggered("The file /etc/foo does not have the expected action") do
43
42
  file.must_have(:name, '/etc/foo').with(:action, 'delete')
44
43
  end
45
- assert_triggered(/The file does not have the expected action/) do
44
+ assert_triggered("The file /etc/foo does not have the expected action") do
46
45
  file.must_have(:name, '/etc/foo').and(:action, 'delete')
47
46
  end
48
47
  end
@@ -58,6 +57,28 @@ describe MiniTest::Chef::Resources do
58
57
  file.must_have(:mode, '755')
59
58
  end
60
59
  end
60
+
61
+ [["integer", 0755], ["string", "0755"]].each do |type, mode|
62
+ it "fails with incorrect modes on #{type} mode" do
63
+ file.define_singleton_method(:mode){ mode }
64
+ def file.mode; 0755; end
65
+ assert_triggered(/Expected: "644"\n Actual: "755"/) do
66
+ file.must_have(:mode, 0644)
67
+ end
68
+ assert_triggered(/Expected: "644"\n Actual: "755"/) do
69
+ file.must_have(:mode, 0644)
70
+ end
71
+ end
72
+
73
+ it "passes with correct modes on #{type} mode" do
74
+ file.define_singleton_method(:mode){ mode }
75
+ file.must_have(:mode, '00755')
76
+ file.must_have(:mode, '00755')
77
+ file.must_have(:mode, '0755')
78
+ file.must_have(:mode, '755')
79
+ file.must_have(:mode, 0755)
80
+ end
81
+ end
61
82
  end
62
83
 
63
84
  describe "asserting with :owner" do
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'minitest/autorun'
2
- require 'minitest/pride'
3
2
  require 'minitest/spec'
4
3
  require 'mocha/setup'
5
4
 
6
- require File.expand_path('../../lib/minitest-chef-handler', __FILE__)
5
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
6
+ require "minitest-chef-handler"
7
7
 
8
8
  # Borrowed from MiniTest
9
9
  def assert_triggered(expected)
@@ -13,5 +13,9 @@ def assert_triggered(expected)
13
13
  msg = e.message.sub(/(---Backtrace---).*/m, '\1')
14
14
  msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
15
15
 
16
- assert_match expected, msg
16
+ if expected.is_a?(String)
17
+ assert_includes msg, expected
18
+ else
19
+ assert_match expected, msg
20
+ end
17
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-chef-handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.9
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-23 00:00:00.000000000 Z
12
+ date: 2013-03-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
@@ -91,6 +91,86 @@ dependencies:
91
91
  - - ! '>='
92
92
  - !ruby/object:Gem::Version
93
93
  version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: appraisal
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: ffi
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '1'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '1'
126
+ - !ruby/object:Gem::Dependency
127
+ name: vagrant
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '1.1'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '1.1'
142
+ - !ruby/object:Gem::Dependency
143
+ name: berkshelf
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: 1.3.1
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: 1.3.1
158
+ - !ruby/object:Gem::Dependency
159
+ name: berkshelf-vagrant
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
94
174
  description: Run minitest suites after your Chef recipes to check the status of your
95
175
  system.
96
176
  email:
@@ -100,34 +180,22 @@ extensions: []
100
180
  extra_rdoc_files: []
101
181
  files:
102
182
  - .gitignore
183
+ - .travis.yml
184
+ - Appraisals
185
+ - Berksfile
186
+ - Gemfile
103
187
  - History.txt
104
188
  - LICENSE
105
189
  - README.md
106
190
  - Rakefile
107
191
  - Vagrantfile
108
- - examples/chef-handler-cookbook/cookbooks/chef_handler/README.md
109
- - examples/chef-handler-cookbook/cookbooks/chef_handler/attributes/default.rb
110
- - examples/chef-handler-cookbook/cookbooks/chef_handler/files/default/handlers/README
111
- - examples/chef-handler-cookbook/cookbooks/chef_handler/metadata.json
112
- - examples/chef-handler-cookbook/cookbooks/chef_handler/metadata.rb
113
- - examples/chef-handler-cookbook/cookbooks/chef_handler/providers/default.rb
114
- - examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/default.rb
115
- - examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/json_file.rb
116
- - examples/chef-handler-cookbook/cookbooks/chef_handler/recipes/minitest.rb
117
- - examples/chef-handler-cookbook/cookbooks/chef_handler/resources/default.rb
118
- - examples/simple-solo/cookbooks/foo/recipes/default.rb
119
- - examples/simple-solo/cookbooks/foo/test/test_foo.rb
120
- - examples/simple-solo/dna.json
121
- - examples/simple-solo/solo.rb
122
- - examples/simple-solo/spec/foo_spec.rb
123
- - examples/simple-solo/test/test_foo.rb
124
192
  - examples/spec_examples/attributes/default.rb
125
- - examples/spec_examples/files/default/tests/minitest/example_test.rb
193
+ - examples/spec_examples/files/default/tests/minitest/default_test.rb
126
194
  - examples/spec_examples/metadata.rb
127
195
  - examples/spec_examples/recipes/default.rb
128
196
  - examples/spec_examples/templates/default/foo.erb
129
- - gemfile_chef_10
130
- - gemfile_chef_11
197
+ - gemfiles/chef_10.gemfile
198
+ - gemfiles/chef_11.gemfile
131
199
  - lib/minitest-chef-handler.rb
132
200
  - lib/minitest-chef-handler/assertions.rb
133
201
  - lib/minitest-chef-handler/ci_runner.rb
@@ -140,8 +208,6 @@ files:
140
208
  - lib/minitest-chef-handler/spec.rb
141
209
  - lib/minitest-chef-handler/unit.rb
142
210
  - minitest-chef-handler.gemspec
143
- - script/bootstrap
144
- - script/test
145
211
  - spec/minitest-chef-handler/assertions_spec.rb
146
212
  - spec/minitest-chef-handler/infections_spec.rb
147
213
  - spec/minitest-chef-handler/lookup_spec.rb
@@ -160,12 +226,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
226
  - - ! '>='
161
227
  - !ruby/object:Gem::Version
162
228
  version: '0'
229
+ segments:
230
+ - 0
231
+ hash: 1044882646950604713
163
232
  required_rubygems_version: !ruby/object:Gem::Requirement
164
233
  none: false
165
234
  requirements:
166
235
  - - ! '>='
167
236
  - !ruby/object:Gem::Version
168
237
  version: '0'
238
+ segments:
239
+ - 0
240
+ hash: 1044882646950604713
169
241
  requirements: []
170
242
  rubyforge_project:
171
243
  rubygems_version: 1.8.23
@@ -1,115 +0,0 @@
1
- Description
2
- ===========
3
-
4
- 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.
5
-
6
- Attributes
7
- ==========
8
-
9
- `node["chef_handler"]["handler_path"]` - location to drop off handlers directory, default is `/var/chef/handlers`.
10
-
11
- Resource/Provider
12
- =================
13
-
14
- `chef_handler`
15
- --------------
16
-
17
- 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.
18
-
19
- 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.
20
-
21
- ### Actions
22
-
23
- - :enable: Enables the Chef handler for the current Chef run on the current node
24
- - :disable: Disables the Chef handler for the current Chef run on the current node
25
-
26
- ### Attribute Parameters
27
-
28
- - class_name: name attribute. The name of the handler class (can be module name-spaced).
29
- - source: full path to the handler file. can also be a gem path if the handler ships as part of a Ruby gem.
30
- - arguments: an array of arguments to pass the handler's class initializer
31
- - supports: type of Chef Handler to register as, ie :report, :exception or both. default is `:report => true, :exception => true`
32
-
33
- ### Example
34
-
35
- # register the Chef::Handler::JsonFile handler
36
- # that ships with the Chef gem
37
- chef_handler "Chef::Handler::JsonFile" do
38
- source "chef/handler/json_file"
39
- arguments :path => '/var/chef/reports'
40
- action :enable
41
- end
42
-
43
- # do the same but during the compile phase
44
- chef_handler "Chef::Handler::JsonFile" do
45
- source "chef/handler/json_file"
46
- arguments :path => '/var/chef/reports'
47
- action :nothing
48
- end.run_action(:enable)
49
-
50
- # handle exceptions only
51
- chef_handler "Chef::Handler::JsonFile" do
52
- source "chef/handler/json_file"
53
- arguments :path => '/var/chef/reports'
54
- supports exception => true
55
- action :enable
56
- end
57
-
58
-
59
- # enable the CloudkickHandler which was
60
- # dropped off in the default handler path.
61
- # passes the oauth key/secret to the handler's
62
- # intializer.
63
- chef_handler "CloudkickHandler" do
64
- source "#{node['chef_handler']['handler_path']}/cloudkick_handler.rb"
65
- arguments [node['cloudkick']['oauth_key'], node['cloudkick']['oauth_secret']]
66
- action :enable
67
- end
68
-
69
-
70
- Usage
71
- =====
72
-
73
- default
74
- -------
75
-
76
- 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.
77
-
78
- For information on how to write report and exception handlers for Chef, please see the Chef wiki pages:
79
- http://wiki.opscode.com/display/chef/Exception+and+Report+Handlers
80
-
81
- json_file
82
- ---------
83
-
84
- 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`.
85
-
86
- Changes/Roadmap
87
- ===============
88
-
89
- ## 1.0.4
90
-
91
- * [COOK-654] dont try and access a class before it has been loaded
92
- * fix bad boolean check (if vs unless)
93
-
94
- ## 1.0.2:
95
-
96
- * [COOK-620] ensure handler code is reloaded during daemonized chef runs
97
-
98
- License and Author
99
- ==================
100
-
101
- Author:: Seth Chisamore (<schisamo@opscode.com>)
102
-
103
- Copyright:: 2011, Opscode, Inc
104
-
105
- Licensed under the Apache License, Version 2.0 (the "License");
106
- you may not use this file except in compliance with the License.
107
- You may obtain a copy of the License at
108
-
109
- http://www.apache.org/licenses/LICENSE-2.0
110
-
111
- Unless required by applicable law or agreed to in writing, software
112
- distributed under the License is distributed on an "AS IS" BASIS,
113
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
- See the License for the specific language governing permissions and
115
- limitations under the License.
@@ -1,21 +0,0 @@
1
- #
2
- # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
- # Cookbook Name:: chef_handlers
4
- # Attribute:: default
5
- #
6
- # Copyright 2011, Opscode, Inc
7
- #
8
- # Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- # See the License for the specific language governing permissions and
18
- # limitations under the License.
19
- #
20
-
21
- default["chef_handler"]["handler_path"] = "#{File.expand_path(File.join(Chef::Config[:file_cache_path],'..'))}/handlers"
@@ -1 +0,0 @@
1
- This directory contains Chef handlers to distribute to your nodes.
@@ -1,29 +0,0 @@
1
- {
2
- "name": "chef_handler",
3
- "description": "Distribute and enable Chef Exception and Report handlers",
4
- "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",
5
- "maintainer": "Opscode, Inc.",
6
- "maintainer_email": "cookbooks@opscode.com",
7
- "license": "Apache 2.0",
8
- "platforms": {
9
- },
10
- "dependencies": {
11
- },
12
- "recommendations": {
13
- },
14
- "suggestions": {
15
- },
16
- "conflicting": {
17
- },
18
- "providing": {
19
- },
20
- "replacing": {
21
- },
22
- "attributes": {
23
- },
24
- "groupings": {
25
- },
26
- "recipes": {
27
- },
28
- "version": "1.0.4"
29
- }
@@ -1,6 +0,0 @@
1
- maintainer "Opscode, Inc."
2
- maintainer_email "cookbooks@opscode.com"
3
- license "Apache 2.0"
4
- description "Distribute and enable Chef Exception and Report handlers"
5
- long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
6
- version "1.0.4"
@@ -1,83 +0,0 @@
1
- #
2
- # Author:: Seth Chisamore <schisamo@opscode.com>
3
- # Cookbook Name:: chef_handler
4
- # Provider:: default
5
- #
6
- # Copyright:: 2011, Opscode, Inc <legal@opscode.com>
7
- #
8
- # Licensed under the Apache License, Version 2.0 (the "License");
9
- # you may not use this file except in compliance with the License.
10
- # You may obtain a copy of the License at
11
- #
12
- # http://www.apache.org/licenses/LICENSE-2.0
13
- #
14
- # Unless required by applicable law or agreed to in writing, software
15
- # distributed under the License is distributed on an "AS IS" BASIS,
16
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- # See the License for the specific language governing permissions and
18
- # limitations under the License.
19
- #
20
-
21
- action :enable do
22
- # use load instead of require to ensure the handler file
23
- # is reloaded into memory each chef run. fixes COOK-620
24
- begin
25
- Object.send(:remove_const, klass)
26
- GC.start
27
- rescue
28
- Chef::Log.debug("#{@new_resource.class_name} has not been loaded.")
29
- end
30
- file_name = @new_resource.source
31
- file_name << ".rb" unless file_name =~ /.*\.rb$/
32
- load file_name
33
- handler = klass.send(:new, *collect_args(@new_resource.arguments))
34
- @new_resource.supports.each do |type, enable|
35
- if enable
36
- # we have to re-enable the handler every chef run
37
- # to ensure daemonized Chef always has the latest
38
- # handler code. TODO: add a :reload action
39
- Chef::Log.info("Enabling #{@new_resource} as a #{type} handler")
40
- Chef::Config.send("#{type.to_s}_handlers").delete_if {|v| v.class.to_s.include? @new_resource.class_name}
41
- Chef::Config.send("#{type.to_s}_handlers") << handler
42
- new_resource.updated_by_last_action(true)
43
- end
44
- end
45
- end
46
-
47
- action :disable do
48
- @new_resource.supports.each_key do |type|
49
- if enabled?(type)
50
- Chef::Log.info("Disabling #{@new_resource} as a #{type} handler")
51
- Chef::Config.send("#{type.to_s}_handlers").delete_if {|v| v.class.to_s.include? @new_resource.class_name}
52
- new_resource.updated_by_last_action(true)
53
- end
54
- end
55
- end
56
-
57
- def load_current_resource
58
- @current_resource = Chef::Resource::ChefHandler.new(@new_resource.name)
59
- @current_resource.class_name(@new_resource.class_name)
60
- @current_resource.source(@new_resource.source)
61
- @current_resource
62
- end
63
-
64
- private
65
- def enabled?(type)
66
- Chef::Config.send("#{type.to_s}_handlers").select do |handler|
67
- handler.class.to_s.include? @new_resource.class_name
68
- end.size >= 1
69
- end
70
-
71
- def collect_args(resource_args = [])
72
- if resource_args.is_a? Array
73
- resource_args
74
- else
75
- [resource_args]
76
- end
77
- end
78
-
79
- def klass
80
- @klass ||= begin
81
- @new_resource.class_name.split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
82
- end
83
- end