Class: Akabei::Omakase::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/akabei/omakase/s3.rb

Constant Summary collapse

SIG_MIME_TYPE =
'application/pgp-signature'
GZIP_MIME_TYPE =
'application/gzip'
XZ_MIME_TYPE =
'application/x-xz'

Instance Method Summary collapse

Constructor Details

#initialize(aws_config, shell) ⇒ S3

Returns a new instance of S3.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/akabei/omakase/s3.rb', line 4

def initialize(aws_config, shell)
  if aws_config
    require 'aws-sdk-resources'
    @bucket = Aws::S3::Resource.new(
      access_key_id: aws_config['access_key_id'],
      secret_access_key: aws_config['secret_access_key'],
      region: aws_config['region'],
    ).bucket(aws_config['bucket'])
    @write_options = aws_config['write_options']
    @shell = shell
  end
end

Instance Method Details

#after!(config, arch, packages) ⇒ Object



21
22
23
# File 'lib/akabei/omakase/s3.rb', line 21

def after!(config, arch, packages)
  upload_repository(config, arch, packages) if @bucket
end

#before!(config, arch) ⇒ Object



17
18
19
# File 'lib/akabei/omakase/s3.rb', line 17

def before!(config, arch)
  download_repository(config, arch) if @bucket
end

#download_repository(config, arch) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/akabei/omakase/s3.rb', line 25

def download_repository(config, arch)
  get(config.db_path(arch))
  if config.repo_signer
    get(Pathname.new("#{config.db_path(arch)}.sig"))
  end
  get(config.files_path(arch))
  get(config.abs_path(arch))
end

#get(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/akabei/omakase/s3.rb', line 34

def get(path)
  @shell.say("Download #{path}", :blue)
  path.open('wb') do |f|
    @bucket.object(path.to_s).get do |chunk|
      f.write(chunk)
    end
  end
rescue Aws::S3::Errors::NoSuchKey
  @shell.say("S3: #{path} not found", :red)
  FileUtils.rm_f(path)
end

#put(path, mime_type) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/akabei/omakase/s3.rb', line 65

def put(path, mime_type)
  @shell.say("Upload #{path}", :green)
  path.open do |f|
    @bucket.object(path.to_s).put(@write_options.merge(
      body: f,
      content_type: mime_type,
    ))
  end
end

#upload_repository(config, arch, packages) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/akabei/omakase/s3.rb', line 50

def upload_repository(config, arch, packages)
  packages.each do |package|
    put(package.path, XZ_MIME_TYPE)
    if config.package_signer
      put(Pathname.new("#{package.path}.sig"), SIG_MIME_TYPE)
    end
  end
  put(config.abs_path(arch), GZIP_MIME_TYPE)
  put(config.files_path(arch), GZIP_MIME_TYPE)
  put(config.db_path(arch), GZIP_MIME_TYPE)
  if config.repo_signer
    put(Pathname.new("#{config.db_path(arch)}.sig"), SIG_MIME_TYPE)
  end
end