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.


80
81
82
83
84
85
# File 'lib/puppet/forge/repository.rb', line 80

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

#forge_authorizationObject


58
59
60
61
62
63
64
# File 'lib/puppet/forge/repository.rb', line 58

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
# 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 }

    if forge_authorization
      uri.user = nil
      uri.password = nil
      headers["Authorization"] = forge_authorization
    end

    http = Puppet.runtime[:http]
    response = http.get(uri, headers: headers, options: {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”).


68
69
70
71
# File 'lib/puppet/forge/repository.rb', line 68

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

#to_sObject

Return the URI string for this repository.


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

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