compat_resource 12.9.1 → 12.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +8 -3
  3. data/files/lib/chef_compat/copied_from_chef/chef/dsl/core.rb +56 -0
  4. data/files/lib/chef_compat/copied_from_chef/chef/dsl/declare_resource.rb +182 -7
  5. data/files/lib/chef_compat/copied_from_chef/chef/dsl/recipe.rb +5 -2
  6. data/files/lib/chef_compat/copied_from_chef/chef/mixin/lazy_module_include.rb +90 -0
  7. data/files/lib/chef_compat/copied_from_chef/chef/mixin/notifying_block.rb +66 -0
  8. data/files/lib/chef_compat/copied_from_chef/chef/mixin/powershell_out.rb +109 -0
  9. data/files/lib/chef_compat/copied_from_chef/chef/property.rb +9 -2
  10. data/files/lib/chef_compat/copied_from_chef/chef/provider.rb +2 -2
  11. data/files/lib/chef_compat/copied_from_chef/chef/resource.rb +1 -0
  12. data/files/lib/chef_compat/copied_from_chef/chef/resource/action_class.rb +3 -0
  13. data/files/lib/chef_compat/copied_from_chef/chef/resource_builder.rb +1 -1
  14. data/files/lib/chef_compat/monkeypatches.rb +7 -3
  15. data/files/lib/chef_compat/monkeypatches/chef.rb +3 -2
  16. data/files/lib/chef_compat/monkeypatches/chef/log.rb +13 -0
  17. data/files/lib/chef_compat/monkeypatches/chef/resource_collection.rb +103 -0
  18. data/files/lib/chef_compat/monkeypatches/chef/resource_collection/resource_list.rb +49 -0
  19. data/files/lib/chef_compat/monkeypatches/chef/resource_collection/resource_set.rb +49 -0
  20. data/files/lib/chef_compat/monkeypatches/chef/run_context.rb +74 -9
  21. data/files/lib/chef_compat/monkeypatches/chef/runner.rb +60 -0
  22. data/files/lib/compat_resource/version.rb +1 -1
  23. data/files/spec/cookbook_spec.rb +10 -1
  24. data/files/spec/data/.bundle/config +1 -0
  25. data/files/spec/data/Gemfile.lock +18 -50
  26. data/files/spec/data/config.rb +1 -1
  27. data/files/spec/data/cookbooks/notifications/metadata.rb +4 -0
  28. data/files/spec/data/cookbooks/notifications/recipes/default.rb +5 -0
  29. data/files/spec/data/cookbooks/notifications/resources/resource.rb +8 -0
  30. metadata +15 -3
