Class: Librarian::Puppet::Source::Forge::Repo
- Inherits:
-
Object
- Object
- Librarian::Puppet::Source::Forge::Repo
- Defined in:
- lib/librarian/puppet/source/forge.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #cache_path ⇒ Object
- #cache_version_unpacked!(version) ⇒ Object
- #check_puppet_module_options ⇒ Object
- #debug(*args, &block) ⇒ Object
- #dependencies(version) ⇒ Object
- #download(name, version, &block) ⇒ Object
- #environment ⇒ Object
- #hexdigest(value) ⇒ Object
-
#initialize(source, name) ⇒ Repo
constructor
A new instance of Repo.
- #install_version!(version, install_path) ⇒ Object
- #manifests ⇒ Object
- #stream(file, &block) ⇒ Object
- #vendor_cache(name, version) ⇒ Object
- #vendored?(name, version) ⇒ Boolean
- #vendored_path(name, version) ⇒ Object
- #version_unpacked_cache_path(version) ⇒ Object
- #versions ⇒ Object
Constructor Details
#initialize(source, name) ⇒ Repo
Returns a new instance of Repo.
14 15 16 17 |
# File 'lib/librarian/puppet/source/forge.rb', line 14 def initialize(source, name) self.source = source self.name = name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/librarian/puppet/source/forge.rb', line 11 def name @name end |
#source ⇒ Object
Returns the value of attribute source.
11 12 13 |
# File 'lib/librarian/puppet/source/forge.rb', line 11 def source @source end |
Instance Method Details
#cache_path ⇒ Object
70 71 72 |
# File 'lib/librarian/puppet/source/forge.rb', line 70 def cache_path @cache_path ||= source.cache_path.join(name) end |
#cache_version_unpacked!(version) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/librarian/puppet/source/forge.rb', line 82 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 . # path.mkpath target = vendored?(name, version) ? vendored_path(name, version) : name command = "puppet module install --version #{version} --target-dir '#{path}' --modulepath '#{path}' --ignore-dependencies '#{target}'" 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_options ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/librarian/puppet/source/forge.rb', line 111 def min_version = Gem::Version.create('2.7.13') puppet_version = Gem::Version.create(`puppet --version`.split(' ').first.strip.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 |
#debug(*args, &block) ⇒ Object
153 154 155 |
# File 'lib/librarian/puppet/source/forge.rb', line 153 def debug(*args, &block) environment.logger.debug(*args, &block) end |
#dependencies(version) ⇒ Object
30 31 32 33 |
# File 'lib/librarian/puppet/source/forge.rb', line 30 def dependencies(version) data = api_call("api/v1/releases.json?module=#{name}&version=#{version}") data[name].first['dependencies'] end |
#download(name, version, &block) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/librarian/puppet/source/forge.rb', line 136 def download(name, version, &block) data = api_call("api/v1/releases.json?module=#{name}&version=#{version}") info = data[name].detect {|h| h['version'] == version.to_s } stream(info['file'], &block) end |
#environment ⇒ Object
66 67 68 |
# File 'lib/librarian/puppet/source/forge.rb', line 66 def environment source.environment end |
#hexdigest(value) ⇒ Object
78 79 80 |
# File 'lib/librarian/puppet/source/forge.rb', line 78 def hexdigest(value) Digest::MD5.hexdigest(value) end |
#install_version!(version, install_path) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/librarian/puppet/source/forge.rb', line 41 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 FileUtils.cp_r(unpacked_path, install_path) end end |
#manifests ⇒ Object
35 36 37 38 39 |
# File 'lib/librarian/puppet/source/forge.rb', line 35 def manifests versions.map do |version| Manifest.new(source, name, version) end end |
#stream(file, &block) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/librarian/puppet/source/forge.rb', line 144 def stream(file, &block) #Net::HTTP.get_response(URI.parse("#{source}#{file}")) do |res| http_get(URI.parse("#{source}#{file}")) do |res| res.code res.read_body(&block) end end |
#vendor_cache(name, version) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/librarian/puppet/source/forge.rb', line 128 def vendor_cache(name, version) File.open(vendored_path(name, version).to_s, 'w') do |f| download(name, version) do |data| f << data end end end |
#vendored?(name, version) ⇒ Boolean
120 121 122 |
# File 'lib/librarian/puppet/source/forge.rb', line 120 def vendored?(name, version) vendored_path(name, version).exist? end |
#vendored_path(name, version) ⇒ Object
124 125 126 |
# File 'lib/librarian/puppet/source/forge.rb', line 124 def vendored_path(name, version) environment.vendor_cache.join("#{name.sub("/", "-")}-#{version}.tar.gz") end |
#version_unpacked_cache_path(version) ⇒ Object
74 75 76 |
# File 'lib/librarian/puppet/source/forge.rb', line 74 def version_unpacked_cache_path(version) cache_path.join('version').join(hexdigest(version.to_s)) end |
#versions ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/librarian/puppet/source/forge.rb', line 19 def versions data = api_call("#{name}.json") if data.nil? raise Error, "Unable to find module '#{name}' on #{source}" end versions = data['releases'].map { |r| r['version'] }.sort.reverse versions.select { |v| ! Gem::Version.correct? v }.each { |v| debug { "Ignoring invalid version '#{v}' for module #{name}" } } versions.select { |v| Gem::Version.correct? v } end |