garcun 0.0.8 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eeeedb355ad159a8faeb0d4c401c71ef0dc166fb
4
- data.tar.gz: 1b19a2c1df2d56c69d36efff9d2abcabcb2f2742
3
+ metadata.gz: e52dcfec4d7603a91b49130dae8bb0feff98a5c0
4
+ data.tar.gz: edf505348e58014492bae2d9ca13f6daf3be10b1
5
5
  SHA512:
6
- metadata.gz: b9898663cc3d33bc53c8984e04568f79cacee5bfa74d9fbfafadcab29b788f86b7219997ae2499b5eed19223fad6dea205972d3d940d416ec7ce01ebb85d283f
7
- data.tar.gz: 081221281ad4578e9d0da6cb89044d3432f05ce8997febe54c2295b4f316232f26b57f968b5287a40f79527805ad1590c0d351ce5ca806a5ff59f52b2a1c5579
6
+ metadata.gz: 64afc0d6acc4babd462e53cde38105b20806da93727c2979df21a101d796c30958c2fefd1c8d9b1409e1bb9e61dff4f2d5d60aba3fcf6ed292594a3d854f5754
7
+ data.tar.gz: 572ba85781a08f15c0888b48ca17c431adeb648cf82d56cfb08333c8e6052378856646062183a59bb5d55d879d202d839583f3a16d95718a4f3c5229465488ba
@@ -25,6 +25,7 @@ module Garcon
25
25
  extend self
26
26
 
27
27
  include Chef::Mixin::ShellOut
28
+ include Chef::DSL::IncludeRecipe
28
29
 
29
30
  def chef_run_context
30
31
  ::Chef::RunContext.new(chef_node, nil, nil)
