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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, name) ⇒ Repo

Returns a new instance of Repo.



14
15
16
17
18
# File 'lib/librarian/puppet/source/forge.rb', line 14

def initialize(source, name)
  self.source = source
  self.name = name
  @dependencies = {}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/librarian/puppet/source/forge.rb', line 11

def name
  @name
end

#sourceObject

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_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
# 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}'"
  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



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

def check_puppet_module_options
  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



159
160
161
# File 'lib/librarian/puppet/source/forge.rb', line 159

def debug(*args, &block)
  environment.logger.debug(*args, &block)
end

#dependencies(version) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/librarian/puppet/source/forge.rb', line 33

def dependencies(version)
  return @dependencies[version] if @dependencies[version]
  data = api_call("api/v1/releases.json?module=#{name}&version=#{version}")
  if data.nil?
    raise Error, "Unable to find version #{version} for module '#{name}' on #{source}"
  end
  @dependencies[version] = data[name].first['dependencies']
end

#download(name, version, &block) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/librarian/puppet/source/forge.rb', line 143

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

#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
    FileUtils.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

#stream(file, &block) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/librarian/puppet/source/forge.rb', line 151

def stream(file, &block)
  Net::HTTP.get_response(URI.parse("#{source}#{file}")) do |res|
    res.code

    res.read_body(&block)
  end
end

#vendor_cache(name, version) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/librarian/puppet/source/forge.rb', line 135

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

Returns:

  • (Boolean)


127
128
129
# File 'lib/librarian/puppet/source/forge.rb', line 127

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

#vendored_path(name, version) ⇒ Object



131
132
133
# File 'lib/librarian/puppet/source/forge.rb', line 131

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



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/librarian/puppet/source/forge.rb', line 20

def versions
  return @versions if @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'] }
  debug { "  Module #{name} found versions: #{versions.join(", ")}" }
  versions.select { |v| ! Gem::Version.correct? v }.each { |v| debug { "Ignoring invalid version '#{v}' for module #{name}" } }
  @versions = versions.select { |v| Gem::Version.correct? v }
end