chef-sugar 1.1.0 → 1.2.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/LICENSE +1 -1
  4. data/README.md +91 -1
  5. data/Rakefile +6 -0
  6. data/chef-extensions.gemspec +3 -6
  7. data/lib/chef/sugar.rb +5 -1
  8. data/lib/chef/sugar/architecture.rb +1 -1
  9. data/lib/chef/sugar/cloud.rb +1 -1
  10. data/lib/chef/sugar/constraints.rb +161 -0
  11. data/lib/chef/sugar/core_extensions.rb +18 -0
  12. data/lib/chef/sugar/core_extensions/array.rb +34 -0
  13. data/lib/chef/sugar/core_extensions/string.rb +49 -0
  14. data/lib/chef/sugar/data_bag.rb +34 -9
  15. data/lib/chef/sugar/ip.rb +1 -1
  16. data/lib/chef/sugar/kernel.rb +49 -0
  17. data/lib/chef/sugar/node.rb +157 -1
  18. data/lib/chef/sugar/platform.rb +1 -1
  19. data/lib/chef/sugar/platform_family.rb +1 -1
  20. data/lib/chef/sugar/ruby.rb +1 -1
  21. data/lib/chef/sugar/shell.rb +1 -1
  22. data/lib/chef/sugar/vagrant.rb +1 -1
  23. data/lib/chef/sugar/version.rb +2 -2
  24. data/metadata.rb +1 -1
  25. data/recipes/default.rb +2 -2
  26. data/spec/spec_helper.rb +3 -0
  27. data/spec/support/shared_examples.rb +1 -1
  28. data/spec/unit/chef/{extensions → sugar}/architecture_spec.rb +0 -0
  29. data/spec/unit/chef/{extensions → sugar}/cloud_spec.rb +0 -0
  30. data/spec/unit/chef/sugar/constraints_spec.rb +43 -0
  31. data/spec/unit/chef/sugar/core_extensions/array_spec.rb +10 -0
  32. data/spec/unit/chef/sugar/core_extensions/string_spec.rb +16 -0
  33. data/spec/unit/chef/{extensions → sugar}/data_bag_spec.rb +23 -4
  34. data/spec/unit/chef/{extensions → sugar}/ip_spec.rb +0 -0
  35. data/spec/unit/chef/sugar/kernel_spec.rb +16 -0
  36. data/spec/unit/chef/sugar/node_spec.rb +117 -0
  37. data/spec/unit/chef/{extensions → sugar}/platform_family_spec.rb +0 -0
  38. data/spec/unit/chef/{extensions → sugar}/platform_spec.rb +0 -0
  39. data/spec/unit/chef/{extensions → sugar}/ruby_spec.rb +0 -0
  40. data/spec/unit/chef/{extensions → sugar}/shell_spec.rb +0 -0
  41. data/spec/unit/chef/{extensions → sugar}/vagrant_spec.rb +0 -0
  42. metadata +45 -61
  43. data/.kitchen.yml +0 -16
  44. data/recipes/development.rb +0 -65
  45. data/spec/unit/chef/extensions/node_spec.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 222ec2b0f50c5d0a482190cc2b4baec1423f124d
4
- data.tar.gz: 09af24376426ba383f8b85c4e08c8ab6ad4ceef9
3
+ metadata.gz: 1f389cb9756b7c955888e0673f9b7cace261b3fc
4
+ data.tar.gz: b933fd4654ddcf8e27082a1ad0af50678f603ff6
5
5
  SHA512:
