Module: Deb::S3::Utils

Included in:
Manifest, Package, Package, Release
Defined in:
lib/deb/s3/utils.rb

Defined Under Namespace

Classes: AlreadyExistsError, SafeSystemError

Class Method Summary collapse

Class Method Details

.access_policyObject



11
# File 'lib/deb/s3/utils.rb', line 11

def access_policy; @access_policy end

.access_policy=(v) ⇒ Object



12
# File 'lib/deb/s3/utils.rb', line 12

def access_policy= v; @access_policy = v end

.bucketObject



9
# File 'lib/deb/s3/utils.rb', line 9

def bucket; @bucket end

.bucket=(v) ⇒ Object



10
# File 'lib/deb/s3/utils.rb', line 10

def bucket= v; @bucket = v end

.debianize_op(op) ⇒ Object



33
34
35
36
37
# File 'lib/deb/s3/utils.rb', line 33

def debianize_op(op)
  # Operators in debian packaging are <<, <=, =, >= and >>
  # So any operator like < or > must be replaced
  {:< => "<<", :> => ">>"}[op.to_sym] or op
end

.encryptionObject



19
# File 'lib/deb/s3/utils.rb', line 19

def encryption; @encryption end

.encryption=(v) ⇒ Object



20
# File 'lib/deb/s3/utils.rb', line 20

def encryption= v; @encryption = v end

.gpg_optionsObject



15
# File 'lib/deb/s3/utils.rb', line 15

def gpg_options; @gpg_options end

.gpg_options=(v) ⇒ Object



16
# File 'lib/deb/s3/utils.rb', line 16

def gpg_options= v; @gpg_options = v end

.prefixObject



17
# File 'lib/deb/s3/utils.rb', line 17

def prefix; @prefix end

.prefix=(v) ⇒ Object



18
# File 'lib/deb/s3/utils.rb', line 18

def prefix= v; @prefix = v end

.s3Object



7
# File 'lib/deb/s3/utils.rb', line 7

def s3; @s3 end

.s3=(v) ⇒ Object



8
# File 'lib/deb/s3/utils.rb', line 8

def s3= v; @s3 = v end

.s3_escape(string) ⇒ Object

from fog, Fog::AWS.escape



50
51
52
53
54
# File 'lib/deb/s3/utils.rb', line 50

def s3_escape(string)
  string.gsub(/([^a-zA-Z0-9_.\-~+]+)/) {
    "%" + $1.unpack("H2" * $1.bytesize).join("%").upcase
  }
end

.s3_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/deb/s3/utils.rb', line 56

def s3_exists?(path)
  Deb::S3::Utils.s3.head_object(
    :bucket => Deb::S3::Utils.bucket,
    :key => s3_path(path),
  )
rescue Aws::S3::Errors::NotFound
  false
end

.s3_path(path) ⇒ Object



45
46
47
# File 'lib/deb/s3/utils.rb', line 45

def s3_path(path)
  File.join(*[Deb::S3::Utils.prefix, path].compact)
end

.s3_read(path) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/deb/s3/utils.rb', line 65

def s3_read(path)
  Deb::S3::Utils.s3.get_object(
    :bucket => Deb::S3::Utils.bucket,
    :key => s3_path(path),
  )[:body].read
rescue Aws::S3::Errors::NoSuchKey
  nil
end

.s3_remove(path) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/deb/s3/utils.rb', line 107

def s3_remove(path)
  if s3_exists?(path)
    Deb::S3::Utils.s3.delete_object(
      :bucket =>Deb::S3::Utils.bucket,
      :key => s3_path(path),
    )
  end
end

.s3_store(path, filename = nil, content_type = 'application/octet-stream; charset=binary', cache_control = nil, fail_if_exists = false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/deb/s3/utils.rb', line 74

def s3_store(path, filename=nil, content_type='application/octet-stream; charset=binary', cache_control=nil, fail_if_exists=false)
  filename = File.basename(path) unless filename
  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('"', '') or file_md5.to_s == obj[:metadata]['md5'])
    raise AlreadyExistsError, "file #{filename} already exists with different contents" if fail_if_exists
  end

  options = {
    :bucket => Deb::S3::Utils.bucket,
    :key => s3_path(filename),
    :acl => Deb::S3::Utils.access_policy,
    :content_type => content_type,
    :metadata => { "md5" => file_md5.to_s },
  }
  if !cache_control.nil?
    options[:cache_control] = cache_control
  end

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

  # upload the file
  File.open(path) do |f|
    options[:body] = f
    Deb::S3::Utils.s3.put_object(options)
  end
end

.safesystem(*args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/deb/s3/utils.rb', line 25

def safesystem(*args)
  success = system(*args)
  if !success
    raise SafeSystemError, "'system(#{args.inspect})' failed with error code: #{$?.exitstatus}"
  end
  return success
end

.signing_keyObject



13
# File 'lib/deb/s3/utils.rb', line 13

def signing_key; @signing_key end

.signing_key=(v) ⇒ Object



14
# File 'lib/deb/s3/utils.rb', line 14

def signing_key= v; @signing_key = v end

.template(path) ⇒ Object



39
40
41
42
43
# File 'lib/deb/s3/utils.rb', line 39

def template(path)
  template_file = File.join(File.dirname(__FILE__), "templates", path)
  template_code = File.read(template_file)
  ERB.new(template_code, trim_mode: "-")
end