Class: Capistrano::SCM::S3Archive::RemoteCache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, download_dir, archive_object) ⇒ RemoteCache

Returns a new instance of RemoteCache.



7
8
9
10
11
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 7

def initialize(backend, download_dir, archive_object)
  @backend = backend
  @download_dir = download_dir
  @archive_object = archive_object
end

Instance Attribute Details

#archive_objectObject (readonly)

Returns the value of attribute archive_object.



5
6
7
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 5

def archive_object
  @archive_object
end

#backendObject (readonly)

Returns the value of attribute backend.



5
6
7
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 5

def backend
  @backend
end

#download_dirObject (readonly)

Returns the value of attribute download_dir.



5
6
7
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 5

def download_dir
  @download_dir
end

Instance Method Details

#cleanup(keep: 0) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 27

def cleanup(keep: 0)
  downloaded_files = backend.capture(:ls, "-xtr", download_dir).split
  return if downloaded_files.count <= keep

  to_be_removes = (downloaded_files - downloaded_files.last(keep)).flat_map do |file|
    [File.join(download_dir, file), File.join(download_dir, ".#{f}.etag")]
  end
  backend.execute(:rm, '-f', *to_be_removes)
end

#downloadObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/capistrano/scm/s3_archive/remote_cache.rb', line 13

def download
  etag_file = File.join(download_dir, ".#{archive_object.key_basename}.etag")
  if backend.test("[ -f #{target_file} -a -f #{etag_file} ]") &&
     backend.capture(:cat, etag_file) == archive_object.etag
    backend.info "#{target_file} (etag:#{archive_object.etag}) is found. download skipped."
  else
    backend.info "Download s3://#{archive_object.bucket}/#{archive_object.key} to #{target_file}"
    backend.execute(:mkdir, "-p", download_dir)
    backend.execute(:aws, *['s3api', 'get-object', "--bucket #{archive_object.bucket}", "--key #{archive_object.key}", archive_object.version_id ? "--version-id #{archive_object.version_id}" : nil, target_file].compact)

    backend.execute(:echo, "-n", "'#{archive_object.etag}'", "|tee", etag_file)
  end
end

#target_fileObject



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

def target_file
  basename = [archive_object.key_basename, archive_object.version_id].join('__')
  File.join(download_dir, basename)
end