compat_resource 12.5.14 → 12.5.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2dc72a948f1cb7b4ae30ace971a375f319ef8d4
4
- data.tar.gz: 7b4d49278d4c10aec56e7e5dd89ca94ad7dfc551
3
+ metadata.gz: f6db2428a9f97eb486526016e223b22e7d9aeb90
4
+ data.tar.gz: a68c64f80db32ae72d7e8ab455714696930fed20
5
5
  SHA512:
6
- metadata.gz: 59b1fc8b98642673dc507e80e3931d24c4c8edc52fbbba39353e6a004dbac177799c3b8b2d108e6ee7ab19eef734eb5974b91bcf78b04ea4ae93d176124b57c9
7
- data.tar.gz: 0ef0958d11750723c38094733724a82bc3bfa7c381ad4d475e2fd703360dd725f7d760a2668a1a7c17b56fadb9f73e1946a04db4675086df29256156093b5f88
6
+ metadata.gz: 2192e789fc249938436c3c2e21de729794d4340bf62e11ee0a500b32b69b8820f5d40d7ed54462d834a8f786c396cdc45c0624954b433c7c07dc744871e00e04
7
+ data.tar.gz: 8deb65e3e46dda8f5dab098a79930a30e9cd62456863bc83639449c3d5106bbc02ca90162f9f7d18da703a22c4f22dceed22a7f89fda3a7fabd86a75ba11ce25
data/Rakefile CHANGED
@@ -13,10 +13,21 @@ task default: :spec
13
13
  #
14
14
  # "rake update" updates the copied_from_chef files so we can grab bugfixes or new features
15
15
  #
