garcun 0.0.2

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.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/.gitattributes +17 -0
  3. data/.gitignore +197 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +22 -0
  6. data/LICENSE +201 -0
  7. data/README.md +521 -0
  8. data/Rakefile +47 -0
  9. data/garcun.gemspec +83 -0
  10. data/lib/garcon.rb +290 -0
  11. data/lib/garcon/chef/chef_helpers.rb +343 -0
  12. data/lib/garcon/chef/coerce/coercer.rb +134 -0
  13. data/lib/garcon/chef/coerce/coercions/boolean_definitions.rb +34 -0
  14. data/lib/garcon/chef/coerce/coercions/date_definitions.rb +32 -0
  15. data/lib/garcon/chef/coerce/coercions/date_time_definitions.rb +32 -0
  16. data/lib/garcon/chef/coerce/coercions/fixnum_definitions.rb +34 -0
  17. data/lib/garcon/chef/coerce/coercions/float_definitions.rb +32 -0
  18. data/lib/garcon/chef/coerce/coercions/hash_definitions.rb +29 -0
  19. data/lib/garcon/chef/coerce/coercions/integer_definitions.rb +31 -0
  20. data/lib/garcon/chef/coerce/coercions/string_definitions.rb +45 -0
  21. data/lib/garcon/chef/coerce/coercions/time_definitions.rb +32 -0
  22. data/lib/garcon/chef/handler/devreporter.rb +127 -0
  23. data/lib/garcon/chef/log.rb +64 -0
  24. data/lib/garcon/chef/node.rb +100 -0
  25. data/lib/garcon/chef/provider/civilize.rb +209 -0
  26. data/lib/garcon/chef/provider/development.rb +159 -0
  27. data/lib/garcon/chef/provider/download.rb +420 -0
  28. data/lib/garcon/chef/provider/house_keeping.rb +265 -0
  29. data/lib/garcon/chef/provider/node_cache.rb +31 -0
  30. data/lib/garcon/chef/provider/partial.rb +183 -0
  31. data/lib/garcon/chef/provider/recovery.rb +80 -0
  32. data/lib/garcon/chef/provider/zip_file.rb +271 -0
  33. data/lib/garcon/chef/resource/attribute.rb +52 -0
  34. data/lib/garcon/chef/resource/base_dsl.rb +174 -0
  35. data/lib/garcon/chef/resource/blender.rb +140 -0
  36. data/lib/garcon/chef/resource/lazy_eval.rb +66 -0
  37. data/lib/garcon/chef/resource/resource_name.rb +109 -0
  38. data/lib/garcon/chef/secret_bag.rb +204 -0
  39. data/lib/garcon/chef/validations.rb +76 -0
  40. data/lib/garcon/chef_inclusions.rb +151 -0
  41. data/lib/garcon/configuration.rb +138 -0
  42. data/lib/garcon/core_ext.rb +39 -0
  43. data/lib/garcon/core_ext/array.rb +27 -0
  44. data/lib/garcon/core_ext/binding.rb +64 -0
  45. data/lib/garcon/core_ext/boolean.rb +66 -0
  46. data/lib/garcon/core_ext/duration.rb +271 -0
  47. data/lib/garcon/core_ext/enumerable.rb +34 -0
  48. data/lib/garcon/core_ext/file.rb +127 -0
  49. data/lib/garcon/core_ext/filetest.rb +62 -0
  50. data/lib/garcon/core_ext/hash.rb +279 -0
  51. data/lib/garcon/core_ext/kernel.rb +159 -0
  52. data/lib/garcon/core_ext/lazy.rb +222 -0
  53. data/lib/garcon/core_ext/method_access.rb +243 -0
  54. data/lib/garcon/core_ext/module.rb +92 -0
  55. data/lib/garcon/core_ext/nil.rb +53 -0
  56. data/lib/garcon/core_ext/numeric.rb +44 -0
  57. data/lib/garcon/core_ext/object.rb +342 -0
  58. data/lib/garcon/core_ext/pathname.rb +152 -0
  59. data/lib/garcon/core_ext/process.rb +41 -0
  60. data/lib/garcon/core_ext/random.rb +497 -0
  61. data/lib/garcon/core_ext/string.rb +312 -0
  62. data/lib/garcon/core_ext/struct.rb +49 -0
  63. data/lib/garcon/core_ext/symbol.rb +170 -0
  64. data/lib/garcon/core_ext/time.rb +234 -0
  65. data/lib/garcon/exceptions.rb +101 -0
  66. data/lib/garcon/inflections.rb +237 -0
  67. data/lib/garcon/inflections/defaults.rb +79 -0
  68. data/lib/garcon/inflections/inflections.rb +182 -0
  69. data/lib/garcon/inflections/rules_collection.rb +37 -0
  70. data/lib/garcon/secret.rb +271 -0
  71. data/lib/garcon/stash/format.rb +114 -0
  72. data/lib/garcon/stash/journal.rb +226 -0
  73. data/lib/garcon/stash/queue.rb +83 -0
  74. data/lib/garcon/stash/serializer.rb +86 -0
  75. data/lib/garcon/stash/store.rb +435 -0
  76. data/lib/garcon/task.rb +31 -0
  77. data/lib/garcon/task/atomic.rb +151 -0
  78. data/lib/garcon/task/atomic_boolean.rb +127 -0
  79. data/lib/garcon/task/condition.rb +99 -0
  80. data/lib/garcon/task/copy_on_notify_observer_set.rb +154 -0
  81. data/lib/garcon/task/copy_on_write_observer_set.rb +153 -0
  82. data/lib/garcon/task/count_down_latch.rb +92 -0
  83. data/lib/garcon/task/delay.rb +196 -0
  84. data/lib/garcon/task/dereferenceable.rb +144 -0
  85. data/lib/garcon/task/event.rb +119 -0
  86. data/lib/garcon/task/executor.rb +275 -0
  87. data/lib/garcon/task/executor_options.rb +59 -0
  88. data/lib/garcon/task/future.rb +107 -0
  89. data/lib/garcon/task/immediate_executor.rb +84 -0
  90. data/lib/garcon/task/ivar.rb +171 -0
  91. data/lib/garcon/task/lazy_reference.rb +74 -0
  92. data/lib/garcon/task/monotonic_time.rb +69 -0
  93. data/lib/garcon/task/obligation.rb +256 -0
  94. data/lib/garcon/task/observable.rb +101 -0
  95. data/lib/garcon/task/priority_queue.rb +234 -0
  96. data/lib/garcon/task/processor_count.rb +128 -0
  97. data/lib/garcon/task/read_write_lock.rb +304 -0
  98. data/lib/garcon/task/safe_task_executor.rb +58 -0
  99. data/lib/garcon/task/single_thread_executor.rb +97 -0
  100. data/lib/garcon/task/thread_pool/cached.rb +71 -0
  101. data/lib/garcon/task/thread_pool/executor.rb +294 -0
  102. data/lib/garcon/task/thread_pool/fixed.rb +61 -0
  103. data/lib/garcon/task/thread_pool/worker.rb +90 -0
  104. data/lib/garcon/task/timer.rb +44 -0
  105. data/lib/garcon/task/timer_set.rb +194 -0
  106. data/lib/garcon/task/timer_task.rb +377 -0
  107. data/lib/garcon/task/waitable_list.rb +58 -0
  108. data/lib/garcon/utility/ansi.rb +199 -0
  109. data/lib/garcon/utility/at_random.rb +77 -0
  110. data/lib/garcon/utility/crypto.rb +292 -0
  111. data/lib/garcon/utility/equalizer.rb +146 -0
  112. data/lib/garcon/utility/faker/extensions/array.rb +22 -0
  113. data/lib/garcon/utility/faker/extensions/symbol.rb +9 -0
  114. data/lib/garcon/utility/faker/faker.rb +164 -0
  115. data/lib/garcon/utility/faker/faker/company.rb +17 -0
  116. data/lib/garcon/utility/faker/faker/hacker.rb +30 -0
  117. data/lib/garcon/utility/faker/faker/version.rb +3 -0
  118. data/lib/garcon/utility/faker/locales/en-US.yml +83 -0
  119. data/lib/garcon/utility/faker/locales/en.yml +21 -0
  120. data/lib/garcon/utility/file_helper.rb +170 -0
  121. data/lib/garcon/utility/hookers.rb +178 -0
  122. data/lib/garcon/utility/interpolation.rb +90 -0
  123. data/lib/garcon/utility/memstash.rb +364 -0
  124. data/lib/garcon/utility/misc.rb +54 -0
  125. data/lib/garcon/utility/msg_from_god.rb +62 -0
  126. data/lib/garcon/utility/retry.rb +238 -0
  127. data/lib/garcon/utility/timeout.rb +58 -0
  128. data/lib/garcon/utility/uber/builder.rb +91 -0
  129. data/lib/garcon/utility/uber/callable.rb +7 -0
  130. data/lib/garcon/utility/uber/delegates.rb +13 -0
  131. data/lib/garcon/utility/uber/inheritable_attr.rb +37 -0
  132. data/lib/garcon/utility/uber/options.rb +101 -0
  133. data/lib/garcon/utility/uber/uber_version.rb +3 -0
  134. data/lib/garcon/utility/uber/version.rb +33 -0
  135. data/lib/garcon/utility/url_helper.rb +100 -0
  136. data/lib/garcon/utils.rb +29 -0
  137. data/lib/garcon/version.rb +62 -0
  138. data/lib/garcun.rb +24 -0
  139. metadata +680 -0
