Class: Berkshelf::Downloader

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

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>)


23
24
25
26
# File 'lib/berkshelf/downloader.rb', line 23

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



18
19
20
# File 'lib/berkshelf/downloader.rb', line 18

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)


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

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

Download the given CookbookSource.

Parameters:

Returns:

  • (Array)

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

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/berkshelf/downloader.rb', line 71

def download(source)
  if source.location
    begin
      location = source.location
      cached   = download_location(source, location, true)
      source.cached_cookbook = cached

      return [cached, location]
    rescue => e
      raise if e.kind_of?(CookbookValidationFailure)
      Berkshelf.formatter.error "Failed to download '#{source.name}' from #{source.location}"
    end
  else
    locations.each do |loc|
      options = loc[:options].merge(loc[:type] => loc[:value])
      location = Location.init(source.name, source.version_constraint, options)

      cached = download_location(source, location)
      if cached
        source.cached_cookbook = cached
        return [cached, location]
      end
    end
  end

  raise CookbookNotFound, "Cookbook '#{source.name}' not found in any of the default locations"
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)


59
60
61
# File 'lib/berkshelf/downloader.rb', line 59

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.



32
33
34
# File 'lib/berkshelf/downloader.rb', line 32

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