Class: Puppet::Module::Tool::Repository

Inherits:
Object
  • Object
show all
Includes:
Utils::Interrogation, Utils::URI
Defined in:
lib/puppet/module/tool/repository.rb

Overview

Repository

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Interrogation

#confirms?, #header, #prompt, #say, #subheader

Methods included from Utils::URI

#normalize

Constructor Details

#initialize(url = Puppet::Module::Tool::REPOSITORY_URL) ⇒ Repository

Instantiate a new repository instance rooted at the optional string url, else an instance of the default Puppet modules repository.



17
18
19
20
# File 'lib/puppet/module/tool/repository.rb', line 17

def initialize(url=Puppet::Module::Tool::REPOSITORY_URL)
  @uri = normalize(url)
  @cache = Cache.new(self)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/puppet/module/tool/repository.rb', line 13

def cache
  @cache
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/puppet/module/tool/repository.rb', line 13

def uri
  @uri
end

Instance Method Details

#authenticate(request) ⇒ Object

Set the HTTP Basic Authentication parameters for the Net::HTTPRequest request by asking the user for input on the console.



49
50
51
52
53
54
# File 'lib/puppet/module/tool/repository.rb', line 49

def authenticate(request)
  header "Authenticating for #{@uri}"
  email = prompt('Email Address')
  password = prompt('Password', true)
  request.basic_auth(email, password)
end

#cache_keyObject

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



69
70
71
72
73
74
# File 'lib/puppet/module/tool/repository.rb', line 69

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

#contact(request, options = {}) ⇒ Object

Return a Net::HTTPResponse read for this request.

Options:

  • :authenticate => Request authentication on the terminal. Defaults to false.



26
27
28
29
30
31
32
33
34
# File 'lib/puppet/module/tool/repository.rb', line 26

def contact(request, options = {})
  if options[:authenticate]
    authenticate(request)
  end
  if ! @uri.user.nil? && ! @uri.password.nil?
    request.basic_auth(@uri.user, @uri.password)
  end
  return read_contact(request)
end

#read_contact(request) ⇒ Object

Return a Net::HTTPResponse read from this HTTPRequest request.



37
38
39
40
41
42
43
44
45
# File 'lib/puppet/module/tool/repository.rb', line 37

def read_contact(request)
  begin
    Net::HTTP.start(@uri.host, @uri.port) do |http|
      http.request(request)
    end
  rescue Errno::ECONNREFUSED, SocketError
    abort "Could not reach remote repository"
  end
end

#retrieve(release) ⇒ Object

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



58
59
60
# File 'lib/puppet/module/tool/repository.rb', line 58

def retrieve(release)
  return cache.retrieve(@uri + release)
end

#to_sObject

Return the URI string for this repository.



63
64
65
# File 'lib/puppet/module/tool/repository.rb', line 63

def to_s
  return @uri.to_s
end