fpm-fry 0.7.2.1 → 0.7.3

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
  SHA256:
3
- metadata.gz: 4775633d070116e35a3bdfb7bdb0359d8af49b29b08f9b68e862b5dfd6a0a91d
4
- data.tar.gz: b6b317af4559f1deeca752e42a96c9973885e1014caa0fd5fac22d6653644617
3
+ metadata.gz: 0544f44b67264b68111bb6f5a96e8132a423e8816afd6367fede81aa505bf1aa
4
+ data.tar.gz: 1e987b5493f30d69e0cf7501dcf4acec92f5190825f701c0401f9e1d72df97f3
5
5
  SHA512:
6
- metadata.gz: c05ab42fc6a12903c1987313a07cdb27b7fcbd46bf3b995f20745a7da7c358b1e00ffde004eeebdcf99a4ebe05069fb6f45e9c77b7d0a2de9c19874e9f70a86d
7
- data.tar.gz: af93127bbfc24a0bf3de4250523e86004ec49548b4517e2f55b2ddb3bb30639f669889e2ee8b83bd58a8b21d2ff314b290c60c380be3962ef69882b6752c69c0
6
+ metadata.gz: b32603c869cfb37fb0062996affc6dddee51d3bd55d92c2fb6ce16560f18fb078c3399626537d8c79a8f57db72c56876bfc7fa18563cb4052505255e36caf7b0
7
+ data.tar.gz: 991eff30e175e76f634a120ee8fe14c45c7a04034f66af39bc24912c91b7d1c7e928b9de7ca272d205540432408cd1c9fc0c1033753a61020714a13204ad3db0
@@ -22,6 +22,10 @@ class FPM::Fry::Client
22
22
  include FPM::Fry::WithData
23
23
  end
24
24
 
25
+ class ContainerDeletionFailed < StandardError
26
+ include FPM::Fry::WithData
27
+ end
28
+
25
29
  # Raised when trying to read file that can't be read e.g. because it's a
26
30
  # directory.
27
31
  class NotAFile < StandardError
@@ -111,7 +115,7 @@ class FPM::Fry::Client
111
115
  )
112
116
  end
113
117
  rescue Excon::Error => e
114
- @logger.error("unexpected response when reading resource: url: #{url}, error: #{e}")
118
+ logger.error("unexpected response when reading resource: url: #{url}, error: #{e}")
115
119
  raise
116
120
  end
117
121
  if [404,500].include? res.status
@@ -165,14 +169,14 @@ class FPM::Fry::Client
165
169
  end
166
170
 
167
171
  def copy(name, resource, map, options = {})
168
- ex = FPM::Fry::Tar::Extractor.new(logger: @logger)
172
+ ex = FPM::Fry::Tar::Extractor.new(logger: logger)
169
173
  base = File.dirname(resource)
170
174
  read(name, resource) do | entry |
171
175
  file = File.join(base, entry.full_name).chomp('/')
172
176
  file = file.sub(%r"\A\./",'')
173
177
  to = map[file]
174
178
  next unless to
175
- @logger.debug("Copy",name: file, to: to)
179
+ logger.debug("Copy",name: file, to: to)
176
180
  ex.extract_entry(to, entry, options)
177
181
  end
178
182
  end
@@ -182,7 +186,7 @@ class FPM::Fry::Client
182
186
  res = agent.get(path: url, expects: [200, 204])
183
187
  return JSON.parse(res.body)
184
188
  rescue Excon::Error => e
185
- @logger.error("could not retrieve changes for: #{name}, url: #{url}, error: #{e}")
189
+ logger.error("could not retrieve changes for: #{name}, url: #{url}, error: #{e}")
186
190
  raise
187
191
  end
188
192
 
@@ -220,27 +224,35 @@ class FPM::Fry::Client
220
224
  )
221
225
  data = JSON.parse(res.body)
222
226
  if res.status != 201
223
- @logger.error(data["message"])
227
+ logger.error(data["message"])
224
228
  if res.status == 404
225
- @logger.info("execute docker pull #{image} first or specify --pull argument for fpm-fry")
229
+ logger.info("execute docker pull #{image} first or specify --pull argument for fpm-fry")
226
230
  end
227
231
  raise ContainerCreationFailed.new("could not create container from #{image}", message: data["message"])
228
232
  end
229
233
  data['Id']
230
234
  rescue Excon::Error => e
231
- @logger.error("could not create container from #{image}, url: #{url}, error: #{e}")
235
+ logger.error("could not create container from #{image}, url: #{url}, error: #{e}")
232
236
  raise
233
237
  end
234
238
 
235
239
  def destroy(container)
236
240
  return unless container
241
+
237
242
  url = self.url('containers', container)
