docker-api 1.7.5 → 1.7.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/docker.rb CHANGED
@@ -10,7 +10,7 @@ require 'uri'
10
10
  # The top-level module for this gem. It's purpose is to hold global
11
11
  # configuration variables that are used as defaults in other classes.
12
12
  module Docker
13
- attr_accessor :creds
13
+ attr_accessor :creds, :logger
14
14
 
15
15
  def default_socket_url
16
16
  'unix:///var/run/docker.sock'
@@ -76,8 +76,9 @@ module Docker
76
76
  end
77
77
 
78
78
  module_function :default_socket_url, :env_url, :url, :url=, :options,
79
- :options=, :creds, :creds=, :connection, :reset_connection!,
80
- :version, :info, :authenticate!, :validate_version!
79
+ :options=, :creds, :creds=, :logger, :logger=,
80
+ :connection, :reset_connection!, :version, :info,
81
+ :authenticate!, :validate_version!
81
82
  end
82
83
 
83
84
  require 'docker/version'
@@ -33,7 +33,13 @@ class Docker::Connection
33
33
 
34
34
  # Send a request to the server with the `
35
35
  def request(*args, &block)
36
- resource.request(compile_request_params(*args, &block)).body
36
+ request = compile_request_params(*args, &block)
37
+ if Docker.logger
38
+ Docker.logger.info(
39
+ [request[:method], request[:path], request[:query], request[:body]]
40
+ )
41
+ end
42
+ resource.request(request).body
37
43
  rescue Excon::Errors::BadRequest => ex
38
44
  raise ClientError, ex.message
39
45
  rescue Excon::Errors::Unauthorized => ex
data/lib/docker/image.rb CHANGED
@@ -157,10 +157,12 @@ class Docker::Image
157
157
  end
158
158
 
159
159
  # Given a Dockerfile as a string, builds an Image.
160
- def build(commands, opts = {}, connection = Docker.connection)
161
- body = connection.post(
160
+ def build(commands, opts = {}, connection = Docker.connection, &block)
161
+ body = ""
162
+ connection.post(
162
163
  '/build', opts,
163
- :body => Docker::Util.create_tar('Dockerfile' => commands)
164
+ :body => Docker::Util.create_tar('Dockerfile' => commands),
165
+ :response_block => response_block_for_build(body, &block)
164
166
  )
165
167
  new(connection, Docker::Util.extract_id(body))
166
168
  rescue Docker::Error::ServerError
@@ -168,12 +170,19 @@ class Docker::Image
168
170
  end
169
171
 
170
172
  # Given a directory that contains a Dockerfile, builds an Image.
171
- def build_from_dir(dir, opts = {}, connection = Docker.connection)
173
+ #
174
+ # If a block is passed, chunks of output produced by Docker will be passed
175
+ # to that block.
176
+ def build_from_dir(dir, opts = {}, connection = Docker.connection, &block)
172
177
  tar = Docker::Util.create_dir_tar(dir)
173
- body = connection.post(
178
+
179
+ # The response_block passed to Excon will build up this body variable.
180
+ body = ""
181
+ connection.post(
174
182
  '/build', opts,
175
183
  :headers => { 'Content-Type' => 'application/tar',
176
- 'Transfer-Encoding' => 'chunked' }
184
+ 'Transfer-Encoding' => 'chunked' },
185
+ :response_block => response_block_for_build(body, &block)
177
186
  ) { tar.read(Excon.defaults[:chunk_size]).to_s }
178
187
  new(connection, Docker::Util.extract_id(body))
179
188
  ensure
@@ -200,4 +209,14 @@ class Docker::Image
200
209
 
201
210
  dockerfile
202
211
  end
212
+
213
+ # Generates the block to be passed as a reponse block to Excon. The returned
214
+ # lambda will append Docker output to the first argument, and yield output to
215
+ # the passed block, if a block is given.
216
+ def self.response_block_for_build(body)
217
+ lambda do |chunk, remaining, total|
218
+ body << chunk
219
+ yield chunk if block_given?
220
+ end
221
+ end
203
222
  end
@@ -1,6 +1,6 @@
1
1
  module Docker
2
2
  # The version of the docker-api gem.
3
- VERSION = '1.7.5'
3
+ VERSION = '1.7.6'
4
4
 
5
5
  # The version of the compatible Docker remote API.
6
6
  API_VERSION = '1.6'
@@ -336,6 +336,16 @@ describe Docker::Image do
336
336
  expect(images.first.info["Repository"]).to eq("swipely/base")
337
337
  end
338
338
  end
339
+
340
+ context 'with a block capturing build output' do
341
+ let(:build_output) { "" }
342
+ let(:block) { Proc.new { |chunk| build_output << chunk } }
343
+ let!(:image) { subject.build("FROM base\n", &block) }
344
+
345
+ it 'calls the block and passes build output', :vcr do
346
+ expect(build_output).to start_with('Step 1 : FROM base')
347
+ end
348
+ end
339
349
  end
340
350
  end
341
351
 
@@ -347,8 +357,9 @@ describe Docker::Image do
347
357
  File.join(File.dirname(__FILE__), '..', 'fixtures', 'build_from_dir')
348
358
  }