@@ -0,0 +1,66 @@
1
+ begin
2
+ require 'chef/mixin/notifying_block'
3
+ rescue LoadError; end
4
+
5
+ require 'chef_compat/copied_from_chef'
6
+ class Chef
7
+ module ::ChefCompat
8
+ module CopiedFromChef
9
+ #--
10
+ # Author:: Lamont Granquist <lamont@chef.io>
11
+ # Copyright:: Copyright 2010-2016, Chef Software Inc.
12
+ # License:: Apache License, Version 2.0
13
+ #
14
+ # Licensed under the Apache License, Version 2.0 (the "License");
15
+ # you may not use this file except in compliance with the License.
16
+ # You may obtain a copy of the License at
17
+ #
18
+ # http://www.apache.org/licenses/LICENSE-2.0
19
+ #
20
+ # Unless required by applicable law or agreed to in writing, software
21
+ # distributed under the License is distributed on an "AS IS" BASIS,
22
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23
+ # See the License for the specific language governing permissions and
24
+ # limitations under the License.
25
+
26
+ class Chef < (defined?(::Chef) ? ::Chef : Object)
27
+ module Mixin
28
+ CopiedFromChef.extend_chef_module(::Chef::Mixin, self) if defined?(::Chef::Mixin)
29
+ module NotifyingBlock
30
+ CopiedFromChef.extend_chef_module(::Chef::Mixin::NotifyingBlock, self) if defined?(::Chef::Mixin::NotifyingBlock)
31
+
32
+ def notifying_block(&block)
33
+ begin
34
+ subcontext = subcontext_block(&block)
35
+ Chef::Runner.new(subcontext).converge
36
+ ensure
37
+ # recipes don't have a new_resource
38
+ if respond_to?(:new_resource)
39
+ if subcontext && subcontext.resource_collection.any?(&:updated?)
40
+ new_resource.updated_by_last_action(true)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def subcontext_block(parent_context = nil, &block)
47
+ parent_context ||= @run_context
48
+ sub_run_context = parent_context.create_child
49
+
50
+ begin
51
+ outer_run_context = @run_context
52
+ @run_context = sub_run_context
53
+ instance_eval(&block)
54
+ ensure
55
+ @run_context = outer_run_context
56
+ end
57
+
58
+ sub_run_context
59
+ end
60
+
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,109 @@
1
+ begin
2
+ require 'chef/mixin/powershell_out'
3
+ rescue LoadError; end
4
+
5
+ require 'chef_compat/copied_from_chef'
6
+ class Chef
7
+ module ::ChefCompat
8
+ module CopiedFromChef
9
+ #--
10
+ # Copyright:: Copyright 2015-2016, Chef Software, Inc.
11
+ # License:: Apache License, Version 2.0
12
+ #
13
+ # Licensed under the Apache License, Version 2.0 (the "License");
14
+ # you may not use this file except in compliance with the License.
15
+ # You may obtain a copy of the License at
16
+ #
17
+ # http://www.apache.org/licenses/LICENSE-2.0
18
+ #
19
+ # Unless required by applicable law or agreed to in writing, software
20
+ # distributed under the License is distributed on an "AS IS" BASIS,
21
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ # See the License for the specific language governing permissions and
23
+ # limitations under the License.
24
+
25
+
26
+ class Chef < (defined?(::Chef) ? ::Chef : Object)
27
+ module Mixin
28
+ CopiedFromChef.extend_chef_module(::Chef::Mixin, self) if defined?(::Chef::Mixin)
29
+ module PowershellOut
30
+ CopiedFromChef.extend_chef_module(::Chef::Mixin::PowershellOut, self) if defined?(::Chef::Mixin::PowershellOut)
31
+ include Chef::Mixin::ShellOut
32
+ include Chef::Mixin::WindowsArchitectureHelper
33
+
34
+ # Run a command under powershell with the same API as shell_out. The
35
+ # options hash is extended to take an "architecture" flag which
36
+ # can be set to :i386 or :x86_64 to force the windows architecture.
37
+ #
38
+ # @param script [String] script to run
39
+ # @param options [Hash] options hash
40
+ # @return [Mixlib::Shellout] mixlib-shellout object
41
+ def powershell_out(*command_args)
42
+ script = command_args.first
43
+ options = command_args.last.is_a?(Hash) ? command_args.last : nil
44
+
45
+ run_command_with_os_architecture(script, options)
46
+ end
47
+
48
+ # Run a command under powershell with the same API as shell_out!
49
+ # (raises exceptions on errors)
50
+ #
51
+ # @param script [String] script to run
52
+ # @param options [Hash] options hash
53
+ # @return [Mixlib::Shellout] mixlib-shellout object
54
+ def powershell_out!(*command_args)
55
+ cmd = powershell_out(*command_args)
56
+ cmd.error!
57
+ cmd
58
+ end
59
+
60
+ private
61
+
62
+ # Helper function to run shell_out and wrap it with the correct
63
+ # flags to possibly disable WOW64 redirection (which we often need
64
+ # because chef-client runs as a 32-bit app on 64-bit windows).
65
+ #
66
+ # @param script [String] script to run
67
+ # @param options [Hash] options hash
68
+ # @return [Mixlib::Shellout] mixlib-shellout object
69
+ def run_command_with_os_architecture(script, options)
70
+ options ||= {}
71
+ options = options.dup
72
+ arch = options.delete(:architecture)
73
+
74
+ with_os_architecture(nil, architecture: arch) do
75
+ shell_out(
76
+ build_powershell_command(script),
77
+ options
78
+ )
79
+ end
80
+ end
81
+
82
+ # Helper to build a powershell command around the script to run.
83
+ #
84
+ # @param script [String] script to run
85
+ # @retrurn [String] powershell command to execute
86
+ def build_powershell_command(script)
87
+ flags = [
88
+ # Hides the copyright banner at startup.
89
+ "-NoLogo",
90
+ # Does not present an interactive prompt to the user.
91
+ "-NonInteractive",
92
+ # Does not load the Windows PowerShell profile.
93
+ "-NoProfile",
94
+ # always set the ExecutionPolicy flag
95
+ # see http://technet.microsoft.com/en-us/library/ee176961.aspx
96
+ "-ExecutionPolicy Unrestricted",
97
+ # Powershell will hang if STDIN is redirected
98
+ # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
99
+ "-InputFormat None",
100
+ ]
101
+
102
+ "powershell.exe #{flags.join(' ')} -Command \"#{script}\""
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -77,6 +77,9 @@ class Chef < (defined?(::Chef) ? ::Chef : Object)
77
77
  # property defaults to the same value as `name`. Equivalent to
