Class: R10K::Module::Forge

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

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes inherited from Base

#dirname, #name, #owner, #path, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Methods inherited from Base

#accept, #full_path

Constructor Details

#initialize(title, dirname, expected_version) ⇒ Forge

Returns a new instance of Forge.



31
32
33
34
35
36
37
38
# File 'lib/r10k/module/forge.rb', line 31

def initialize(title, dirname, expected_version)
  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

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



22
23
24
# File 'lib/r10k/module/forge.rb', line 22

def 
  @metadata
end

#v3_moduleObject (readonly)

Returns the value of attribute v3_module.



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

def v3_module
  @v3_module
end

Class Method Details

.implement?(name, args) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/r10k/module/forge.rb', line 15

def self.implement?(name, args)
  !!(name.match %r[\w+[/-]\w+])
end

Instance Method Details

#current_versionString Also known as: version

Returns The version of the currently installed module.

Returns:

  • (String)

    The version of the currently installed module



72
73
74
# File 'lib/r10k/module/forge.rb', line 72

def current_version
  @metadata ? @metadata.version : nil
end

#exist?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/r10k/module/forge.rb', line 78

def exist?
  path.exist?
end

#expected_versionString

Returns The expected version that the module.

Returns:

  • (String)

    The expected version that the module



60
61
62
63
64
65
66
67
68
69
# File 'lib/r10k/module/forge.rb', line 60

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 #{PuppetForge::V3::Release.conn.url_prefix}.", e.backtrace
    end
  end
  @expected_version
end

#installObject Also known as: upgrade



126
127
128
129
130
131
132
133
# File 'lib/r10k/module/forge.rb', line 126

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

#insync?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/r10k/module/forge.rb', line 82

def insync?
  status == :insync
end

#propertiesObject



51
52
53
54
55
56
57
# File 'lib/r10k/module/forge.rb', line 51

def properties
  {
    :expected => expected_version,
    :actual   => current_version,
    :type     => :forge,
  }
end

#reinstallObject



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

def reinstall
  uninstall
  install
end

#statusSymbol

Determine the status of the forge module.

Returns:

  • (Symbol)

    :absent If the directory doesn’t exist

  • (Symbol)

    :mismatched If the module is not a forge module, or isn’t the right forge module

  • (Symbol)

    :mismatched If the module was previously a git checkout

  • (Symbol)

    :outdated If the installed module is older than expected

  • (Symbol)

    :insync If the module is in the desired state



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/r10k/module/forge.rb', line 94

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(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/r10k/module/forge.rb', line 40

def sync(options = {})
  case status
  when :absent
    install
  when :outdated
    upgrade
  when :mismatched
    reinstall
  end
end

#uninstallObject



137
138
139
# File 'lib/r10k/module/forge.rb', line 137

def uninstall
  FileUtils.rm_rf full_path
end