Module: R10K::Util::Purgeable Abstract

Included in:
Puppetfile, Source::Git, Source::SVN, Basedir
Defined in:
lib/r10k/util/purgeable.rb

Overview

This module is abstract.

Classes using this mixin need to implement #managed_directory and #desired_contents

Mixin for purging stale directory contents.

Instance Method Summary collapse

Instance Method Details

#current_contentsArray<String>

Returns The present directory entries in ‘self.managed_directory`.

Returns:

  • (Array<String>)

    The present directory entries in ‘self.managed_directory`



27
28
29
30
31
32
33
34
# File 'lib/r10k/util/purgeable.rb', line 27

def current_contents
  dir = self.managed_directory
  glob_exp = File.join(dir, '*')

  Dir.glob(glob_exp).map do |fname|
    File.basename(fname)
  end
end

#desired_contentsArray<String>

This method is abstract.

Including classes must implement this method to list the expected filenames of managed_directory

Returns A list of directory contents that should be present.

Returns:

  • (Array<String>)

    A list of directory contents that should be present



# File 'lib/r10k/util/purgeable.rb', line 16

#loggerLog4r::Logger

This method is abstract.

Including classes must provide a logger method

Returns:

  • (Log4r::Logger)


# File 'lib/r10k/util/purgeable.rb', line 12

#managed_directoryString

This method is abstract.

Including classes must implement this method to return the path to the directory that can be purged

Returns The path to the directory to be purged.

Returns:

  • (String)

    The path to the directory to be purged



# File 'lib/r10k/util/purgeable.rb', line 21

#pending_contentsArray<String>

Returns Directory contents that are expected but not present.

Returns:

  • (Array<String>)

    Directory contents that are expected but not present



37
38
39
# File 'lib/r10k/util/purgeable.rb', line 37

def pending_contents
  desired_contents - current_contents
end

#purge!Object

Forcibly remove all unmanaged content in ‘self.managed_directory`



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/r10k/util/purgeable.rb', line 47

def purge!
  if stale_contents.empty?
    logger.debug1 "No stale contents in #{managed_directory}, nothing to purge"
  else
    stale_contents.each do |fname|
      fpath = File.join(self.managed_directory, fname)
      logger.debug "Removing stale path #{fpath}"
      FileUtils.rm_rf(fpath, :secure => true)
    end
  end
end

#stale_contentsArray<String>

Returns Directory contents that are present but not expected.

Returns:

  • (Array<String>)

    Directory contents that are present but not expected



42
43
44
# File 'lib/r10k/util/purgeable.rb', line 42

def stale_contents
  current_contents - desired_contents
end