349
359
  let(:docker_file) { File.new("#{dir}/Dockerfile") }
350
- let(:image) { subject.build_from_dir(dir, opts) }
360
+ let(:image) { subject.build_from_dir(dir, opts, &block) }
351
361
  let(:opts) { {} }
362
+ let(:block) { Proc.new {} }
352
363
  let(:container) do
353
364
  Docker::Container.create('Image' => image.id,
354
365
  'Cmd' => %w[cat /Dockerfile])
@@ -370,6 +381,16 @@ describe Docker::Image do
370
381
  expect(images.first.info["Repository"]).to eq("swipely/base2")
371
382
  end
372
383
  end
384
+
385
+ context 'with a block capturing build output' do
386
+ let(:build_output) { "" }
387
+ let(:block) { Proc.new { |chunk| build_output << chunk } }
388
+
389
+ it 'calls the block and passes build output', :vcr do
390
+ image # Create the image variable, which is lazy-loaded by Rspec
391
+ expect(build_output).to start_with("Step 1 : FROM base")
392
+ end
393
+ end
373
394
  end
374
395
  end
375
396
  end
@@ -0,0 +1,78 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: unix:///var/run/docker.sock/v1.6/build
6
+ body:
7
+ encoding: ISO-8859-1
8
+ string: !binary |-
9
+ RG9ja2VyZmlsZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
11
+ AAAAAAAAAAAAADAwMDA2NDAAMDAwMDAwMAAwMDAwMDAwADAwMDAwMDAwMDEy
12
+ ADEyMjU3Mzc3MTAwADAxMzMwMQAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
13
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
14
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1c3RhcgAwMHdoZWVs
15
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAA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 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
48
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
49
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
50
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
51
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
52
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
53
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
54
+ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
55
+ headers:
56
+ User-Agent:
57
+ - Swipely/Docker-API 1.7.5
58
+ Content-Type:
59
+ - application/json
60
+ response:
61
+ status:
62
+ code: 200
63
+ message:
64
+ headers:
65
+ Date:
66
+ - Fri, 27 Dec 2013 22:25:04 GMT
67
+ Content-Type:
68
+ - text/plain; charset=utf-8
69
+ Connection:
70
+ - close
71
+ Transfer-Encoding:
72
+ - ''
73
+ body:
74
+ encoding: UTF-8
75
+ string: "Step 1 : FROM base\n\r ---> b750fe79269d\n\rSuccessfully built b750fe79269d\n\r"
76
+ http_version:
77
+ recorded_at: Fri, 27 Dec 2013 22:25:04 GMT
78
+ recorded_with: VCR 2.8.0
@@ -0,0 +1,35 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: unix:///var/run/docker.sock/v1.6/build
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Swipely/Docker-API 1.7.5
12
+ Content-Type:
13
+ - application/tar
14
+ Transfer-Encoding:
15
+ - chunked
16
+ response:
17
+ status:
18
+ code: 200
19
+ message:
20
+ headers:
21
+ Date:
22
+ - Tue, 24 Dec 2013 19:58:27 GMT
23
+ Content-Type:
24
+ - text/plain; charset=utf-8
25
+ Connection:
26
+ - close
27
+ Transfer-Encoding:
28
+ - ''
29
+ body:
30
+ encoding: UTF-8
31
+ string: "Step 1 : FROM base\n\r ---> b750fe79269d\n\rStep 2 : ADD / /\n\r --->
32
+ ee5973279d79\n\rSuccessfully built ee5973279d79\n\r"
33
+ http_version:
34
+ recorded_at: Tue, 24 Dec 2013 19:58:27 GMT
35
+ recorded_with: VCR 2.8.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.5
4
+ version: 1.7.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-18 00:00:00.000000000 Z
12
+ date: 2013-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: excon
@@ -222,9 +222,11 @@ files:
222
222
  - spec/vcr/Docker_Container/_wait/when_an_argument_is_given/and_a_command_runs_for_too_long/raises_a_ServerError.yml
