Class: Librarian::Puppet::Source::GitHubTarball::Repo

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

Constant Summary collapse

TOKEN_KEY =
'GITHUB_API_TOKEN'

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.



22
23
24
25
# File 'lib/librarian/puppet/source/githubtarball.rb', line 22

def initialize(source, name)
  self.source = source
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/librarian/puppet/source/githubtarball.rb', line 19

def name
  @name
end

#sourceObject

Returns the value of attribute source.



19
20
21
# File 'lib/librarian/puppet/source/githubtarball.rb', line 19

def source
  @source
end

Instance Method Details

#cache_pathObject



69
70
71
# File 'lib/librarian/puppet/source/githubtarball.rb', line 69

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

#cache_version_unpacked!(version) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/librarian/puppet/source/githubtarball.rb', line 81

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

  path.mkpath

  target = vendored?(source.uri, version) ? vendored_path(source.uri, version) : name

  `tar xzf #{target} -C #{path}`
end

#clean_up_old_cached_versions(name) ⇒ Object



122
123
124
125
126
# File 'lib/librarian/puppet/source/githubtarball.rb', line 122

def clean_up_old_cached_versions(name)
  Dir["#{environment.vendor_cache}/#{name.sub('/', '-')}*.tar.gz"].each do |old_version|
    FileUtils.rm old_version
  end
end

#environmentObject



65
66
67
# File 'lib/librarian/puppet/source/githubtarball.rb', line 65

def environment
  source.environment
end

#hexdigest(value) ⇒ Object



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

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
# File 'lib/librarian/puppet/source/githubtarball.rb', line 48

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

  vendor_cache(source.uri, version) unless vendored?(source.uri, version)

  cache_version_unpacked! version

  if install_path.exist?
    install_path.rmtree
  end

  unpacked_path = version_unpacked_cache_path(version).children.first
  cp_r(unpacked_path, install_path)
end

#manifestsObject



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

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

#vendor_cache(name, version) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/librarian/puppet/source/githubtarball.rb', line 101

def vendor_cache(name, version)
  clean_up_old_cached_versions(name)

  url = "https://api.github.com/repos/#{name}/tarball/#{version}"
  url << "?access_token=#{ENV['GITHUB_API_TOKEN']}" if ENV['GITHUB_API_TOKEN']

  File.open(vendored_path(name, version).to_s, 'wb') do |f|
    begin
      debug { "Downloading <#{url}> to <#{f.path}>" }
      open(url,
        "User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}") do |res|
        while buffer = res.read(8192)
          f.write(buffer)
        end
      end
    rescue OpenURI::HTTPError => e
      raise e, "Error requesting <#{url}>: #{e.to_s}"
    end
  end
end

#vendored?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/librarian/puppet/source/githubtarball.rb', line 92

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

#vendored_path(name, version) ⇒ Object



96
97
98
99
# File 'lib/librarian/puppet/source/githubtarball.rb', line 96

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

#version_unpacked_cache_path(version) ⇒ Object



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

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
37
38
39
40
# File 'lib/librarian/puppet/source/githubtarball.rb', line 27

def versions
  data = api_call("/repos/#{source.uri}/tags")
  if data.nil?
    raise Error, "Unable to find module '#{source.uri}' on https://github.com"
  end

  all_versions = data.map { |r| r['name'].gsub(/^v/, '') }.sort.reverse

  all_versions.delete_if do |version|
    version !~ /\A\d\.\d(\.\d.*)?\z/
  end

  all_versions.compact
end