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
16
# File 'lib/akabei/omakase/s3.rb', line 4

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

Instance Method Details

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



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

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

#before!(config, arch) ⇒ Object



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

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

#download_repository(config, arch) ⇒ Object



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

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



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

def get(path)
  @shell.say("Download #{path}", :blue)
  @s3.get_object(
    bucket: @bucket_name,
    key: path.to_s,
    response_target: path,
  )
rescue Aws::S3::Errors::NoSuchKey
  @shell.say("S3: #{path} not found", :red)
  FileUtils.rm_f(path)
end

#put(path, mime_type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/akabei/omakase/s3.rb', line 66

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

#upload_repository(config, arch, packages) ⇒ Object



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

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