Class: Puppet::Forge

Inherits:
SemanticPuppet::Dependency::Source
  • Object
show all
Includes:
Errors
Defined in:
lib/puppet/forge.rb,
lib/puppet/forge/cache.rb,
lib/puppet/forge/repository.rb

Defined Under Namespace

Modules: Errors Classes: Cache, ModuleRelease, Repository

Constant Summary collapse

USER_AGENT =
"PMT/1.1.1 (v3; Net::HTTP)".freeze
MODULE_RELEASE_EXCLUSIONS =
%w[readme changelog license uri module tags supported file_size downloads created_at updated_at deleted_at].join(',').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = Puppet[:module_repository]) ⇒ Forge


25
26
27
28
# File 'lib/puppet/forge.rb', line 25

def initialize(host = Puppet[:module_repository])
  @host = host
  @repository = Puppet::Forge::Repository.new(host, USER_AGENT)
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host


23
24
25
# File 'lib/puppet/forge.rb', line 23

def host
  @host
end

#repositoryObject (readonly)

Returns the value of attribute repository


23
24
25
# File 'lib/puppet/forge.rb', line 23

def repository
  @repository
end

Instance Method Details

#fetch(input) ⇒ Array<SemanticPuppet::Dependency::ModuleRelease>

Fetches ModuleRelease entries for each release of the named module.

See Also:

  • SemanticPuppet::Dependency::Source#fetch

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet/forge.rb', line 93

def fetch(input)
  name = input.tr('/', '-')
  uri = "/v3/releases?module=#{name}&sort_by=version&exclude_fields=#{MODULE_RELEASE_EXCLUSIONS}"
  if Puppet[:module_groups]
    uri += "&module_groups=#{Puppet[:module_groups].tr('+', ' ')}"
  end
  releases = []

  while uri
    # make_http_request URI encodes parameters
    response = make_http_request(uri)

    if response.code == '200'
      response = Puppet::Util::Json.load(response.body)
    else
      raise ResponseError.new(:uri => URI.parse(@host).merge(uri), :response => response)
    end

    releases.concat(process(response['results']))
    uri = decode_uri(response['pagination']['next'])
  end

  return releases
end

#make_http_request(*args) ⇒ Object


118
119
120
# File 'lib/puppet/forge.rb', line 118

def make_http_request(*args)
  @repository.make_http_request(*args)
end

#search(term) ⇒ Array

Return a list of module metadata hashes that match the search query. This return value is used by the module_tool face install search, and displayed to on the console.

Example return value:

[

{
  "author"      => "puppetlabs",
  "name"        => "bacula",
  "tag_list"    => ["backup", "bacula"],
  "releases"    => [{"version"=>"0.0.1"}, {"version"=>"0.0.2"}],
  "full_name"   => "puppetlabs/bacula",
  "version"     => "0.0.2",
  "project_url" => "https://github.com/puppetlabs/puppetlabs-bacula",
  "desc"        => "bacula"
}

]

Raises:


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppet/forge.rb', line 57

def search(term)
  matches = []
  uri = "/v3/modules?query=#{term}"
  if Puppet[:module_groups]
    uri += "&module_groups=#{Puppet[:module_groups].tr('+', ' ')}"
  end

  while uri
    # make_http_request URI encodes parameters
    response = make_http_request(uri)

    if response.code == '200'
      result = Puppet::Util::Json.load(response.body)
      uri = decode_uri(result['pagination']['next'])
      matches.concat result['results']
    else
      raise ResponseError.new(:uri => URI.parse(@host).merge(uri), :response => response)
    end
  end

  matches.each do |mod|
    mod['author'] = mod['owner']['username']
    mod['tag_list'] = mod['current_release']['tags']
    mod['full_name'] = "#{mod['author']}/#{mod['name']}"
    mod['version'] = mod['current_release']['version']
    mod['project_url'] = mod['homepage_url']
    mod['desc'] = mod['current_release']['metadata']['summary'] || ''
  end
end