chef-sugar-ng 4.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.github/lock.yml +3 -0
  3. data/.github/reaction.yml +1 -0
  4. data/.gitignore +26 -0
  5. data/.kitchen.yml +16 -0
  6. data/.travis.yml +17 -0
  7. data/CHANGELOG.md +261 -0
  8. data/CONTRIBUTING.md +20 -0
  9. data/Gemfile +2 -0
  10. data/LICENSE +201 -0
  11. data/README.md +540 -0
  12. data/Rakefile +11 -0
  13. data/chef-sugar-ng.gemspec +33 -0
  14. data/lib/chef/sugar.rb +51 -0
  15. data/lib/chef/sugar/architecture.rb +171 -0
  16. data/lib/chef/sugar/cloud.rb +192 -0
  17. data/lib/chef/sugar/constraints.rb +108 -0
  18. data/lib/chef/sugar/constraints_dsl.rb +83 -0
  19. data/lib/chef/sugar/core_extensions.rb +19 -0
  20. data/lib/chef/sugar/core_extensions/array.rb +34 -0
  21. data/lib/chef/sugar/core_extensions/object.rb +27 -0
  22. data/lib/chef/sugar/core_extensions/string.rb +66 -0
  23. data/lib/chef/sugar/data_bag.rb +146 -0
  24. data/lib/chef/sugar/docker.rb +40 -0
  25. data/lib/chef/sugar/filters.rb +227 -0
  26. data/lib/chef/sugar/init.rb +62 -0
  27. data/lib/chef/sugar/ip.rb +48 -0
  28. data/lib/chef/sugar/kernel.rb +49 -0
  29. data/lib/chef/sugar/kitchen.rb +40 -0
  30. data/lib/chef/sugar/node.rb +213 -0
  31. data/lib/chef/sugar/platform.rb +327 -0
  32. data/lib/chef/sugar/platform_family.rb +179 -0
  33. data/lib/chef/sugar/ruby.rb +51 -0
  34. data/lib/chef/sugar/run_context.rb +41 -0
  35. data/lib/chef/sugar/shell.rb +147 -0
  36. data/lib/chef/sugar/vagrant.rb +77 -0
  37. data/lib/chef/sugar/version.rb +21 -0
  38. data/lib/chef/sugar/virtualization.rb +151 -0
  39. data/libraries/chef-sugar.rb +1 -0
  40. data/metadata.rb +24 -0
  41. data/recipes/default.rb +20 -0
  42. data/spec/spec_helper.rb +25 -0
  43. data/spec/support/shared_examples.rb +20 -0
  44. data/spec/unit/chef/sugar/architecture_spec.rb +129 -0
  45. data/spec/unit/chef/sugar/cloud_spec.rb +149 -0
  46. data/spec/unit/chef/sugar/constraints_spec.rb +45 -0
  47. data/spec/unit/chef/sugar/core_extensions/array_spec.rb +10 -0
  48. data/spec/unit/chef/sugar/core_extensions/object_spec.rb +62 -0
  49. data/spec/unit/chef/sugar/core_extensions/string_spec.rb +48 -0
  50. data/spec/unit/chef/sugar/data_bag_spec.rb +118 -0
  51. data/spec/unit/chef/sugar/docker_spec.rb +39 -0
  52. data/spec/unit/chef/sugar/init_spec.rb +74 -0
  53. data/spec/unit/chef/sugar/ip_spec.rb +53 -0
  54. data/spec/unit/chef/sugar/kernel_spec.rb +16 -0
  55. data/spec/unit/chef/sugar/kitchen_spec.rb +18 -0
  56. data/spec/unit/chef/sugar/node_spec.rb +172 -0
  57. data/spec/unit/chef/sugar/platform_family_spec.rb +166 -0
  58. data/spec/unit/chef/sugar/platform_spec.rb +342 -0
  59. data/spec/unit/chef/sugar/ruby_spec.rb +39 -0
  60. data/spec/unit/chef/sugar/run_context_spec.rb +19 -0
  61. data/spec/unit/chef/sugar/shell_spec.rb +104 -0
  62. data/spec/unit/chef/sugar/vagrant_spec.rb +37 -0
  63. data/spec/unit/chef/sugar/virtualization_spec.rb +135 -0
  64. data/spec/unit/recipes/default_spec.rb +9 -0
  65. metadata +202 -0
