class Minimart::Download::Cookbook

This class will download a cookbook from one of the inventory specified sources.

Attributes

cookbook[R]

@return [Minimart::Mirror::SourceCookbook] The cookbook to download

Public Class Methods

new(cookbook) click to toggle source

@param [Minimart::Mirror::SourceCookbook] cookbook The cookbook to download

# File lib/minimart/download/cookbook.rb, line 16
def initialize(cookbook)
  @cookbook = cookbook
end

Public Instance Methods

fetch(&block) click to toggle source

Download the cookbook @yield Minimart::Cookbook]

# File lib/minimart/download/cookbook.rb, line 22
def fetch(&block)
  Configuration.output.puts "-- Downloading #{cookbook.name} #{cookbook.version}"

  unless respond_to?("download_#{cookbook.location_type}", true)
    raise Minimart::Error::UnknownLocationType,
      "Minimart cannot download #{cookbook.name} because it has an unknown location type #{cookbook.location_type}"
  end

  begin
    Dir.mktmpdir do |dir|
      send("download_#{cookbook.location_type}", dir)
      block.call(Minimart::Cookbook.from_path(dir)) if block
    end
  rescue Exception => ex
    Configuration.output.puts_yellow %{failed to download #{cookbook.name} #{cookbook.version} with error message \"#{ex.message}\"}
  end
end

Private Instance Methods

download_chef_server(dir) click to toggle source
# File lib/minimart/download/cookbook.rb, line 51
def download_chef_server(dir)
  opts = Minimart::Configuration.chef_server_config.merge(server_url: cookbook.location_path)

  Ridley.open(opts) do |ridley_client|
    ridley_client.cookbook.download(cookbook.name, cookbook.version, dir)
  end
end
download_github(dir) click to toggle source
# File lib/minimart/download/cookbook.rb, line 59
def download_github(dir)
  location_path = cookbook.location_path_uri.path.gsub(/\A\//, '')
  client        = Octokit::Client.new(Minimart::Configuration.github_config)
  url           = client.archive_link(location_path, ref: "v#{cookbook.version}")

  get_archive(url, dir)
end
download_opscode(dir) click to toggle source
# File lib/minimart/download/cookbook.rb, line 42
def download_opscode(dir)
  details = Utils::Http.get_json(cookbook.location_path, "cookbooks/#{cookbook.name}/versions/#{cookbook.web_friendly_version}")
  get_archive(details['file'], dir)
end
download_uri(dir) click to toggle source
# File lib/minimart/download/cookbook.rb, line 47
def download_uri(dir)
  get_archive(cookbook.location_path, dir)
end
get_archive(url, dir) click to toggle source
# File lib/minimart/download/cookbook.rb, line 67
def get_archive(url, dir)
  archive_file = Utils::Http.get_binary("#{cookbook.name}-#{cookbook.version}", url)
  Utils::Archive.extract_archive(archive_file, dir)
end