@@ -0,0 +1,435 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ #
5
+ # Copyright (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ # _____ ______ ____ _____ __ __ __ ____ __ __ __ ___
21
+ # / ___/| T / T/ ___/| T T / ] / T / ]| T T / _]
22
+ # ( \_ | |Y o ( \_ | l | / / Y o | / / | l | / [_
23
+ # \__ Tl_j l_j| |\__ T| _ | / / | |/ / | _ |Y _]
24
+ # / \ | | | | _ |/ \ || | | / \_ | _ / \_ | | || [_
25
+ # \ | | | | | |\ || | | \ || | \ || | || T
26
+ # \___j l__j l__j__j \___jl__j__j \____jl__j__j\____jl__j__jl_____j
27
+ #
28
+ # __ __ ____ _____ __ __ _____ ______ ___ ____ ___
29
+ # | T T / T/ ___/| T T / ___/| T / \ | \ / _]
30
+ # | l |Y o ( \_ | l | ( \_ | |Y Y| D ) / [_
31
+ # | _ || |\__ T| _ | \__ Tl_j l_j| O || / Y _]
32
+ # | | || _ |/ \ || | | / \ | | | | || \ | [_
33
+ # | | || | |\ || | | \ | | | l !| . Y| T
34
+ # l__j__jl__j__j \___jl__j__j \___j l__j \___/ l__j\_jl_____j
35
+ #
36
+
37
+ require 'thread'
38
+ require 'zlib'
39
+ require_relative 'serializer'
40
+ require_relative 'format'
41
+ require_relative 'queue'
42
+ require_relative 'journal'
43
+
44
+ module Garcon
45
+ module Stash
46
+ # Stash::Store contains the public api for Stash. It includes
47
+ # Enumerable for functional goodies like map, each, reduce and friends.
48
+ #
49
+ # @api public
50
+ class Store
51
+ include Enumerable
52
+
53
+ # Set default value, can be a callable
54
+ attr_writer :default
55
+
56
+ # Create a new Stash::Store. The second argument is the default value
57
+ # to store when accessing a previously unset key, this follows the
58
+ # Hash standard.
59
+ #
60
+ # @param [String] file
61
+ # The path to the Stash Store file.
62
+ #
63
+ # @param [Hash] opts
64
+ # Options hash for creating a new stash.
65
+ #
66
+ # @option opts [Class] :serializer
67
+ # Serializer class
68
+ #
69
+ # @option opts [Class] :format
70
+ # Format class
71
+ #
72
+ # @option opts [Object] :default
73
+ # Default value
74
+ #
75
+ # @yield [key] a block that will return the default value to store.
76
+ #
77
+ # @yieldparam [String] key the key to be stored.
78
+ #
79
+ def initialize(file, opts = {}, &block)
80
+ opts = {
81
+ serializer: opts.fetch(:serializer, Serializer::Default),
82
+ format: opts.fetch(:format, Format),
83
+ default: opts.fetch(:default, nil)
84
+ }
85
+ @format = (opts[:format]).new
86
+ @serializer = (opts[:serializer]).new
87
+ @table = Hash.new(&method(:hash_default))
88
+ @journal = Journal.new(file, @format, @serializer) do |record|
89
+ if !record
90
+ @table.clear
91
+ elsif record.size == 1
92
+ @table.delete(record.first)
93
+ else
94
+ @table[record.first] = @serializer.load(record.last)
95
+ end
96
+ end
97
+
98
+ @default = block ? block : opts[:default]
99
+ @mutex = Mutex.new
100
+ @@stashs_mutex.synchronize { @@stashs << self }
101
+ end
102
+
103
+ # Stash store file name.
104
+ #
105
+ # @return [String]
106
+ # stash store file name
107
+ #
108
+ def file
109
+ @journal.file
110
+ end
111
+
112
+ # Return default value belonging to key.
113
+ #
114
+ # @param [Object] key
115
+ # the default value to retrieve.
116
+ #
117
+ # @return [Object]
118
+ # value the default value
119
+ #
120
+ def default(key = nil)
121
+ @table.default(@serializer.key_for(key))
122
+ end
123
+
124
+ # Retrieve a value at key from the stash. If the default value was
125
+ # specified when this stash was created, that value will be set and
126
+ # returned. Aliased as `#get`.
127
+ #
128
+ # @param [Object] key
129
+ # the value to retrieve from the stash
130
+ #
131
+ # @return [Object]
132
+ # the value
133
+ #
134
+ def [](key)
135
+ @table[@serializer.key_for(key)]
136
+ end
137
+ alias_method :get, '[]'
138
+
139
+ # Set a key in the stash to be written at some future date. If the data
140
+ # needs to be persisted immediately, call `#store.set(key, value, true)`.
141
+ #
142
+ # @param [Object] key
143
+ # the key of the storage slot in the stash
144
+ #
145
+ # @param [Object] value
146
+ # the value to store
147
+ #
148
+ # @return [Object]
149
+ # the value
150
+ #
151
+ def []=(key, value)
152
+ key = @serializer.key_for(key)
153
+ @journal << [key, value]
154
+ @table[key] = value
155
+ end
156
+ alias_method :set, '[]='
157
+
158
+ # Flushes data immediately to disk.
159
+ #
160
+ # @param [Object] key
161
+ # the key of the storage slot in the stash
162
+ #
163
+ # @param [Object] value
164
+ # the value to store
165
+ #
166
+ # @return [Object]
167
+ # the value
168
+ #
169
+ def set!(key, value)
170
+ set(key, value)
171
+ flush
172
+ value
173
+ end
174
+
175
+ # Delete a key from the stash.
176
+ #
177
+ # @param [Object] key
178
+ # the key of the storage slot in the stash
179
+ #
180
+ # @return [Object]
181
+ # the value
182
+ #
183
+ def delete(key)
184
+ key = @serializer.key_for(key)
185
+ @journal << [key]
186
+ @table.delete(key)
187
+ end
188
+
189
+ # Immediately delete the key on disk.
190
+ #
191
+ # @param [Object] key
192
+ # the key of the storage slot in the stash
193
+ #
194
+ # @return [Object]
195
+ # the value
196
+ #
197
+ def delete!(key)
198
+ value = delete(key)
199
+ flush
200
+ value
201
+ end
202
+
203
+ # Update stash with hash (fast batch update).
204
+ #
205
+ # @param [Hash] hash
206
+ # the key/value hash
207
+ #
208
+ # @return [Stash] self
209
+ #
210
+ def update(hash)
211
+ shash = {}
212
+ hash.each { |key, value| shash[@serializer.key_for(key)] = value }
213
+ @journal << shash
214
+ @table.update(shash)
215
+ self
216
+ end
217
+
218
+ # Updata stash and flush data to disk.
219
+ #
220
+ # @param [Hash] hash
221
+ # the key/value hash
222
+ #
223
+ # @return [Stash] self
224
+ #
225
+ def update!(hash)
226
+ update(hash)
227
+ @journal.flush
228
+ end
229
+
230
+ # Does this stash have this key?
231
+ #
232
+ #
233
+ # @param [Object] key
234
+ # the key to check if the stash has it
235
+ #
236
+ # @return [Boolean]
237
+ #
238
+ def has_key?(key)
239
+ @table.has_key?(@serializer.key_for(key))
240
+ end
241
+ alias_method :key?, :has_key?
242
+ alias_method :include?, :has_key?
243
+ alias_method :member?, :has_key?
244
+
245
+ # Does this stash have this value?
246
+ #
247
+ # @param [Object] value
248
+ # the value to check if the Stash has it
249
+ #
250
+ # @return [Boolean]
251
+ #
252
+ def has_value?(value)
253
+ @table.has_value?(value)
254
+ end
255
+ alias_method :value?, :has_value?
256
+
257
+ # Return the number of stored items.
258
+ #
259
+ # @return [Fixnum]
260
+ #
261
+ def size
262
+ @table.size
263
+ end
264
+ alias_method :length, :size
265
+
266
+ # Utility method that will return the size of the stash in bytes, useful
267
+ # for determining when to compact.
268
+ #
269
+ # @return [Fixnum]
270
+ #
271
+ def bytesize
272
+ @journal.bytesize
273
+ end
274
+
275
+ # Counter of how many records are in the journal.
276
+ #
277
+ # @return [Fixnum]
278
+ #
279
+ def logsize
280
+ @journal.size
281
+ end
282
+
283
+ # Return true if stash is empty.
284
+ #
285
+ # @return [Boolean]
286
+ #
287
+ def empty?
288
+ @table.empty?
289
+ end
290
+
291
+ # Iterate over the key, value pairs in the stash.
292
+ #
293
+ # @yield [key, value] block
294
+ # the iterator for each key value pair
295
+ #
296
+ # @yieldparam key the key.
297
+ #
298
+ # @yieldparam value the value from the stash.
299
+ #
300
+ def each(&block)
301
+ @table.each(&block)
302
+ end
303
+
304
+ # Return the keys in the stash.
305
+ #
306
+ # @return [Array<String>]
307
+ #
308
+ def keys
309
+ @table.keys
310
+ end
311
+
312
+ # Flush all changes to disk.
313
+ #
314
+ # @return [Stash] self
315
+ #
316
+ def flush
317
+ @journal.flush
318
+ self
319
+ end
320
+
321
+ # Sync the stash with what is on disk, by first flushing changes, and then
322
+ # loading the new records if necessary.
323
+ #
324
+ # @return [Stash] self
325
+ #
326
+ def load
327
+ @journal.load
328
+ self
329
+ end
330
+ alias_method :sunrise, :load
331
+
332
+ # Lock the stash for an exclusive commit across processes and threads.
333
+ # @note This method performs an expensive locking over process boundaries.
334
+ # If you want to synchronize only between threads, use `#synchronize`.
335
+ # @see #synchronize
336
+ #
337
+ # @yield a block where every change to the stash is synced
338
+ # @yieldparam [Stash] stash
339
+ # @return result of the block
340
+ #
341
+ def lock
342
+ synchronize { @journal.lock { yield self } }
343
+ end
344
+
345
+ # Synchronize access to the stash from multiple threads.
346
+ # @note Stash is not thread safe, if you want to access it from multiple
347
+ # threads, all accesses have to be in the #synchronize block.
348
+ # @see #lock
349
+ #
350
+ # @yield a block where every change to the stash is synced
351
+ #
352
+ # @yieldparam [Stash] stash
353
+ #
354
+ # @return result of the block
355
+ #
356
+ def synchronize
357
+ @mutex.synchronize { yield self }
358
+ end
359
+
360
+ # Remove all keys and values from the stash.
361
+ #
362
+ # @return [Stash] self
363
+ #
364
+ def clear
365
+ @table.clear
366
+ @journal.clear
367
+ self
368
+ end
369
+
370
+ # Compact the stash to remove stale commits and reduce the file size.
371
+ #
372
+ # @return [Stash] self
373
+ #
374
+ def compact
375
+ @journal.compact { @table }
376
+ self
377
+ end
378
+
379
+ # Close the stash for reading and writing.
380
+ #
381
+ # @return nil
382
+ #
383
+ def close
384
+ @journal.close
385
+ @@stashs_mutex.synchronize { @@stashs.delete(self) }
386
+ nil
387
+ end
388
+
389
+ # Check to see if we've already closed the stash.
390
+ #
391
+ # @return [Boolean]
392
+ #
393
+ def closed?
394
+ @journal.closed?
395
+ end
396
+
397
+ private # P R O P R I E T À P R I V A T A Vietato L'accesso
398
+
399
+ # @private
400
+ @@stashs = []
401
+
402
+ # @private
403
+ @@stashs_mutex = Mutex.new
404
+
405
+ # A handler that will ensure that stashs are closed and synced when the
406
+ # current process exits.
407
+ #
408
+ # @private
409
+ def self.exit_handler
410
+ loop do
411
+ stash = @@stashs_mutex.synchronize { @@stashs.shift }
412
+ break unless stash
413
+ warn "Stash #{stash.file} was not closed, state might be inconsistent"
414
+ begin
415
+ stash.close
416
+ rescue Exception => e
417
+ warn "Failed to close stash store: #{e.message}"
418
+ end
419
+ end
420
+ end
421
+
422
+ at_exit { Garcon::Stash::Store.exit_handler }
423
+
424
+ # The block used in @table for new records.
425
+ #
426
+ def hash_default(_, key)
427
+ if @default != nil
428
+ value = @default.respond_to?(:call) ? @default.call(key) : @default
429
+ @journal << [key, value]
430
+ @table[key] = value
431
+ end
432
+ end
433
+ end
434
+ end
435
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Author: Stefano Harding <riddopic@gmail.com>
4
+ # License: Apache License, Version 2.0
5
+ # Copyright: (C) 2014-2015 Stefano Harding
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require_relative 'task/atomic_boolean'
21
+ require_relative 'task/condition'
22
+ require_relative 'task/count_down_latch'
23
+ require_relative 'task/event'
24
+ require_relative 'task/future'
25
+ require_relative 'task/lazy_reference'
26
+ require_relative 'task/read_write_lock'
27
+ require_relative 'task/observable'
28
+ require_relative 'task/processor_count'
29
+ require_relative 'task/thread_pool/cached'
30
+ require_relative 'task/thread_pool/fixed'
31
+ require_relative 'task/timer_set'