Class: Berkshelf::Downloader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/berkshelf/downloader.rb

Overview

Author:

Constant Summary collapse

DEFAULT_LOCATIONS =
[
  {
    type: :site,
    value: Location::OPSCODE_COMMUNITY_API,
    options: Hash.new
  }
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook_store, options = {}) ⇒ Downloader

Returns a new instance of Downloader.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • locations (Array<Hash>)


21
22
23
24
# File 'lib/berkshelf/downloader.rb', line 21

def initialize(cookbook_store, options = {})
  @cookbook_store = cookbook_store
  @locations = options.fetch(:locations, Array.new)
end

Instance Attribute Details

#cookbook_storeString (readonly)

Returns a filepath to download cookbook sources to.

Returns:

  • (String)

    a filepath to download cookbook sources to



16
17
18
# File 'lib/berkshelf/downloader.rb', line 16

def cookbook_store
  @cookbook_store
end

Instance Method Details

#add_location(type, value, options = {}) ⇒ Hash

Create a location hash and add it to the end of the array of locations.

subject.add_location(:chef_api, “chef:8080”, node_name: “reset”, client_key: “/Users/reset/.chef/reset.pem”) =>

[ { type: :chef_api, value: "http://chef:8080/", node_name: "reset", client_key: "/Users/reset/.chef/reset.pem" } ]

Parameters:

  • type (Symbol)
  • value (String, Symbol)
  • options (Hash) (defaults to: {})

Returns:

  • (Hash)


44
45
46
47
48
49
50
51
# File 'lib/berkshelf/downloader.rb', line 44

def add_location(type, value, options = {})
  if has_location?(type, value)
    raise DuplicateLocationDefined,
      "A default '#{type}' location with the value '#{value}' is already defined"
  end

  @locations.push(type: type, value: value, options: options)
end

#download(source) ⇒ Array

Downloads the given CookbookSource.

Parameters:

Returns:

  • (Array)

    an array containing the downloaded CachedCookbook and the Location used to download the cookbook



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/berkshelf/downloader.rb', line 69

def download(source)
  cached_cookbook, location = if source.location
    begin
      [source.location.download(storage_path), source.location]
    rescue
      Berkshelf.formatter.error "Failed to download #{source.name} from #{source.location}"

      raise
    end
  else
    search_locations(source)
  end

  source.cached_cookbook = cached_cookbook

  [cached_cookbook, location]
end

#has_location?(type, value) ⇒ Boolean

Checks the list of default locations if a location of the given type and value has already been added and returns true or false.

Returns:

  • (Boolean)


57
58
59
# File 'lib/berkshelf/downloader.rb', line 57

def has_location?(type, value)
  @locations.select { |loc| loc[:type] == type && loc[:value] == value }.any?
end

#locationsArray<Hash>

Returns an Array of Hashes representing each default location that can be used to attempt to download cookbook sources which do not have an explicit location. An array of default locations will be used if no locations are explicitly added by the #add_location function.

Returns:

  • (Array<Hash>)

    an Array of Hashes representing each default location that can be used to attempt to download cookbook sources which do not have an explicit location. An array of default locations will be used if no locations are explicitly added by the #add_location function.



30
31
32
# File 'lib/berkshelf/downloader.rb', line 30

def locations
  @locations.any? ? @locations : DEFAULT_LOCATIONS
end