@@ -0,0 +1,40 @@
1
+ #
2
+ # Copyright 2013-2015, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Docker
20
+ extend self
21
+
22
+ #
23
+ # Returns true if the current node is a docker container.
24
+ #
25
+ # @param [Chef::Node] node
26
+ # the node to check
27
+ #
28
+ # @return [Boolean]
29
+ #
30
+ def docker?(node)
31
+ File.exist?('/.dockerinit') || File.exist?('/.dockerenv')
32
+ end
33
+ end
34
+
35
+ module DSL
36
+ # @see Chef::Sugar::Docker#docker?
37
+ def docker?; Chef::Sugar::Docker.docker?(node); end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,227 @@
1
+ class Chef
2
+ module Sugar
3
+ module Filters
4
+ #
5
+ # Evaluate resources at compile time instead of converge time.
6
+ #
7
+ class AtCompileTime
8
+ def initialize(recipe)
9
+ @recipe = recipe
10
+ end
11
+
12
+ def evaluate(&block)
13
+ instance_eval(&block)
14
+ end
15
+
16
+ def method_missing(m, *args, &block)
17
+ resource = @recipe.send(m, *args, &block)
18
+
19
+ if resource.is_a?(Chef::Resource)
20
+ actions = Array(resource.action)
21
+ resource.action(:nothing)
22
+
23
+ actions.each do |action|
24
+ resource.run_action(action)
25
+ end
26
+ end
27
+
28
+ resource
29
+ end
30
+ end
31
+
32
+ #
33
+ # A top-level class for manipulation the resource collection.
34
+ #
35
+ class Injector
36
+ def initialize(recipe, identifier, placement)
37
+ @recipe = recipe
38
+ @resource_collection = @recipe.run_context.resource_collection
39
+ @resource = @resource_collection.lookup(identifier)
40
+ @placement = placement
41
+ end
42
+
43
+ def evaluate(&block)
44
+ instance_eval(&block)
45
+ end
46
+
47
+ def insert_before(resource, new_resource)
48
+ @resource_collection.instance_eval do
49
+ # Remove the resource because it's automatically created
50
+ @resources.delete_at(@resources_by_name[new_resource.to_s])
51
+ @resources_by_name.delete(new_resource.to_s)
52
+
53
+ index = @resources_by_name[resource.to_s]
54
+ @resources.insert(index, new_resource)
55
+ @resources_by_name[new_resource.to_s] = index
56
+ end
57
+ end
58
+
59
+ def insert_after(resource, new_resource)
60
+ @resource_collection.instance_eval do
61
+ # Remove the resource because it's automatically created
62
+ @resources.delete_at(@resources_by_name[new_resource.to_s])
63
+ @resources_by_name.delete(new_resource.to_s)
64
+
65
+ index = @resources_by_name[resource.to_s] + 2
66
+ @resources.insert(index, new_resource)
67
+ @resources_by_name[new_resource.to_s] = index
68
+ end
69
+ end
70
+
71
+ def method_missing(m, *args, &block)
72
+ new_resource = @recipe.send(m, *args, &block)
73
+
74
+ case @placement
75
+ when :before
76
+ insert_before(@resource, new_resource)
77
+ when :after
78
+ insert_after(@resource, new_resource)
79
+ else
80
+ super
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ module DSL
87
+ #
88
+ # Dynamically run resources specified in the block during the compilation
89
+ # phase, instead of the convergence phase.
90
+ #
91
+ # @example The old way
92
+ # package('apache2') do
93
+ # action :nothing
94
+ # end.run_action(:install)
95
+ #
96
+ # @example The new way
97
+ # at_compile_time do
98
+ # package('apache2')
99
+ # end
100
+ #
101
+ # @example Resource actions are run in order
102
+ # at_compile_time do
103
+ # service 'apache2' do
104
+ # action [:enable, :start] # run_action(:enable), run_action(:start)
105
+ # end
106
+ # end
107
+ #
108
+ def at_compile_time(&block)
109
+ Chef::Sugar::Filters::AtCompileTime.new(self).evaluate(&block)
110
+ end
111
+
112
+ #
113
+ # Dynamically insert resources before an existing resource in the
114
+ # resource_collection.
115
+ #
116
+ # @example Write a custom template before the apache2 service actions
117
+ # are run
118
+ # before 'service[apache2]' do
119
+ # template '/etc/apache2/thing.conf' do
120
+ # source '...'
121
+ # end
122
+ # end
123
+ #
124
+ #
125
+ # @param [String] identifier
126
+ # the +resource[name]+ identifier string
127
+ #
128
+ def before(identifier, &block)
129
+ Chef::Sugar::Filters::Injector.new(self, identifier, :before).evaluate(&block)
130
+ end
131
+
132
+ #
133
+ # Dynamically insert resources after an existing resource in the
134
+ # resource_collection.
135
+ #
136
+ # @example Write a custom template after the apache2 service actions
137
+ # are run
138
+ # after 'service[apache2]' do
139
+ # template '/etc/apache2/thing.conf' do
140
+ # source '...'
141
+ # end
142
+ # end
143
+ #
144
+ #
145
+ # @param [String] identifier
146
+ # the +resource[name]+ identifier string
147
+ #
148
+ def after(identifier, &block)
149
+ Chef::Sugar::Filters::Injector.new(self, identifier, :after).evaluate(&block)
150
+ end
151
+ end
152
+
153
+ module RecipeDSL
154
+ #
155
+ # @deprecated The description is in the method body pretty accurately...
156
+ #
157
+ def compile_time(&block)
158
+ message = <<-EOH
159
+
160
+ The Chef Sugar recipe DSL method `compile_time' has been renamed to
161
+ `at_compile_time'! This is a breaking change that was released in a patch
162
+ version, so please continue reading to understand the necessary semantic
163
+ versioning violation.
164
+
165
+ Chef Software implemented a version of `compile_time' in Chef 12.1, breaking any
166
+ cookbook that uses or depends on Chef Sugar on Chef 12.1:
167
+
168
+ https://www.chef.io/blog/2015/03/03/chef-12-1-0-released
169
+
170
+ In order to progress Chef Sugar forward, the DSL method has been renamed to
171
+ avoid the namespace collision.
172
+
173
+ In short, you should change this:
174
+
175
+ compile_time do
176
+ # ...
177
+ end
178
+
179
+ to this:
180
+
181
+ at_compile_time do
182
+ # ...
183
+ end
184
+
185
+ EOH
186
+
187
+ if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time)
188
+ message << <<-EOH
189
+ You are running a version of Chef Client that includes the `compile_time'
190
+ attribute on core Chef resources (most likely Chef 12.1+). Instead of continuing
191
+ and having Chef provide you with an obscure error message, I am going to error
192
+ here. There is no way for the Chef Recipe to successfully continue unless you
193
+ change the Chef Sugar `compile_time' method to `at_compile_time' in your Chef
194
+ recipes.
195
+
196
+ You should NOT change resource-level `compile_time' attributes:
197
+
198
+ package "foo" do
199
+ compile_time true # Do NOT change these
200
+ end
201
+
202
+ I truly apologize for the inconvienence and hope the reason for introducing this
203
+ breaking change is clear. I am sorry if it causes extra work, but I promise this
204
+ error message is much more informative than "wrong number of arguments".
205
+
206
+ If you have any questions, please feel free to open an issue on GitHub at:
207
+
208
+ https://github.com/sethvargo/chef-sugar
209
+
210
+ Thank you, and have a great day!
211
+ EOH
212
+ raise RuntimeError, message
213
+ else
214
+ message << <<-EOH
215
+ You are running a version of Chef Client that does not include the
216
+ `compile_time' attribute on core Chef resources (most likely less than
217
+ Chef 12.1), so this is just a warning. Please consider changing the Chef Sugar
218
+ `compile_time' method to `at_compile_time' in your Chef recipes. If you upgrade
219
+ Chef, your recipes WILL break.
220
+ EOH
221
+ Chef::Log.warn(message)
222
+ at_compile_time(&block)
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,62 @@
1
+ #
2
+ # Copyright 2015, Nathan Williams <nath.e.will@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Init
20
+ extend self
21
+
22
+ #
23
+ # Determine if the current init system is systemd.
24
+ #
25
+ # @return [Boolean]
26
+ #
27
+ def systemd?(node)
28
+ file = '/proc/1/comm'
29
+ File.exist?(file) && IO.read(file).chomp == 'systemd'
30
+ end
31
+
32
+ #
33
+ # Determine if the current init system is upstart.
34
+ #
35
+ # @return [Boolean]
36
+ #
37
+ def upstart?(node)
38
+ File.executable?('/sbin/initctl')
39
+ end
40
+
41
+ #
42
+ # Determine if the current init system is runit.
43
+ #
44
+ # @return [Boolean]
45
+ #
46
+ def runit?(node)
47
+ File.executable?('/sbin/runit-init')
48
+ end
49
+ end
50
+
51
+ module DSL
52
+ # @see Chef::Sugar::Init#systemd?
53
+ def systemd?; Chef::Sugar::Init.systemd?(node); end
54
+
55
+ # @see Chef::Sugar::Init#upstart?
56
+ def upstart?; Chef::Sugar::Init.upstart?(node); end
57
+
58
+ # @see Chef::Sugar::Init#runit?
59
+ def runit?; Chef::Sugar::Init.runit?(node); end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,48 @@
1
+ #
2
+ # Copyright 2013-2015, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module IP
20
+ extend self
21
+
22
+ #
23
+ # The best IP address for the given node, in the context of
24
+ # the current node. Useful for choosing a local IP address
25
+ # over a public one to limit bandwidth on cloud providers.
26
+ #
27
+ # @param [Chef::Node] other
28
+ # the node to calculate the best IP address for
29
+ #
30
+ def best_ip_for(node, other)
31
+ if other['cloud']
32
+ if node['cloud'] && other['cloud']['provider'] == node['cloud']['provider']
33
+ other['cloud']['local_ipv4']
34
+ else
35
+ other['cloud']['public_ipv4']
36
+ end
37
+ else
38
+ other['ipaddress']
39
+ end
40
+ end
41
+ end
42
+
43
+ module DSL
44
+ # @see Chef::Sugar::IP#best_ip_for
45
+ def best_ip_for(other); Chef::Sugar::IP.best_ip_for(node, other); end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ #
2
+ # Copyright 2013-2015, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Kernel
20
+ class ChefGemLoadError < StandardError
21
+ def initialize(name)
22
+ super <<-EOH
23
+ Chef could not load the gem `#{name}'! You may need to install the gem manually
24
+ with `gem install #{name}', or include a recipe before you can use this
25
+ resource. Please consult the documentation for this cookbook for proper usage.
26
+ EOH
27
+ end
28
+ end
29
+
30
+ #
31
+ # Require a gem that should have been installed by Chef, such as in a
32
+ # recipes as a +chef_gem+. This method will gracefully degrade if the
33
+ # gem cannot be loaded.
34
+ #
35
+ # @param [String] name
36
+ # the name of the gem to install
37
+ #
38
+ # @return [Boolean]
39
+ # true if the require is successful and false if the gem is already
40
+ # loaded
41
+ #
42
+ def require_chef_gem(name)
43
+ require(name)
44
+ rescue LoadError
45
+ raise ChefGemLoadError.new(name)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,40 @@
1
+ #
2
+ # Copyright 2013-2015, Seth Vargo <sethvargo@gmail.com>
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ class Chef
18
+ module Sugar
19
+ module Kitchen
20
+ extend self
21
+
22
+ #
23
+ # Returns true if the current node is provisioned by Test Kitchen.
24
+ #
25
+ # @param [Chef::Node] node
26
+ # the node to check
27
+ #
28
+ # @return [Boolean]
29
+ #
30
+ def kitchen?(node)
31
+ !ENV['TEST_KITCHEN'].nil?
32
+ end
33
+ end
34
+
35
+ module DSL
36
+ # @see Chef::Sugar::Kitchen#kitchen?
37
+ def kitchen?; Chef::Sugar::Kitchen.kitchen?(node); end
38
+ end
39
+ end
40
+ end