@@ -0,0 +1,439 @@
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 'fileutils'
21
+ require 'garcon'
22
+ require 'find'
23
+
24
+ class Chef
25
+ class Resource
26
+ # Extracts an archive file by determining the extention of the file and
27
+ # sending it unzip method or extract. The source can be a file path or
28
+ # a URL, for the later the file is downloaded in the Chef cache path. By
29
+ # default the archive file will not be deleted. To have Chef remove the
30
+ # file after it has been extracted set `remove_after true` on the
31
+ # resource.
32
+ #
33
+ # @example
34
+ # archive 'file.tar.gz' do
35
+ # source 'http://server.example.com/file.tar.gz'
36
+ # owner 'tomcat'
37
+ # group 'tomcat'
38
+ # overwrite true
39
+ # remove_after true
40
+ # end
41
+ #
42
+ class Archive < Chef::Resource
43
+ include Garcon
44
+
45
+ # Chef attributes
46
+ identity_attr :path
47
+ provides :archive
48
+ state_attrs :checksum, :owner, :group, :mode
49
+
50
+ # Actions
51
+ actions :zip, :extract
52
+ default_action :extract
53
+
54
+ # Attributes
55
+ attribute :path,
56
+ kind_of: String,
57
+ name_attribute: true
58
+ attribute :source,
59
+ kind_of: [String, URI::HTTP],
60
+ callbacks: source_callbacks,
61
+ required: true
62
+ attribute :remove_after,
63
+ kind_of: [TrueClass, FalseClass],
64
+ default: false
65
+ attribute :overwrite,
66
+ kind_of: [TrueClass, FalseClass],
67
+ default: false
68
+ attribute :checksum,
69
+ kind_of: String,
70
+ regex: /^[0-9a-f]{32}$|^[a-zA-Z0-9]{40,64}$/
71
+ attribute :owner,
72
+ kind_of: [String, Integer],
73
+ regex: Chef::Config[:user_valid_regex]
74
+ attribute :group,
75
+ kind_of: [String, Integer],
76
+ regex: Chef::Config[:group_valid_regex]
77
+ attribute :mode,
78
+ kind_of: Integer,
79
+ regex: /^0?\d{3,4}$/
80
+ attribute :check_cert,
81
+ kind_of: [TrueClass, FalseClass],
82
+ default: true
83
+ attribute :header,
84
+ kind_of: String
85
+ attribute :options,
86
+ kind_of: [String, Array, Symbol],
87
+ default: Array.new
88
+ end
89
+ end
90
+
91
+ class Provider
92
+ class Archive < Chef::Provider
93
+ include Chef::Mixin::EnforceOwnershipAndPermissions
94
+ include Chef::DSL::IncludeRecipe
95
+ include Garcon
96
+
97
+ def initialize(new_resource, run_context)
98
+ super
99
+ __zip__ unless defined?(Zip)
100
+ end
101
+
102
+ # Shortcut to new_resource.
103
+ #
104
+ alias_method :r, :new_resource
105
+
106
+ # Boolean indicating if WhyRun is supported by this provider.
107
+ #
108
+ # @return [TrueClass, FalseClass]
109
+ #
110
+ # @api private
111
+ def whyrun_supported?
112
+ true
113
+ end
114
+
115
+ # Load and return the current resource.
116
+ #
117
+ # @return [Chef::Provider]
118
+ #
119
+ # @api private
120
+ def load_current_resource
121
+ @current_resource ||= Chef::Resource::Archive.new(r.name)
122
+ @current_resource
123
+ end
124
+
125
+ def action_extract
126
+ converge_by "Extracting #{r.source} to #{r.path}" do
127
+ extract
128
+ do_acl_changes
129
+ ::File.unlink(cached_file) if r.remove_after
130
+ end
131
+ r.updated_by_last_action(true)
132
+ end
133
+
134
+ def action_zip
135
+ if ::File.exists?(r.path) && !r.overwrite
136
+ Chef::Log.info "#{r.path} already exists - nothing to do"
137
+ else
138
+ ::File.unlink(r.path) if ::File.exists?(r.path)
139
+ if ::File.directory?(r.source)
140
+ converge_by "Zip #{r.source}" do
141
+ z = Zip::File.new(r.path, true)
142
+ Find.find(r.source) do |f|
143
+ next if f == r.source
144
+ zip_fname = f.sub(r.source, '')
145
+ z.add(zip_fname, f)
146
+ end
147
+ z.close
148
+ do_acl_changes
149
+ r.updated_by_last_action(true)
150
+ end
151
+ else
152
+ Chef::Log.warn 'A valid directory must be specified for ziping.'
153
+ end
154
+ end
155
+ end
156
+
157
+ # Implementation components *should* follow symlinks when managing access
158
+ # control (e.g., use chmod instead of lchmod even if the path we're
159
+ # managing is a symlink).
160
+ #
161
+ def manage_symlink_access?
162
+ false
163
+ end
164
+
165
+ private # P R O P R I E T À P R I V A T A Vietato L'accesso
166
+
167
+ # Change file ownership and mode
168
+ #
169
+ # @return [undefined]
170
+ #
171
+ # @api private
172
+ def do_acl_changes
173
+ if access_controls.requires_changes?
174
+ converge_by(access_controls.describe_changes) do
175
+ access_controls.set_all
176
+ end
177
+ end
178
+ end
179
+
180
+ def archive_formats
181
+ %w[.zip .tar .gz .bz2 .tar.gz .tar.bz2]
182
+ end
183
+
184
+ # Extracts an archive file by determining the extention of the file and
185
+ # sending it unzip method or extract. The source can be a file path or
186
+ # a URL, for the later the file is downloaded in the Chef cache path. By
187
+ # default the archive file will not be deleted. To have Chef remove the
188
+ # file after it has been extracted set `remove_after true` on the
189
+ # resource.
190
+ #
191
+ # @example
192
+ # archive 'file.tar.gz' do
193
+ # source 'http://server.example.com/file.tar.gz'
194
+ # remove_after true
195
+ # end
196
+ #
197
+ # @return[undefined]
198
+ #
199
+ def extract
200
+ src = ::File.extname(::File.basename r.source)
201
+ Chef::Log.warn ''
202
+ Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
203
+ Chef::Log.warn " The source file: #{r.source}"
204
+ Chef::Log.warn "The file extention is: #{src}"
205
+ Chef::Log.warn "Reckon it's a archive? #{archive_formats.include? src}"
206
+ Chef::Log.warn "Whatcha going to do with that thing then?"
207
+ Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
208
+ Chef::Log.warn ''
209
+
210
+ if archive_formats.include? src
211
+ if archive_formats.include?(src) && src =~ /^.zip$/i
212
+ Chef::Log.warn "Unzip the mother fucker!"
213
+ elsif src =~ /^(.tar.gz|.tar|.gz|.bz2|.tar.bz2)$/
214
+ Chef::Log.warn "Liberate the archive!!!"
215
+ else
216
+ Chef::Log.warn "Fuck dude, can't do shit with it."
217
+ end
218
+ else
219
+ Chef::Log.warn 'Aint for format I can fuck with dude, stick it'
220
+ end
221
+ Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
222
+ Chef::Log.warn ''
223
+ Chef::Log.warn '- - - - - - - - - - - - - - - - - - - - - - - - - - - -'
224
+
225
+ if archive_formats.include? src
226
+ if src =~ /^.zip$/i
227
+ Chef::Log.info "Extracting Zip file #{r.source} to #{r.path}"
228
+ elsif src =~ /^(.tar.gz|.tar|.gz|.bz2|.tar.bz2)$/
229
+ Chef::Log.info "Extracting archive file #{r.source} to #{r.path}"
230
+ updated = extract(r.source, r.path, Array(r.options))
231
+ end
232
+ else
233
+ Chef::Log.info "Copying cached file into #{r.path}"
234
+ FileUtils.cp cached_file, r.path
235
+ end
236
+ end
237
+
238
+ # Unzip the archive.
239
+ #
240
+ # @return[undefined]
241
+ #
242
+ def unzip
243
+ converge_by "Unzip #{r.source} to #{r.path}" do
244
+ Zip::File.open(cached_file) do |zip|
245
+ zip.each do |entry|
246
+ path = ::File.join(r.path, entry.name)
247
+ FileUtils.mkdir_p(::File.dirname(path))
248
+ if r.overwrite && ::File.exist?(path) && !::File.directory?(path)
249
+ FileUtils.rm(path)
250
+ end
251
+ zip.extract(entry, path)
252
+ end
253
+ end
254
+ do_acl_changes
255
+ ::File.unlink(cached_file) if r.remove_after
256
+ r.updated_by_last_action(true)
257
+ end
258
+ end
259
+
260
+ def options_map
261
+ {
262
+ owner: ::Archive::EXTRACT_OWNER,
263
+ permissions: ::Archive::EXTRACT_PERM,
264
+ time: ::Archive::EXTRACT_TIME,
265
+ no_overwrite: ::Archive::EXTRACT_NO_OVERWRITE,
266
+ acl: ::Archive::EXTRACT_ACL,
267
+ fflags: ::Archive::EXTRACT_FFLAGS,
268
+ extended_information: ::Archive::EXTRACT_XATTR,
269
+ xattr: ::Archive::EXTRACT_XATTR,
270
+ }
271
+ end
272
+
273
+ # Extract a tar, gz, bz2 archives file.
274
+ #
275
+ # @return[undefined]
276
+ #
277
+ def extract
278
+ require 'archive'
279
+ r.options ||= Array.new
280
+ r.options.collect! { |option| options_map[option] }.compact!
281
+ Dir.chdir(r.path) do
282
+ archive = ::Archive.new(cached_file)
283
+ archive_files = archive.map { |entry| entry.path }
284
+ existing, missing = archive_files.partition do |file|
285
+ ::File.exist?(::File.join(r.path, file))
286
+ end
287
+ current_times = existing.reduce({}) do |times, file|
288
+ times[file] = ::File.mtime(file)
289
+ times
290
+ end
291
+ archive.extract(extract: r.options.reduce(:|))
292
+ unless missing.empty?
293
+ still_missing = missing.reject { |f| ::File.exist?(f) }
294
+ return true if still_missing.length < missing.length
295
+ end
296
+ changed_files = current_times.select do |file, time|
297
+ ::File.mtime(file) != time
298
+ end
299
+
300
+ return true unless changed_files.empty?
301
+ end
302
+
303
+ false
304
+ end
305
+
306
+ # Cache a file locally in Chef::Config[:file_cache_path].
307
+ #
308
+ # @note The file is gargbage collected at the end of a run.
309
+ #
310
+ # @return [String]
311
+ # Path to the cached file.
312
+ #
313
+ def cached_file
314
+ if r.source =~ URI::ABS_URI &&
315
+ %w[ftp http https].include?(URI.parse(r.source).scheme)
316
+ file = ::File.basename(URI.unescape(URI.parse(r.source).path))
317
+ cache_file_path = file_cache_path(file)
318
+
319
+ d ||= Chef::Resource::Download.new(cache_file_path, run_context)
320
+ d.backup false
321
+ d.source r.source
322
+ d.owner r.owner if r.owner
323
+ d.group r.group if r.group
324
+ d.header r.header if r.header
325
+ d.checksum r.checksum if r.checksum
326
+ d.check_cert r.check_cert
327
+ d.run_action :create
328
+ else
329
+ cache_file_path = r.source
330
+ end
331
+
332
+ cache_file_path
333
+ end
334
+
335
+ # Safely install and require the zip Gem
336
+ #
337
+ # @return [undefined]
338
+ #
339
+ # @api private
340
+ def __zip__
341
+ __libarchive__
342
+ require 'zip' unless defined?(Zip)
343
+ rescue LoadError
344
+ g = Chef::Resource::ChefGem.new('zip', run_context)
345
+ g.compile_time(false) if respond_to?(:compile_time)
346
+ g.run_action :install
347
+ require 'zip'
348
+ end
349
+
350
+ # Install libarchive package and Gem.
351
+ #
352
+ # @return [undefined]
353
+ #
354
+ # @api private
355
+ def __libarchive__
356
+ recipe_eval do
357
+ run_context.include_recipe 'build-essential::default'
358
+ end
359
+
360
+ p = Chef::Resource::Package.new('libarchive', run_context)
361
+ p.only_if { node[:platform_family] == 'rhel' }
362
+ p.run_action :install
363
+
364
+ p = Chef::Resource::Package.new('libarchive-dev', run_context)
365
+ p.package_name libarchive
366
+ p.run_action :install
367
+
368
+ g = Chef::Resource::ChefGem.new('libarchive-ruby', run_context)
369
+ g.compile_time(false) if respond_to?(:compile_time)
370
+ g.version '0.0.3'
371
+ g.run_action :install
372
+ rescue LoadError
373
+ recipe_eval do
374
+ run_context.include_recipe 'build-essential::default'
375
+ end
376
+ g.run_action :install
377
+ end
378
+
379
+ def libarchive
380
+ node[:platform_family] == 'rhel' ? 'libarchive-devel' : 'libarchive-dev'
381
+ end
382
+ end
383
+ end
384
+ end
385
+
386
+ # Chef::Platform mapping for resource and providers
387
+ #
388
+ # @return [undefined]
389
+ #
390
+ # @api private.
391
+ Chef::Platform.set(
392
+ platform: :amazon,
393
+ resource: :archive,
394
+ provider: Chef::Provider::Archive
395
+ )
396
+
397
+ # Chef::Platform mapping for resource and providers
398
+ #
399
+ # @return [undefined]
400
+ #
401
+ # @api private.
402
+ Chef::Platform.set(
403
+ platform: :centos,
404
+ resource: :archive,
405
+ provider: Chef::Provider::Archive
406
+ )
407
+
408
+ # Chef::Platform mapping for resource and providers
409
+ #
410
+ # @return [undefined]
411
+ #
412
+ # @api private.
413
+ Chef::Platform.set(
414
+ platform: :oracle,
415
+ resource: :archive,
416
+ provider: Chef::Provider::Archive
417
+ )
418
+
419
+ # Chef::Platform mapping for resource and providers
420
+ #
421
+ # @return [undefined]
422
+ #
423
+ # @api private.
424
+ Chef::Platform.set(
425
+ platform: :redhat,
426
+ resource: :archive,
427
+ provider: Chef::Provider::Archive
428
+ )
429
+
430
+ # Chef::Platform mapping for resource and providers
431
+ #
432
+ # @return [undefined]
433
+ #
434
+ # @api private.
435
+ Chef::Platform.set(
436
+ platform: :scientific,
437
+ resource: :archive,
438
+ provider: Chef::Provider::Archive
439
+ )
@@ -61,6 +61,7 @@ class Chef
61
61
 
