chef-sugar-sre 5.1.13

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.
@@ -0,0 +1,61 @@
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.exist?('/bin/systemctl')
29
+ end
30
+
31
+ #
32
+ # Determine if the current init system is upstart.
33
+ #
34
+ # @return [Boolean]
35
+ #
36
+ def upstart?(node)
37
+ File.executable?('/sbin/initctl')
38
+ end
39
+
40
+ #
41
+ # Determine if the current init system is runit.
42
+ #
43
+ # @return [Boolean]
44
+ #
45
+ def runit?(node)
46
+ File.executable?('/sbin/runit-init')
47
+ end
48
+ end
49
+
50
+ module DSL
51
+ # @see Chef::Sugar::Init#systemd?
52
+ def systemd?; Chef::Sugar::Init.systemd?(node); end
53
+
54
+ # @see Chef::Sugar::Init#upstart?
55
+ def upstart?; Chef::Sugar::Init.upstart?(node); end
56
+
57
+ # @see Chef::Sugar::Init#runit?
58
+ def runit?; Chef::Sugar::Init.runit?(node); end
59
+ end
60
+ end
61
+ 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,45 @@
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
+ # these helpers have been moved to core-chef
23
+ if !defined?(Chef::VERSION) || Gem::Requirement.new("< 15.4.70").satisfied_by?(Gem::Version.new(Chef::VERSION))
24
+ #
25
+ # Returns true if the current node is provisioned by Test Kitchen.
26
+ #
27
+ # @param [Chef::Node] node
28
+ # the node to check
29
+ #
30
+ # @return [Boolean]
31
+ #
32
+ def kitchen?(node)
33
+ !ENV['TEST_KITCHEN'].nil?
34
+ end
35
+ end
36
+ end
37
+
38
+ module DSL
39
+ if !defined?(Chef::VERSION) || Gem::Requirement.new("< 15.4.70").satisfied_by?(Gem::Version.new(Chef::VERSION))
40
+ # @see Chef::Sugar::Kitchen#kitchen?
41
+ def kitchen?; Chef::Sugar::Kitchen.kitchen?(node); end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,219 @@
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 "deprecation"
18
+
19
+ class Chef
20
+ class Node
21
+ class AttributeDoesNotExistError < StandardError
22
+ def initialize(keys, key)
23
+ hash = keys.map { |key| "['#{key}']" }
24
+
25
+ super <<-EOH
26
+ No attribute `node#{hash.join}' exists on
27
+ the current node. Specifically the `#{key}' attribute is not
28
+ defined. Please make sure you have spelled everything correctly.
29
+ EOH
30
+ end
31
+ end
32
+ #
33
+ # Determine if the current node is in the given Chef environment
34
+ # (or matches the given regular expression).
35
+ #
36
+ # @param [String, Regex] environment
37
+ #
38
+ # @return [Boolean]
39
+ #
40
+ def in?(environment)
41
+ Chef::Sugar::Deprecation.deprecated "the chef-sugar node.in? method is deprecated"
42
+ environment === chef_environment
43
+ end
44
+
45
+ #
46
+ # Safely fetch a deeply nested attribute by specifying a list of keys,
47
+ # bypassing Ruby's Hash notation. This method swallows +NoMethodError+
48
+ # exceptions, avoiding the most common error in Chef-land.
49
+ #
50
+ # This method will return +nil+ if any deeply nested key does not exist.
51
+ #
52
+ # @see [Node#deep_fetch!]
53
+ #
54
+ def deep_fetch(*keys)
55
+ Chef::Sugar::Deprecation.deprecated "the chef-sugar deep_fetch method is deprecated and should be replaced by node.read"
56
+ deep_fetch!(*keys)
57
+ rescue NoMethodError, AttributeDoesNotExistError
58
+ nil
59
+ end
60
+
61
+ #
62
+ # Deeply fetch a node attribute by specifying a list of keys, bypassing
63
+ # Ruby's Hash notation.
64
+ #
65
+ # This method will raise any exceptions, such as
66
+ # +undefined method `[]' for nil:NilClass+, just as if you used the native
67
+ # attribute notation. If you want a safely vivified hash, see {deep_fetch}.
68
+ #
69
+ # @example Fetch a deeply nested key
70
+ # node.deep_fetch(:foo, :bar, :zip) #=> node['foo']['bar']['zip']
71
+ #
72
+ # @param [Array<String, Symbol>] keys
73
+ # the list of keys to kdeep fetch
74
+ #
75
+ # @return [Object]
76
+ #
77
+ def deep_fetch!(*keys)
78
+ Chef::Sugar::Deprecation.deprecated "the chef-sugar deep_fetch method is deprecated and should be replaced by node.read!"
79
+ keys.map!(&:to_s)
80
+
81
+ keys.inject(attributes.to_hash) do |hash, key|
82
+ if hash.key?(key)
83
+ hash[key]
84
+ else
85
+ raise AttributeDoesNotExistError.new(keys, key)
86
+ end
87
+ end
88
+ end
89
+
90
+ #
91
+ # Dynamically define the current namespace. Multiple namespaces may be
92
+ # nested.
93
+ #
94
+ # @example Define a simple namespace
95
+ #
96
+ # namespace 'apache2' do
97
+ # # ...
98
+ # end
99
+ #
100
+ # @example Define a nested namespace
101
+ #
102
+ # namespace 'apache2', 'config' do
103
+ # # ...
104
+ # end
105
+ #
106
+ # @example Define a complex nested namespace
107
+ #
108
+ # namespace 'apache2' do
109
+ # namespace 'config' do
110
+ # # ...
111
+ # end
112
+ # end
113
+ #
114
+ # @example Define a namespace with a custom precedence level
115
+ #
116
+ # namespace 'apache2', precedence: normal do
117
+ # # Attributes here will use the "normal" level
118
+ # end
119
+ #
120
+ # @example Define different nested precedence levels
121
+ #
122
+ # namespace 'apache2', precedence: normal do
123
+ # # Attributes defined here will use the "normal" level
124
+ #
125
+ # namespace 'config', precedence: override do
126
+ # # Attributes defined here will use the "override" level
127
+ # end
128
+ # end
129
+ #
130
+ #
131
+ # @param [Array] args
132
+ # the list of arguments (such as the namespace and precedence levels)
133
+ # the user gave
134
+ # @param [Proc] block
135
+ # the nested evaluation context
136
+ #
137
+ # @return [nil]
138
+ # to prevent accidential method chaining if the block isn't closed
139
+ #
140
+ def namespace(*args, &block)
141
+ Chef::Sugar::Deprecation.deprecated "the chef-sugar attribute namespace setting is deprecated, please use traditional chef attribute notation"
142
+ @namespace_options = namespace_options.merge(args.last.is_a?(Hash) ? args.pop : {})
143
+
144
+ keys = args.map(&:to_s)
145
+
146
+ @current_namespace = current_namespace + keys
147
+ instance_eval(&block)
148
+ @current_namespace = current_namespace - keys
149
+
150
+ if @current_namespace.empty?
151
+ @namespace_options = nil
152
+ end
153
+ nil
154
+ end
155
+
156
+ alias_method :old_method_missing, :method_missing
157
+ #
158
+ # Provide a nice DSL for defining attributes. +method_missing+ is called
159
+ # on all the attribute names. For more information on how to use the DSL,
160
+ # see the class-level documentation.
161
+ #
162
+ # @return [nil]
163
+ # to prevent accidential method chaining if the block isn't closed
164
+ # @return [Object]
165
+ # If no argument is passed in, method becomes an attribute accessor
166
+ #
167
+ def method_missing(m, *args, &block)
168
+ old_method_missing(m, *args, &block)
169
+ rescue NoMethodError
170
+ # The Node Attribute's key is the method name
171
+ key = m.to_s
172
+ # If arguments are passed in, set node attribute with args as the value
173
+ if args.size > 0
174
+ vivified[key] = args.size == 1 ? args.first : args
175
+ return nil
176
+ # If no arguments are passed in, attempt to access corresponding attribute
177
+ else
178
+ deep_key = current_namespace.dup << key
179
+ return deep_fetch!(*deep_key)
180
+ end
181
+ end
182
+
183
+ private
184
+
185
+ #
186
+ # The namespace options.
187
+ #
188
+ # @return [Hash]
189
+ #
190
+ def namespace_options
191
+ @namespace_options ||= {
192
+ precedence: default
193
+ }
194
+ end
195
+
196
+ #
197
+ # The current namespace. This is actually a reverse-ordered array that
198
+ # vivifies the correct hash.#
199
+ #
200
+ # @return [Array<String>]
201
+ #
202
+ def current_namespace
203
+ @current_namespace ||= []
204
+ end
205
+
206
+ #
207
+ # The vivified (fake-filled) hash. It is assumed that the default value
208
+ # for non-existent keys in the hash is a new, empty hash.
209
+ #
210
+ # @return [Hash<String, Hash>]
211
+ #
212
+ def vivified
213
+ current_namespace.inject(namespace_options[:precedence]) do |hash, item|
214
+ hash[item] ||= {}
215
+ hash[item]
216
+ end
217
+ end
218
+ end
219
+ end