@@ -212,73 +212,11 @@ def pull_base_image!
212
212
end
213
213
214
214
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' ]
215
+ container = create_build_container
233
216
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
248
-
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
266
-
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" , json )
276
- end
277
- rescue Excon ::Error
278
- logger . error "could not wait successfully for container #{ container } , url: #{ url } "
279
- raise
280
- end
281
-
217
+ start_build_container ( container )
218
+ attach_to_build_container_and_stream_logs ( container )
219
+ wait_for_build_container_to_shut_down ( container )
282
220
yield container
283
221
ensure
284
222
unless keep?
@@ -287,6 +225,73 @@ def build!
287
225
end
288
226
end
289
227
228
+ def create_build_container
229
+ url = client . url ( 'containers' , 'create' )
230
+ args = {
231
+ headers : {
232
+ 'Content-Type' => 'application/json'
233
+ } ,
234
+ path : url ,
235
+ expects : [ 201 ] ,
236
+ body : JSON . generate ( { "Image" => build_image } )
237
+ }
238
+ args [ :query ] = { platform : platform } if platform
239
+ res = client . post ( args )
240
+ JSON . parse ( res . body ) [ 'Id' ]
241
+ rescue Excon ::Error
242
+ logger . error "could not create #{ build_image } , url: #{ url } "
243
+ raise
244
+ end
245
+
246
+ def start_build_container ( container )
247
+ url = client . url ( 'containers' , container , 'start' )
248
+ client . post (
249
+ headers : {
250
+ 'Content-Type' => 'application/json'
251
+ } ,
252
+ path : url ,
253
+ expects : [ 204 ] ,
254
+ body : JSON . generate ( { } )
255
+ )
256
+ rescue Excon ::Error
257
+ logger . error "could not start container #{ container } , url: #{ url } "
258
+ raise
259
+ end
260
+
261
+ def attach_to_build_container_and_stream_logs ( container )
262
+ url = client . url ( 'containers' , container , 'attach?stderr=1&stdout=1&stream=1&logs=1' )
263
+ res = client . post (
264
+ path : url ,
265
+ body : '' ,
266
+ expects : [ 200 ] ,
267
+ middlewares : [
268
+ StreamParser . new ( out , err ) ,
269
+ Excon ::Middleware ::Expects ,
270
+ Excon ::Middleware ::Instrumentor ,
271
+ Excon ::Middleware ::Mock
272
+ ]
273
+ )
274
+ puts "----> #{ res . inspect } <------"
275
+ rescue Excon ::Error
276
+ logger . error "could not attach to container #{ container } , url: #{ url } "
277
+ raise
278
+ end
279
+
280
+ def wait_for_build_container_to_shut_down ( container )
281
+ res = client . post (
282
+ path : client . url ( 'containers' , container , 'wait' ) ,
283
+ expects : [ 200 ] ,
284
+ body : ''
285
+ )
286
+ json = JSON . parse ( res . body )
287
+ if json [ "StatusCode" ] != 0
288
+ raise Fry ::WithData ( "Build failed" , json )
289
+ end
290
+ rescue Excon ::Error
291
+ logger . error "could not wait successfully for container #{ container } , url: #{ url } "
292
+ raise
293
+ end
294
+
290
295
def input_package ( container )
291
296
input = FPM ::Package ::Docker . new (
292
297
logger : logger ,
0 commit comments