6
- metadata.gz: 3341414fb14b31ca32baf45395e084f1d095cb830569669b7048429c66d92bbfa6a10103da4273cd554496289c27958fa09f03db2c3da1e7a1cb37892f2f94d7
7
- data.tar.gz: 0eb0b352c56911e7c55d26f85259baf418e32c8731401ba37b6ce134b5561549ccdd2e3824a80b14018ad37b17a12bd6f322b4d89f232590cc01a9923625b1d8
6
+ metadata.gz: 9816746e63bc7714ea9c6b17f599b39685cbdc120abb74c7bd36c2fed268e0a221144192334de78f2ed049f2cf9b23939d6781a742cee66bdfefea8c4b4a233b
7
+ data.tar.gz: 434e4dd1d91d7a73d1ea4d08c25042e34b958184415561baea1df6f24b6218f0d32b99722507b04b7a70d340c4ad52a5bdf3ec2ddf10468803ef12d5c98f50b2
data/.gitignore CHANGED
@@ -16,7 +16,10 @@ Gemfile.lock
16
16
  .kitchen/
17
17
  .vagrant/
18
18
  .vagrant.d/
19
+ .yardoc/
19
20
  bin/
21
+ coverage/
22
+ doc/
20
23
  pkg/
21
24
  tmp/
22
25
  vendor/
data/LICENSE CHANGED
@@ -186,7 +186,7 @@ APPENDIX: How to apply the Apache License to your work.
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright 2013 Seth Vargo
189
+ Copyright 2013-2014 Seth Vargo
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  Chef::Sugar
2
2
  ================
