class Bellboy::Installer

Downloads data bags from the remote API for any Cookbook that contains a Bellboy manifest.

Public Class Methods

install(berksfile, options = {}) click to toggle source
# File lib/bellboy/installer.rb, line 26
def install(berksfile, options = {})
  @bellboyfile = options[:bellboyfile]

  # Make sure no one (E.g. rspec) did anything silly
  abort if @bellboyfile.nil?

  local_sources = Bellboy.berks_sources(berksfile)

  local_sources.each do |source|
    Bellboy.logger.debug "Source: #{source.path}"

    if File.exists?(File.join(source.path, @bellboyfile))
      site = berksfile.databags_source

      if site.nil?
        # Try the first source
        site = berksfile.sources.first

        fail Berkshelf::InvalidChefAPILocation if site.nil?

        location = "#{site.uri}/databags"
      else
        location = site.uri
      end

      Bellboy.logger.debug "Using #{site} for databags API location"

      download_databags(source, location)
    end

  end
end

Private Class Methods

download_databags(source, site) click to toggle source
# File lib/bellboy/installer.rb, line 61
def download_databags(source, site)
  Bellboy.logger.log "Downloading databags for #{source.name}"

  path = source.path
  containerpath = File.join(path, 'data_bags')

  begin
    Dir.mkdir(containerpath) unless Dir.exists?(containerpath)
  rescue SystemCallError => ex
    raise Bellboy::DatabagWriteError.new(containerpath), ex
  end

  manifest = File.join(path, @bellboyfile)
  begin
    File.read(manifest).split.each do |line|
      databag, item = line.split('/')

      databagpath = File.join(containerpath, databag)
      Dir.mkdir(databagpath) unless Dir.exists?(databagpath)

      itempath = File.join(databagpath, "#{item}.json")
      if File.exists?(itempath)
        Bellboy.logger.verbose "Skipping download of #{itempath} as it already exists"
      else
        download_item(site, databag, item, itempath)
      end

    end

  rescue SystemCallError, IOError => ex
    raise Bellboy::DatabagReadError.new(manifest), ex
  end
end
download_item(site, databag, item, itempath) click to toggle source
# File lib/bellboy/installer.rb, line 95
def download_item(site, databag, item, itempath)
  location = File.join(site, databag, item)

  Bellboy.logger.log "Downloading data bag item #{databag}/#{item} from '#{location}'"

  response = Faraday.get(location)
  if response.success?
    begin
      Bellboy.logger.debug "Creating data bag item #{itempath}"

      item = File.open(itempath, 'w')
      item.write(response.body)
      item.close
    rescue SystemCallError, IOError => ex
      raise Bellboy::DatabagWriteError.new(databagpath), ex
    end
  else
    Bellboy.logger.log "Failed to download #{location}"
    fail Bellboy::DatabagAPIError, location
  end
end