chef-utils 16.10.17 → 17.10.19
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/LICENSE +201 -201
- data/Rakefile +15 -15
- data/chef-utils.gemspec +50 -46
- data/lib/chef-utils/dist.rb +151 -98
- data/lib/chef-utils/dsl/architecture.rb +150 -150
- data/lib/chef-utils/dsl/cloud.rb +155 -144
- data/lib/chef-utils/dsl/default_paths.rb +60 -60
- data/lib/chef-utils/dsl/introspection.rb +134 -123
- data/lib/chef-utils/dsl/os.rb +58 -58
- data/lib/chef-utils/dsl/path_sanity.rb +39 -39
- data/lib/chef-utils/dsl/platform.rb +387 -372
- data/lib/chef-utils/dsl/platform_family.rb +355 -344
- data/lib/chef-utils/dsl/platform_version.rb +41 -41
- data/lib/chef-utils/dsl/service.rb +112 -112
- data/lib/chef-utils/dsl/train_helpers.rb +87 -87
- data/lib/chef-utils/dsl/virtualization.rb +272 -250
- data/lib/chef-utils/dsl/which.rb +123 -123
- data/lib/chef-utils/dsl/windows.rb +86 -86
- data/lib/chef-utils/internal.rb +114 -114
- data/lib/chef-utils/mash.rb +263 -240
- data/lib/chef-utils/parallel_map.rb +131 -0
- data/lib/chef-utils/version.rb +20 -20
- data/lib/chef-utils/version_string.rb +160 -160
- data/lib/chef-utils.rb +53 -53
- data/spec/spec_helper.rb +100 -100
- data/spec/unit/dsl/architecture_spec.rb +151 -151
- data/spec/unit/dsl/cloud_spec.rb +93 -89
- data/spec/unit/dsl/dsl_spec.rb +34 -34
- data/spec/unit/dsl/introspection_spec.rb +201 -189
- data/spec/unit/dsl/os_spec.rb +175 -175
- data/spec/unit/dsl/path_sanity_spec.rb +86 -86
- data/spec/unit/dsl/platform_family_spec.rb +235 -223
- data/spec/unit/dsl/platform_spec.rb +252 -238
- data/spec/unit/dsl/service_spec.rb +117 -117
- data/spec/unit/dsl/virtualization_spec.rb +75 -75
- data/spec/unit/dsl/which_spec.rb +171 -171
- data/spec/unit/dsl/windows_spec.rb +84 -84
- data/spec/unit/mash_spec.rb +51 -51
- data/spec/unit/parallel_map_spec.rb +156 -0
- metadata +26 -10
data/lib/chef-utils/mash.rb
CHANGED
@@ -1,240 +1,263 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# Copyright 2009-2016, Dan Kubb
|
3
|
-
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
-
# a copy of this software and associated documentation files (the
|
6
|
-
# "Software"), to deal in the Software without restriction, including
|
7
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
-
# the following conditions:
|
11
|
-
|
12
|
-
# The above copyright notice and this permission notice shall be
|
13
|
-
# included in all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
-
|
23
|
-
# ---
|
24
|
-
# ---
|
25
|
-
|
26
|
-
# Some portions of blank.rb and mash.rb are verbatim copies of software
|
27
|
-
# licensed under the MIT license. That license is included below:
|
28
|
-
|
29
|
-
# Copyright 2005-2016, David Heinemeier Hansson
|
30
|
-
|
31
|
-
# Permission is hereby granted, free of charge, to any person obtaining
|
32
|
-
# a copy of this software and associated documentation files (the
|
33
|
-
# "Software"), to deal in the Software without restriction, including
|
34
|
-
# without limitation the rights to use, copy, modify, merge, publish,
|
35
|
-
# distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
-
# permit persons to whom the Software is furnished to do so, subject to
|
37
|
-
# the following conditions:
|
38
|
-
|
39
|
-
# The above copyright notice and this permission notice shall be
|
40
|
-
# included in all copies or substantial portions of the Software.
|
41
|
-
|
42
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
43
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
45
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
46
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
47
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
48
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
49
|
-
|
50
|
-
# This class has dubious semantics and we only have it so that people can write
|
51
|
-
# params[:key] instead of params['key'].
|
52
|
-
module ChefUtils
|
53
|
-
class Mash < Hash
|
54
|
-
|
55
|
-
# @param constructor<Object>
|
56
|
-
# The default value for the mash. Defaults to an empty hash.
|
57
|
-
#
|
58
|
-
# @details [Alternatives]
|
59
|
-
# If constructor is a Hash, a new mash will be created based on the keys of
|
60
|
-
# the hash and no default value will be set.
|
61
|
-
def initialize(constructor = {})
|
62
|
-
if constructor.is_a?(Hash)
|
63
|
-
super()
|
64
|
-
update(constructor)
|
65
|
-
else
|
66
|
-
super(constructor)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# @param orig<Object> Mash being copied
|
71
|
-
#
|
72
|
-
# @return [Object] A new copied Mash
|
73
|
-
def initialize_copy(orig)
|
74
|
-
super
|
75
|
-
# Handle nested values
|
76
|
-
each do |k, v|
|
77
|
-
if v.is_a?(Mash) || v.is_a?(Array)
|
78
|
-
self[k] = v.dup
|
79
|
-
end
|
80
|
-
end
|
81
|
-
self
|
82
|
-
end
|
83
|
-
|
84
|
-
# @param key<Object> The default value for the mash. Defaults to nil.
|
85
|
-
#
|
86
|
-
# @details [Alternatives]
|
87
|
-
# If key is a Symbol and it is a key in the mash, then the default value will
|
88
|
-
# be set to the value matching the key.
|
89
|
-
def default(key = nil)
|
90
|
-
if key.is_a?(Symbol) && include?(key = key.to_s)
|
91
|
-
self[key]
|
92
|
-
else
|
93
|
-
super
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
unless method_defined?(:
|
98
|
-
alias_method :
|
99
|
-
end
|
100
|
-
|
101
|
-
unless method_defined?(:
|
102
|
-
alias_method :
|
103
|
-
end
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
# @
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
# @return [
|
149
|
-
def
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
# @
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
#
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
# @param key<Object>
|
169
|
-
#
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
# @
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
#
|
186
|
-
#
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
# @
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
#
|
203
|
-
#
|
204
|
-
def
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
#
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
# @return [
|
226
|
-
#
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# Copyright 2009-2016, Dan Kubb
|
3
|
+
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
|
23
|
+
# ---
|
24
|
+
# ---
|
25
|
+
|
26
|
+
# Some portions of blank.rb and mash.rb are verbatim copies of software
|
27
|
+
# licensed under the MIT license. That license is included below:
|
28
|
+
|
29
|
+
# Copyright 2005-2016, David Heinemeier Hansson
|
30
|
+
|
31
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
32
|
+
# a copy of this software and associated documentation files (the
|
33
|
+
# "Software"), to deal in the Software without restriction, including
|
34
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
35
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
36
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
37
|
+
# the following conditions:
|
38
|
+
|
39
|
+
# The above copyright notice and this permission notice shall be
|
40
|
+
# included in all copies or substantial portions of the Software.
|
41
|
+
|
42
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
43
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
44
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
45
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
46
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
47
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
48
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
49
|
+
|
50
|
+
# This class has dubious semantics and we only have it so that people can write
|
51
|
+
# params[:key] instead of params['key'].
|
52
|
+
module ChefUtils
|
53
|
+
class Mash < Hash
|
54
|
+
|
55
|
+
# @param constructor<Object>
|
56
|
+
# The default value for the mash. Defaults to an empty hash.
|
57
|
+
#
|
58
|
+
# @details [Alternatives]
|
59
|
+
# If constructor is a Hash, a new mash will be created based on the keys of
|
60
|
+
# the hash and no default value will be set.
|
61
|
+
def initialize(constructor = {})
|
62
|
+
if constructor.is_a?(Hash)
|
63
|
+
super()
|
64
|
+
update(constructor)
|
65
|
+
else
|
66
|
+
super(constructor)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# @param orig<Object> Mash being copied
|
71
|
+
#
|
72
|
+
# @return [Object] A new copied Mash
|
73
|
+
def initialize_copy(orig)
|
74
|
+
super
|
75
|
+
# Handle nested values
|
76
|
+
each do |k, v|
|
77
|
+
if v.is_a?(Mash) || v.is_a?(Array)
|
78
|
+
self[k] = v.dup
|
79
|
+
end
|
80
|
+
end
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
# @param key<Object> The default value for the mash. Defaults to nil.
|
85
|
+
#
|
86
|
+
# @details [Alternatives]
|
87
|
+
# If key is a Symbol and it is a key in the mash, then the default value will
|
88
|
+
# be set to the value matching the key.
|
89
|
+
def default(key = nil)
|
90
|
+
if key.is_a?(Symbol) && include?(key = key.to_s)
|
91
|
+
self[key]
|
92
|
+
else
|
93
|
+
super
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
unless method_defined?(:regular_reader)
|
98
|
+
alias_method :regular_reader, :[]
|
99
|
+
end
|
100
|
+
|
101
|
+
unless method_defined?(:regular_writer)
|
102
|
+
alias_method :regular_writer, :[]=
|
103
|
+
end
|
104
|
+
|
105
|
+
unless method_defined?(:regular_update)
|
106
|
+
alias_method :regular_update, :update
|
107
|
+
end
|
108
|
+
|
109
|
+
unless method_defined?(:regular_clear)
|
110
|
+
alias_method :regular_clear, :clear
|
111
|
+
end
|
112
|
+
|
113
|
+
unless method_defined?(:regular_delete)
|
114
|
+
alias_method :regular_delete, :delete
|
115
|
+
end
|
116
|
+
|
117
|
+
# @param key<Object> The key to get.
|
118
|
+
def [](key)
|
119
|
+
regular_reader(key)
|
120
|
+
end
|
121
|
+
|
122
|
+
# @param key<Object> The key to set.
|
123
|
+
# @param value<Object>
|
124
|
+
# The value to set the key to.
|
125
|
+
#
|
126
|
+
# @see Mash#convert_key
|
127
|
+
# @see Mash#convert_value
|
128
|
+
def []=(key, value)
|
129
|
+
regular_writer(convert_key(key), convert_value(value))
|
130
|
+
end
|
131
|
+
|
132
|
+
# internal API for use by Chef's deep merge cache
|
133
|
+
# @api private
|
134
|
+
def internal_get(key)
|
135
|
+
regular_reader(key)
|
136
|
+
end
|
137
|
+
|
138
|
+
# internal API for use by Chef's deep merge cache
|
139
|
+
# @api private
|
140
|
+
def internal_set(key, value)
|
141
|
+
regular_writer(key, convert_value(value))
|
142
|
+
end
|
143
|
+
|
144
|
+
# @param other_hash<Hash>
|
145
|
+
# A hash to update values in the mash with. The keys and the values will be
|
146
|
+
# converted to Mash format.
|
147
|
+
#
|
148
|
+
# @return [Mash] The updated mash.
|
149
|
+
def update(other_hash)
|
150
|
+
other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
|
151
|
+
self
|
152
|
+
end
|
153
|
+
|
154
|
+
alias_method :merge!, :update
|
155
|
+
|
156
|
+
# @param key<Object> The key to check for. This will be run through convert_key.
|
157
|
+
#
|
158
|
+
# @return [Boolean] True if the key exists in the mash.
|
159
|
+
def key?(key)
|
160
|
+
super(convert_key(key))
|
161
|
+
end
|
162
|
+
|
163
|
+
# def include? def has_key? def member?
|
164
|
+
alias_method :include?, :key?
|
165
|
+
alias_method :has_key?, :key?
|
166
|
+
alias_method :member?, :key?
|
167
|
+
|
168
|
+
# @param key<Object> The key to fetch. This will be run through convert_key.
|
169
|
+
# @param *extras<Array> Default value.
|
170
|
+
#
|
171
|
+
# @return [Object] The value at key or the default value.
|
172
|
+
def fetch(key, *extras)
|
173
|
+
super(convert_key(key), *extras)
|
174
|
+
end
|
175
|
+
|
176
|
+
# @param *indices<Array>
|
177
|
+
# The keys to retrieve values for. These will be run through +convert_key+.
|
178
|
+
#
|
179
|
+
# @return [Array] The values at each of the provided keys
|
180
|
+
def values_at(*indices)
|
181
|
+
indices.collect { |key| self[convert_key(key)] }
|
182
|
+
end
|
183
|
+
|
184
|
+
# @param hash<Hash> The hash to merge with the mash.
|
185
|
+
#
|
186
|
+
# @return [Mash] A new mash with the hash values merged in.
|
187
|
+
def merge(hash)
|
188
|
+
dup.update(hash)
|
189
|
+
end
|
190
|
+
|
191
|
+
# @param key<Object>
|
192
|
+
# The key to delete from the mash.\
|
193
|
+
def delete(key)
|
194
|
+
super(convert_key(key))
|
195
|
+
end
|
196
|
+
|
197
|
+
# @param *rejected<Array[(String, Symbol)] The mash keys to exclude.
|
198
|
+
#
|
199
|
+
# @return [Mash] A new mash without the selected keys.
|
200
|
+
#
|
201
|
+
# @example
|
202
|
+
# { :one => 1, :two => 2, :three => 3 }.except(:one)
|
203
|
+
# #=> { "two" => 2, "three" => 3 }
|
204
|
+
def except(*keys)
|
205
|
+
super(*keys.map { |k| convert_key(k) })
|
206
|
+
end
|
207
|
+
|
208
|
+
# Used to provide the same interface as Hash.
|
209
|
+
#
|
210
|
+
# @return [Mash] This mash unchanged.
|
211
|
+
def stringify_keys!; self end
|
212
|
+
|
213
|
+
# @return [Hash] The mash as a Hash with symbolized keys.
|
214
|
+
def symbolize_keys
|
215
|
+
h = Hash.new(default)
|
216
|
+
each { |key, val| h[key.to_sym] = val }
|
217
|
+
h
|
218
|
+
end
|
219
|
+
|
220
|
+
# @return [Hash] The mash as a Hash with string keys.
|
221
|
+
def to_hash
|
222
|
+
Hash.new(default).merge(self)
|
223
|
+
end
|
224
|
+
|
225
|
+
# @return [Mash] Convert a Hash into a Mash
|
226
|
+
# The input Hash's default value is maintained
|
227
|
+
def self.from_hash(hash)
|
228
|
+
mash = Mash.new(hash)
|
229
|
+
mash.default = hash.default
|
230
|
+
mash
|
231
|
+
end
|
232
|
+
|
233
|
+
protected
|
234
|
+
|
235
|
+
# @param key<Object> The key to convert.
|
236
|
+
#
|
237
|
+
# @param [Object]
|
238
|
+
# The converted key. If the key was a symbol, it will be converted to a
|
239
|
+
# string.
|
240
|
+
#
|
241
|
+
# @api private
|
242
|
+
def convert_key(key)
|
243
|
+
key.is_a?(Symbol) ? key.to_s : key
|
244
|
+
end
|
245
|
+
|
246
|
+
# @param value<Object> The value to convert.
|
247
|
+
#
|
248
|
+
# @return [Object]
|
249
|
+
# The converted value. A Hash or an Array of hashes, will be converted to
|
250
|
+
# their Mash equivalents.
|
251
|
+
#
|
252
|
+
# @api private
|
253
|
+
def convert_value(value)
|
254
|
+
if value.class == Hash
|
255
|
+
Mash.from_hash(value)
|
256
|
+
elsif value.is_a?(Array)
|
257
|
+
value.collect { |e| convert_value(e) }
|
258
|
+
else
|
259
|
+
value
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Copyright:: Copyright (c) Chef Software Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require "concurrent/executors"
|
20
|
+
require "concurrent/future"
|
21
|
+
require "singleton" unless defined?(Singleton)
|
22
|
+
|
23
|
+
module ChefUtils
|
24
|
+
#
|
25
|
+
# This module contains ruby refinements that adds several methods to the Enumerable
|
26
|
+
# class which are useful for parallel processing.
|
27
|
+
#
|
28
|
+
module ParallelMap
|
29
|
+
refine Enumerable do
|
30
|
+
|
31
|
+
# Enumerates through the collection in parallel using the thread pool provided
|
32
|
+
# or the default thread pool. By using the default thread pool this supports
|
33
|
+
# recursively calling the method without deadlocking while using a globally
|
34
|
+
# fixed number of workers. This method supports lazy collections. It returns
|
35
|
+
# synchronously, waiting until all the work is done. Failures are only reported
|
36
|
+
# after the collection has executed and only the first exception is raised.
|
37
|
+
#
|
38
|
+
# (0..).lazy.parallel_map { |i| i*i }.first(5)
|
39
|
+
#
|
40
|
+
# @return [Array] output results
|
41
|
+
#
|
42
|
+
def parallel_map(pool: nil)
|
43
|
+
return self unless block_given?
|
44
|
+
|
45
|
+
pool ||= ChefUtils::DefaultThreadPool.instance.pool
|
46
|
+
|
47
|
+
futures = map do |item|
|
48
|
+
Concurrent::Future.execute(executor: pool) do
|
49
|
+
yield item
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
futures.map(&:value!)
|
54
|
+
end
|
55
|
+
|
56
|
+
# This has the same behavior as parallel_map but returns the enumerator instead of
|
57
|
+
# the return values.
|
58
|
+
#
|
59
|
+
# @return [Enumerable] the enumerable for method chaining
|
60
|
+
#
|
61
|
+
def parallel_each(pool: nil, &block)
|
62
|
+
return self unless block_given?
|
63
|
+
|
64
|
+
parallel_map(pool: pool, &block)
|
65
|
+
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
# The flat_each method is tightly coupled to the usage of parallel_map within the
|
70
|
+
# ChefFS implementation. It is not itself a parallel method, but it is used to
|
71
|
+
# iterate through the 2nd level of nested structure, which is tied to the nested
|
72
|
+
# structures that ChefFS returns.
|
73
|
+
#
|
74
|
+
# This is different from Enumerable#flat_map because that behaves like map.flatten(1) while
|
75
|
+
# this behaves more like flatten(1).each. We need this on an Enumerable, so we have no
|
76
|
+
# Enumerable#flatten method to call.
|
77
|
+
#
|
78
|
+
# [ [ 1, 2 ], [ 3, 4 ] ].flat_each(&block) calls block four times with 1, 2, 3, 4
|
79
|
+
#
|
80
|
+
# [ [ 1, 2 ], [ 3, 4 ] ].flat_map(&block) calls block twice with [1, 2] and [3,4]
|
81
|
+
#
|
82
|
+
def flat_each(&block)
|
83
|
+
map do |value|
|
84
|
+
if value.is_a?(Enumerable)
|
85
|
+
value.each(&block)
|
86
|
+
else
|
87
|
+
yield value
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# The DefaultThreadPool has a fixed thread size and has no
|
95
|
+
# queue of work and the behavior on failure to find a thread is for the
|
96
|
+
# caller to run the work. This contract means that the thread pool can
|
97
|
+
# be called recursively without deadlocking and while keeping the fixed
|
98
|
+
# number of threads (and not exponentially growing the thread pool with
|
99
|
+
# the depth of recursion).
|
100
|
+
#
|
101
|
+
class DefaultThreadPool
|
102
|
+
include Singleton
|
103
|
+
|
104
|
+
DEFAULT_THREAD_SIZE = 10
|
105
|
+
|
106
|
+
# Size of the thread pool, must be set before getting the thread pool or
|
107
|
+
# calling parallel_map/parallel_each. Does not (but could be modified to)
|
108
|
+
# support dynamic resizing. To get fully synchronous behavior set this equal to
|
109
|
+
# zero rather than one since the caller will get work if the threads are
|
110
|
+
# busy.
|
111
|
+
#
|
112
|
+
# @return [Integer] number of threads
|
113
|
+
attr_accessor :threads
|
114
|
+
|
115
|
+
# Memoizing accessor for the thread pool
|
116
|
+
#
|
117
|
+
# @return [Concurrent::ThreadPoolExecutor] the thread pool
|
118
|
+
def pool
|
119
|
+
@pool ||= Concurrent::ThreadPoolExecutor.new(
|
120
|
+
min_threads: threads || DEFAULT_THREAD_SIZE,
|
121
|
+
max_threads: threads || DEFAULT_THREAD_SIZE,
|
122
|
+
max_queue: 0,
|
123
|
+
# "synchronous" redefines the 0 in max_queue to mean 'no queue' instead of 'infinite queue'
|
124
|
+
# it does not mean synchronous execution (no threads) but synchronous offload to the threads.
|
125
|
+
synchronous: true,
|
126
|
+
# this prevents deadlocks on recursive parallel usage
|
127
|
+
fallback_policy: :caller_runs
|
128
|
+
)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|