Module: Puppet::Forge

Defined in:
lib/vendor/puppet/forge.rb,
lib/vendor/puppet/forge/cache.rb,
lib/vendor/puppet/forge/repository.rb

Defined Under Namespace

Classes: Cache, Repository

Class Method Summary collapse

Class Method Details

.get_release_package_from_filesystem(filename) ⇒ Object

Locate a module release package on the local filesystem and move it into the ‘Puppet.settings`. Do not unpack it, just return the location of the package on disk.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vendor/puppet/forge.rb', line 83

def self.get_release_package_from_filesystem(filename)
  if File.exist?(File.expand_path(filename))
    repository = Repository.new('file:///')
    uri = URI.parse("file://#{URI.escape(File.expand_path(filename))}")
    cache_path = repository.retrieve(uri)
  else
    raise ArgumentError, "File does not exists: #{filename}"
  end

  cache_path
end

.get_release_packages_from_repository(install_list) ⇒ Object



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

def self.get_release_packages_from_repository(install_list)
  install_list.map do |release|
    modname, version, file = release
    cache_path = nil
    if file
      begin
        cache_path = repository.retrieve(file)
      rescue OpenURI::HTTPError => e
        raise RuntimeError, "Could not download module: #{e.message}"
      end
    else
      raise RuntimeError, "Malformed response from module repository."
    end
    cache_path
  end
end

.remote_dependency_info(author, mod_name, version) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vendor/puppet/forge.rb', line 45

def self.remote_dependency_info(author, mod_name, version)
  version_string = version ? "&version=#{version}" : ''
  request = Net::HTTP::Get.new("/api/v1/releases.json?module=#{author}/#{mod_name}" + version_string)
  response = repository.make_http_request(request)
  json = PSON.parse(response.body) rescue {}
  case response.code
  when "200"
    return json
  else
    error = json['error'] || ''
    if error =~ /^Module #{author}\/#{mod_name} has no release/
      return []
    else
      raise RuntimeError, "Could not find release information for this module (#{author}/#{mod_name}) (HTTP #{response.code})"
    end
  end
end

.repositoryObject



95
96
97
# File 'lib/vendor/puppet/forge.rb', line 95

def self.repository
  @repository ||= Puppet::Forge::Repository.new
end

.search(term) ⇒ Object

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" => "http://github.com/puppetlabs/puppetlabs-bacula",
  "desc"        => "bacula"
}

]



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

def self.search(term)
  server = Puppet.settings[:module_repository].sub(/^(?!https?:\/\/)/, 'http://')
  Puppet.notice "Searching #{server} ..."
  request = Net::HTTP::Get.new("/modules.json?q=#{URI.escape(term)}")
  response = repository.make_http_request(request)

  case response.code
  when "200"
    matches = PSON.parse(response.body)
  else
    raise RuntimeError, "Could not execute search (HTTP #{response.code})"
    matches = []
  end

  matches
end