chef-cookie_cutter 0.2.0 → 1.0.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 +4 -4
- data/lib/chef/cookie_cutter/extended_provides/monkey_patches.rb +39 -0
- data/lib/chef/cookie_cutter/extended_provides.rb +5 -22
- data/lib/chef/cookie_cutter/fancy_property/cookbook_doc.rb +48 -0
- data/lib/chef/cookie_cutter/fancy_property/property.rb +150 -0
- data/lib/chef/cookie_cutter/fancy_property.rb +4 -162
- data/lib/chef/cookie_cutter/include_properties/dsl.rb +40 -0
- data/lib/chef/cookie_cutter/include_properties/errors.rb +41 -0
- data/lib/chef/cookie_cutter/include_properties.rb +33 -0
- data/lib/chef/cookie_cutter/{lwrp_include.rb → include_resource/cookbook_doc.rb} +5 -62
- data/lib/chef/cookie_cutter/include_resource/dsl.rb +56 -0
- data/lib/chef/cookie_cutter/include_resource/fake_resource.rb +40 -0
- data/lib/chef/cookie_cutter/include_resource/monkey_patches.rb +42 -0
- data/lib/chef/cookie_cutter/include_resource.rb +39 -0
- data/lib/chef/cookie_cutter/{fake_resource.rb → namespace/dsl.rb} +8 -15
- data/lib/chef/cookie_cutter/namespace/monkey_patches.rb +67 -0
- data/lib/chef/cookie_cutter/namespace/namespace.rb +46 -0
- data/lib/chef/cookie_cutter/namespace.rb +12 -82
- data/lib/chef/cookie_cutter/run_state/dsl.rb +50 -0
- data/lib/chef/cookie_cutter/run_state/errors.rb +34 -0
- data/lib/chef/cookie_cutter/run_state.rb +8 -51
- data/lib/chef/cookie_cutter/spec_matchers/monkey_patches.rb +48 -0
- data/lib/chef/cookie_cutter/spec_matchers.rb +3 -30
- data/lib/chef/cookie_cutter/version.rb +22 -0
- data/lib/chef/cookie_cutter.rb +3 -18
- metadata +20 -6
- data/lib/chef/cookie_cutter/lwrp_build_params.rb +0 -71
- data/lib/chef/cookie_cutter/shared_blocks.rb +0 -64
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
require 'chef/cookbook/file_vendor'
|
18
|
+
|
19
|
+
class Chef
|
20
|
+
module CookieCutter
|
21
|
+
module IncludeResource
|
22
|
+
module_function
|
23
|
+
|
24
|
+
def try_file(filename)
|
25
|
+
return if File.exist?(filename) && File.readable?(filename)
|
26
|
+
fail IOError, "Cannot open or read #{filename}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def filename_for_record(run_context, cookbook_name, segment, name)
|
30
|
+
name += '.rb' unless name.end_with?('.rb')
|
31
|
+
cookbook_version = run_context.cookbook_collection[cookbook_name]
|
32
|
+
file_vendor = ::Chef::Cookbook::FileVendor.create_from_manifest(cookbook_version.manifest)
|
33
|
+
manifest_record = cookbook_version.preferred_manifest_record(run_context.node, segment.to_s, name)
|
34
|
+
file_vendor.get_filename(manifest_record[:path])
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_resource_module_from_file(filename)
|
38
|
+
try_file(filename)
|
39
|
+
resource_module = Module.new
|
40
|
+
resource_module.instance_variable_set('@filename', filename)
|
41
|
+
def resource_module.included(cls)
|
42
|
+
cls.class_eval(IO.read(@filename), @filename, 1)
|
43
|
+
end
|
44
|
+
resource_module
|
45
|
+
end
|
46
|
+
|
47
|
+
module DSL
|
48
|
+
def include_resource(name, cookbook: nil)
|
49
|
+
cookbook = resource_cookbook_name if cookbook.nil?
|
50
|
+
filename = IncludeResource.filename_for_record(run_context, cookbook, :resources, name)
|
51
|
+
include IncludeResource.build_resource_module_from_file(filename)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
class Chef
|
19
|
+
module CookieCutter
|
20
|
+
module IncludeResource
|
21
|
+
module FakeResource
|
22
|
+
class FakeClass
|
23
|
+
extend FakeResource
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_missing(*)
|
27
|
+
# do nothing
|
28
|
+
end
|
29
|
+
|
30
|
+
def respond_to?(*)
|
31
|
+
true
|
32
|
+
end
|
33
|
+
|
34
|
+
def const_missing(*)
|
35
|
+
FakeClass
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
class Chef
|
19
|
+
module CookieCutter
|
20
|
+
module IncludeResource
|
21
|
+
module MonkeyPatches
|
22
|
+
# Monkey Patches for Chef::Resource::LWRPBase
|
23
|
+
# Makes the parameters of build_from_file (i.e. cookbook_name, filename
|
24
|
+
# and run_context) available in the created class.
|
25
|
+
module CustomResource
|
26
|
+
module ClassMethods
|
27
|
+
def build_from_file(cookbook_name, filename, run_context)
|
28
|
+
define_singleton_method(:resource_cookbook_name) { cookbook_name }
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.prepended(base)
|
34
|
+
class << base
|
35
|
+
prepend ClassMethods
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
require 'chef/resource/lwrp_base'
|
18
|
+
|
19
|
+
class Chef
|
20
|
+
module CookieCutter
|
21
|
+
module IncludeResource
|
22
|
+
require_relative 'include_resource/dsl'
|
23
|
+
require_relative 'include_resource/monkey_patches'
|
24
|
+
require_relative 'include_resource/cookbook_doc'
|
25
|
+
require_relative 'include_resource/fake_resource'
|
26
|
+
|
27
|
+
Chef::Resource::LWRPBase.send :extend, DSL
|
28
|
+
Chef::Resource::LWRPBase.send :prepend, MonkeyPatches::CustomResource
|
29
|
+
if defined?(DocumentingLWRPBase)
|
30
|
+
DocumentingLWRPBase.send :extend, CookbookDocDSL
|
31
|
+
DocumentingLWRPBase.send :extend, FakeResource
|
32
|
+
end
|
33
|
+
if defined?(KnifeCookbookDoc)
|
34
|
+
KnifeCookbookDoc::ReadmeModel.send :prepend, MonkeyPatches::DocumentReadmeModel
|
35
|
+
KnifeCookbookDoc::ResourceModel.send :prepend, MonkeyPatches::DocumentResourceModel
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -14,24 +14,17 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
#
|
17
|
+
require_relative 'namespace'
|
17
18
|
|
18
19
|
class Chef
|
19
20
|
module CookieCutter
|
20
|
-
module
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
def respond_to?(*)
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
def const_missing(*)
|
34
|
-
FakeClass
|
21
|
+
module Namespace
|
22
|
+
module DSL
|
23
|
+
def namespace(*args)
|
24
|
+
keys = args.map(&:to_s)
|
25
|
+
attribute = Namespace.deep_fetch(node.attributes, keys)
|
26
|
+
yield attribute if block_given?
|
27
|
+
end
|
35
28
|
end
|
36
29
|
end
|
37
30
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
require_relative 'namespace'
|
18
|
+
|
19
|
+
class Chef
|
20
|
+
module CookieCutter
|
21
|
+
module Namespace
|
22
|
+
module MonkeyPatches
|
23
|
+
module Node
|
24
|
+
def namespace(*args, **kwargs, &blk)
|
25
|
+
@namespace_options = namespace_options.merge(kwargs)
|
26
|
+
keys = args.map(&:to_s)
|
27
|
+
@current_namespace = current_namespace + keys
|
28
|
+
instance_eval(&blk) if block_given?
|
29
|
+
@current_namespace = current_namespace - keys
|
30
|
+
@namespace_options = nil if @current_namespace.empty?
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(method_name, *args)
|
35
|
+
super
|
36
|
+
rescue NoMethodError
|
37
|
+
if args.empty?
|
38
|
+
deep_key = current_namespace.dup << method_name.to_s
|
39
|
+
return Namespace.deep_fetch!(attributes, deep_key)
|
40
|
+
else
|
41
|
+
vivified[method_name.to_s] = args.size == 1 ? args.first : args
|
42
|
+
return nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def namespace_options
|
49
|
+
@namespace_options ||= { precedence: default }
|
50
|
+
end
|
51
|
+
|
52
|
+
def current_namespace
|
53
|
+
@current_namespace ||= []
|
54
|
+
end
|
55
|
+
|
56
|
+
def vivified
|
57
|
+
precedence = namespace_options[:precedence]
|
58
|
+
current_namespace.inject(precedence) do |hash, item|
|
59
|
+
hash[item] ||= {}
|
60
|
+
hash[item]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
class Chef
|
19
|
+
module CookieCutter
|
20
|
+
module Namespace
|
21
|
+
module_function
|
22
|
+
|
23
|
+
class AttributeDoesNotExistError < StandardError
|
24
|
+
def initialize(keys, key)
|
25
|
+
super <<-EOH
|
26
|
+
No attribute `node#{keys.map { |k| "['#{k}']" }.join}' exists on the current
|
27
|
+
node. Specifically the `#{key}' attribute is not defined. Please make sure you
|
28
|
+
have spelled everything
|
29
|
+
correctly.
|
30
|
+
EOH
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def deep_fetch(attributes, keys)
|
35
|
+
keys.map!(&:to_s)
|
36
|
+
keys.inject(attributes.to_hash) do |hash, key|
|
37
|
+
if hash.key?(key)
|
38
|
+
hash[key]
|
39
|
+
else
|
40
|
+
fail AttributeDoesNotExistError.new(keys, key)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -14,92 +14,22 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
#
|
17
|
+
require 'chef/recipe'
|
18
|
+
require 'chef/resource'
|
19
|
+
require 'chef/provider'
|
20
|
+
require 'chef/node'
|
17
21
|
|
18
22
|
class Chef
|
19
23
|
module CookieCutter
|
20
24
|
module Namespace
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
class AttributeDoesNotExistError < StandardError
|
31
|
-
def initialize(keys, key)
|
32
|
-
super <<-EOH
|
33
|
-
No attribute `node#{keys.map { |k| "['#{k}']" }.join}' exists on the current
|
34
|
-
node. Specifically the `#{key}' attribute is not defined. Please make sure you
|
35
|
-
have spelled everything
|
36
|
-
correctly.
|
37
|
-
EOH
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def deep_fetch(attributes, keys)
|
42
|
-
keys.map!(&:to_s)
|
43
|
-
keys.inject(attributes.to_hash) do |hash, key|
|
44
|
-
if hash.key?(key)
|
45
|
-
hash[key]
|
46
|
-
else
|
47
|
-
fail AttributeDoesNotExistError.new(keys, key)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
module DSL
|
53
|
-
def namespace(*args)
|
54
|
-
keys = args.map(&:to_s)
|
55
|
-
attribute = Namespace.deep_fetch(node.attributes, keys)
|
56
|
-
yield attribute if block_given?
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
module MonkeyPatches
|
61
|
-
module Node
|
62
|
-
def namespace(*args, **kwargs, &blk)
|
63
|
-
@namespace_options = namespace_options.merge(kwargs)
|
64
|
-
keys = args.map(&:to_s)
|
65
|
-
@current_namespace = current_namespace + keys
|
66
|
-
instance_eval(&blk) if block_given?
|
67
|
-
@current_namespace = current_namespace - keys
|
68
|
-
@namespace_options = nil if @current_namespace.empty?
|
69
|
-
nil
|
70
|
-
end
|
71
|
-
|
72
|
-
def method_missing(method_name, *args)
|
73
|
-
super
|
74
|
-
rescue NoMethodError
|
75
|
-
if args.empty?
|
76
|
-
deep_key = current_namespace.dup << method_name.to_s
|
77
|
-
return Namespace.deep_fetch!(attributes, deep_key)
|
78
|
-
else
|
79
|
-
vivified[method_name.to_s] = args.size == 1 ? args.first : args
|
80
|
-
return nil
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
private
|
85
|
-
|
86
|
-
def namespace_options
|
87
|
-
@namespace_options ||= { precedence: default }
|
88
|
-
end
|
89
|
-
|
90
|
-
def current_namespace
|
91
|
-
@current_namespace ||= []
|
92
|
-
end
|
93
|
-
|
94
|
-
def vivified
|
95
|
-
precedence = namespace_options[:precedence]
|
96
|
-
current_namespace.inject(precedence) do |hash, item|
|
97
|
-
hash[item] ||= {}
|
98
|
-
hash[item]
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
25
|
+
require_relative 'namespace/dsl'
|
26
|
+
require_relative 'namespace/monkey_patches'
|
27
|
+
require_relative 'namespace/namespace'
|
28
|
+
|
29
|
+
::Chef::Recipe.send :include, DSL
|
30
|
+
::Chef::Resource.send :include, DSL
|
31
|
+
::Chef::Provider.send :include, DSL
|
32
|
+
::Chef::Node.send :prepend, MonkeyPatches::Node
|
103
33
|
end
|
104
34
|
end
|
105
35
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
require_relative 'errors'
|
18
|
+
|
19
|
+
class Chef
|
20
|
+
module CookieCutter
|
21
|
+
module RunState
|
22
|
+
module DSL
|
23
|
+
def store_state(*subkeys, key, value)
|
24
|
+
subkeys.map!(&:to_s)
|
25
|
+
hash = node.run_state
|
26
|
+
subkeys.each do |k|
|
27
|
+
hash[k] = {} if hash[k].nil?
|
28
|
+
hash = hash[k]
|
29
|
+
end
|
30
|
+
hash[key.to_s] = value
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_state(*keys)
|
34
|
+
keys.map!(&:to_s)
|
35
|
+
keys.inject(node.run_state) do |hash, key|
|
36
|
+
fail Errors::RunStateDoesNotExistError.new(keys, key) unless hash.key? key
|
37
|
+
hash[key]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def exist_state?(*keys)
|
42
|
+
fetch_state(*keys)
|
43
|
+
true
|
44
|
+
rescue Errors::RunStateDoesNotExistError
|
45
|
+
false
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
class Chef
|
19
|
+
module CookieCutter
|
20
|
+
module RunState
|
21
|
+
module Errors
|
22
|
+
class RunStateDoesNotExistError < StandardError
|
23
|
+
def initialize(keys, key)
|
24
|
+
hash = keys.map { |k| "['#{k}']" }
|
25
|
+
super <<-EOH
|
26
|
+
The run_state does not contain an element at run_state#{hash.join}.
|
27
|
+
Specifically, #{key} is not defined.
|
28
|
+
EOH
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -14,62 +14,19 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
#
|
17
|
+
require 'chef/recipe'
|
18
|
+
require 'chef/resource'
|
19
|
+
require 'chef/provider'
|
17
20
|
|
18
21
|
class Chef
|
19
22
|
module CookieCutter
|
20
23
|
module RunState
|
21
|
-
|
24
|
+
require_relative 'run_state/dsl'
|
25
|
+
require_relative 'run_state/errors'
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
Chef::Provider.send :include, DSL
|
27
|
-
end
|
28
|
-
|
29
|
-
class RunStateDoesNotExistError < StandardError
|
30
|
-
def initialize(keys, key)
|
31
|
-
hash = keys.map { |k| "['#{k}']" }
|
32
|
-
super <<-EOH
|
33
|
-
The run_state does not contain an element at run_state#{hash.join}.
|
34
|
-
Specifically, #{key} is not defined.
|
35
|
-
EOH
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def store_state(node, *subkeys, key, value)
|
40
|
-
subkeys.map!(&:to_s)
|
41
|
-
hash = node.run_state
|
42
|
-
subkeys.each do |k|
|
43
|
-
hash[k] = {} if hash[k].nil?
|
44
|
-
hash = hash[k]
|
45
|
-
end
|
46
|
-
hash[key.to_s] = value
|
47
|
-
end
|
48
|
-
|
49
|
-
def fetch_state(node, *keys)
|
50
|
-
keys.map!(&:to_s)
|
51
|
-
keys.inject(node.run_state) do |hash, key|
|
52
|
-
fail RunState::RunStateDoesNotExistError.new(keys, key) unless hash.key? key
|
53
|
-
hash[key]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
module DSL
|
58
|
-
def store_state(*subkeys, key, value)
|
59
|
-
RunState.store_state(node, *subkeys, key, value)
|
60
|
-
end
|
61
|
-
|
62
|
-
def fetch_state(*keys)
|
63
|
-
RunState.fetch_state(node, *keys)
|
64
|
-
end
|
65
|
-
|
66
|
-
def exist_state?(*keys)
|
67
|
-
RunState.fetch_state(node, *keys)
|
68
|
-
true
|
69
|
-
rescue RunState::RunStateDoesNotExistError
|
70
|
-
false
|
71
|
-
end
|
72
|
-
end
|
27
|
+
::Chef::Recipe.send :include, DSL
|
28
|
+
::Chef::Resource.send :include, DSL
|
29
|
+
::Chef::Provider.send :include, DSL
|
73
30
|
end
|
74
31
|
end
|
75
32
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015, Ole Claussen <claussen.ole@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
class Chef
|
19
|
+
module CookieCutter
|
20
|
+
module SpecMatchers
|
21
|
+
module MonkeyPatches
|
22
|
+
# Monkey Patches for Chef::Resource::LWRPBase
|
23
|
+
# Automatically registers chef spec matchers after building the resource
|
24
|
+
module CustomResource
|
25
|
+
module ClassMethods
|
26
|
+
def build_from_file(cookbook_name, filename, run_context)
|
27
|
+
resource = super
|
28
|
+
if defined?(ChefSpec) && !resource.is_a?(TrueClass)
|
29
|
+
resource.actions.each do |action|
|
30
|
+
Object.send :define_method, "#{action}_#{resource.resource_name}" do |msg|
|
31
|
+
::ChefSpec::Matchers::ResourceMatcher.new(resource.resource_name, action, msg)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
resource
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.prepended(base)
|
40
|
+
class << base
|
41
|
+
prepend ClassMethods
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -14,41 +14,14 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
#
|
17
|
+
require 'chef/resource'
|
17
18
|
|
18
19
|
class Chef
|
19
20
|
module CookieCutter
|
20
21
|
module SpecMatchers
|
21
|
-
|
22
|
+
require_relative 'spec_matchers/monkey_patches'
|
22
23
|
|
23
|
-
|
24
|
-
Chef::Resource::LWRPBase.send :prepend, MonkeyPatches::LWRPResource
|
25
|
-
end
|
26
|
-
|
27
|
-
module MonkeyPatches
|
28
|
-
# Monkey Patches for Chef::Resource::LWRPBase
|
29
|
-
# Automatically registers chef spec matchers after building the resource
|
30
|
-
module LWRPResource
|
31
|
-
module ClassMethods
|
32
|
-
def build_from_file(cookbook_name, filename, run_context)
|
33
|
-
resource = super
|
34
|
-
if defined?(ChefSpec) && !resource.is_a?(TrueClass)
|
35
|
-
resource.actions.each do |action|
|
36
|
-
Object.send :define_method, "#{action}_#{resource.resource_name}" do |msg|
|
37
|
-
::ChefSpec::Matchers::ResourceMatcher.new(resource.resource_name, action, msg)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
resource
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.prepended(base)
|
46
|
-
class << base
|
47
|
-
prepend ClassMethods
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
24
|
+
::Chef::Resource::LWRPBase.send :prepend, MonkeyPatches::CustomResource
|
52
25
|
end
|
53
26
|
end
|
54
27
|
end
|