Class: Puppet::Forge::Repository

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/puppet/forge/repository.rb

Overview

Repository

This class is a file for accessing remote repositories with modules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, for_agent) ⇒ Repository

Instantiate a new repository instance rooted at the url. The library will report for_agent in the User-Agent to the repository.



20
21
22
23
24
25
26
27
28
# File 'lib/puppet/forge/repository.rb', line 20

def initialize(host, for_agent)
  @host  = host
  @agent = for_agent
  @cache = Cache.new(self)
  @uri   = URI.parse(host)

  ssl_provider = Puppet::SSL::SSLProvider.new
  @ssl_context = ssl_provider.create_system_context(cacerts: [])
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



16
17
18
# File 'lib/puppet/forge/repository.rb', line 16

def cache
  @cache
end

#uriObject (readonly)

Returns the value of attribute uri.



16
17
18
# File 'lib/puppet/forge/repository.rb', line 16

def uri
  @uri
end

Instance Method Details

#cache_keyObject

Return the cache key for this repository, this a hashed string based on the URI.



84
85
86
87
88
89
# File 'lib/puppet/forge/repository.rb', line 84

def cache_key
  return @cache_key ||= [
    @host.to_s.gsub(/[^[:alnum:]]+/, '_').sub(/_$/, ''),
    Digest::SHA1.hexdigest(@host.to_s)
  ].join('-').freeze
end

#forge_authorizationObject



62
63
64
65
66
67
68
# File 'lib/puppet/forge/repository.rb', line 62

def forge_authorization
  if Puppet[:forge_authorization]
    Puppet[:forge_authorization]
  elsif Puppet.features.pe_license?
    PELicense.load_license_key.authorization_token
  end
end

#make_http_request(path, io = nil) ⇒ Object

Return a Net::HTTPResponse read for this path.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet/forge/repository.rb', line 31

def make_http_request(path, io = nil)
  raise ArgumentError, "Path must start with forward slash" unless path.start_with?('/')
  begin
    str = @uri.to_s
    str.chomp!('/')
    str += Puppet::Util.uri_encode(path)
    uri = URI(str)

    headers = { "User-Agent" => user_agent }
    basic_auth = nil

    if forge_authorization
      headers["Authorization"] = forge_authorization
    elsif @uri.user && @uri.password
      basic_auth = {
        user: @uri.user,
        password: @uri.password
      }
    end

    http = Puppet.runtime[:http]
    response = http.get(uri, headers: headers, options: {basic_auth: basic_auth, ssl_context: @ssl_context})
    io.write(response.body) if io.respond_to?(:write)
    response
  rescue Puppet::SSL::CertVerifyError => e
    raise SSLVerifyError.new(:uri => @uri.to_s, :original => e.cause)
  rescue => e
    raise CommunicationError.new(:uri => @uri.to_s, :original => e)
  end
end

#retrieve(release) ⇒ Object

Return the local file name containing the data downloaded from the repository at release (e.g. “myuser-mymodule”).



72
73
74
75
# File 'lib/puppet/forge/repository.rb', line 72

def retrieve(release)
  path = @host.chomp('/') + release
  return cache.retrieve(path)
end

#to_sObject

Return the URI string for this repository.



78
79
80
# File 'lib/puppet/forge/repository.rb', line 78

def to_s
  "#<#{self.class} #{@host}>"
end