Class: Capistrano::SCM::S3Archive

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

Defined Under Namespace

Classes: ArchiveObject, LocalCache, RemoteCache

Instance Method Summary collapse

Instance Method Details

#archive_objectObject



131
132
133
134
135
136
137
# File 'lib/capistrano/scm/s3_archive.rb', line 131

def archive_object
  @archive_object ||= ArchiveObject.new(repo_url: fetch(:repo_url),
                                        version_id: fetch(:s3_archive_object_version_id),
                                        sort_proc: fetch(:s3_archive_sort_proc),
                                        branch: fetch(:branch),
                                        client_options: fetch(:s3_archive_client_options))
end

#cleanup_local_cacheObject



95
96
97
# File 'lib/capistrano/scm/s3_archive.rb', line 95

def cleanup_local_cache
  local_cache.cleanup(keep: fetch(:keep_releases))
end

#cleanup_shared_pathObject



127
128
129
# File 'lib/capistrano/scm/s3_archive.rb', line 127

def cleanup_shared_path
  remote_cache.cleanup(keep: fetch(:keep_releases))
end

#current_revisionObject



66
67
68
# File 'lib/capistrano/scm/s3_archive.rb', line 66

def current_revision
  archive_object.current_revision
end

#define_tasksObject



17
18
19
# File 'lib/capistrano/scm/s3_archive.rb', line 17

def define_tasks
  eval_rakefile File.expand_path("tasks/s3_archive.rake", __dir__)
end

#deploy_to_release_pathObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/capistrano/scm/s3_archive.rb', line 70

def deploy_to_release_path
  case strategy
  when :direct
    archive_file = remote_cache.target_file
    case archive_file
    when /\.zip\?.*\Z/
      backend.execute :unzip, "-q -d", release_path, archive_file
    when /\.tar\.gz\?.*\Z|\.tar\.bz2\?.*\Z|\.tgz\?.*\Z/
      backend.execute :tar, "xf", archive_file, "-C", release_path
    end
  when :rsync
    link_option = if fetch(:s3_archive_hardlink_release) && backend.test("[ `readlink #{current_path}` != #{release_path} ]")
                    "--link-dest `readlink #{current_path}`"
                  end
    create_release = %[rsync #{fetch(:s3_archive_remote_rsync_copy_option)} #{link_option} "#{rsync_cache_dir}/" "#{release_path}/"]
    backend.execute create_release
  end
end

#download_to_local_cacheObject

for rsync



90
91
92
93
# File 'lib/capistrano/scm/s3_archive.rb', line 90

def download_to_local_cache
  local_cache.download
  local_cache.extract
end

#download_to_shared_pathObject

for direct



123
124
125
# File 'lib/capistrano/scm/s3_archive.rb', line 123

def download_to_shared_path
  remote_cache.download
end

#local_cacheObject



147
148
149
150
151
152
153
154
# File 'lib/capistrano/scm/s3_archive.rb', line 147

def local_cache
  @local_cache ||= LocalCache.new(
    backend,
    File.join(fetch(:s3_archive_local_download_dir), fetch(:stage).to_s),
    File.join(fetch(:s3_archive_local_cache_dir), fetch(:stage).to_s),
    archive_object
  )
end

#local_checkObject



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

def local_check
  archive_object.check_access!
end

#register_hooksObject



21
22
23
24
25
# File 'lib/capistrano/scm/s3_archive.rb', line 21

def register_hooks
  after "deploy:new_release_path", "s3_archive:create_release"
  before "deploy:check", "s3_archive:check"
  before "deploy:set_current_revision", "s3_archive:set_current_revision"
end

#remote_cacheObject



139
140
141
142
143
144
145
# File 'lib/capistrano/scm/s3_archive.rb', line 139

def remote_cache
  @remote_cache ||= RemoteCache.new(
    backend,
    File.join(fetch(:s3_archive_remote_cache_dir), fetch(:stage).to_s),
    archive_object
  )
end

#remote_checkObject



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

def remote_check
  case strategy
  when :direct
    backend.execute :aws, "s3", "ls", ["s3:/", archive_object.bucket, archive_object.key].join("/")
  when :rsync
    backend.execute :echo, "ssh connected"
  end
end

#rsync_cache_dirObject



118
119
120
# File 'lib/capistrano/scm/s3_archive.rb', line 118

def rsync_cache_dir
  File.join(deploy_to, fetch(:s3_archive_rsync_cache_dir))
end

#set_defaultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/capistrano/scm/s3_archive.rb', line 27

def set_defaults
  set_if_empty :s3_archive_client_options, {}
  set_if_empty(:s3_archive_sort_proc, ->(new, old) { old.key <=> new.key })
  set_if_empty :s3_archive_strategy, :rsync
  set_if_empty :s3_archive_object_version_id, nil

  # strategy direct (alpha)
  set_if_empty :s3_archive_remote_cache_dir, -> { File.join(shared_path, "archives") }

  # strategy rsync
  set_if_empty :s3_archive_skip_download, nil
  set_if_empty :s3_archive_local_download_dir, "tmp/archives"
  set_if_empty :s3_archive_local_cache_dir, "tmp/deploy"
  set_if_empty :s3_archive_remote_rsync_options, ['-az', '--delete']
  set_if_empty :s3_archive_remote_rsync_ssh_options, []
  set_if_empty :s3_archive_remote_rsync_runner_options, {}
  set_if_empty :s3_archive_rsync_cache_dir, "shared/deploy"
  set_if_empty :s3_archive_hardlink_release, false
  set_if_empty :s3_archive_remote_rsync_copy_option, "--archive --acls --xattrs"
end

#strategyObject



62
63
64
# File 'lib/capistrano/scm/s3_archive.rb', line 62

def strategy
  @strategy ||= fetch(:s3_archive_strategy)
end

#transfer_sources(dest) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/capistrano/scm/s3_archive.rb', line 99

def transfer_sources(dest)
  rsync_options = []
  rsync_options.concat fetch(:s3_archive_remote_rsync_options, [])
  rsync_options << local_cache.cache_dir + "/"

  if dest.local?
    rsync_options << '--no-compress'
    rsync_options << rsync_cache_dir
  else
    rsync_ssh_options = []
    rsync_ssh_options << dest.ssh_key_option unless dest.ssh_key_option.empty?
    rsync_ssh_options.concat fetch(:s3_archive_remote_rsync_ssh_options)
    rsync_options << "-e 'ssh #{rsync_ssh_options.join(' ')}'" unless rsync_ssh_options.empty?
    rsync_options << "#{dest.}#{dest.hostname}:#{rsync_cache_dir}"
  end

  backend.execute :rsync, *rsync_options
end