16
- CHEF_FILES = %w(chef/constants chef/delayed_evaluator chef/property
17
- chef/resource chef/resource/action_class chef/provider
18
- chef/mixin/params_validate chef/mixin/properties)
19
- SPEC_FILES = %w(unit/mixin/properties_spec.rb
16
+ CHEF_FILES = %w(
17
+ chef/constants
18
+ chef/delayed_evaluator
19
+ chef/dsl/declare_resource
20
+ chef/dsl/recipe
21
+ chef/mixin/params_validate
22
+ chef/mixin/properties
23
+ chef/property
24
+ chef/provider
25
+ chef/resource
26
+ chef/resource_builder
27
+ chef/resource/action_class
28
+ )
29
+ SPEC_FILES = %w(
30
+ unit/mixin/properties_spec.rb
20
31
  unit/property_spec.rb
21
32
  unit/property/state_spec.rb
22
33
  unit/property/validation_spec.rb
@@ -33,6 +44,7 @@ KEEP_FUNCTIONS = {
33
44
  resource_name self.use_automatic_resource_name
34
45
 
35
46
  identity state state_for_resource_reporter property_is_set reset_property
47
+ to_hash
36
48
  self.properties self.state_properties self.state_attr
37
49
  self.identity_properties self.identity_property self.identity_attrs
38
50
  self.property self.property_type
@@ -47,17 +59,21 @@ KEEP_FUNCTIONS = {
47
59
  'chef/provider' => %w(
48
60
  initialize
49
61
  converge_if_changed
62
+ compile_and_converge_action
63
+ action
50
64
  self.use_inline_resources
51
65
  self.include_resource_dsl
52
66
  self.include_resource_dsl_module
53
67
  ),
68
+ 'chef/dsl/recipe' => %w(),
54
69
  }
55
70
  KEEP_INCLUDES = {
56
71
  'chef/resource' => %w(Chef::Mixin::ParamsValidate Chef::Mixin::Properties),
57
- 'chef/provider' => [],
72
+ 'chef/provider' => %w(Chef::DSL::Recipe::FullDSL),
73
+ 'chef/dsl/recipe' => %w(Chef::DSL::DeclareResource Chef::DSL::Recipe),
58
74
  }
59
75
  KEEP_CLASSES = {
60
- 'chef/provider' => %w(Chef::Provider)
76
+ 'chef/provider' => %w(Chef::Provider Chef::Provider::InlineResources Chef::Provider::InlineResources::ClassMethods)
61
77
  }
62
78
  SKIP_LINES = {
63
79
  'chef/dsl/recipe' => [ /include Chef::Mixin::PowershellOut/ ]
@@ -150,11 +166,14 @@ task :update do
150
166
  line = "#{indent}#{type}#{space}#{class_name} < (defined?(#{original_class}) ? #{original_class} : #{superclass_name})"
151
167
  else
152
168
  # Modules have a harder time of it because of self methods
153
- line += "#{indent} if defined?(#{original_class})\n"
154
- line += "#{indent} require 'chef_compat/delegating_class'\n"
155
- line += "#{indent} extend DelegatingClass\n"
156
- line += "#{indent} @delegates_to = #{original_class}\n"
157
- line += "#{indent} end"
169
+ line += <<-EOM
170
+ #{indent} if defined?(#{original_class})
171
+ #{indent} include #{original_class}
172
+ #{indent} @delegates_to = #{original_class}
173
+ #{indent} require 'chef_compat/delegating_class'
174
+ #{indent} extend DelegatingClass
175
+ #{indent} end
176
+ EOM
158
177
  end
159
178
 
160
179
  # If we're not in a class we care about, don't print stuff
@@ -163,7 +182,7 @@ task :update do
163
182
  end
164
183
 
165
184
  # Modify requires to overridden files to bring in the local version
166
- if line =~ /\A(\s*require\s*['"])(.+)(['"]\s*)$/
185
+ if line =~ /\A(\s*require\s*['"])([^'"]+)(['"].*)/
167
186
  if CHEF_FILES.include?($2)
168
187
  line = "#{$1}chef_compat/copied_from_chef/#{$2}#{$3}"
169
188
  else
@@ -1,7 +1,34 @@
1
- require 'chef_compat/version'
2
- require 'chef_compat/resource'
3
- require 'chef_compat/property'
4
- require 'chef_compat/mixin/properties'
5
1
 
6
- module ChefCompat
2
+ if Gem::Requirement.new(">= 12.0").satisfied_by?(Gem::Version.new(Chef::VERSION))
3
+
4
+ require 'chef_compat/version'
5
+ require 'chef_compat/resource'
6
+ require 'chef_compat/property'
7
+ require 'chef_compat/mixin/properties'
8
+
9
+ module ChefCompat
10
+ end
11
+
12
+ else
13
+
14
+ class Chef
15
+ class Resource
16
+ def self.property(args, &block)
17
+ raise_chef_11_error
18
+ end
19
+
20
+ def self.resource_name(args, &block)
21
+ raise_chef_11_error
22
+ end
23
+
24
+ def self.action(args, &block)
25
+ raise_chef_11_error
26
+ end
27
+
28
+ def self.raise_chef_11_error
29
+ raise "This resource is written with Chef 12.5 custom resources, and requires at least Chef 12.0 used with the compat_resource cookbook, it will not work with Chef 11.x clients, and those users must pin their cookbooks to older versions or upgrade."
30
+ end
31
+ end
32
+ end
33
+
7
34
  end
@@ -1,2 +1,16 @@
1
- # Because resource does not include chef/provider, we have to do it here
2
- require 'chef_compat/copied_from_chef/chef/provider'
1
+ module ChefCompat
2
+ module CopiedFromChef
3
+ # This patch to ActionClass is necessary for the include to work
4
+ require 'chef/resource'
5
+ class Chef < ::Chef
6
+ class Resource < ::Chef::Resource
7
+ module ActionClass
8
+ def self.use_inline_resources
9
+ end
10
+ def self.include_resource_dsl(include_resource_dsl)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,126 @@
1
+ require 'chef_compat/copied_from_chef'
2
+ class Chef
3
+ module ::ChefCompat
4
+ module CopiedFromChef
5
+ #--
6
+ # Author:: Adam Jacob (<adam@opscode.com>)
7
+ # Author:: Christopher Walters (<cw@opscode.com>)
8
+ # Copyright:: Copyright (c) 2008, 2009-2015 Chef Software, Inc.
9
+ # License:: Apache License, Version 2.0
10
+ #
11
+ # Licensed under the Apache License, Version 2.0 (the "License");
12
+ # you may not use this file except in compliance with the License.
13
+ # You may obtain a copy of the License at
14
+ #
15
+ # http://www.apache.org/licenses/LICENSE-2.0
16
+ #
17
+ # Unless required by applicable law or agreed to in writing, software
18
+ # distributed under the License is distributed on an "AS IS" BASIS,
19
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ # See the License for the specific language governing permissions and
21
+ # limitations under the License.
22
+ #
23
+
24
+
25
+ class Chef < (defined?(::Chef) ? ::Chef : Object)
26
+ module DSL
27
+ if defined?(::Chef::DSL)
28
+ include ::Chef::DSL
29
+ @delegates_to = ::Chef::DSL
30
+ require 'chef_compat/delegating_class'
31
+ extend DelegatingClass
32
+ end
33
+ module DeclareResource
34
+ if defined?(::Chef::DSL::DeclareResource)
35
+ include ::Chef::DSL::DeclareResource
36
+ @delegates_to = ::Chef::DSL::DeclareResource
37
+ require 'chef_compat/delegating_class'
38
+ extend DelegatingClass
39
+ end
40
+
41
+ #
42
+ # Instantiates a resource (via #build_resource), then adds it to the
43
+ # resource collection. Note that resource classes are looked up directly,
44
+ # so this will create the resource you intended even if the method name
45
+ # corresponding to that resource has been overridden.
46
+ #
47
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
48
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
49
+ # @param created_at [String] The caller of the resource. Use `caller[0]`
50
+ # to get the caller of your function. Defaults to the caller of this
51
+ # function.
52
+ # @param resource_attrs_block A block that lets you set attributes of the
53
+ # resource (it is instance_eval'd on the resource instance).
54
+ #
55
+ # @return [Chef::Resource] The new resource.
56
+ #
57
+ # @example
58
+ # declare_resource(:file, '/x/y.txy', caller[0]) do
59
+ # action :delete
60
+ # end
61
+ # # Equivalent to
62
+ # file '/x/y.txt' do
63
+ # action :delete
64
+ # end
65
+ #
66
+ def declare_resource(type, name, created_at=nil, run_context: self.run_context, create_if_missing: false, &resource_attrs_block)
67
+ created_at ||= caller[0]
68
+
69
+ if create_if_missing
70
+ begin
71
+ resource = run_context.resource_collection.find(type => name)
72
+ return resource
73
+ rescue Chef::Exceptions::ResourceNotFound
74
+ end
75
+ end
76
+
77
+ resource = build_resource(type, name, created_at, &resource_attrs_block)
78
+
79
+ run_context.resource_collection.insert(resource, resource_type: type, instance_name: name)
80
+ resource
81
+ end
82
+
83
+ #
84
+ # Instantiate a resource of the given +type+ with the given +name+ and
85
+ # attributes as given in the +resource_attrs_block+.
86
+ #
87
+ # The resource is NOT added to the resource collection.
88
+ #
89
+ # @param type [Symbol] The type of resource (e.g. `:file` or `:package`)
90
+ # @param name [String] The name of the resource (e.g. '/x/y.txt' or 'apache2')
91
+ # @param created_at [String] The caller of the resource. Use `caller[0]`
92
+ # to get the caller of your function. Defaults to the caller of this
93
+ # function.
94
+ # @param resource_attrs_block A block that lets you set attributes of the
95
+ # resource (it is instance_eval'd on the resource instance).
96
+ #
97
+ # @return [Chef::Resource] The new resource.
98
+ #
99
+ # @example
100
+ # build_resource(:file, '/x/y.txy', caller[0]) do
101
+ # action :delete
102
+ # end
103
+ #
104
+ def build_resource(type, name, created_at=nil, run_context: self.run_context, &resource_attrs_block)
105
+ created_at ||= caller[0]
106
+ Thread.exclusive do
107
+ require 'chef_compat/copied_from_chef/chef/resource_builder' unless defined?(Chef::ResourceBuilder)
108
+ end
109
+
110
+ Chef::ResourceBuilder.new(
111
+ type: type,
112
+ name: name,
113
+ created_at: created_at,
114
+ params: @params,
115
+ run_context: run_context,
116
+ cookbook_name: cookbook_name,
117
+ recipe_name: recipe_name,
118
+ enclosing_provider: self.is_a?(Chef::Provider) ? self : nil
119
+ ).build(&resource_attrs_block)
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,37 @@
1
+ require 'chef_compat/copied_from_chef'
2
+ class Chef
3
+ module ::ChefCompat
4
+ module CopiedFromChef
5
+ require 'chef_compat/copied_from_chef/chef/dsl/declare_resource'
6
+ class Chef < (defined?(::Chef) ? ::Chef : Object)
7
+ module DSL
8
+ if defined?(::Chef::DSL)
9
+ include ::Chef::DSL
10
+ @delegates_to = ::Chef::DSL
11
+ require 'chef_compat/delegating_class'
12
+ extend DelegatingClass
13
+ end
14
+ module Recipe
15
+ if defined?(::Chef::DSL::Recipe)
16
+ include ::Chef::DSL::Recipe
17
+ @delegates_to = ::Chef::DSL::Recipe
18
+ require 'chef_compat/delegating_class'
19
+ extend DelegatingClass
20
+ end
21
+ include Chef::DSL::DeclareResource
22
+ module FullDSL
23
+ if defined?(::Chef::DSL::Recipe::FullDSL)
24
+ include ::Chef::DSL::Recipe::FullDSL
25
+ @delegates_to = ::Chef::DSL::Recipe::FullDSL
26
+ require 'chef_compat/delegating_class'
27
+ extend DelegatingClass
28
+ end
29
+ include Chef::DSL::Recipe
30
+ end
31
+ end
32
+ end
33
+ end
34
+ require 'chef_compat/copied_from_chef/chef/resource'
35
+ end
36
+ end
37
+ end
@@ -26,15 +26,17 @@ require 'chef_compat/copied_from_chef/chef/delayed_evaluator'
26
26
  class Chef < (defined?(::Chef) ? ::Chef : Object)
27
27
  module Mixin
28
28
  if defined?(::Chef::Mixin)
29
+ include ::Chef::Mixin
30
+ @delegates_to = ::Chef::Mixin
29
31
  require 'chef_compat/delegating_class'
30
32
  extend DelegatingClass
31
- @delegates_to = ::Chef::Mixin
32
33
  end
33
34
  module ParamsValidate
34
35
  if defined?(::Chef::Mixin::ParamsValidate)
36
+ include ::Chef::Mixin::ParamsValidate
37
+ @delegates_to = ::Chef::Mixin::ParamsValidate
35
38
  require 'chef_compat/delegating_class'
36
39
  extend DelegatingClass
37
- @delegates_to = ::Chef::Mixin::ParamsValidate
38
40
  end
39
41
 
40
42
  # Takes a hash of options, along with a map to validate them. Returns the original
@@ -9,21 +9,24 @@ require 'chef_compat/copied_from_chef/chef/property'
9
9
  class Chef < (defined?(::Chef) ? ::Chef : Object)
10
10
  module Mixin
11
11
  if defined?(::Chef::Mixin)
12
+ include ::Chef::Mixin
13
+ @delegates_to = ::Chef::Mixin
12
14
  require 'chef_compat/delegating_class'
13
15
  extend DelegatingClass
14
- @delegates_to = ::Chef::Mixin
15
16
  end
16
17
  module Properties
17
18
  if defined?(::Chef::Mixin::Properties)
19
+ include ::Chef::Mixin::Properties
20
+ @delegates_to = ::Chef::Mixin::Properties
18
21
  require 'chef_compat/delegating_class'
19
22
  extend DelegatingClass
20
- @delegates_to = ::Chef::Mixin::Properties
21
23
  end
22
24
  module ClassMethods
23
25
  if defined?(::Chef::Mixin::Properties::ClassMethods)
26
+ include ::Chef::Mixin::Properties::ClassMethods
27
+ @delegates_to = ::Chef::Mixin::Properties::ClassMethods
24
28
  require 'chef_compat/delegating_class'
25
29
  extend DelegatingClass
26
- @delegates_to = ::Chef::Mixin::Properties::ClassMethods
27
30
  end
28
31
  #
29
32
  # The list of properties defined on this resource.
@@ -4,6 +4,7 @@ module ::ChefCompat
4
4
  module CopiedFromChef
5
5
  class Chef < (defined?(::Chef) ? ::Chef : Object)
6
6
  class Provider < (defined?(::Chef::Provider) ? ::Chef::Provider : Object)
7
+ attr_accessor :action
7
8
  def initialize(new_resource, run_context)
8
9
  super if defined?(::Chef::Provider)
9
10
  @new_resource = new_resource
@@ -109,6 +110,50 @@ super if defined?(::Chef::Provider)
109
110
  extend InlineResources::ClassMethods
110
111
  include InlineResources
111
112
  end
113
+ module InlineResources
114
+ if defined?(::Chef::Provider::InlineResources)
115
+ include ::Chef::Provider::InlineResources
116
+ @delegates_to = ::Chef::Provider::InlineResources
117
+ require 'chef_compat/delegating_class'
118
+ extend DelegatingClass
119
+ end
120
+ def compile_and_converge_action(&block)
121
+ old_run_context = run_context
122
+ @run_context = run_context.create_child
123
+ return_value = instance_eval(&block)
124
+ Chef::Runner.new(run_context).converge
125
+ return_value
126
+ ensure
127
+ if run_context.resource_collection.any? { |r| r.updated? }
128
+ new_resource.updated_by_last_action(true)
129
+ end
130
+ @run_context = old_run_context
131
+ end
132
+ module ClassMethods
133
+ if defined?(::Chef::Provider::InlineResources::ClassMethods)
134
+ include ::Chef::Provider::InlineResources::ClassMethods
135
+ @delegates_to = ::Chef::Provider::InlineResources::ClassMethods
136
+ require 'chef_compat/delegating_class'
137
+ extend DelegatingClass
138
+ end
139
+ def action(name, &block)
140
+ # We need the block directly in a method so that `super` works
141
+ define_method("compile_action_#{name}", &block)
142
+ # We try hard to use `def` because define_method doesn't show the method name in the stack.
143
+ begin
144
+ class_eval <<-EOM
145
+ def action_#{name}
146
+ compile_and_converge_action { compile_action_#{name} }
147
+ end
148
+ EOM
149
+ rescue SyntaxError
150
+ define_method("action_#{name}") { send("compile_action_#{name}") }
151
+ end
152
+ end
153
+ end
154
+ require 'chef_compat/copied_from_chef/chef/dsl/recipe'
155
+ include Chef::DSL::Recipe::FullDSL
156
+ end
112
157
  protected
113
158
  end
114
159
  end
@@ -3,6 +3,7 @@ class Chef
3
3
  module ::ChefCompat
4
4
  module CopiedFromChef
5
5
  require 'chef_compat/copied_from_chef/chef/resource/action_class'
6
+ require 'chef_compat/copied_from_chef/chef/provider'
6
7
  require 'chef_compat/copied_from_chef/chef/mixin/properties'
7
8
  class Chef < (defined?(::Chef) ? ::Chef : Object)
8
9
  class Resource < (defined?(::Chef::Resource) ? ::Chef::Resource : Object)
@@ -72,6 +73,20 @@ super if defined?(::Chef::Resource)
72
73
  return result.values.first if identity_properties.size == 1
73
74
  result
74
75
  end
76
+ def to_hash
77
+ # Grab all current state, then any other ivars (backcompat)
78
+ result = {}
79
+ self.class.state_properties.each do |p|
80
+ result[p.name] = p.get(self)
81
+ end
82
+ safe_ivars = instance_variables.map { |ivar| ivar.to_sym } - FORBIDDEN_IVARS
83
+ safe_ivars.each do |iv|
84
+ key = iv.to_s.sub(/^@/,'').to_sym
85
+ next if result.has_key?(key)
86
+ result[key] = instance_variable_get(iv)
87
+ end
88
+ result
89
+ end
75
90
  def self.identity_property(name=nil)
76
91
  result = identity_properties(*Array(name))
77
92
  if result.size > 1
@@ -25,9 +25,10 @@ class Chef < (defined?(::Chef) ? ::Chef : Object)
25
25
  class Resource < (defined?(::Chef::Resource) ? ::Chef::Resource : Object)
26
26
  module ActionClass
27
27
  if defined?(::Chef::Resource::ActionClass)
28
+ include ::Chef::Resource::ActionClass
29
+ @delegates_to = ::Chef::Resource::ActionClass
28
30
  require 'chef_compat/delegating_class'
29
31
  extend DelegatingClass
30
- @delegates_to = ::Chef::Resource::ActionClass
31
32
  end
32
33
  #
33
34
  # If load_current_value! is defined on the resource, use that.
@@ -72,9 +73,10 @@ class Chef < (defined?(::Chef) ? ::Chef : Object)
72
73
 
73
74
  module ClassMethods
74
75
  if defined?(::Chef::Resource::ActionClass::ClassMethods)
76
+ include ::Chef::Resource::ActionClass::ClassMethods
77
+ @delegates_to = ::Chef::Resource::ActionClass::ClassMethods
75
78
  require 'chef_compat/delegating_class'
76
79
  extend DelegatingClass
77
- @delegates_to = ::Chef::Resource::ActionClass::ClassMethods
78
80
  end
79
81
  #
80
82
  # The Chef::Resource class this ActionClass was declared against.
@@ -0,0 +1,150 @@
1
+ require 'chef_compat/copied_from_chef'
2
+ class Chef
3
+ module ::ChefCompat
4
+ module CopiedFromChef
5
+ #
6
+ # Author:: Lamont Granquist (<lamont@chef.io>)
7
+ # Copyright:: Copyright (c) 2015-2015 Chef Software, Inc.
8
+ # License:: Apache License, Version 2.0
9
+ #
10
+ # Licensed under the Apache License, Version 2.0 (the "License");
11
+ # you may not use this file except in compliance with the License.
12
+ # You may obtain a copy of the License at
13
+ #
14
+ # http://www.apache.org/licenses/LICENSE-2.0
15
+ #
16
+ # Unless required by applicable law or agreed to in writing, software
17
+ # distributed under the License is distributed on an "AS IS" BASIS,
18
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ # See the License for the specific language governing permissions and
20
+ # limitations under the License.
21
+ #
22
+
23
+ # NOTE: this was extracted from the Recipe DSL mixin, relevant specs are in spec/unit/recipe_spec.rb
24
+
25
+ class Chef < (defined?(::Chef) ? ::Chef : Object)
26
+ class ResourceBuilder < (defined?(::Chef::ResourceBuilder) ? ::Chef::ResourceBuilder : Object)
27
+ attr_reader :type
28
+ attr_reader :name
29
+ attr_reader :created_at
30
+ attr_reader :params
31
+ attr_reader :run_context
32
+ attr_reader :cookbook_name
33
+ attr_reader :recipe_name
34
+ attr_reader :enclosing_provider
35
+ attr_reader :resource
36
+
37
+ # FIXME (ruby-2.1 syntax): most of these are mandatory
38
+ def initialize(type:nil, name:nil, created_at: nil, params: nil, run_context: nil, cookbook_name: nil, recipe_name: nil, enclosing_provider: nil)
39
+ super if defined?(::Chef::ResourceBuilder)
40
+ @type = type
41
+ @name = name
42
+ @created_at = created_at
43
+ @params = params
44
+ @run_context = run_context
45
+ @cookbook_name = cookbook_name
46
+ @recipe_name = recipe_name
47
+ @enclosing_provider = enclosing_provider
48
+ end
49
+
50
+ def build(&block)
51
+ raise ArgumentError, "You must supply a name when declaring a #{type} resource" if name.nil?
52
+
53
+ @resource = resource_class.new(name, run_context)
54
+ if resource.resource_name.nil?
55
+ raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`! Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?"
56
+ end
57
+ resource.source_line = created_at
58
+ resource.declared_type = type
59
+
60
+ # If we have a resource like this one, we want to steal its state
61
+ # This behavior is very counter-intuitive and should be removed.
62
+ # See CHEF-3694, https://tickets.opscode.com/browse/CHEF-3694
63
+ # Moved to this location to resolve CHEF-5052, https://tickets.opscode.com/browse/CHEF-5052
64
+ if prior_resource
65
+ resource.load_from(prior_resource)
66
+ end
67
+
68
+ resource.cookbook_name = cookbook_name
69
+ resource.recipe_name = recipe_name
70
+ # Determine whether this resource is being created in the context of an enclosing Provider
71
+ resource.enclosing_provider = enclosing_provider
72
+
73
+ # XXX: this is required for definition params inside of the scope of a
74
+ # subresource to work correctly.
75
+ resource.params = params
76
+
77
+ # Evaluate resource attribute DSL
78
+ resource.instance_eval(&block) if block_given?
79
+
80
+ # emit a cloned resource warning if it is warranted
81
+ if prior_resource
82
+ if is_trivial_resource?(prior_resource) && identicalish_resources?(prior_resource, resource)
83
+ emit_harmless_cloning_debug
84
+ else
85
+ emit_cloned_resource_warning
86
+ end
87
+ end
88
+
89
+ # Run optional resource hook
90
+ resource.after_created
91
+
92
+ resource
93
+ end
94
+
95
+ private
96
+
97
+ def resource_class
98
+ # Checks the new platform => short_name => resource mapping initially
99
+ # then fall back to the older approach (Chef::Resource.const_get) for
100
+ # backward compatibility
101
+ @resource_class ||= Chef::Resource.resource_for_node(type, run_context.node)
102
+ end
103
+
104
+ def is_trivial_resource?(resource)
105
+ identicalish_resources?(resource_class.new(name, run_context), resource)
106
+ end
107
+
108
+ # this is an equality test specific to checking for 3694 cloning warnings
109
+ def identicalish_resources?(first, second)
110
+ skipped_ivars = [ :@source_line, :@cookbook_name, :@recipe_name, :@params, :@elapsed_time, :@declared_type ]
111
+ checked_ivars = ( first.instance_variables | second.instance_variables ) - skipped_ivars
112
+ non_matching_ivars = checked_ivars.reject do |iv|
113
+ if iv == :@action && ( [first.instance_variable_get(iv)].flatten == [:nothing] || [second.instance_variable_get(iv)].flatten == [:nothing] )
114
+ # :nothing action on either side of the comparison always matches
115
+ true
116
+ else
117
+ first.instance_variable_get(iv) == second.instance_variable_get(iv)
118
+ end
119
+ end
120
+ Chef::Log.debug("ivars which did not match with the prior resource: #{non_matching_ivars}")
121
+ non_matching_ivars.empty?
122
+ end
123
+
124
+ def emit_cloned_resource_warning
125
+ Chef::Log.warn("Cloning resource attributes for #{resource} from prior resource (CHEF-3694)")
126
+ Chef::Log.warn("Previous #{prior_resource}: #{prior_resource.source_line}") if prior_resource.source_line
127
+ Chef::Log.warn("Current #{resource}: #{resource.source_line}") if resource.source_line
128
+ end
129
+
130
+ def emit_harmless_cloning_debug
131
+ Chef::Log.debug("Harmless resource cloning from #{prior_resource}:#{prior_resource.source_line} to #{resource}:#{resource.source_line}")
132
+ end
133
+
134
+ def prior_resource
135
+ @prior_resource ||=
136
+ begin
137
+ key = "#{type}[#{name}]"
138
+ prior_resource = run_context.resource_collection.lookup(key)
139
+ rescue Chef::Exceptions::ResourceNotFound
140
+ nil
141
+ end
142
+ end
143
+
144
+ end
145
+ end
146
+
147
+ require 'chef_compat/copied_from_chef/chef/resource'
148
+ end
149
+ end
150
+ end
@@ -1,5 +1,7 @@
1
1
  require 'chef_compat/monkeypatches/chef'
2
+ require 'chef_compat/monkeypatches/chef/recipe'
2
3
  require 'chef_compat/monkeypatches/chef/resource'
3
4
  require 'chef_compat/monkeypatches/chef/provider'
4
5
  require 'chef_compat/monkeypatches/chef/resource/lwrp_base'
5
6
  require 'chef_compat/monkeypatches/chef/exceptions'
7
+ require 'chef_compat/monkeypatches/chef/run_context'
@@ -0,0 +1,18 @@
1
+ require 'chef/recipe'
2
+ require 'chef_compat/recipe'
3
+
4
+ class Chef::Recipe
5
+ # If the cookbook depends on compat_resource, create a ChefCompat::Recipe object
6
+ # instead of Chef::Recipe, for the extra goodies.
7
+ def self.new(cookbook_name, recipe_name, run_context)
8
+ if run_context &&
9
+ run_context.cookbook_collection &&
10
+ run_context.cookbook_collection[cookbook_name] &&
11
+ run_context.cookbook_collection[cookbook_name].metadata.dependencies.has_key?('compat_resource') &&
12
+ self != ::ChefCompat::Recipe
13
+ ::ChefCompat::Recipe.new(cookbook_name, recipe_name, run_context)
14
+ else
15
+ super
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'chef/resource'
2
+
3
+ class Chef
4
+ class RunContext
5
+ if !method_defined?(:create_child)
6
+ def create_child
7
+ result = dup
8
+ result.resource_collection = Chef::ResourceCollection.new
9
+ result
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ require 'chef/recipe'
2
+ require 'chef_compat/copied_from_chef/chef/dsl/recipe'
3
+
4
+ module ChefCompat
5
+ class Recipe < Chef::Recipe
6
+ include ChefCompat::CopiedFromChef::Chef::DSL::Recipe::FullDSL
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module ChefCompat
2
- VERSION = '12.5.14' if !defined?(VERSION)
2
+ VERSION = '12.5.15' if !defined?(VERSION)
3
3
  end
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compat_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 12.5.14
4
+ version: 12.5.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Keiser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-04 00:00:00.000000000 Z
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: cheffish
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: stove
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: chef
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: Bring some new features of Chef 12.5 to previous 12.X releases
@@ -86,33 +86,39 @@ executables: []
86
86
  extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
- - LICENSE
90
- - README.md
91
89
  - CHANGELOG.md
92
90
  - Gemfile
91
+ - LICENSE
92
+ - README.md
93
93
  - Rakefile
94
+ - files/lib/chef_compat.rb
95
+ - files/lib/chef_compat/copied_from_chef.rb
94
96
  - files/lib/chef_compat/copied_from_chef/chef/constants.rb
95
97
  - files/lib/chef_compat/copied_from_chef/chef/delayed_evaluator.rb
98
+ - files/lib/chef_compat/copied_from_chef/chef/dsl/declare_resource.rb
99
+ - files/lib/chef_compat/copied_from_chef/chef/dsl/recipe.rb
96
100
  - files/lib/chef_compat/copied_from_chef/chef/mixin/params_validate.rb
97
101
  - files/lib/chef_compat/copied_from_chef/chef/mixin/properties.rb
98
102
  - files/lib/chef_compat/copied_from_chef/chef/property.rb
99
103
  - files/lib/chef_compat/copied_from_chef/chef/provider.rb
100
- - files/lib/chef_compat/copied_from_chef/chef/resource/action_class.rb
101
104
  - files/lib/chef_compat/copied_from_chef/chef/resource.rb
102
- - files/lib/chef_compat/copied_from_chef.rb
105
+ - files/lib/chef_compat/copied_from_chef/chef/resource/action_class.rb
106
+ - files/lib/chef_compat/copied_from_chef/chef/resource_builder.rb
103
107
  - files/lib/chef_compat/delegating_class.rb
104
108
  - files/lib/chef_compat/mixin/properties.rb
109
+ - files/lib/chef_compat/monkeypatches.rb
110
+ - files/lib/chef_compat/monkeypatches/chef.rb
105
111
  - files/lib/chef_compat/monkeypatches/chef/exceptions.rb
106
112
  - files/lib/chef_compat/monkeypatches/chef/provider.rb
107
- - files/lib/chef_compat/monkeypatches/chef/resource/lwrp_base.rb
113
+ - files/lib/chef_compat/monkeypatches/chef/recipe.rb
108
114
  - files/lib/chef_compat/monkeypatches/chef/resource.rb
109
- - files/lib/chef_compat/monkeypatches/chef.rb
110
- - files/lib/chef_compat/monkeypatches.rb
115
+ - files/lib/chef_compat/monkeypatches/chef/resource/lwrp_base.rb
116
+ - files/lib/chef_compat/monkeypatches/chef/run_context.rb
111
117
  - files/lib/chef_compat/property.rb
112
- - files/lib/chef_compat/resource/lwrp_base.rb
118
+ - files/lib/chef_compat/recipe.rb
113
119
  - files/lib/chef_compat/resource.rb
120
+ - files/lib/chef_compat/resource/lwrp_base.rb
114
121
  - files/lib/chef_compat/version.rb
115
- - files/lib/chef_compat.rb
116
122
  homepage: http://chef.io
117
123
  licenses:
118
124
  - Apache 2.0
@@ -123,17 +129,17 @@ require_paths:
123
129
  - files/lib
124
130
  required_ruby_version: !ruby/object:Gem::Requirement
125
131
  requirements:
126
- - - '>='
132
+ - - ">="
127
133
  - !ruby/object:Gem::Version
128
134
  version: '0'
129
135
  required_rubygems_version: !ruby/object:Gem::Requirement
130
136
  requirements:
131
- - - '>='
137
+ - - ">="
132
138
  - !ruby/object:Gem::Version
133
139
  version: '0'
134
140
  requirements: []
135
141
  rubyforge_project:
136
- rubygems_version: 2.0.14
142
+ rubygems_version: 2.5.0
137
143
  signing_key:
138
144
  specification_version: 4
139
145
  summary: Bring some new features of Chef 12.5 to previous 12.X releases