Class: R10K::Module::Forge

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

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP

Instance Attribute Summary collapse

Attributes inherited from Base

#dirname, #environment, #name, #origin, #owner, #path, #spec_deletable, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

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

Methods inherited from Base

#accept, #cachedir, #delete_spec_dir, #full_path, #maybe_delete_spec_dir, #should_sync?

Constructor Details

#initialize(title, dirname, opts, environment = nil) ⇒ Forge

Returns a new instance of Forge.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/r10k/module/forge.rb', line 35

def initialize(title, dirname, opts, environment=nil)
  super

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

  setopts(opts, {
    # Standard option interface
    :version => :expected_version,
    :source  => ::R10K::Util::Setopts::Ignore,
    :type    => ::R10K::Util::Setopts::Ignore,
  }, :raise_on_unhandled => false)

  # Validate version and raise on issue. Title is validated by base class.
  unless valid_version?(@expected_version)
    raise ArgumentError, _("Module version %{ver} is not a valid Forge module version") % {ver: @expected_version}
  end

  @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.



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

def 
  @metadata
end

#v3_moduleObject (readonly)

Returns the value of attribute v3_module.



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

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)
  args[:type].to_s == 'forge'
end

.statically_defined_version(name, args) ⇒ Object



19
20
21
# File 'lib/r10k/module/forge.rb', line 19

def self.statically_defined_version(name, args)
  args[:version] if args[:version].is_a?(String)
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



108
109
110
111
112
113
114
# File 'lib/r10k/module/forge.rb', line 108

def current_version
  if insync?
    (@metadata ||= @metadata_file.read).nil? ? nil : @metadata.version
  else
    nil
  end
end

#deprecated?Boolean

Returns:

  • (Boolean)


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

def deprecated?
  begin
    @v3_module.fetch && @v3_module.has_attribute?('deprecated_at') && !@v3_module.deprecated_at.nil?
  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

#exist?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/r10k/module/forge.rb', line 118

def exist?
  path.exist?
end

#expected_versionString

Returns The expected version that the module.

Returns:

  • (String)

    The expected version that the module



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

def expected_version
  if @expected_version == :latest
    begin
      if @v3_module.current_release
        @expected_version = @v3_module.current_release.version
      else
        raise PuppetForge::ReleaseNotFound, _("The module %{title} does not appear to have any published releases, cannot determine latest version.") % { title: @title }
      end
    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

#installObject Also known as: upgrade



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/r10k/module/forge.rb', line 174

def install
  if deprecated?
    logger.warn "Puppet Forge module '#{@v3_module.slug}' has been deprecated, visit https://forge.puppet.com/#{@v3_module.slug.tr('-','/')} for more information."
  end

  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)


122
123
124
# File 'lib/r10k/module/forge.rb', line 122

def insync?
  status == :insync
end

#propertiesObject



83
84
85
86
87
88
89
# File 'lib/r10k/module/forge.rb', line 83

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

#reinstallObject



193
194
195
196
# File 'lib/r10k/module/forge.rb', line 193

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/r10k/module/forge.rb', line 142

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 = {}) ⇒ Boolean

Returns true if the module was updated, false otherwise.

Parameters:

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

    Deprecated

Returns:

  • (Boolean)

    true if the module was updated, false otherwise



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/r10k/module/forge.rb', line 64

def sync(opts={})
  updated = false
  if should_sync?
    case status
    when :absent
      install
      updated = true
    when :outdated
      upgrade
      updated = true
    when :mismatched
      reinstall
      updated = true
    end
    maybe_delete_spec_dir
  end
  updated
end

#uninstallObject



189
190
191
# File 'lib/r10k/module/forge.rb', line 189

def uninstall
  FileUtils.rm_rf full_path
end

#valid_version?(version) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/r10k/module/forge.rb', line 58

def valid_version?(version)
  version == :latest || version.nil? || PuppetForge::Util.version_valid?(version)
end