Class: Puppet::Forge

Inherits:
SemanticPuppet::Dependency::Source 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SemanticPuppet::Dependency::Source

#create_release, #priority, priority

Constructor Details

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

Returns a new instance of Forge.



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

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.



20
21
22
# File 'lib/puppet/forge.rb', line 20

def host
  @host
end

#repositoryObject (readonly)

Returns the value of attribute repository.



20
21
22
# File 'lib/puppet/forge.rb', line 20

def repository
  @repository
end

Instance Method Details

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

Fetches ModuleRelease entries for each release of the named module.

Parameters:

  • the module name to look up

Returns:

  • a list of releases for the given name

See Also:

  • SemanticPuppet::Dependency::Source#fetch


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppet/forge.rb', line 89

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

  while uri
    response = make_http_request(uri)

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

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

  return releases
end

#make_http_request(*args) ⇒ Object



113
114
115
# File 'lib/puppet/forge.rb', line 113

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

]

Parameters:

  • search term

Returns:

  • modules found

Raises:

  • if there is a network related error

  • if there is a problem verifying the remote SSL certificate

  • if the repository returns a bad HTTP response



54
55
56
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
# File 'lib/puppet/forge.rb', line 54

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

  while uri
    response = make_http_request(uri)

    if response.code == '200'
      result = JSON.parse(response.body)
      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