class R10K::Module::Forge

Attributes

metadata[R]

@!attribute [r] metadata

@api private
@return [PuppetForge::Metadata]
v3_module[R]

@!attribute [r] #v3_module

@api private
@return [PuppetForge::V3::Module] The Puppet Forge module metadata

Public Class Methods

implement?(name, args) click to toggle source
# File lib/r10k/module/forge.rb, line 15
def self.implement?(name, args)
  !!(name.match %r[\w+[/-]\w+]) && valid_version?(args)
end
new(title, dirname, expected_version, environment=nil) click to toggle source
Calls superclass method R10K::Module::Base.new
# File lib/r10k/module/forge.rb, line 35
def initialize(title, dirname, expected_version, environment=nil)
  super

  @metadata_file = R10K::Module::MetadataFile.new(path + 'metadata.json')
  @metadata = @metadata_file.read

  @expected_version = expected_version || current_version || :latest
  @v3_module = PuppetForge::V3::Module.new(:slug => @title)
end
valid_version?(expected_version) click to toggle source
# File lib/r10k/module/forge.rb, line 19
def self.valid_version?(expected_version)
  expected_version == :latest || expected_version.nil? || SemanticPuppet::Version.valid?(expected_version)
end

Public Instance Methods

current_version() click to toggle source

@return [String] The version of the currently installed module

# File lib/r10k/module/forge.rb, line 77
def current_version
  @metadata ? @metadata.version : nil
end
Also aliased as: version
exist?() click to toggle source
# File lib/r10k/module/forge.rb, line 83
def exist?
  path.exist?
end
expected_version() click to toggle source

@return [String] The expected version that the module

# File lib/r10k/module/forge.rb, line 65
def expected_version
  if @expected_version == :latest
    begin
      @expected_version = @v3_module.current_release.version
    rescue Faraday::ResourceNotFound => e
      raise PuppetForge::ReleaseNotFound, _("The module %{title} does not exist on %{url}.") % {title: @title, url: PuppetForge::V3::Release.conn.url_prefix}, e.backtrace
    end
  end
  @expected_version
end
install() click to toggle source
# File lib/r10k/module/forge.rb, line 131
def install
  parent_path = @path.parent
  if !parent_path.exist?
    parent_path.mkpath
  end
  module_release = R10K::Forge::ModuleRelease.new(@title, expected_version)
  module_release.install(@path)
end
Also aliased as: upgrade
insync?() click to toggle source
# File lib/r10k/module/forge.rb, line 87
def insync?
  status == :insync
end
properties() click to toggle source
# File lib/r10k/module/forge.rb, line 56
def properties
  {
    :expected => expected_version,
    :actual   => current_version,
    :type     => :forge,
  }
end
reinstall() click to toggle source
# File lib/r10k/module/forge.rb, line 146
def reinstall
  uninstall
  install
end
status() click to toggle source

Determine the status of the forge module.

@return [Symbol] :absent If the directory doesn't exist @return [Symbol] :mismatched If the module is not a forge module, or

isn't the right forge module

@return [Symbol] :mismatched If the module was previously a git checkout @return [Symbol] :outdated If the installed module is older than expected @return [Symbol] :insync If the module is in the desired state

# File lib/r10k/module/forge.rb, line 99
def status
  if not self.exist?
    # The module is not installed
    return :absent
  elsif not File.exist?(@path + 'metadata.json')
    # The directory exists but doesn't have a metadata file; it probably
    # isn't a forge module.
    return :mismatched
  end

  if File.directory?(@path + '.git')
    return :mismatched
  end

  # The module is present and has a metadata file, read the metadata to
  # determine the state of the module.
  @metadata = @metadata_file.read(@path + 'metadata.json')

  if not @title.tr('/','-') == @metadata.full_module_name.tr('/','-')

    # This is a forge module but the installed module is a different author
    # than the expected author.
    return :mismatched
  end

  if expected_version && (expected_version != @metadata.version)
    return :outdated
  end

  return :insync
end
sync(opts={}) click to toggle source
# File lib/r10k/module/forge.rb, line 45
def sync(opts={})
  case status
  when :absent
    install
  when :outdated
    upgrade
  when :mismatched
    reinstall
  end
end
uninstall() click to toggle source
# File lib/r10k/module/forge.rb, line 142
def uninstall
  FileUtils.rm_rf full_path
end
upgrade()
Alias for: install
version()
Alias for: current_version

Private Instance Methods

parse_title(title) click to toggle source

Override the base parse_title to ensure we have a fully qualified name

# File lib/r10k/module/forge.rb, line 154
def parse_title(title)
  if (match = title.match(/\A(\w+)[-\/](\w+)\Z/))
    [match[1], match[2]]
  else
    raise ArgumentError, _("Forge module names must match 'owner/modulename'")
  end
end