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,108 @@
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 Constraints
20
+ #
21
+ # This class is a wrapper around a version requirement that adds a nice
22
+ # DSL for comparing constraints:
23
+ #
24
+ # @example Comparing a single constraint
25
+ # Constraint.new('~> 1.2.3').satisfied_by?('1.2.7')
26
+ #
27
+ # @example Comparing multiple constraints
28
+ # Constraint.new('> 1.2.3', '< 2.0.0').satisfied_by?('1.2.7')
29
+ #
30
+ class Constraint
31
+ #
32
+ # Create a new constraint object.
33
+ #
34
+ # @param [String, Array<String>] constraints
35
+ # the list of constraints
36
+ #
37
+ def initialize(*constraints)
38
+ @requirement = Gem::Requirement.new(*constraints)
39
+ end
40
+
41
+ #
42
+ # Determine if the given version string is satisfied by this constraint
43
+ # or group of constraints.
44
+ #
45
+ # @example Given a satisified constraint
46
+ # Constraint.new('~> 1.2.0').satisfied_by?('1.2.5') #=> true
47
+ #
48
+ # @example Given an unsatisfied constraint
49
+ # Constraint.new('~> 1.2.0').satisfied_by?('2.0.0') #=> false
50
+ #
51
+ #
52
+ # @param [String] version
53
+ # the version to compare
54
+ #
55
+ # @return [Boolean]
56
+ # true if the constraint is satisfied, false otherwise
57
+ #
58
+ def satisfied_by?(version)
59
+ @requirement.satisfied_by?(Gem::Version.new(version))
60
+ end
61
+ end
62
+
63
+ #
64
+ # This class exposes a single version constraint object that wraps the
65
+ # string representation of a version string and proved helpful comparator
66
+ # methods.
67
+ #
68
+ # @example Create a new version
69
+ # Chef::Sugar::Version('1.2.3')
70
+ #
71
+ # @example Compare a version with constraints
72
+ # Chef::Sugar::Version('1.2.3').satisfies?('~> 1.3.4', '< 2.0.5')
73
+ #
74
+ class Version < String
75
+ #
76
+ # Create a new version object.
77
+ #
78
+ # @param [String] version
79
+ # the version to create
80
+ #
81
+ def initialize(version)
82
+ super
83
+ @version = Gem::Version.new(version)
84
+ end
85
+
86
+ #
87
+ # Determine if the given constraint is satisfied by this version.
88
+ #
89
+ # @example Given a satisified version
90
+ # Version.new('1.2.5').satisfies?('~> 1.2.0') #=> true
91
+ #
92
+ # @example Given an unsatisfied version
93
+ # Version.new('2.0.0').satisfies?('~> 1.2.0') #=> false
94
+ #
95
+ #
96
+ # @param [String, Array<String>] constraints
97
+ # the constraints to satisfy
98
+ #
99
+ # @return [Boolean]
100
+ # true if the version satisfies the constraints, false otherwise
101
+ #
102
+ def satisfies?(*constraints)
103
+ Gem::Requirement.new(*constraints).satisfied_by?(@version)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,83 @@
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
+ require_relative 'constraints'
18
+
19
+ class Chef
20
+ module Sugar
21
+ #
22
+ # The Constraints DSL methods were broken out into this separate
23
+ # file to allow projects (such as Omnibus) to consume the
24
+ # Chef::Sugar::Constraints classes without the DSL methods
25
+ # stepping on existing methods of the same name.
26
+ #
27
+ module Constraints
28
+ extend self
29
+
30
+ #
31
+ # Shortcut method for creating a new {Version} object.
32
+ #
33
+ # @param [String] version
34
+ # the version (as a string) to create
35
+ #
36
+ # @return [Chef::Sugar::Constraints::Version]
37
+ # the new version object
38
+ #
39
+ def version(version)
40
+ Chef::Sugar::Constraints::Version.new(version)
41
+ end
42
+
43
+ #
44
+ # Shortcut method for creating a new {Constraint} object.
45
+ #
46
+ # @param [String, Array<String>] constraints
47
+ # the list of constraints to use
48
+ #
49
+ # @return [Chef::Sugar::Constraints::Constraint]
50
+ # the new constraint object
51
+ #
52
+ def constraint(*constraints)
53
+ Chef::Sugar::Constraints::Constraint.new(*constraints)
54
+ end
55
+ end
56
+
57
+ module DSL
58
+ # @see Chef::Sugar::Constraints#version
59
+ def version(version)
60
+ Chef::Sugar::Constraints::Version.new(version)
61
+ end
62
+
63
+ # @see Chef::Sugar::Constraints#constraint
64
+ def constraint(*constraints)
65
+ Chef::Sugar::Constraints.constraint(*constraints)
66
+ end
67
+
68
+ #
69
+ # This wrapper/convenience method is only available in the recipe DSL. It
70
+ # creates a new version object from the {Chef::VERSION}.
71
+ #
72
+ # @example Check if Chef 11+
73
+ # chef_version.satisfies?('>= 11.0.0')
74
+ #
75
+ # @return [Chef::Sugar::Constraints::Version]
76
+ # a version object, wrapping the current {Chef::VERSION}
77
+ #
78
+ def chef_version
79
+ version(Chef::VERSION)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,19 @@
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
+ require_relative 'core_extensions/array'
18
+ require_relative 'core_extensions/string'
19
+ require_relative 'core_extensions/object'
@@ -0,0 +1,34 @@
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
+ 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 unless method_defined?(:satisfied_by?)
34
+ end
@@ -0,0 +1,27 @@
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 Object
18
+ # An object is blank if it's false, empty, or a whitespace string.
19
+ # This is implemented in rails.
20
+ #
21
+ # @example foo.nil? || foo.empty? can be replaced by foo.blank?
22
+ #
23
+ # @return [true, false]
24
+ def blank?
25
+ respond_to?(:empty?) ? empty? : !self
26
+ end unless method_defined?(:blank?)
27
+ end
@@ -0,0 +1,66 @@
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
+ # require_relative '../constraints'
18
+
19
+ class String
20
+ #
21
+ # Treat strings as version objects.
22
+ #
23
+ # @see Chef::Sugar::Constraints::Version
24
+ #
25
+ # @example Using pure string objects like versions
26
+ # '1.2.3'.satisfies?('~> 1.2.0')
27
+ #
28
+ # @param [String, Array<String>] constraints
29
+ # the list of constraints to satisfy
30
+ #
31
+ def satisfies?(*constraints)
32
+ Chef::Sugar::Constraints::Version.new(dup).satisfies?(*constraints)
33
+ end unless method_defined?(:satisfies?)
34
+
35
+ #
36
+ # Treat strings as version constraints.
37
+ #
38
+ # @see Chef::Sugar::Constraints::Constraint
39
+ #
40
+ # @example Using pure string objects like constraints
41
+ # '~> 1.2.0'.satisfied_by?('1.2.3')
42
+ #
43
+ # @param [String] version
44
+ # the version to check if it is satisfied
45
+ #
46
+ def satisfied_by?(version)
47
+ Chef::Sugar::Constraints::Constraint.new(dup).satisfied_by?(version)
48
+ end unless method_defined?(:satisfied_by?)
49
+
50
+ #
51
+ # Left-flush a string based off of the number of whitespace characters on the
52
+ # first line. This is especially useful for heredocs when whitespace matters.
53
+ #
54
+ # @example Remove leading whitespace and flush
55
+ # <<-EOH.flush
56
+ # def method
57
+ # 'This is a string!'
58
+ # end
59
+ # EOH #=>"def method\n 'This is a string!'\nend"
60
+ #
61
+ # @return [String]
62
+ #
63
+ def flush
64
+ gsub(/^#{self[/\A\s*/]}/, '').chomp
65
+ end unless method_defined?(:flush)
66
+ end
@@ -0,0 +1,146 @@
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 DataBag
20
+ class EncryptedDataBagSecretNotGiven < StandardError
21
+ def initialize
22
+ super <<-EOH
23
+ You did not set your `encrypted_data_bag_secret'! In order to use the
24
+ `encrypted_data_bag_item' helper, you must load your encrypted data bag secret
25
+ into the `Chef::Config'.
26
+
27
+ Alternatively, you can pass the secret key as the last parameter to the method
28
+ call. For more information, please see
29
+ http://docs.opscode.com/chef/essentials_data_bags.html#access-from-recipe.
30
+ EOH
31
+ end
32
+ end
33
+
34
+ extend self
35
+
36
+ #
37
+ # Helper method for loading an encrypted data bag item in a similar
38
+ # syntax/recipe DSL method.
39
+ #
40
+ # @param [String] bag
41
+ # the name of the encrypted data bag
42
+ # @param [String] id
43
+ # the id of the encrypted data bag
44
+ # @param [String] secret
45
+ # the encrypted data bag secret raw value
46
+ #
47
+ # @return [Hash]
48
+ #
49
+ def encrypted_data_bag_item(bag, id, secret = nil)
50
+ Chef::Log.debug "Loading encrypted data bag item #{bag}/#{id}"
51
+
52
+ if secret.nil? && Chef::Config[:encrypted_data_bag_secret].nil?
53
+ raise EncryptedDataBagSecretNotGiven.new
54
+ end
55
+
56
+ secret ||= File.read(Chef::Config[:encrypted_data_bag_secret]).strip
57
+ Chef::EncryptedDataBagItem.load(bag, id, secret)
58
+ end
59
+
60
+ #
61
+ # This algorithm attempts to find the data bag entry for the current
62
+ # node's Chef environment. If there are no environment-specific
63
+ # values, the "default" bucket is used. The data bag must follow the
64
+ # schema:
65
+ #
66
+ # {
67
+ # "default": {...},
68
+ # "environment_name": {...},
69
+ # "other_environment": {...},
70
+ # }
71
+ #
72
+ # @param [Node] node
73
+ # the current Chef node
74
+ # @param [String] bag
75
+ # the name of the encrypted data bag
76
+ # @param [String] id
77
+ # the id of the encrypted data bag
78
+ # @param [String] secret
79
+ # the encrypted data bag secret (default's to the +Chef::Config+ value)
80
+ #
81
+ # @return [Hash]
82
+ #
83
+ def encrypted_data_bag_item_for_environment(node, bag, id, secret = nil)
84
+ data = encrypted_data_bag_item(bag, id, secret)
85
+
86
+ if data[node.chef_environment]
87
+ Chef::Log.debug "Using #{node.chef_environment} as the key"
88
+ data[node.chef_environment]
89
+ else
90
+ Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
91
+ data['default']
92
+ end
93
+ end
94
+
95
+ #
96
+ # This algorithm attempts to find the data bag entry for the current
97
+ # node's Chef environment. If there are no environment-specific
98
+ # values, the "default" bucket is used. The data bag must follow the
99
+ # schema:
100
+ #
101
+ # {
102
+ # "default": {...},
103
+ # "environment_name": {...},
104
+ # "other_environment": {...},
105
+ # }
106
+ #
107
+ # @param [Node] node
108
+ # the current Chef node
109
+ # @param [String] bag
110
+ # the name of the data bag
111
+ # @param [String] id
112
+ # the id of the data bag
113
+ #
114
+ # @return [Hash]
115
+ #
116
+ def data_bag_item_for_environment(node, bag, id)
117
+ data = Chef::DataBagItem.load(bag, id)
118
+
119
+ if data[node.chef_environment]
120
+ Chef::Log.debug "Using #{node.chef_environment} as the key"
121
+ data[node.chef_environment]
122
+ else
123
+ Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
124
+ data['default']
125
+ end
126
+ end
127
+ end
128
+
129
+ module DSL
130
+ # @see Chef::Sugar::DataBag#encrypted_data_bag_item
131
+ def encrypted_data_bag_item(bag, id, secret = nil)
132
+ Chef::Sugar::DataBag.encrypted_data_bag_item(bag, id, secret)
133
+ end
134
+
135
+ # @see Chef::Sugar::DataBag#encrypted_data_bag_item_for_environment
136
+ def encrypted_data_bag_item_for_environment(bag, id, secret = nil)
137
+ Chef::Sugar::DataBag.encrypted_data_bag_item_for_environment(node, bag, id, secret)
138
+ end
139
+
140
+ # @see Chef::Sugar::DataBag#data_bag_item_for_environment
141
+ def data_bag_item_for_environment(bag, id)
142
+ Chef::Sugar::DataBag.data_bag_item_for_environment(node, bag, id)
143
+ end
144
+ end
145
+ end
146
+ end