Class: Librarian::Puppet::Source::Forge::Repo

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/librarian/puppet/source/forge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#cp_r, #debug, #info

Constructor Details

#initialize(source, name) ⇒ Repo

Returns a new instance of Repo.



16
17
18
19
20
21
22
23
24
25
# File 'lib/librarian/puppet/source/forge.rb', line 16

def initialize(source, name)
  self.source = source
  self.name = name
  # API returned data for this module including all versions and dependencies, indexed by module name
  # from http://forge.puppetlabs.com/api/v1/releases.json?module=#{name}
  @api_data = nil
  # API returned data for this module and a specific version, indexed by version
  # from http://forge.puppetlabs.com/api/v1/releases.json?module=#{name}&version=#{version}
  @api_version_data = {}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/librarian/puppet/source/forge.rb', line 13

def name
  @name
end

#sourceObject

Returns the value of attribute source.



13
14
15
# File 'lib/librarian/puppet/source/forge.rb', line 13

def source
  @source
end

Instance Method Details

#cache_pathObject



77
78
79
# File 'lib/librarian/puppet/source/forge.rb', line 77

def cache_path
  @cache_path ||= source.cache_path.join(name)
end

#cache_version_unpacked!(version) ⇒ Object



89
90
91
92
93
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
# File 'lib/librarian/puppet/source/forge.rb', line 89

def cache_version_unpacked!(version)
  path = version_unpacked_cache_path(version)
  return if path.directory?

  # The puppet module command is only available from puppet versions >= 2.7.13
  #
  # Specifying the version in the gemspec would force people to upgrade puppet while it's still usable for git
  # So we do some more clever checking
  #
  # Executing older versions or via puppet-module tool gives an exit status = 0 .
  #
  check_puppet_module_options

  path.mkpath

  target = vendored?(name, version) ? vendored_path(name, version) : name


  command = "puppet module install --version #{version} --target-dir '#{path}' --module_repository '#{source}' --modulepath '#{path}' --ignore-dependencies '#{target}'"
  debug { "Executing puppet module install for #{name} #{version}" }
  output = `#{command}`

  # Check for bad exit code
  unless $? == 0
    # Rollback the directory if the puppet module had an error
    path.unlink
    raise Error, "Error executing puppet module install:\n#{command}\nError:\n#{output}"
  end

end

#check_puppet_module_optionsObject



120
121
122
123
124
125
126
127
# File 'lib/librarian/puppet/source/forge.rb', line 120

def check_puppet_module_options
  min_version    = Gem::Version.create('2.7.13')
  puppet_version = Gem::Version.create(PUPPET_VERSION.gsub('-', '.'))

  if puppet_version < min_version
    raise Error, "To get modules from the forge, we use the puppet faces module command. For this you need at least puppet version 2.7.13 and you have #{puppet_version}"
  end
end

#dependencies(version) ⇒ Object



38
39
40
# File 'lib/librarian/puppet/source/forge.rb', line 38

def dependencies(version)
  api_version_data(name, version)['dependencies']
end

#environmentObject



73
74
75
# File 'lib/librarian/puppet/source/forge.rb', line 73

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



85
86
87
# File 'lib/librarian/puppet/source/forge.rb', line 85

def hexdigest(value)
  Digest::MD5.hexdigest(value)
end

#install_version!(version, install_path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/librarian/puppet/source/forge.rb', line 48

def install_version!(version, install_path)
  if environment.local? && !vendored?(name, version)
    raise Error, "Could not find a local copy of #{name} at #{version}."
  end

  if environment.vendor?
    vendor_cache(name, version) unless vendored?(name, version)
  end

  cache_version_unpacked! version

  if install_path.exist?
    install_path.rmtree
  end

  unpacked_path = version_unpacked_cache_path(version).join(name.split('/').last)

  unless unpacked_path.exist?
    raise Error, "#{unpacked_path} does not exist, something went wrong. Try removing it manually"
  else
    cp_r(unpacked_path, install_path)
  end

end

#manifestsObject



42
43
44
45
46
# File 'lib/librarian/puppet/source/forge.rb', line 42

def manifests
  versions.map do |version|
    Manifest.new(source, name, version)
  end
end

#vendor_cache(name, version) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/librarian/puppet/source/forge.rb', line 137

def vendor_cache(name, version)
  info = api_version_data(name, version)
  url = "#{source}#{info[name].first['file']}"
  path = vendored_path(name, version).to_s
  debug { "Downloading #{url} into #{path}"}
  File.open(path, 'wb') do |f|
    open(url, "rb") do |input|
      f.write(input.read)
    end
  end
end

#vendored?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/librarian/puppet/source/forge.rb', line 129

def vendored?(name, version)
  vendored_path(name, version).exist?
end

#vendored_path(name, version) ⇒ Object



133
134
135
# File 'lib/librarian/puppet/source/forge.rb', line 133

def vendored_path(name, version)
  environment.vendor_cache.join("#{name.sub("/", "-")}-#{version}.tar.gz")
end

#version_unpacked_cache_path(version) ⇒ Object



81
82
83
# File 'lib/librarian/puppet/source/forge.rb', line 81

def version_unpacked_cache_path(version)
  cache_path.join('version').join(hexdigest(version.to_s))
end

#versionsObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/librarian/puppet/source/forge.rb', line 27

def versions
  return @versions if @versions
  @versions = api_data(name).map { |r| r['version'] }.reverse
  if @versions.empty?
    info { "No versions found for module #{name}" }
  else
    debug { "  Module #{name} found versions: #{@versions.join(", ")}" }
  end
  @versions
end