78
78
  # `default: lazy { name }`, except that #property_is_set? will
79
79
  # return `true` if the property is set *or* if `name` is set.
80
+ # @option options [Boolean] :nillable `true` opt-in to Chef-13 style behavior where
81
+ # attempting to set a nil value will really set a nil value instead of issuing
82
+ # a warning and operating like a getter
80
83
  # @option options [Object] :default The value this property
81
84
  # will return if the user does not set one. If this is `lazy`, it will
82
85
  # be run in the context of the instance (and able to access other
@@ -239,7 +242,7 @@ super if defined?(::Chef::Property)
239
242
  #
240
243
  def validation_options
241
244
  @validation_options ||= options.reject { |k, v|
242
- [:declared_in, :name, :instance_variable_name, :desired_state, :identity, :default, :name_property, :coerce, :required].include?(k)
245
+ [:declared_in, :name, :instance_variable_name, :desired_state, :identity, :default, :name_property, :coerce, :required, :nillable].include?(k)
243
246
  }
244
247
  end
245
248
 
@@ -268,7 +271,7 @@ super if defined?(::Chef::Property)
268
271
  return get(resource)
269
272
  end
270
273
 
271
- if value.nil?
274
+ if value.nil? && !nillable?
272
275
  # In Chef 12, value(nil) does a *get* instead of a set, so we
273
276
  # warn if the value would have been changed. In Chef 13, it will be
274
277
  # equivalent to value = nil.
@@ -676,6 +679,10 @@ super if defined?(::Chef::Property)
676
679
 
677
680
  result
678
681
  end
682
+
683
+ def nillable?
684
+ !!options[:nillable]
685
+ end
679
686
  end
680
687
  end
681
688
  end
@@ -6,8 +6,10 @@ require 'chef_compat/copied_from_chef'
6
6
  class Chef
7
7
  module ::ChefCompat
8
8
  module CopiedFromChef
9
+ require "chef_compat/copied_from_chef/chef/dsl/core"
9
10
  class Chef < (defined?(::Chef) ? ::Chef : Object)
10
11
  class Provider < (defined?(::Chef::Provider) ? ::Chef::Provider : Object)
12
+ include Chef::DSL::Core
11
13
  attr_accessor :action
12
14
  def initialize(new_resource, run_context)
13
15
  super if defined?(::Chef::Provider)
@@ -145,8 +147,6 @@ super if defined?(::Chef::Provider)
145
147
  end
146
148
  end
147
149
  end
148
- require "chef_compat/copied_from_chef/chef/dsl/recipe"
149
- include Chef::DSL::Recipe::FullDSL
150
150
  end
151
151
  protected
152
152
  end
@@ -9,6 +9,7 @@ module CopiedFromChef
9
9
  require "chef_compat/copied_from_chef/chef/resource/action_class"
