Class: R10K::Module::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/r10k/module/base.rb

Overview

This class defines a common interface for module implementations.

Direct Known Subclasses

Definition, Forge, Git, Local, SVN

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Constructor Details

#initialize(title, dirname, args, environment = nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • title (String)
  • dirname (String)
  • args (Hash)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/r10k/module/base.rb', line 54

def initialize(title, dirname, args, environment=nil)
  @title   = PuppetForge::V3.normalize_name(title)
  @dirname = dirname
  @args    = args
  @owner, @name = parse_title(@title)
  @path = Pathname.new(File.join(@dirname, @name))
  @environment = environment
  @overrides = args.delete(:overrides) || {}
  @spec_deletable = true
  @exclude_spec = args.delete(:exclude_spec)
  @exclude_spec = @overrides[:modules].delete(:exclude_spec) if @overrides.dig(:modules, :exclude_spec)
  @origin = 'external' # Expect Puppetfile or R10k::Environment to set this to a specific value

  @requested_modules = @overrides.dig(:modules, :requested_modules) || []
  @should_sync = (@requested_modules.empty? || @requested_modules.include?(@name))
end

Instance Attribute Details

#dirnameObject (readonly) Also known as: basedir

Parameters:

  • dirname (r)

    @return [String] The name of the directory containing this module



20
21
22
# File 'lib/r10k/module/base.rb', line 20

def dirname
  @dirname
end

#environmentObject (readonly)

Returns the value of attribute environment.



35
36
37
# File 'lib/r10k/module/base.rb', line 35

def environment
  @environment
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/r10k/module/base.rb', line 16

def name
  @name
end

#originString

Returns Where the module was sourced from. E.g., “Puppetfile”.

Returns:

  • (String)

    Where the module was sourced from. E.g., “Puppetfile”



39
40
41
# File 'lib/r10k/module/base.rb', line 39

def origin
  @origin
end

#ownerObject (readonly) Also known as: author

Returns the value of attribute owner.



27
28
29
# File 'lib/r10k/module/base.rb', line 27

def owner
  @owner
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/r10k/module/base.rb', line 31

def path
  @path
end

#spec_deletableBoolean

Returns set this to true if the spec dir can be safely removed, ie in the moduledir.

Returns:

  • (Boolean)

    set this to true if the spec dir can be safely removed, ie in the moduledir



43
44
45
# File 'lib/r10k/module/base.rb', line 43

def spec_deletable
  @spec_deletable
end

#titleObject (readonly) Also known as: full_name

Returns the value of attribute title.



12
13
14
# File 'lib/r10k/module/base.rb', line 12

def title
  @title
end

Instance Method Details

#accept(visitor) ⇒ Object

Deprecated



147
148
149
# File 'lib/r10k/module/base.rb', line 147

def accept(visitor)
  visitor.visit(:module, self)
end

#cachedirString, :none

Return the module’s cachedir. Subclasses that implement a cache will override this to return a real directory location.

Returns:

  • (String, :none)


163
164
165
# File 'lib/r10k/module/base.rb', line 163

def cachedir
  :none
end

#delete_spec_dirObject

Actually remove the spec dir



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/r10k/module/base.rb', line 89

def delete_spec_dir
  spec_path = @path + 'spec'
  if spec_path.symlink?
    spec_path = spec_path.realpath
  end
  if spec_path.directory?
    logger.debug2 _("Deleting spec data at #{spec_path}")
    # Use the secure flag for the #rm_rf method to avoid security issues
    # involving TOCTTOU(time of check to time of use); more details here:
    # https://ruby-doc.org/stdlib-2.7.0/libdoc/fileutils/rdoc/FileUtils.html#method-c-rm_rf
    # Additionally, #rm_rf also has problems in windows with with symlink targets
    # also being deleted; this should be revisted if Windows becomes higher priority.
    FileUtils.rm_rf(spec_path, secure: true)
  else
    logger.debug2 _("No spec dir detected at #{spec_path}, skipping deletion")
  end
end

#full_pathString

Deprecated.

Returns The full filesystem path to the module.

Returns:

  • (String)

    The full filesystem path to the module.



73
74
75
# File 'lib/r10k/module/base.rb', line 73

def full_path
  path.to_s
end

#maybe_delete_spec_dirObject

Delete the spec dir if @exclude_spec has been set to true and @spec_deletable is also true



78
79
80
81
82
83
84
85
86
# File 'lib/r10k/module/base.rb', line 78

def maybe_delete_spec_dir
  if @exclude_spec
    if @spec_deletable
      delete_spec_dir
    else
      logger.info _("Spec dir for #{@title} will not be deleted because it is not in the moduledir")
    end
  end
end

#propertiesHash

This method is abstract.

Return the properties of the module

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


155
156
157
# File 'lib/r10k/module/base.rb', line 155

def properties
  raise NotImplementedError
end

#should_sync?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'lib/r10k/module/base.rb', line 114

def should_sync?
  if @should_sync
    logger.info _("Deploying module to %{path}") % {path: path}
    true
  else
    logger.debug1(_("Only updating modules %{modules}, skipping module %{name}") % {modules: @requested_modules.inspect, name: name})
    false
  end
end

#statusSymbol

This method is abstract.

Return the status of the currently installed module.

This can return the following values:

* :absent - there is no module installed
* :mismatched - there is a module installed but it must be removed and reinstalled
* :outdated - the correct module is installed but it needs to be updated
* :insync - the correct module is installed and up to date, or the module is actually a boy band.

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


142
143
144
# File 'lib/r10k/module/base.rb', line 142

def status
  raise NotImplementedError
end

#sync(opts = {}) ⇒ Boolean

Synchronize this module with the indicated state.

Parameters:

  • opts (Hash) (defaults to: {})

    Deprecated

Returns:

  • (Boolean)

    true if the module was updated, false otherwise

Raises:

  • (NotImplementedError)


110
111
112
# File 'lib/r10k/module/base.rb', line 110

def sync(opts={})
  raise NotImplementedError
end

#versionObject

This method is abstract.

Return the desired version of this module

Raises:

  • (NotImplementedError)


127
128
129
# File 'lib/r10k/module/base.rb', line 127

def version
  raise NotImplementedError
end