capistrano-s3_archive 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a65462e28020e98554628d02a2bc8a109970fa4
4
- data.tar.gz: 361c7fe4058246b23d276252063257622c30e2b9
3
+ metadata.gz: 270d0445f78fdccee13f4d069148a4c73fef1dd0
4
+ data.tar.gz: 269e6176739259c329cd6c401ddc3326b6625317
5
5
  SHA512:
6
- metadata.gz: 2066614fc32d715b597ddac1a00db38d74ab33bb8b6558669031600ceeb58a1e1d7f0ec0ea43e9c227be29c95ee4b7ebb67aae96edfda652f98047b2c5b5539d
7
- data.tar.gz: f133689db1b32b029505f5fb0064c91d24fd9ae9a7d9a8acf197a95129ee1b65751b00c7782a8d6950137b7136f04dc3beba3ebdc06ac07febd71adfd7b1dc5d
6
+ metadata.gz: 150b7fea2b9bf4fe5c51a61d567252792c7435e7a0537a390c496bf9f4c6ea959286925e2b0f1a6942e35dba28b6b294dc84f19e976aa4626459e583e233ac81
7
+ data.tar.gz: d8978b666b74a52d118f60323b427f282dde0332615b7c80c7b875d7e67c8a1a224da98144b99ef6d95e09aa5b2017da830343635b665c4fea89066430bc94df
@@ -65,35 +65,47 @@ module Capistrano
65
65
  ### Default strategy
66
66
  private
67
67
  module RsyncStrategy
68
+ class MissingSSHKyesError < StandardError; end
69
+ class ResourceBusyError < StandardError; end
70
+
68
71
  def check
69
72
  list_objects(false)
73
+ ssh_key = context.host.keys.first || Array(context.host.ssh_options[:keys]).first
74
+ if ssh_key.nil?
75
+ fail MissingSSHKyesError, "#{RsyncStrategy} only supports publickey authentication. Please set #{context.host.hostname}.keys or ssh_options."
76
+ end
70
77
  end
71
78
 
72
79
  def stage
73
- archive_file = File.join(fetch(:s3_archive), fetch(:stage).to_s, File.basename(archive_object_key))
74
- if not File.exist?(archive_file)
75
- mkdir_p(File.dirname(archive_file))
76
- content = get_object.body.read
77
- File.open(archive_file, 'w') do |f|
78
- f.write(content)
80
+ stage_lock do
81
+ archive_file = File.join(fetch(:s3_archive), fetch(:stage).to_s, File.basename(archive_object_key))
82
+ tmp_file = "#{archive_file}.part"
83
+ fail "#{tmp_file} is found. Another process is running?" if File.exist?(tmp_file)
84
+ if not File.exist?(archive_file)
85
+ File.open(tmp_file, 'w') do |f|
86
+ mkdir_p(File.dirname(archive_file))
87
+ content = get_object.body.read
88
+ f.write(content)
89
+ end
90
+ move(tmp_file, archive_file)
91
+ else
92
+ context.info "#{archive_file} is found."
79
93
  end
80
- else
81
- context.info "#{archive_file} is found."
82
- end
83
94
 
84
- remove_entry_secure(fetch(:local_cache_dir)) if File.exist? fetch(:local_cache_dir)
85
- mkdir_p(fetch(:local_cache_dir))
86
- case archive_file
87
- when /\.zip\Z/
88
- dir = archive_file.gsub(/\.zip\Z/, '')
89
- cmd = "unzip -q -d #{fetch(:local_cache_dir)} #{archive_file}"
90
- when /\.tar\.gz\Z|\.tar\.bz2\Z/
91
- dir = archive_file.gsub(/\.tar\.gz\Z|\.tar\.bz2\Z/, '')
92
- cmd = "tar xf #{archive_file} -C #{fetch(:local_cache_dir)}"
93
- end
95
+ remove_entry_secure(fetch(:local_cache_dir)) if File.exist? fetch(:local_cache_dir)
96
+ mkdir_p(fetch(:local_cache_dir))
97
+ case archive_file
98
+ when /\.zip\Z/
99
+ cmd = "unzip -q -d #{fetch(:local_cache_dir)} #{archive_file}"
100
+ when /\.tar\.gz\Z|\.tar\.bz2\Z/
101
+ cmd = "tar xf #{archive_file} -C #{fetch(:local_cache_dir)}"
102
+ end
94
103
 
95
- run_locally do
96
- execute cmd
104
+ release_lock(true) do
105
+ run_locally do
106
+ execute cmd
107
+ end
108
+ end
97
109
  end
98
110
  end
99
111
 
@@ -108,8 +120,10 @@ module Capistrano
108
120
  rsync << "-e 'ssh -i #{key} #{ssh_port_option}'"
109
121
  rsync << "#{user}#{server.hostname}:#{rsync_cache || release_path}"
110
122
 
111
- run_locally do
112
- execute *rsync
123
+ release_lock do
124
+ run_locally do
125
+ execute *rsync
126
+ end
113
127
  end
114
128
 
115
129
  unless fetch(:rsync_cache).nil?
@@ -131,6 +145,35 @@ module Capistrano
131
145
  cache = deploy_to + "/" + cache if cache && cache !~ /^\//
132
146
  cache
133
147
  end
148
+
149
+ def stage_lock(&block)
150
+ lockfile = "#{fetch(:local_cache)}.#{fetch(:stage)}.lock"
151
+ File.open(lockfile, "w") do |f|
152
+ if f.flock(File::LOCK_EX | File::LOCK_NB)
153
+ block.call
154
+ else
155
+ fail ResourceBusyError, "Could not get #{lockfile}"
156
+ end
157
+ end
158
+ ensure
159
+ rm lockfile if File.exist? lockfile
160
+ end
161
+
162
+ def release_lock(exclusive = false, &block)
163
+ lockfile = "#{fetch(:local_cache)}.#{fetch(:stage)}.release.lock"
164
+ File.open(lockfile, File::RDONLY|File::CREAT) do |f|
165
+ mode = if exclusive
166
+ File::LOCK_EX | File::LOCK_NB
167
+ else
168
+ File::LOCK_SH
169
+ end
170
+ if f.flock(mode)
171
+ block.call
172
+ else
173
+ fail ResourceBusyError, "Could not get #{fetch(:lockfile)}"
174
+ end
175
+ end
176
+ end
134
177
  end
135
178
  end
136
179
  end
@@ -1,5 +1,5 @@
1
1
  module Capistrano
2
2
  module S3Archive
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capistrano-s3_archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takuto Komazaki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-28 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano