Module: R10K::Util::Purgeable Abstract
- Included in:
- Environment::Git, Puppetfile, Basedir
- Defined in:
- lib/r10k/util/purgeable.rb
Overview
Classes using this mixin need to implement #managed_directory and #desired_contents
Mixin for purging stale directory contents.
Instance Method Summary collapse
-
#current_contents(recurse) ⇒ Array<String>
The present directory entries in ‘self.managed_directories`.
-
#desired_contents ⇒ Array<String>
abstract
The full paths to all the content this object is managing.
- #logger ⇒ Log4r::Logger abstract
-
#managed_directories ⇒ Array<String>
abstract
The paths to the directories to be purged.
-
#pending_contents(recurse) ⇒ Array<String>
Directory contents that are expected but not present.
-
#purge!(opts = {}) ⇒ Object
Forcibly remove all unmanaged content in ‘self.managed_directories`.
-
#stale_contents(recurse, exclusions, whitelist) ⇒ Array<String>
Directory contents that are present but not expected.
Instance Method Details
#current_contents(recurse) ⇒ Array<String>
Returns The present directory entries in ‘self.managed_directories`.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/r10k/util/purgeable.rb', line 27 def current_contents(recurse) dirs = self.managed_directories dirs.flat_map do |dir| if recurse glob_exp = File.join(dir, '**', '{*,.[^.]*}') else glob_exp = File.join(dir, '*') end Dir.glob(glob_exp) end end |
#desired_contents ⇒ Array<String>
Including classes must implement this method to list the expected filenames of managed_directories
Returns The full paths to all the content this object is managing.
|
# File 'lib/r10k/util/purgeable.rb', line 16
|
#logger ⇒ Log4r::Logger
Including classes must provide a logger method
|
# File 'lib/r10k/util/purgeable.rb', line 12
|
#managed_directories ⇒ Array<String>
Including classes must implement this method to return an array of paths that can be purged
Returns The paths to the directories to be purged.
|
# File 'lib/r10k/util/purgeable.rb', line 21
|
#pending_contents(recurse) ⇒ Array<String>
Returns Directory contents that are expected but not present.
42 43 44 |
# File 'lib/r10k/util/purgeable.rb', line 42 def pending_contents(recurse) desired_contents - current_contents(recurse) end |
#purge!(opts = {}) ⇒ Object
Forcibly remove all unmanaged content in ‘self.managed_directories`
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/r10k/util/purgeable.rb', line 62 def purge!(opts={}) recurse = opts[:recurse] || false whitelist = opts[:whitelist] || [] exclusions = self.respond_to?(:purge_exclusions) ? purge_exclusions : [] stale = stale_contents(recurse, exclusions, whitelist) if stale.empty? logger.debug1 _("No unmanaged contents in %{managed_dirs}, nothing to purge") % {managed_dirs: managed_directories.join(', ')} else stale.each do |fpath| begin FileUtils.rm_r(fpath, :secure => true) logger.info _("Removing unmanaged path %{path}") % {path: fpath} rescue Errno::ENOENT # Don't log on ENOENT since we may encounter that from recursively deleting # this item's parent earlier in the purge. rescue logger.debug1 _("Unable to remove unmanaged path: %{path}") % {path: fpath} end end end end |
#stale_contents(recurse, exclusions, whitelist) ⇒ Array<String>
Returns Directory contents that are present but not expected.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/r10k/util/purgeable.rb', line 47 def stale_contents(recurse, exclusions, whitelist) fn_match_opts = File::FNM_PATHNAME | File::FNM_DOTMATCH (current_contents(recurse) - desired_contents).reject do |item| if exclusion_match = exclusions.find { |ex_item| (ex_item == item) || File.fnmatch?(ex_item, item, fn_match_opts) } logger.debug2 _("Not purging %{item} due to internal exclusion match: %{exclusion_match}") % {item: item, exclusion_match: exclusion_match} elsif whitelist_match = whitelist.find { |wl_item| (wl_item == item) || File.fnmatch?(wl_item, item, fn_match_opts) } logger.debug _("Not purging %{item} due to whitelist match: %{whitelist_match}") % {item: item, whitelist_match: whitelist_match} end !!exclusion_match || !!whitelist_match end end |