Class: Envirobly::Aws::S3

Inherits:
Object show all
Defined in:
lib/envirobly/aws/s3.rb

Constant Summary collapse

OBJECTS_PREFIX =
"blobs"
MANIFESTS_PREFIX =
"manifests"
CONCURRENCY =
6

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, region:, credentials: nil) ⇒ S3

Returns a new instance of S3.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/envirobly/aws/s3.rb', line 12

def initialize(bucket:, region:, credentials: nil)
  @region = region
  @bucket = bucket

  client_options = { region: }
  unless credentials.nil?
    client_options.merge! credentials.transform_keys(&:to_sym)
  end

  @client = Aws::S3::Client.new(client_options)
  resource = Aws::S3::Resource.new(client: @client)
  @bucket_resource = resource.bucket(@bucket)
end

Instance Method Details

#pull(object_tree_checksum, target_dir) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/envirobly/aws/s3.rb', line 55

def pull(object_tree_checksum, target_dir)
  puts "Pulling #{object_tree_checksum} into #{target_dir}"

  manifest = fetch_manifest(object_tree_checksum)
  FileUtils.mkdir_p(target_dir)

  puts "Downloading #{manifest.size} files"
  pool = Concurrent::FixedThreadPool.new(CONCURRENCY)

  manifest.each do |(mode, type, object_hash, path)|
    pool.post do
      target_path = File.join target_dir, path

      if mode == Envirobly::Git::Commit::SYMLINK_FILE_MODE
        fetch_symlink(object_hash, target_path:)
      else
        fetch_object(object_hash, target_path:)

        if mode == Envirobly::Git::Commit::EXECUTABLE_FILE_MODE
          FileUtils.chmod("+x", target_path)
        end
      end
    end
  end

  pool.shutdown
  pool.wait_for_termination
end

#push(commit) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/envirobly/aws/s3.rb', line 26

def push(commit)
  if object_exists?(manifest_key(commit.object_tree_checksum))
    print "Build context is already uploaded"
    $stdout.flush
    return
  end

  # puts "Pushing #{commit.object_tree_checksum} to #{@bucket}"

  manifest = []
  objects_count = 0
  objects_to_upload = []
  remote_object_hashes = list_object_hashes

  commit.object_tree.each do |chdir, objects|
    objects.each do |(mode, type, object_hash, path)|
      objects_count += 1
      path = File.join chdir.delete_prefix(commit.working_dir), path
      manifest << [ mode, type, object_hash, path.delete_prefix("/") ]

      next if remote_object_hashes.include?(object_hash)
      objects_to_upload << [ chdir, object_hash ]
    end
  end

  upload_git_objects(objects_to_upload)
  upload_manifest manifest_key(commit.object_tree_checksum), manifest
end