62
62
  class Provider
63
63
  class Civilize < Chef::Provider
64
+ include Chef::DSL::IncludeRecipe
64
65
  include Garcon
65
66
 
66
67
  # Shortcut to new_resource.
@@ -163,9 +164,9 @@ class Chef
163
164
  users = if r.dotfiles.is_a?(TrueClass)
164
165
  Array('root')
165
166
  elsif r.dotfiles.respond_to?(:to_ary)
166
- users = r.dotfiles
167
+ r.dotfiles
167
168
  elsif r.dotfiles.respond_to?(:to_str)
168
- users = Array(r.dotfiles)
169
+ Array(r.dotfiles)
169
170
  end
170
171
 
171
172
  users.each do |user|
@@ -40,6 +40,7 @@ class Chef
40
40
 
41
41
  class Provider
42
42
  class Development < Chef::Provider
43
+ include Chef::DSL::IncludeRecipe
43
44
  include Garcon
44
45
 
45
46
  # Shortcut to new_resource.
@@ -319,8 +319,8 @@ class Chef
319
319
 
320
320
  def yumrepo(action = :nothing)
321
321
  y = Chef::Resource::YumRepository.new('garcon', run_context)
322
- y.mirrorlist Repos.mirrorlist
323
- y.gpgkey Repos.gpgkey
322
+ y.mirrorlist node[:garcon][:repo][:url]
323
+ y.gpgkey node[:garcon][:repo][:gpg]
324
324
  y.gpgcheck true