10
10
  require "chef_compat/copied_from_chef/chef/provider"
11
11
  require "chef_compat/copied_from_chef/chef/mixin/properties"
12
+ require "chef_compat/copied_from_chef/chef/mixin/powershell_out"
12
13
  class Chef < (defined?(::Chef) ? ::Chef : Object)
13
14
  class Resource < (defined?(::Chef::Resource) ? ::Chef::Resource : Object)
14
15
  include Chef::Mixin::Properties
@@ -24,11 +24,14 @@ module CopiedFromChef
24
24
  # limitations under the License.
25
25
  #
26
26
 
27
+ require "chef_compat/copied_from_chef/chef/dsl/recipe"
27
28
 
28
29
  class Chef < (defined?(::Chef) ? ::Chef : Object)
29
30
  class Resource < (defined?(::Chef::Resource) ? ::Chef::Resource : Object)
30
31
  module ActionClass
31
32
  CopiedFromChef.extend_chef_module(::Chef::Resource::ActionClass, self) if defined?(::Chef::Resource::ActionClass)
33
+ include Chef::DSL::Recipe
34
+
32
35
  def to_s
33
36
  "#{new_resource || "<no resource>"} action #{action ? action.inspect : "<no action>"}"
34
37
  end
@@ -146,7 +146,7 @@ super if defined?(::Chef::ResourceBuilder)
146
146
  @prior_resource ||=
147
147
  begin
148
148
  key = "#{type}[#{name}]"
149
- run_context.resource_collection.lookup(key)
149
+ run_context.resource_collection.lookup_local(key)
150
150
  rescue Chef::Exceptions::ResourceNotFound
151
151
  nil
152
152
  end
@@ -1,10 +1,14 @@
1
1
  require 'chef_compat/monkeypatches/chef'
2
+ require 'chef_compat/monkeypatches/chef/exceptions'
2
3
  require 'chef_compat/monkeypatches/chef/log'
3
4
  require 'chef_compat/monkeypatches/chef/mixin/params_validate'
4
- require 'chef_compat/monkeypatches/chef/recipe'
5
- require 'chef_compat/monkeypatches/chef/resource'
6
5
  require 'chef_compat/monkeypatches/chef/property'
7
6
  require 'chef_compat/monkeypatches/chef/provider'
7
+ require 'chef_compat/monkeypatches/chef/recipe'
8
+ require 'chef_compat/monkeypatches/chef/resource'
8
9
  require 'chef_compat/monkeypatches/chef/resource/lwrp_base'
9
- require 'chef_compat/monkeypatches/chef/exceptions'
10
+ require 'chef_compat/monkeypatches/chef/resource_collection'
11
+ require 'chef_compat/monkeypatches/chef/resource_collection/resource_list'
12
+ require 'chef_compat/monkeypatches/chef/resource_collection/resource_set'
10
13
  require 'chef_compat/monkeypatches/chef/run_context'
14
+ require 'chef_compat/monkeypatches/chef/runner'
@@ -18,8 +18,8 @@ class Chef
18
18
 
19
19
  begin
20
20
  super
21
- # Bleagh. `super_method` doesn't exist on older rubies and I haven't
22
- # figured out a way to check for its existence otherwise.
21
+ # Bleagh. `super_method` doesn't exist on older rubies and I haven't
22
+ # figured out a way to check for its existence otherwise.
23
23
  rescue NoMethodError
24
24
  Chef::Log.warn(message)
25
25
  end
@@ -29,4 +29,5 @@ class Chef
29
29
  class<<self
30
30
  prepend ChefCompatDeprecation
31
31
  end
32
+
32
33
  end
@@ -19,3 +19,16 @@ end
19
19
  class<<::Chef::Log
20
20
  prepend ChefCompat::Monkeypatches::Log
21
21
  end