238
- agent.delete(
243
+ res = agent.delete(
239
244
  path: url,
240
- expects: [204]
245
+ expects: [204, 409]
241
246
  )
247
+ return unless res.status == 409
248
+ data = JSON.parse(res.body) rescue ({"message" => "could not parse response body: '#{res.body}'"})
249
+ if data["message"] =~ /removal of container .* is already in progress/
250
+ logger.info(data["message"])
251
+ else
252
+ raise ContainerDeletionFailed.new("could not destroy container #{container}", data)
253
+ end
242
254
  rescue Excon::Error => e
243
- @logger.error("could not destroy container: #{container}, url: #{url}, error: #{e}")
255
+ logger.error("could not destroy container: #{container}, url: #{url}, error: #{e}")
244
256
  raise
245
257
  end
246
258
 
@@ -2,6 +2,10 @@ require 'fpm/fry/command'
2
2
  module FPM; module Fry
3
3
  class Command::Cook < Command
4
4
 
5
+ class BuildFailed < StandardError
6
+ include FPM::Fry::WithData
7
+ end
8
+
5
9
  option '--keep', :flag, 'Keep the container after build'
6
10
  option '--overwrite', :flag, 'Overwrite package', default: true
7
11
  option '--verbose', :flag, 'Verbose output', default: false
@@ -212,79 +216,81 @@ module FPM; module Fry
212
216
  end
213
217
 
214
218
  def build!
215
- body = begin
216
- url = client.url('containers','create')
217
- args = {
218
- headers: {
219
- 'Content-Type' => 'application/json'
220
- },
221
- path: url,
222
- expects: [201],
223
- body: JSON.generate({"Image" => build_image})
224
- }
225
- args[:query] = { platform: platform } if platform
226
- res = client.post(args)
227
- JSON.parse(res.body)
228
- rescue Excon::Error
229
- logger.error "could not create #{build_image}, url: #{url}"
230
- raise
231
- end
232
- container = body['Id']
233
- begin
234
- begin
235
- url = client.url('containers',container,'start')
236
- client.post(
237
- headers: {
238
- 'Content-Type' => 'application/json'
239
- },
240
- path: url,
241
- expects: [204],
242
- body: JSON.generate({})
243
- )
244
- rescue Excon::Error
245
- logger.error "could not start container #{container}, url: #{url}"
246
- raise
247
- end
219
+ container = create_build_container
220
+ start_build_container(container)
221
+ attach_to_build_container_and_stream_logs(container)
222
+ wait_for_build_container_to_shut_down(container)
223
+ yield container
224
+ ensure
225
+ unless keep?
226
+ client.destroy(container) if container
227
+ end
228
+ end
248
229
 
249
- begin
250
- url = client.url('containers',container,'attach?stderr=1&stdout=1&stream=1&logs=1')
251
- client.post(
252
- path: url,
253
- body: '',
254
- expects: [200],
255
- middlewares: [
256
- StreamParser.new(out,err),
257
- Excon::Middleware::Expects,
258
- Excon::Middleware::Instrumentor,
259
- Excon::Middleware::Mock
260
- ]
261
- )
262
- rescue Excon::Error
263
- logger.error "could not attach to container #{container}, url: #{url}"
264
- raise
265
- end
230
+ def create_build_container
231
+ url = client.url('containers','create')
232
+ args = {
233
+ headers: {
234
+ 'Content-Type' => 'application/json'
235
+ },
236
+ path: url,
237
+ expects: [201],
238
+ body: JSON.generate({"Image" => build_image})
239
+ }
240
+ args[:query] = { platform: platform } if platform
241
+ res = client.post(args)
242
+ JSON.parse(res.body)['Id']
243
+ rescue Excon::Error
244
+ logger.error "could not create #{build_image}, url: #{url}"
245
+ raise
246
+ end
266
247
 
267
- begin
268
- res = client.post(
269
- path: client.url('containers',container,'wait'),
270
- expects: [200],
271
- body: ''
272
- )
273
- json = JSON.parse(res.body)
274
- if json["StatusCode"] != 0
275
- raise Fry::WithData("Build failed", exit_code: json["StatusCode"])
276
- end
277
- rescue Excon::Error
278
- logger.error "could not wait successfully for container #{container}, url: #{url}"
279
- raise
280
- end
248
+ def start_build_container(container)
249
+ url = client.url('containers',container,'start')
250
+ client.post(
251
+ headers: {
252
+ 'Content-Type' => 'application/json'
253
+ },
254
+ path: url,
255
+ expects: [204],
256
+ body: JSON.generate({})
257
+ )
258
+ rescue Excon::Error
259
+ logger.error "could not start container #{container}, url: #{url}"
260
+ raise
261
+ end
281
262
 
282
- yield container
283
- ensure
284
- unless keep?
285
- client.destroy(container)
286
- end
263
+ def attach_to_build_container_and_stream_logs(container)
264
+ url = client.url('containers',container,'attach?stderr=1&stdout=1&stream=1&logs=1')
265
+ client.post(
266
+ path: url,
267
+ body: '',
268
+ expects: [200],
269
+ middlewares: [
270
+ StreamParser.new(out,err),
271
+ Excon::Middleware::Expects,
272
+ Excon::Middleware::Instrumentor,
273
+ Excon::Middleware::Mock
274
+ ]
275
+ )
276
+ rescue Excon::Error
277
+ logger.error "could not attach to container #{container}, url: #{url}"
278
+ raise
279
+ end
280
+
281
+ def wait_for_build_container_to_shut_down(container)
282
+ res = client.post(
283
+ path: client.url('containers',container,'wait'),
284
+ expects: [200],
285
+ body: ''
286
+ )
287
+ json = JSON.parse(res.body)
288
+ if json["StatusCode"] != 0
289
+ raise BuildFailed.new("Build script failed with non zero exit code", json)
287
290
  end
291
+ rescue Excon::Error
292
+ logger.error "could not wait successfully for container #{container}, url: #{url}"
293
+ raise
288
294
  end
289
295
 
290
296
  def input_package(container)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fpm-fry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2.1
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Lagresle
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2024-02-09 00:00:00.000000000 Z
16
+ date: 2024-03-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: excon