Class: Puppet::Forge::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/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(url = Puppet[:module_repository]) ⇒ Repository

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



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

def initialize(url=Puppet[:module_repository])
  @uri = url.is_a?(::URI) ? url : ::URI.parse(url.sub(/^(?!https?:\/\/)/, 'http://'))
  @cache = Cache.new(self)
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



11
12
13
# File 'lib/vendor/puppet/forge/repository.rb', line 11

def cache
  @cache
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/vendor/puppet/forge/repository.rb', line 11

def uri
  @uri
end

Instance Method Details

#cache_keyObject

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



95
96
97
98
99
100
# File 'lib/vendor/puppet/forge/repository.rb', line 95

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

#http_proxy_envObject

Read HTTP proxy configurationm from Puppet’s config file, or the http_proxy environment variable.



22
23
24
25
26
27
28
29
30
# File 'lib/vendor/puppet/forge/repository.rb', line 22

def http_proxy_env
  proxy_env = ENV["http_proxy"] || ENV["HTTP_PROXY"] || nil
  begin
    return URI.parse(proxy_env) if proxy_env
  rescue URI::InvalidURIError
    return nil
  end
  return nil
end

#http_proxy_hostObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vendor/puppet/forge/repository.rb', line 32

def http_proxy_host
  env = http_proxy_env

  if env and env.host then
    return env.host
  end

  if Puppet.settings[:http_proxy_host] == 'none'
    return nil
  end

  return Puppet.settings[:http_proxy_host]
end

#http_proxy_portObject



46
47
48
49
50
51
52
53
54
# File 'lib/vendor/puppet/forge/repository.rb', line 46

def http_proxy_port
  env = http_proxy_env

  if env and env.port then
    return env.port
  end

  return Puppet.settings[:http_proxy_port]
end

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

Return a Net::HTTPResponse read for this request.



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

def make_http_request(request, options = {})
  if ! @uri.user.nil? && ! @uri.password.nil?
    request.basic_auth(@uri.user, @uri.password)
  end
  return read_response(request)
end

#read_response(request) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vendor/puppet/forge/repository.rb', line 65

def read_response(request)
  begin
    Net::HTTP::Proxy(
        http_proxy_host,
        http_proxy_port
        ).start(@uri.host, @uri.port) do |http|
      http.request(request)
    end
  rescue Errno::ECONNREFUSED, SocketError
    msg = "Error: Could not connect to #{@uri}\n"
    msg << "  There was a network communications problem\n"
    msg << "    Check your network connection and try again\n"
    Puppet.err msg
    exit(1)
  end
end

#retrieve(release) ⇒ Object

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



84
85
86
# File 'lib/vendor/puppet/forge/repository.rb', line 84

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

#to_sObject

Return the URI string for this repository.



89
90
91
# File 'lib/vendor/puppet/forge/repository.rb', line 89

def to_s
  return @uri.to_s
end