22
+
23
+ class Chef
24
+ class Log
25
+ unless method_defined?(:deprecation)
26
+ module ChefCompatDeprecation
27
+ def deprecation(message, location=nil)
28
+ Chef.log_deprecation(message, location)
29
+ end
30
+ end
31
+ extend ChefCompatDeprecation
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,103 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@chef.io>)
3
+ # Author:: Christopher Walters (<cw@chef.io>)
4
+ # Copyright:: Copyright 2008-2016, Chef Software Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require "chef/resource_collection/resource_set"
21
+ require "chef/resource_collection/resource_list"
22
+ require "chef/resource_collection"
23
+ require "chef/exceptions"
24
+
25
+ module ChefCompat
26
+ module Monkeypatches
27
+ module Chef
28
+ module ResourceCollection
29
+ module RecursiveNotificationLookup
30
+ #
31
+ # Copied verbatim from Chef 12.10.24
32
+ #
33
+ attr_accessor :run_context
34
+
35
+ def initialize(run_context = nil)
36
+ super()
37
+ @run_context = run_context
38
+ end
39
+
40
+ def lookup_local(key)
41
+ resource_set.lookup(key)
42
+ end
43
+
44
+ def find_local(*args)
45
+ resource_set.find(*args)
46
+ end
47
+
48
+ def lookup(key)
49
+ if run_context.nil?
50
+ lookup_local(key)
51
+ else
52
+ lookup_recursive(run_context, key)
53
+ end
54
+ end
55
+
56
+ def find(*args)
57
+ if run_context.nil?
58
+ find_local(*args)
59
+ else
60
+ find_recursive(run_context, *args)
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def lookup_recursive(rc, key)
67
+ rc.resource_collection.send(:resource_set).lookup(key)
68
+ rescue ::Chef::Exceptions::ResourceNotFound
69
+ raise if !rc.respond_to?(:parent_run_context) || rc.parent_run_context.nil?
70
+ lookup_recursive(rc.parent_run_context, key)
71
+ end
72
+
73
+ def find_recursive(rc, *args)
74
+ rc.resource_collection.send(:resource_set).find(*args)
75
+ rescue ::Chef::Exceptions::ResourceNotFound
76
+ raise if !rc.respond_to?(:parent_run_context) || rc.parent_run_context.nil?
77
+ find_recursive(rc.parent_run_context, *args)
78
+ end
79
+ end
80
+
81
+ module DeleteResources
82
+ #
83
+ # Copied verbatim from Chef 12.10.24
84
+ #
85
+ def delete(key)
86
+ resource_list.delete(key)
87
+ resource_set.delete(key)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+
96
+ class Chef::ResourceCollection
97
+ unless method_defined?(:lookup_local)
98
+ prepend ChefCompat::Monkeypatches::Chef::ResourceCollection::RecursiveNotificationLookup
99
+ end
100
+ unless method_defined?(:delete)
101
+ prepend ChefCompat::Monkeypatches::Chef::ResourceCollection::DeleteResources
102
+ end
103
+ end
@@ -0,0 +1,49 @@
1
+ #
2
+ # Author:: Tyler Ball (<tball@chef.io>)
3
+ # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef/resource_collection/resource_list"
20
+ require "chef/exceptions"
21
+
22
+ module ChefCompat
23
+ module Monkeypatches
24
+ module Chef
25
+ module ResourceCollection
26
+ module ResourceList
27
+ module DeleteResource
28
+ # Copied verbatim from Chef 12.10.4
29
+ def delete(key)
30
+ raise ArgumentError, "Must pass a Chef::Resource or String to delete" unless key.is_a?(String) || key.is_a?(Chef::Resource)
31
+ key = key.to_s
32
+ ret = @resources.reject! { |r| r.to_s == key }
33
+ if ret.nil?
34
+ raise ::Chef::Exceptions::ResourceNotFound, "Cannot find a resource matching #{key} (did you define it first?)"
35
+ end
36
+ ret
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class Chef::ResourceCollection::ResourceList
46
+ unless method_defined?(:delete)
47
+ prepend ChefCompat::Monkeypatches::Chef::ResourceCollection::ResourceList::DeleteResource
48
+ end
49
+ end