223
223
  - spec/vcr/Docker_Container/_wait/when_an_argument_is_given/sets_the_read_timeout_to_that_amount_of_time.yml
224
224
  - spec/vcr/Docker_Image/_all/materializes_each_Image_into_a_Docker_Image.yml
225
+ - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/with_a_block_capturing_build_output/calls_the_block_and_passes_build_output.yml
225
226
  - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/with_specifying_a_repo_in_the_query_parameters/builds_an_image_and_tags_it.yml
226
227
  - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/without_query_parameters/builds_an_image.yml
227
228
  - spec/vcr/Docker_Image/_build/with_an_invalid_Dockerfile/throws_a_UnexpectedResponseError.yml
229
+ - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/calls_the_block_and_passes_build_output.yml
228
230
  - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_no_query_parameters/builds_the_image.yml
229
231
  - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_specifying_a_repo_in_the_query_parameters/builds_the_image_and_tags_it.yml
230
232
  - spec/vcr/Docker_Image/_create/when_the_Image_does_not_yet_exist_and_the_body_is_a_Hash/sets_the_id.yml
@@ -312,9 +314,11 @@ test_files:
312
314
  - spec/vcr/Docker_Container/_wait/when_an_argument_is_given/and_a_command_runs_for_too_long/raises_a_ServerError.yml
313
315
  - spec/vcr/Docker_Container/_wait/when_an_argument_is_given/sets_the_read_timeout_to_that_amount_of_time.yml
314
316
  - spec/vcr/Docker_Image/_all/materializes_each_Image_into_a_Docker_Image.yml
317
+ - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/with_a_block_capturing_build_output/calls_the_block_and_passes_build_output.yml
315
318
  - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/with_specifying_a_repo_in_the_query_parameters/builds_an_image_and_tags_it.yml
316
319
  - spec/vcr/Docker_Image/_build/with_a_valid_Dockerfile/without_query_parameters/builds_an_image.yml
317
320
  - spec/vcr/Docker_Image/_build/with_an_invalid_Dockerfile/throws_a_UnexpectedResponseError.yml
321
+ - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_a_block_capturing_build_output/calls_the_block_and_passes_build_output.yml
318
322
  - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_no_query_parameters/builds_the_image.yml
319
323
  - spec/vcr/Docker_Image/_build_from_dir/with_a_valid_Dockerfile/with_specifying_a_repo_in_the_query_parameters/builds_the_image_and_tags_it.yml
320
324
  - spec/vcr/Docker_Image/_create/when_the_Image_does_not_yet_exist_and_the_body_is_a_Hash/sets_the_id.yml
@@ -335,4 +339,3 @@ test_files:
335
339
  - spec/vcr/Docker_Image/_run/when_the_argument_is_nil/no_command_configured_in_image/should_raise_an_error_if_no_command_is_specified.yml
336
340
  - spec/vcr/Docker_Image/_search/materializes_each_Image_into_a_Docker_Image.yml
337
341
  - spec/vcr/Docker_Image/_tag/tags_the_image_with_the_repo_name.yml
338
- has_rdoc: