module Dpkg::S3::Utils

Utils contains functions will be used in Package and Release modules

Attributes

access_policy[RW]
bucket[RW]
encryption[RW]
gpg_options[RW]
prefix[RW]
s3[RW]
signing_key[RW]

Public Instance Methods

debianize_op(operator) click to toggle source
# File lib/dpkg/s3/utils.rb, line 32
def debianize_op(operator)
  # Operators in debian packaging are <<, <=, =, >= and >>
  # So any operator like < or > must be replaced
  { :< => '<<', :> => '>>' }[operator.to_sym] or operator
end
s3_escape(string) click to toggle source

from fog, Fog::AWS.escape

# File lib/dpkg/s3/utils.rb, line 49
def s3_escape(string)
  string.gsub(/([^a-zA-Z0-9_.\-~+]+)/) do
    "%#{Regexp.last_match(1).unpack('H2' * Regexp.last_match(1).bytesize).join('%').upcase}"
  end
end
s3_exists?(path) click to toggle source
# File lib/dpkg/s3/utils.rb, line 55
def s3_exists?(path)
  Dpkg::S3::Utils.s3.head_object(
    bucket: Dpkg::S3::Utils.bucket,
    key: s3_path(path)
  )
rescue Aws::S3::Errors::NotFound
  false
end
s3_path(path) click to toggle source
# File lib/dpkg/s3/utils.rb, line 44
def s3_path(path)
  File.join(*[Dpkg::S3::Utils.prefix, path].compact)
end
s3_read(path) click to toggle source
# File lib/dpkg/s3/utils.rb, line 64
def s3_read(path)
  Dpkg::S3::Utils.s3.get_object(
    bucket: Dpkg::S3::Utils.bucket,
    key: s3_path(path)
  )[:body].read
rescue Aws::S3::Errors::NoSuchKey
  false
end
s3_remove(path) click to toggle source
# File lib/dpkg/s3/utils.rb, line 105
def s3_remove(path)
  return unless s3_exists?(path)

  Dpkg::S3::Utils.s3.delete_object(
    bucket: Dpkg::S3::Utils.bucket,
    key: s3_path(path)
  )
end
s3_store(path, filename = nil, content_type = 'application/x-debian-package', cache_control = nil, fail_if_exists: false) click to toggle source
# File lib/dpkg/s3/utils.rb, line 73
def s3_store(path, filename = nil, content_type = 'application/x-debian-package',
             cache_control = nil, fail_if_exists: false)
  filename ||= File.basename(path)
  obj = s3_exists?(filename)

  file_md5 = Digest::MD5.file(path)

  # check if the object already exists
  if obj != false
    return if (file_md5.to_s == obj[:etag].gsub('"', '')) || (file_md5.to_s == obj[:metadata]['md5'])
    raise AlreadyExistsError, "file #{filename} already exists with different contents" if fail_if_exists
  end

  options = {
    bucket: Dpkg::S3::Utils.bucket,
    key: s3_path(filename),
    acl: Dpkg::S3::Utils.access_policy,
    content_type: content_type,
    metadata: { 'md5' => file_md5.to_s }
  }
  options[:cache_control] = cache_control unless cache_control.nil?

  # specify if encryption is required
  options[:server_side_encryption] = 'AES256' if Dpkg::S3::Utils.encryption

  # upload the file
  File.open(path) do |f|
    options[:body] = f
    Dpkg::S3::Utils.s3.put_object(options)
  end
end
safesystem(*args) click to toggle source
# File lib/dpkg/s3/utils.rb, line 22
def safesystem(*args)
  success = system(*args)
  unless success
    raise SafeSystemError,
          "'system(#{args.inspect})' failed with error code: #{$CHILD_STATUS.exitstatus}"
  end

  success
end
template(path) click to toggle source
# File lib/dpkg/s3/utils.rb, line 38
def template(path)
  template_file = File.join(File.dirname(__FILE__), 'templates', path)
  template_code = File.read(template_file)
  ERB.new(template_code, nil, '-')
end