Class: Capistrano::SCM::S3Archive::ArchiveObject

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/scm/s3_archive/archive_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_url: nil, version_id: nil, sort_proc: nil, branch: :latest, client_options: {}) ⇒ ArchiveObject

Returns a new instance of ArchiveObject.



10
11
12
13
14
15
16
17
18
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 10

def initialize(repo_url: nil, version_id: nil, sort_proc: nil, branch: :latest, client_options: {})
  uri = URI.parse(repo_url)
  @bucket = uri.host
  @prefix = uri.path.sub(%r{/?\Z}, '/').slice(1..-1) # normalize path
  @version_id = version_id
  @sort_proc = sort_proc
  @branch = branch
  @client = Aws::S3::Client.new(client_options)
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def branch
  @branch
end

#bucketObject (readonly)

Returns the value of attribute bucket.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def bucket
  @bucket
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def client
  @client
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def prefix
  @prefix
end

#sort_procObject (readonly)

Returns the value of attribute sort_proc.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def sort_proc
  @sort_proc
end

#version_idObject (readonly)

Returns the value of attribute version_id.



8
9
10
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 8

def version_id
  @version_id
end

Instance Method Details

#check_access!Object



20
21
22
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 20

def check_access!
  client.list_objects(bucket: bucket, prefix: prefix)
end

#current_revisionObject



52
53
54
55
56
57
58
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 52

def current_revision
  if version_id
    "#{key}?versionid=#{version_id}"
  else
    key
  end
end

#etagObject



48
49
50
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 48

def etag
  .tap { |it| raise "No such object: #{current_revision}" if it.nil? }.etag
end

#get_object(io) ⇒ Object



68
69
70
71
72
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 68

def get_object(io)
  options = { bucket: bucket, key: key }
  options[:version_id] = version_id if version_id
  client.get_object(options, target: io)
end

#keyObject



24
25
26
27
28
29
30
31
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 24

def key
  @key ||= case branch.to_sym
           when :master, :latest
             latest_key
           else
             prefix + branch.to_s
           end
end

#key_basenameObject



33
34
35
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 33

def key_basename
  File.basename(key)
end

#latest_keyObject



37
38
39
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 37

def latest_key
  list_all_objects.min(&sort_proc).key
end

#list_all_objectsObject



41
42
43
44
45
46
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 41

def list_all_objects
  response = client.list_objects(bucket: bucket, prefix: prefix)
  response.inject([]) do |objects, page|
    objects + page.contents
  end
end

#metadataObject



60
61
62
63
64
65
66
# File 'lib/capistrano/scm/s3_archive/archive_object.rb', line 60

def 
  client.list_object_versions(bucket: bucket, prefix: key).versions.find do |v|
    if version_id then v.version_id == version_id
    else v.is_latest
    end
  end
end