class Berkshelf::CommunityREST

def download(name, version)

Returns:
  • (String, nil) -

Parameters:
  • version (String) --
  • name (String) --
def download(name, version)
  archive = stream(find(name, version)["file"])
  scratch = Dir.mktmpdir
  extracted = self.class.unpack(archive.path, scratch)
  if File.cookbook?(extracted)
    extracted
  else
    Dir.glob("#{extracted}/*").find do |dir|
      File.cookbook?(dir)
    end
  end
ensure
  archive.unlink unless archive.nil?
end

def find(name, version)

def find(name, version)
  body = connection.get("cookbooks/#{name}/versions/#{self.class.uri_escape_version(version)}")
  # Artifactory responds with a 200 and blank body for unknown cookbooks.
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil?
  body
rescue CookbookNotFound
  raise
rescue Berkshelf::APIClient::ServiceNotFound
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'")
rescue
  raise CommunitySiteError.new(api_uri, "'#{name}' (#{version})")
end

def initialize(uri = V1_API, options = {})

Options Hash: (**options)
  • :retry_interval (Float) --
  • :retries (Integer) --

Parameters:
  • uri (String) -- (CommunityREST::V1_API)
def initialize(uri = V1_API, options = {})
  options = options.dup
  options = { retries: 5, retry_interval: 0.5, ssl: Berkshelf::Config.instance.ssl }.merge(options)
  @api_uri = uri
  options[:server_url] = uri
  @retries = options.delete(:retries)
  @retry_interval = options.delete(:retry_interval)
  @connection = Berkshelf::RidleyCompatJSON.new(**options)
end

def is_gzip_file(path)

def is_gzip_file(path)
  # You cannot write "\x1F\x8B" because the default encoding of
  # ruby >= 1.9.3 is UTF-8 and 8B is an invalid in UTF-8.
  IO.binread(path, 2) == [0x1F, 0x8B].pack("C*")
end

def is_tar_file(path)

def is_tar_file(path)
  IO.binread(path, 8, 257).to_s == "ustar\x0000"
end

def latest_version(name)

Returns:
  • (String) -
def latest_version(name)
  body = connection.get("cookbooks/#{name}")
  # Artifactory responds with a 200 and blank body for unknown cookbooks.
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil?
  self.class.version_from_uri body["latest_version"]
rescue Berkshelf::APIClient::ServiceNotFound
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'")
rescue
  raise CommunitySiteError.new(api_uri, "the latest version of '#{name}'")
end

def satisfy(name, constraint)

Returns:
  • (String) -

Parameters:
  • constraint (String, Semverse::Constraint) --
  • name (String) --
def satisfy(name, constraint)
  Semverse::Constraint.satisfy_best(constraint, versions(name)).to_s
rescue Semverse::NoSolutionError
  nil
end

def stream(target)

Returns:
  • (Tempfile) -

Parameters:
  • target (String) --
def stream(target)
  local = Tempfile.new("community-rest-stream")
  local.binmode
  Retryable.retryable(tries: retries, on: Berkshelf::APIClientError, sleep: retry_interval) do
    connection.streaming_request(target, {}, local)
  end
ensure
  local.close(false) unless local.nil?
end

def unpack(target, destination)

Returns:
  • (String) -

Parameters:
  • destination (String) --
  • target (String) --
def unpack(target, destination)
  if is_gzip_file(target) || is_tar_file(target)
    Mixlib::Archive.new(target).extract(destination)
  else
    raise Berkshelf::UnknownCompressionType.new(target, destination)
  end
  destination
end

def uri_escape_version(version)

Returns:
  • (String) -

Parameters:
  • version (String) --
def uri_escape_version(version)
  version.to_s.tr(".", "_")
end

def version_from_uri(uri)

Returns:
  • (String) -

Parameters:
  • uri (String) --
def version_from_uri(uri)
  File.basename(uri.to_s).tr("_", ".")
end

def versions(name)

Returns:
  • (Array) -

Parameters:
  • name (String) --
def versions(name)
  body = connection.get("cookbooks/#{name}")
  # Artifactory responds with a 200 and blank body for unknown cookbooks.
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'") if body.nil?
  body["versions"].collect do |version_uri|
    self.class.version_from_uri(version_uri)
  end
rescue Berkshelf::APIClient::ServiceNotFound
  raise CookbookNotFound.new(name, nil, "at `#{api_uri}'")
rescue
  raise CommunitySiteError.new(api_uri, "versions of '#{name}'")
end