3
3
  [![Build Status](https://secure.travis-ci.org/sethvargo/chef-sugar.png?branch=master)](http://travis-ci.org/sethvargo/chef-sugar)
4
+ [![Coverage
5
+ Status](https://coveralls.io/repos/sethvargo/chef-sugar/badge.png?branch=master)](https://coveralls.io/r/sethvargo/chef-sugar?branch=master)
4
6
  [![Gem Version](https://badge.fury.io/rb/chef-sugar.png)](http://badge.fury.io/rb/chef-sugar)
5
7
  [![Dependency Status](https://gemnasium.com/sethvargo/chef-sugar.png)](https://gemnasium.com/sethvargo/chef-sugar)
6
8
  [![Code Climate](https://codeclimate.com/github/sethvargo/chef-sugar.png)](https://codeclimate.com/github/sethvargo/chef-sugar)
@@ -41,6 +43,7 @@ Requiring the Chef Sugar Gem will automatically extend the Recipe DSL, `Chef::Re
41
43
  If you are working outside of the Recipe DSL, you can use the module methods instead of the Recipe DSL. In general, the module methods have the same name as their Recipe-DSL counterparts, but require the node object as a parameter. For example:
42
44
 
43
45
  In a Recipe:
46
+
44
47
  ```ruby
45
48
  # cookbook/recipes/default.rb
46
49
  do_something if windows?
@@ -119,6 +122,87 @@ encrypted_data_bag_item('accounts', 'hipchat')
119
122
  encrypted_data_bag_item_for_environment('accounts', 'github')
120
123
  ```
121
124
 
125
+ ### Attributes
126
+ Chef Sugar adds more Chef-like DSL to attribute definitions. Instead of using the Ruby hash syntax, you can define attributes using nested namespaces. This DSL may be more friendly to non-Ruby developers. It can safely be mixed-and-matched with the standard syntax.
127
+
128
+ ```ruby
129
+ # This is functionally the same as default['apache2']['config']['root'] = '/var/www'
130
+ namespace 'apache2' do
131
+ namespace 'config' do
132
+ root '/var/www'
133
+ end
134
+ end
135
+ ```
136
+
137
+ ```ruby
138
+ # Specify multiple keys instead of nesting namespaces
139
+ namespace 'apache2', 'config' do
140
+ root '/var/www'
141
+ end
142
+ ```
143
+
144
+ ```ruby
145
+ # Specify different nested precedence levels
146
+ namespace 'apache2', precedence: normal do
147
+ namespace 'config', precedence: override do
148
+ root '/var/www' #=> override['apache2']['config']['root'] = '/var/www'
149
+ end
150
+ end
151
+ ```
152
+
153
+ ### Constraints
154
+ - `constaints` - create a new contraint (or requirement) that can be used to test version validations.
155
+ - `chef_version` - (DSL only) a wrapper for `version(Chef::VERSION)`
156
+ - `version` - create a new version that can be used to test constraint validation.
157
+
158
+ #### Examples
159
+ ```ruby
160
+ # Check if a version is satisfied by a constraint
161
+ version('1.2.3').satisfies?('~> 1.2.0')
162
+ ```
163
+
164
+ ```ruby
165
+ # Check if a constraint is satisfied by a version
166
+ constraint('~> 1.2.0').satisfied_by?('1.2.3')
167
+ ```
168
+
169
+ ```ruby
170
+ # Support multiple constraints
171
+ version('1.2.3').satisfies?('> 1.2', '< 2.0')
172
+ constraint('> 1.2', '< 2.0').satisfied_by?('1.2.3')
173
+ ```
174
+
175
+ ```ruby
176
+ # Only perform an operation if Chef is at a certain version
177
+ package 'apache2' do
178
+ not_if { chef_version.satisfies?('~> 11.0') } # Ignore Chef 11
179
+ end
180
+ ```
181
+
182
+ ### Kernel
183
+ - `require_chef_gem` - "safely" require a gem. Loading a gem with Chef is sometimes difficult and confusing. The errors that Chef produces are also sometimes not very intuitive. In the event you require a gem to exist on the system, you can use `require_chef_gem`, which wil attempt to require the gem and then produce helpful output if the gem is not installed:
184
+
185
+ Chef could not load the gem `#{name}'! You may need to install the gem
186
+ manually with `gem install #{name}', or include a recipe before you can
187
+ use this resource. Please consult the documentation for this cookbook
188
+ for proper usage.
189
+
190
+ #### Examples
191
+ ```ruby
192
+ # LWRP
193
+ require_chef_gem 'pry'
194
+ ```
195
+
196
+ ```ruby
197
+ class Chef
198
+ class Provider
199
+ class MyProvider > Provider
200
+ require_chef_gem 'pry'
201
+ end
202
+ end
203
+ end
204
+ ```
205
+
122
206
  ### IP
123
207
  - `best_ip_for` - determine the best IP address for the given "other" node, preferring local IP addresses over public ones.
124
208
 
@@ -134,6 +218,8 @@ end
134
218
  ```
135
219
 
136
220
  ### Node
221
+ - `deep_fetch` - safely fetch a nested attribute.
222
+ - `deep_fetch` - fetch a nested attribute, raising a more semantic error if the key does not exist.
137
223
  - `in?` - determine if the node is in the given Chef environment.
138
224
  - `includes_recipe?`
139
225
 
@@ -154,6 +240,10 @@ if includes_recipe?('apache2::default')
154
240
  end
155
241
  ```
156
242
 
243
+ ```ruby
244
+ node.deep_fetch('apache2', 'config', 'root') => node['apache2']['config']['root']
245
+ ```
246
+
157
247
  ### Platform
158
248
  - `amazon_linux?`
159
249
  - `centos?`
@@ -287,7 +377,7 @@ License & Authors
287
377
  - Author: Seth Vargo (sethvargo@gmail.com)
288
378
 
289
379
  ```text
290
- Copyright 2013 Seth Vargo
380
+ Copyright 2013-2014 Seth Vargo
291
381
 
292
382
  Licensed under the Apache License, Version 2.0 (the "License");
293
383
  you may not use this file except in compliance with the License.
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ require 'stove/rake_task'
4
+ desc "Publish chef-sugar v#{Chef::Sugar::VERSION} to the Community Site"
5
+ Stove::RakeTask.new(:publish) do |t|
6
+ t.send(:options)[:version] = Chef::Sugar::VERSION
7
+ end
@@ -26,11 +26,8 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_development_dependency 'bundler', '~> 1.3'
28
28
  spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'stove', '~> 2.0.0.beta'
29
30
 
30
- spec.add_development_dependency 'chef', '~> 11.0'
31
- spec.add_development_dependency 'rspec', '~> 2.11'
32
-
33
- spec.add_development_dependency 'chefspec', '~> 3.0'
34
- spec.add_development_dependency 'test-kitchen', '~> 1.0'
35
- spec.add_development_dependency 'kitchen-vagrant', '~> 0.14'
31
+ spec.add_development_dependency 'chefspec', '~> 3.0'
32
+ spec.add_development_dependency 'coveralls', '~> 0.7'
36
33
  end
data/lib/chef/sugar.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
2
+ # Copyright 2013-2014, Seth Vargo <sethvargo@gmail.com>
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -22,9 +22,11 @@ class Chef
22
22
  module Sugar
23
23
  require_relative 'sugar/architecture'
24
24
  require_relative 'sugar/cloud'
25
+ require_relative 'sugar/constraints'
25
26
  require_relative 'sugar/data_bag'
26
27
  require_relative 'sugar/filters'
27
28
  require_relative 'sugar/ip'
29
+ require_relative 'sugar/kernel'
28
30
  require_relative 'sugar/node'
29
31
  require_relative 'sugar/platform'
30
32
  require_relative 'sugar/platform_family'
@@ -38,3 +40,5 @@ end
38
40
  Chef::Recipe.send(:include, Chef::Sugar::DSL)
39
41
  Chef::Resource.send(:include, Chef::Sugar::DSL)
40
42
  Chef::Provider.send(:include, Chef::Sugar::DSL)
43
+
44
+ Object.send(:include, Chef::Sugar::Kernel)
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
2
+ # Copyright 2013-2014, Seth Vargo <sethvargo@gmail.com>
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2013, Seth Vargo <sethvargo@gmail.com>
2
+ # Copyright 2013-2014, Seth Vargo <sethvargo@gmail.com>
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -0,0 +1,161 @@
1
+ #
2
+ # Copyright 2013-2014, 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 Constraints
20
+ extend self
21
+
22
+ #
23
+ # Shortcut method for creating a new {Version} object.
24
+ #
25
+ # @param [String] version
26
+ # the version (as a string) to create
27
+ #
28
+ # @return [Chef::Sugar::Constraints::Version]
29
+ # the new version object
30
+ #
31
+ def version(version)
32
+ Chef::Sugar::Constraints::Version.new(version)
33
+ end
34
+
35
+ #
36
+ # Shortcut method for creating a new {Constraint} object.
37
+ #
38
+ # @param [String, Array<String>] constraints
39
+ # the list of constraints to use
40
+ #
41
+ # @return [Chef::Sugar::Constraints::Constraint]
42
+ # the new constraint object
43
+ #
44
+ def constraint(*constraints)
45
+ Chef::Sugar::Constraints::Constraint.new(*constraints)
46
+ end
47
+
48
+ #
49
+ # This class is a wrapper around a version requirement that adds a nice
50
+ # DSL for comparing constraints:
51
+ #
52
+ # @example Comparing a single constraint
53
+ # Constraint.new('~> 1.2.3').satisfied_by?('1.2.7')
54
+ #
55
+ # @example Comparing multiple constraints
56
+ # Constraint.new('> 1.2.3', '< 2.0.0').satisfied_by?('1.2.7')
57
+ #
58
+ class Constraint
59
+ #
60
+ # Create a new constraint object.
61
+ #
62
+ # @param [String, Array<String>] constraints
63
+ # the list of constraints
64
+ #
65
+ def initialize(*constraints)
66
+ @requirement = Gem::Requirement.new(*constraints)
67
+ end
68
+
69
+ #
70
+ # Determine if the given version string is satisfied by this constraint
71
+ # or group of constraints.
72
+ #
73
+ # @example Given a satisified constraint
74
+ # Constraint.new('~> 1.2.0').satisfied_by?('1.2.5') #=> true
75
+ #
76
+ # @example Given an unsatisfied constraint
77
+ # Constraint.new('~> 1.2.0').satisfied_by?('2.0.0') #=> false
78
+ #
79
+ #
80
+ # @param [String] version
81
+ # the version to compare
82
+ #
83
+ # @return [Boolean]
84
+ # true if the constraint is satisfied, false otherwise
85
+ #
86
+ def satisfied_by?(version)
87
+ @requirement.satisfied_by?(Gem::Version.new(version))
88
+ end
89
+ end
90
+
91
+ #
92
+ # This class exposes a single version constraint object that wraps the
93
+ # string representation of a version string and proved helpful comparator
94
+ # methods.
95
+ #
96
+ # @example Create a new version
97
+ # Chef::Sugar::Version('1.2.3')
98
+ #
99
+ # @example Compare a version with constraints
100
+ # Chef::Sugar::Version('1.2.3').satisfies?('~> 1.3.4', '< 2.0.5')
101
+ #
102
+ class Version
103
+ #
104
+ # Create a new version object.
105
+ #
106
+ # @param [String] version
107
+ # the version to create
108
+ #
109
+ def initialize(version)
110
+ @version = Gem::Version.new(version)
111
+ end
112
+
113
+ #
114
+ # Determine if the given constraint is satisfied by this version.
115
+ #
116
+ # @example Given a satisified version
117
+ # Version.new('1.2.5').satisfies?('~> 1.2.0') #=> true
118
+ #
119
+ # @example Given an unsatisfied version
120
+ # Version.new('2.0.0').satisfies?('~> 1.2.0') #=> false
121
+ #
122
+ #
123
+ # @param [String, Array<String>] constraints
124
+ # the constraints to satisfy
125
+ #
126
+ # @return [Boolean]
127
+ # true if the version satisfies the constraints, false otherwise
128
+ #
129
+ def satisfies?(*constraints)
130
+ Gem::Requirement.new(*constraints).satisfied_by?(@version)
131
+ end
132
+ end
133
+ end
134
+
135
+ module DSL
136
+ # @see Chef::Sugar::Constraints#version
137
+ def version(version)
138
+ Chef::Sugar::Constraints::Version.new(version)
139
+ end
140
+
141
+ # @see Chef::Sugar::Constraints#constraint
142
+ def constraint(*constraints)
143
+ Chef::Sugar::Constraints.constraint(*constraints)
144
+ end
145
+
146
+ #
147
+ # This wrapper/convenience method is only available in the recipe DSL. It
148
+ # creates a new version object from the {Chef::VERSION}.
149
+ #
150
+ # @example Check if Chef 11+
151
+ # chef_version.satisfies?('>= 11.0.0')
152
+ #
153
+ # @return [Chef::Sugar::Constraints::Version]
154
+ # a version object, wrapping the current {Chef::VERSION}
155
+ #
156
+ def chef_version
157
+ version(Chef::VERSION)
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2013-2014, 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
+ require_relative 'core_extensions/array'
18
+ require_relative 'core_extensions/string'
@@ -0,0 +1,34 @@
1
+ #
2
+ # Copyright 2013-2014, 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
+ require_relative '../constraints'
18
+
19
+ class Array
20
+ #
21
+ # Treat an array of objects as version constraints.
22
+ #
23
+ # @see Chef::Sugar::Constraints::Constraint
24
+ #
25
+ # @example Using pure Array<String> objects like constraints
26
+ # ['> 2.0.0', '< 3.0.0'].satisfied_by?('2.1.0')
27
+ #
28
+ # @param [String] version
29
+ # the version to check if it is satisfied
30
+ #
31
+ def satisfied_by?(version)
32
+ Chef::Sugar::Constraints::Constraint.new(*dup).satisfied_by?(version)
33
+ end
34
+ end