325
325
  y.run_action action
326
326
  end
@@ -329,7 +329,7 @@ class Chef
329
329
  # Seems Chef::Resource::YumRepository action :delete is foobared, so
330
330
  # nuke the repo another way.
331
331
  Future.execute do
332
- ::File.unlink('/etc/yum.repos.d/garcon.repo')
332
+ ::File.unlink('/etc/yum.repos.d/garcon.repo')
333
333
  shell_out!('yum clean all && yum -q makecache')
334
334
  Chef::Provider::Package::Yum::YumCache.instance.reload
335
335
  end
@@ -56,14 +56,14 @@ module Garcon
56
56
  false
57
57
  end
58
58
 
59
- # Hook called when module is included, extends a descendant with class
60
- # and instance methods.
59
+ # Hook called when module is included.
61
60
  #
62
61
  # @param [Module] descendant
63
- # The module or class including Garcon::Resource::Validations
62
+ # The including module or class.
64
63
  #
65
64
  # @return [self]
66
65
  #
66
+ # @api private
67
67
  def included(descendant)
68
68
  super
69
69
  descendant.extend ClassMethods
@@ -135,6 +135,7 @@ unless Chef::Recipe.ancestors.include?(Garcon::Interpolation)
135
135
  Chef::Provider.send(:include, Garcon::Interpolation)
136
136
  end
137
137
 
138
+ require_relative 'chef/provider/archive'
138
139
  require_relative 'chef/provider/civilize'
139
140
  require_relative 'chef/provider/development'
140
141
  require_relative 'chef/provider/download'
@@ -27,7 +27,7 @@ module Garcon
27
27
  module Version
28
28
  MAJOR = 0
29
29
  MINOR = 0
30
- PATCH = 8
30
+ PATCH = 9
31
31
 
32
32
  # Returns a version string by joining MAJOR, MINOR, and PATCH with '.'
33
33
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garcun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Harding
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-04 00:00:00.000000000 Z
11
+ date: 2015-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef
@@ -537,6 +537,7 @@ files:
537
537
  - lib/garcon/chef/coerce/coercions/time_definitions.rb
538
538
  - lib/garcon/chef/log.rb
539
539
  - lib/garcon/chef/node.rb
540
+ - lib/garcon/chef/provider/archive.rb
540
541
  - lib/garcon/chef/provider/civilize.rb
541
542
  - lib/garcon/chef/provider/development.rb
542
543
  - lib/garcon/chef/provider/download.rb