Class: OctocatalogDiff::Util::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/util/util.rb

Overview

Helper class to construct catalogs, performing all necessary steps such as bootstrapping directories, installing facts, and running puppet.

Class Method Summary collapse

Class Method Details

.deep_dup(object) ⇒ ?

Utility Method! This does a “deep” duplication via recursion. Handles hashes and arrays.

Parameters:

  • object (?)

    Object to consider

Returns:

  • (?)

    Duplicated object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/octocatalog-diff/util/util.rb', line 39

def self.deep_dup(object)
  if object.is_a?(Hash)
    result = {}
    object.each { |k, v| result[k] = deep_dup(v) }
    result
  elsif object.is_a?(Array)
    object.map { |ele| deep_dup(ele) }
  else
    safe_dup(object)
  end
end

.object_is_any_of?(object, classes) ⇒ Boolean

Utility Method! ‘is_a?(class)` only allows one method, but this uses an array

Parameters:

  • object (?)

    Object to consider

  • classes (Array)

    Classes to determine if object is a member of

Returns:

  • (Boolean)

    True if object is_a any of the classes, false otherwise



17
18
19
20
# File 'lib/octocatalog-diff/util/util.rb', line 17

def self.object_is_any_of?(object, classes)
  classes.each { |clazz| return true if object.is_a? clazz }
  false
end

.remove_temp_dir(dir) ⇒ Object

Utility method! Remove a directory recursively that has been used as a temporary directory. This should be called within an ‘at_exit` handler, and is only intended to be called via the `temp_dir` method above.

dir - A String with the directory to remove.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/octocatalog-diff/util/util.rb', line 83

def self.remove_temp_dir(dir)
  retries = 0
  while File.directory?(dir) && retries < 10
    retries += 1
    begin
      FileUtils.remove_entry_secure(dir)
    rescue Errno::ENOTEMPTY, Errno::ENOENT # rubocop:disable Lint/HandleExceptions
      # Errno::ENOTEMPTY will trigger a retry because the directory exists
      # Errno::ENOENT will break the loop because the directory won't exist next time it's checked
    end
  end
end

.safe_dup(object) ⇒ ?

Utility Method! ‘.dup` can’t be called on certain objects (Fixnum for example). This method returns the original object if it can’t be duplicated.

Parameters:

  • object (?)

    Object to consider

Returns:

  • (?)

    Duplicated object if possible, otherwise the original object



27
28
29
30
31
32
33
# File 'lib/octocatalog-diff/util/util.rb', line 27

def self.safe_dup(object)
  object.dup
rescue TypeError
  # :nocov:
  object
  # :nocov:
end

.temp_dir(prefix = 'ocd-', basedir = ) ⇒ Object

Utility Method! This creates a temporary directory. If the base directory is specified, then we do not remove the temporary directory at exit, because we assume that something else will remove the base directory.

prefix - A String with the prefix for the temporary directory basedir - A String with the directory in which to make the tempdir

Returns the full path to the temporary directory.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/octocatalog-diff/util/util.rb', line 60

def self.temp_dir(prefix = 'ocd-', basedir = ENV['OCTOCATALOG_DIFF_TEMPDIR'])
  # If the base directory is specified, make sure it exists, and then create the
  # temporary directory within it.
  if basedir
    unless File.directory?(basedir)
      raise Errno::ENOENT, "temp_dir: Base dir #{basedir.inspect} does not exist!"
    end
    return Dir.mktmpdir(prefix, basedir)
  end

  # If the base directory was not specified, then create a temporary directory, and
  # send the `at_exit` to clean it up at the conclusion.
  the_dir = Dir.mktmpdir(prefix)
  at_exit { remove_temp_dir(the_dir) }
  the_dir
end