dockerapi 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a07cc3ef52a91afb13ab99c130cb6cbed118055aba264df3e0fb0a1ec701ef6d
4
- data.tar.gz: d9e6e03672a7a47de2cb0fb840569c45582075be679d21995192247633d58d70
3
+ metadata.gz: 8207aa413525c868d4e1c0f57cacf38c19348dee819da2aeb63f390d5ac41f28
4
+ data.tar.gz: 53406674405580c0dcb91c4c32a914ef0de92ce9a9c1132eb7bf046902e5adc8
5
5
  SHA512:
6
- metadata.gz: 46bbd5e8c6cbb41e13b3830aab5ca8825d37a1c85401dedc33aeba3352657f8410e99c8c321bc5e5d10e9eeac6435769f9b7d7570a8910b7f0d0779a3308381a
7
- data.tar.gz: 672a71377c73a0c0a09796d84afebf2cfe03f3a6182626119c3370ab41112249d881152f1dadbe24ba31775475b4463df5d8c42ca2e9f7d6915c080149fab7ab
6
+ metadata.gz: be5d82c94e72b29d4341330b3c12f4a2007c6e46813cca6eb6cc8ba3f3a0adae2cd8ef00d0020ed1d59bf5bfc12bf6b22f6b3dd96eb5287ee03f02e14f1bb0ce
7
+ data.tar.gz: f4155422936cd0a4e754d4f32a118662c1d6ace28d788aae51e4d09f09b817519ed7ec0bca9e1955bcb2edc66c5ea111181493e43d398de20e4662d665ac8d2b
@@ -1,6 +1,12 @@
1
+ # 0.16.0
2
+
3
+ `Docker::API::Task#logs` method can now receive a block to replace standard output to stdout behavior.
4
+
5
+ Add `auth_encoder` to provide standard implementation for the authentication header where needed.
6
+
1
7
  # 0.15.0
2
8
 
3
- `Docker::API::System#events` and `Docker::API::Exec#start` methods that can now receive a block to replace standard output to stdout behavior.
9
+ `Docker::API::System#events` and `Docker::API::Exec#start` methods can now receive a block to replace standard output to stdout behavior.
4
10
 
5
11
  General refactoring and API documentation.
6
12
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dockerapi (0.15.0)
4
+ dockerapi (0.16.0)
5
5
  excon (~> 0.76.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -31,6 +31,8 @@ Add this line to your application's Gemfile:
31
31
 
32
32
  ```ruby
33
33
  gem 'dockerapi'
34
+ # or
35
+ gem 'dockerapi', github: 'nu12/dockerapi'
34
36
  ```
35
37
 
36
38
  And then execute:
@@ -547,17 +549,14 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
547
549
  | Network | Ok | Ok | Ok |
548
550
  | System | Ok | Ok | Ok |
549
551
  | Exec | Ok | Ok | Ok |
550
- | Swarm | Ok | Ok | 8/11 |
551
- | Node | Ok | Ok | 8/11 |
552
- | Service | Ok | Ok | 8/11 |
553
- | Task | Ok | Ok | 8/11 |
554
- | Secret | Ok | Ok | 8/11 |
552
+ | Swarm | Ok | Ok | Ok |
553
+ | Node | Ok | Ok | Ok |
554
+ | Service | Ok | Ok | Ok |
555
+ | Task | Ok | Ok | Ok |
556
+ | Secret | Ok | Ok | Ok |
555
557
  | Config | Ok | Ok | 8/14 |
556
- | Distribution | Ok | Ok | 8/14 |
557
558
  | Plugin | Ok | Ok | 8/14 |
558
559
 
559
- Add doc in these files: `base`, `connection`, `error`, `response`, `dockerapi`
560
-
561
560
  ## Contributing
562
561
 
563
562
  Bug reports and pull requests are welcome on GitHub at https://github.com/nu12/dockerapi.
@@ -1,24 +1,21 @@
1
+ ##
2
+ # Base class to provide general methods, helpers and implementations accross classes.
1
3
  class Docker::API::Base
2
4
 
3
-
5
+ ##
6
+ # Creates new object and sets the validation to happen automatically when method parameters include "params" or "body".
7
+ #
8
+ # @param connection [Docker::API::Connection]: Connection to be used.
4
9
  def initialize connection = nil
5
10
  raise Docker::API::Error.new("Expected connection to be a Docker::API::Connection class") if connection != nil && !connection.is_a?(Docker::API::Connection)
6
11
  @connection = connection || Docker::API::Connection.new
7
-
8
- (self.methods - Object.methods).each do |method|
9
- params_index = method(method).parameters.map{|ar| ar[1]}.index(:params)
10
- body_index = method(method).parameters.map{|ar| ar[1]}.index(:body)
11
-
12
- define_singleton_method(method) do |*args, &block|
13
- validate Docker::API::InvalidParameter, Docker::API::VALID_PARAMS["#{self.class.name}"]["#{method}"], (args[params_index] || {}) if params_index
14
- validate Docker::API::InvalidRequestBody, Docker::API::VALID_BODY["#{self.class.name}"]["#{method}"], (args[body_index] || {}) if body_index
15
- super(*args,&block)
16
- end
17
- end
12
+ set_automated_validation
18
13
  end
19
14
 
20
15
  private
21
16
 
17
+ ##
18
+ # Outputs to stdout.
22
19
  def default_streamer
23
20
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
24
21
  p chunk.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') if Docker::API::PRINT_TO_STDOUT
@@ -26,6 +23,10 @@ class Docker::API::Base
26
23
  streamer
27
24
  end
28
25
 
26
+ ##
27
+ # Writes file.
28
+ #
29
+ # @param path [String]: Path to the file to be writen.
29
30
  def default_writer path
30
31
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
31
32
  return if "#{chunk}".match(/(No such image)/)
@@ -36,6 +37,13 @@ class Docker::API::Base
36
37
  streamer
37
38
  end
38
39
 
40
+ ##
41
+ # Reads file.
42
+ #
43
+ # @param path [String]: Path to the file to be read.
44
+ # @param url [String]: Endpoint URL where the file is going to be sent.
45
+ # @param header [Hash]: Header of the request.
46
+ # @param &block: Replace the default output to stdout behavior.
39
47
  def default_reader path, url, header = {"Content-Type" => "application/x-tar"}, &block
40
48
  file = File.open(File.expand_path(path), "r")
41
49
  response = @connection.request(method: :post, path: url , headers: header, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s}, response_block: block_given? ? block.call : default_streamer )
@@ -43,24 +51,60 @@ class Docker::API::Base
43
51
  response
44
52
  end
45
53
 
54
+ ##
55
+ # Encodes the authentication parameters.
56
+ #
57
+ # @param authentication [Hash]: Parameters to be encoded.
58
+ def auth_encoder(authentication)
59
+ Base64.urlsafe_encode64(authentication.to_json.to_s).chomp
60
+ end
61
+
62
+ ##
63
+ # Validates a Hash object comparing its keys with a given Array of permitted values. Raise an error if the validation fail.
64
+ #
65
+ # @param error [Error]: Error to be raised of the validation fail.
66
+ # @param permitted [Array]: List of permitted keys.
67
+ # @param params [Hash]: Hash object to be validated.
46
68
  def validate error, permitted, params
47
69
  return if params[:skip_validation]
48
70
  unpermitted = params.keys.map(&:to_s) - permitted.map(&:to_s)
49
71
  raise error.new(permitted, unpermitted) if unpermitted.size > 0
50
72
  end
51
73
 
52
- ## Converts Ruby Hash into query parameters
53
- ## In general, the format is key=value
54
- ## If value is another Hash, it should keep a json syntax {key:value}
55
- def hash_to_params h
74
+ ##
75
+ # Converts Ruby Hash into URL query parameters.
76
+ #
77
+ # In general, query parameters' format is "key=value", but if "value" is another Hash, it should change to a json syntax {key:value}.
78
+ #
79
+ # @param hash [Hash]: Hash object to be converted in a query parameter-like string.
80
+ def hash_to_params hash
56
81
  p = []
57
- h.delete_if{ | k, v | k.to_s == "skip_validation" }.each { |k,v| p.push( v.is_a?(Hash) ? "#{k}=#{v.to_json}" : "#{k}=#{v}") }
82
+ hash.delete_if{ | k, v | k.to_s == "skip_validation" }.each { |k,v| p.push( v.is_a?(Hash) ? "#{k}=#{v.to_json}" : "#{k}=#{v}") }
58
83
  p.join("&").gsub(" ","")
59
84
  end
60
85
 
86
+ ##
87
+ # Builds an URL string using the base path and a set of parameters.
88
+ #
89
+ # @param path [String]: Base URL string.
90
+ # @param hash [Hash]: Hash object to be appended to the URL as query parameters.
61
91
  def build_path path, params = {}
62
- p = path.is_a?(Array) ? ([base_path] << path).join("/") : path # TODO: this line to be removed?
63
- params.size > 0 ? [p, hash_to_params(params)].join("?") : p
92
+ params.size > 0 ? [path, hash_to_params(params)].join("?") : path
93
+ end
94
+
95
+ ##
96
+ # Sets the validation to happen automatically when method parameters include "params" or "body".
97
+ def set_automated_validation
98
+ (self.methods - Object.methods).each do |method|
99
+ params_index = method(method).parameters.map{|ar| ar[1]}.index(:params)
100
+ body_index = method(method).parameters.map{|ar| ar[1]}.index(:body)
101
+
102
+ define_singleton_method(method) do |*args, &block|
103
+ validate Docker::API::InvalidParameter, Docker::API::VALID_PARAMS["#{self.class.name}"]["#{method}"], (args[params_index] || {}) if params_index
104
+ validate Docker::API::InvalidRequestBody, Docker::API::VALID_BODY["#{self.class.name}"]["#{method}"], (args[body_index] || {}) if body_index
105
+ super(*args,&block)
106
+ end
107
+ end
64
108
  end
65
109
 
66
110
  end
@@ -1,20 +1,26 @@
1
- module Docker
2
- module API
3
- class Connection
4
- [:get, :post, :head, :delete, :put].each do | method |
5
- define_method(method) { | path | self.request(method: method, path: path) }
6
- end
7
-
8
- def request params
9
- Docker::API::Response.new(@connection.request(params).data)
10
- end
11
-
12
- def initialize url = nil, params = nil
13
- url ||= 'unix:///'
14
- params ||= url == 'unix:///' ? {socket: '/var/run/docker.sock'} : {}
15
- @connection = Excon.new(url, params)
16
- end
1
+ ##
2
+ # Connection class.
3
+ class Docker::API::Connection
4
+ [:get, :post, :head, :delete, :put].each do | method |
5
+ define_method(method) { | path | self.request(method: method, path: path) }
6
+ end
17
7
 
18
- end
8
+ ##
9
+ # Calls an Excon request and returns a Docker::API::Response object.
10
+ #
11
+ # @param params [Hash]: Request parameters.
12
+ def request params
13
+ Docker::API::Response.new(@connection.request(params).data)
19
14
  end
15
+
16
+ ##
17
+ # Creates an Excon connection.
18
+ #
19
+ # @param url [String]: URL for the connection.
20
+ # @param params [String]: Additional parameters.
21
+ def initialize url = nil, params = nil
22
+ return @connection = Excon.new('unix:///', {socket: '/var/run/docker.sock'}) unless url
23
+ @connection = Excon.new(url, params || {})
24
+ end
25
+
20
26
  end
@@ -1,17 +1,33 @@
1
1
  module Docker
2
2
  module API
3
+
4
+ ##
5
+ # This class represents a validation error.
3
6
  class ValidationError < StandardError
7
+
8
+ ##
9
+ # @params permitted [Array]: permitted values.
10
+ # @params unpermitted [Array]: unpermitted values.
4
11
  def initialize permitted, unpermitted
5
12
  super("Unpermitted options found: #{unpermitted.to_s}. Permitted are #{permitted.to_s}")
6
13
  end
7
14
  end
15
+
16
+ ##
17
+ # This class represents a parameter validation error.
18
+ class InvalidParameter < Docker::API::ValidationError; end
19
+
20
+ ##
21
+ # This class represents a request body validation error.
22
+ class InvalidRequestBody < Docker::API::ValidationError; end
23
+
24
+ ##
25
+ # This class represents a generic error.
8
26
  class Error < StandardError
9
27
  def initialize msg = "Error without specific message"
10
28
  super(msg)
11
29
  end
12
30
  end
13
-
14
- class InvalidParameter < Docker::API::ValidationError; end
15
- class InvalidRequestBody < Docker::API::ValidationError; end
31
+
16
32
  end
17
33
  end
@@ -131,7 +131,7 @@ class Docker::API::Image < Docker::API::Base
131
131
  # @param authentication [Hash]: Authentication parameters.
132
132
  def push name, params = {}, authentication = {}
133
133
  raise Docker::API::Error.new("Provide authentication parameters to push an image") unless authentication.keys.size > 0
134
- @connection.request(method: :post, path: build_path("/images/#{name}/push", params), headers: { "X-Registry-Auth" => Base64.urlsafe_encode64(authentication.to_json.to_s).chomp } )
134
+ @connection.request(method: :post, path: build_path("/images/#{name}/push", params), headers: { "X-Registry-Auth" => auth_encoder(authentication) } )
135
135
  end
136
136
 
137
137
  ##
@@ -164,7 +164,7 @@ class Docker::API::Image < Docker::API::Base
164
164
  params[:fromSrc] = "-"
165
165
  default_reader(path, build_path("/images/create", params))
166
166
  else
167
- request[:headers] = { "X-Registry-Auth" => Base64.encode64(authentication.to_json.to_s).chomp } if authentication.keys.size > 0
167
+ request[:headers] = { "X-Registry-Auth" => auth_encoder(authentication) } if authentication.keys.size > 0
168
168
  @connection.request(request)
169
169
  end
170
170
  end
@@ -183,7 +183,7 @@ class Docker::API::Image < Docker::API::Base
183
183
  raise Docker::API::Error.new("Expected path or params[:remote]") unless path || params[:remote]
184
184
 
185
185
  headers = {"Content-type": "application/x-tar"}
186
- headers.merge!({"X-Registry-Config": Base64.urlsafe_encode64(authentication.to_json.to_s).chomp}) if authentication.keys.size > 0
186
+ headers.merge!({"X-Registry-Config": auth_encoder(authentication) }) if authentication.keys.size > 0
187
187
 
188
188
  if path == nil and params.has_key? :remote
189
189
  response = @connection.request(method: :post, path: build_path("/build", params), headers: headers, response_block: block_given? ? block.call : default_streamer)
@@ -1,34 +1,53 @@
1
- module Docker
2
- module API
3
- class Node < Docker::API::Base
1
+ # This class represents the Docker API endpoints regarding nodes.
2
+ #
3
+ # Nodes are instances of the Engine participating in a swarm. Swarm mode must be enabled for these endpoints to work.
4
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Node
5
+ class Docker::API::Node < Docker::API::Base
4
6
 
5
- def list params = {}
6
- @connection.get(build_path("/nodes", params))
7
- end
8
-
9
- def update name, params = {}, body = {}
10
- @connection.request(method: :post, path: build_path("nodes/#{name}/update", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
11
- end
12
-
13
- def delete name, params = {}
14
- @connection.delete(build_path("/nodes/#{name}", params))
15
- end
7
+ ##
8
+ # List nodes.
9
+ #
10
+ # Docker API: GET /nodes
11
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/NodeList
12
+ #
13
+ # @param params [Hash]: Parameters that are appended to the URL.
14
+ def list params = {}
15
+ @connection.get(build_path("/nodes", params))
16
+ end
16
17
 
17
- def details name
18
- @connection.get("/nodes/#{name}")
19
- end
18
+ ##
19
+ # Update a node.
20
+ #
21
+ # Docker API: POST /nodes/{id}/update
22
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/NodeUpdate
23
+ #
24
+ # @param name [String]: The ID or name of the node.
25
+ # @param params [Hash]: Parameters that are appended to the URL.
26
+ # @param body [Hash]: Request body to be sent as json.
27
+ def update name, params = {}, body = {}
28
+ @connection.request(method: :post, path: build_path("nodes/#{name}/update", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
29
+ end
20
30
 
21
- #################################################
22
- # Items in this area to be removed before 1.0.0 #
23
- #################################################
24
- def inspect *args
25
- return super.inspect if args.size == 0
26
- warn "WARNING: #inspect is deprecated and will be removed in the future, please use #details instead."
27
- name = args[0]
28
- details(name)
29
- end
30
- #################################################
31
+ ##
32
+ # Delete a node.
33
+ #
34
+ # Docker API: DELETE /nodes/{id}
35
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/NodeDelete
36
+ #
37
+ # @param name [String]: The ID or name of the node.
38
+ # @param params [Hash]: Parameters that are appended to the URL.
39
+ def delete name, params = {}
40
+ @connection.delete(build_path("/nodes/#{name}", params))
41
+ end
31
42
 
32
- end
43
+ ##
44
+ # Inspect a node.
45
+ #
46
+ # Docker API: GET /nodes/{id}
47
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/NodeInspect
48
+ #
49
+ # @param name [String]: The ID or name of the node.
50
+ def details name
51
+ @connection.get("/nodes/#{name}")
33
52
  end
34
53
  end
@@ -1,25 +1,33 @@
1
- module Docker
2
- module API
3
- class Response < Excon::Response
4
- attr_reader(:json, :path)
1
+ ##
2
+ # Reponse class.
3
+ class Docker::API::Response < Excon::Response
4
+ attr_reader(:json, :path)
5
5
 
6
- def initialize data
7
- super data
8
- @json = parse_json @body
9
- @path = @data[:path]
10
- end
6
+ ##
7
+ # Initialize a new Response object.
8
+ #
9
+ # @params data [Object]: Reponse's data.
10
+ def initialize data
11
+ super data
12
+ @json = parse_json @body
13
+ @path = @data[:path]
14
+ end
11
15
 
12
- def success?
13
- (200..204).include? @status
14
- end
16
+ ##
17
+ # Returns true if Response status is in 200..204 range.
18
+ def success?
19
+ (200..204).include? @status
20
+ end
15
21
 
16
- private
22
+ private
17
23
 
18
- def parse_json data
19
- return nil unless headers["Content-Type"] == "application/json"
20
- return nil if data == ""
21
- data.split("\r\n").size > 1 ? data.split("\r\n").map{ |e| eval(e) } : JSON.parse(data)
22
- end
23
- end
24
+ ##
25
+ # Creates a json from Response data attribute.
26
+ #
27
+ # @params data [String]: String to be converted in json.
28
+ def parse_json data
29
+ return nil unless headers["Content-Type"] == "application/json"
30
+ return nil if data == ""
31
+ data.split("\r\n").size > 1 ? data.split("\r\n").map{ |e| eval(e) } : JSON.parse(data)
24
32
  end
25
33
  end
@@ -1,14 +1,12 @@
1
1
  # This class represents the Docker API endpoints regarding secrets.
2
2
  #
3
- # @see https://docs.docker.com/engine/api/v1.40/#tag/Secret
4
- #
5
3
  # Secrets are sensitive data that can be used by services. Swarm mode must be enabled for these endpoints to work.
4
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Secret
6
5
  class Docker::API::Secret < Docker::API::Base
7
6
 
8
7
  # List secrets
9
8
  #
10
9
  # Docker API: GET /secrets
11
- #
12
10
  # @see https://docs.docker.com/engine/api/v1.40/#operation/SecretList
13
11
  #
14
12
  # @param params [Hash]: Parameters that are appended to the URL.
@@ -19,7 +17,6 @@ class Docker::API::Secret < Docker::API::Base
19
17
  # Create a secret
20
18
  #
21
19
  # Docker API: POST /secrets/create
22
- #
23
20
  # @see https://docs.docker.com/engine/api/v1.40/#operation/SecretCreate
24
21
  #
25
22
  # @param body [Hash]: Request body to be sent as json.
@@ -30,7 +27,6 @@ class Docker::API::Secret < Docker::API::Base
30
27
  # Inspect a secret
31
28
  #
32
29
  # Docker API: GET /secrets/{id}
33
- #
34
30
  # @see https://docs.docker.com/engine/api/v1.40/#operation/SecretInspect
35
31
  #
36
32
  # @param name [String]: The ID or name of the secret.
@@ -41,13 +37,10 @@ class Docker::API::Secret < Docker::API::Base
41
37
  # Update a secret
42
38
  #
43
39
  # Docker API: POST /secrets/{id}/update
44
- #
45
40
  # @see https://docs.docker.com/engine/api/v1.40/#operation/SecretUpdate
46
41
  #
47
42
  # @param name [String]: The ID or name of the secret.
48
- #
49
43
  # @param params [Hash]: Parameters that are appended to the URL.
50
- #
51
44
  # @param body [Hash]: Request body to be sent as json.
52
45
  def update name, params = {}, body = {}
53
46
  @connection.request(method: :post, path: build_path("/secrets/#{name}/update",params), headers: {"Content-Type": "application/json"}, body: body.to_json)
@@ -56,7 +49,6 @@ class Docker::API::Secret < Docker::API::Base
56
49
  # Delete a secret
57
50
  #
58
51
  # Docker API: DELETE /secrets/{id}
59
- #
60
52
  # @see https://docs.docker.com/engine/api/v1.40/#operation/SecretDelete
61
53
  #
62
54
  # @param name [String]: The ID or name of the secret.
@@ -1,14 +1,12 @@
1
1
  # This class represents the Docker API endpoints regarding services.
2
2
  #
3
- # @see https://docs.docker.com/engine/api/v1.40/#tag/Service
4
- #
5
3
  # Services are the definitions of tasks to run on a swarm. Swarm mode must be enabled for these endpoints to work.
4
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Service
6
5
  class Docker::API::Service < Docker::API::Base
7
6
 
8
7
  # List services
9
8
  #
10
9
  # Docker API: GET /services
11
- #
12
10
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceList
13
11
  #
14
12
  # @param params [Hash]: Parameters that are appended to the URL.
@@ -19,46 +17,38 @@ class Docker::API::Service < Docker::API::Base
19
17
  # Create a service
20
18
  #
21
19
  # Docker API: POST /services/create
22
- #
23
20
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceCreate
24
21
  #
25
22
  # @param body [Hash]: Request body to be sent as json.
26
- #
27
23
  # @param authentication [Hash]: Authentication parameters.
28
24
  def create body = {}, authentication = {}
29
25
  headers = {"Content-Type": "application/json"}
30
- headers.merge!({"X-Registry-Auth" => Base64.urlsafe_encode64(authentication.to_json.to_s)}) if authentication.keys.size > 0
26
+ headers.merge!({"X-Registry-Auth" => auth_encoder(authentication) }) if authentication.keys.size > 0
31
27
  @connection.request(method: :post, path: "/services/create", headers: headers, body: body.to_json)
32
28
  end
33
29
 
34
30
  # Update a service
35
31
  #
36
32
  # Docker API: POST /services/{id}/update
37
- #
38
33
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceUpdate
39
34
  #
40
35
  # @param name [String]: The ID or name of the service.
41
- #
42
36
  # @param params [Hash]: Parameters that are appended to the URL.
43
- #
44
37
  # @param body [Hash]: Request body to be sent as json.
45
- #
46
38
  # @param authentication [Hash]: Authentication parameters.
47
39
  def update name, params = {}, body = {}, authentication = {}
48
40
  # view https://github.com/docker/swarmkit/issues/1394#issuecomment-240850719
49
41
  headers = {"Content-Type": "application/json"}
50
- headers.merge!({"X-Registry-Auth" => Base64.urlsafe_encode64(authentication.to_json.to_s)}) if authentication.keys.size > 0
42
+ headers.merge!({"X-Registry-Auth" => auth_encoder(authentication) }) if authentication.keys.size > 0
51
43
  @connection.request(method: :post, path: build_path("/services/#{name}/update", params), headers: headers, body: body.to_json)
52
44
  end
53
45
 
54
46
  # Inspect a service
55
47
  #
56
48
  # Docker API: GET /services/{id}
57
- #
58
49
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceInspect
59
50
  #
60
51
  # @param name [String]: The ID or name of the service.
61
- #
62
52
  # @param params [Hash]: Parameters that are appended to the URL.
63
53
  def details name, params = {}
64
54
  @connection.get(build_path("/services/#{name}", params))
@@ -66,14 +56,10 @@ class Docker::API::Service < Docker::API::Base
66
56
 
67
57
  # Get stdout and stderr logs from a service.
68
58
  #
69
- # Note: This endpoint works only for services with the local, json-file or journald logging drivers.
70
- #
71
59
  # Docker API: GET /services/{id}/logs
72
- #
73
60
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceLogs
74
61
  #
75
62
  # @param name [String]: The ID or name of the service.
76
- #
77
63
  # @param params [Hash]: Parameters that are appended to the URL.
78
64
  def logs name, params = {}
79
65
  @connection.get(build_path("/services/#{name}/logs", params))
@@ -82,7 +68,6 @@ class Docker::API::Service < Docker::API::Base
82
68
  # Delete a service
83
69
  #
84
70
  # Docker API: DELETE /services/{id}
85
- #
86
71
  # @see https://docs.docker.com/engine/api/v1.40/#operation/ServiceDelete
87
72
  #
88
73
  # @param name [String]: The ID or name of the service.
@@ -1,45 +1,79 @@
1
- module Docker
2
- module API
3
- class Swarm < Docker::API::Base
1
+ ##
2
+ # This class represents the Docker API endpoints regarding swamrs.
3
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Swarm
4
+ class Docker::API::Swarm < Docker::API::Base
4
5
 
5
- def init body = {}
6
- @connection.request(method: :post, path: build_path("/swarm/init"), headers: {"Content-Type": "application/json"}, body: body.to_json)
7
- end
8
-
9
- def update params = {}, body = {}
10
- @connection.request(method: :post, path: build_path("/swarm/update", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
11
- end
6
+ ##
7
+ # Initialize a new swarm.
8
+ #
9
+ # Docker API: POST /swarm/init
10
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmInit
11
+ #
12
+ # @param body [Hash]: Request body to be sent as json.
13
+ def init body = {}
14
+ @connection.request(method: :post, path: build_path("/swarm/init"), headers: {"Content-Type": "application/json"}, body: body.to_json)
15
+ end
12
16
 
13
- def details
14
- @connection.get("/swarm")
15
- end
17
+ ##
18
+ # Update a swarm.
19
+ #
20
+ # Docker API: POST /swarm/update
21
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmUpdate
22
+ #
23
+ # @param params [Hash]: Parameters that are appended to the URL.
24
+ # @param body [Hash]: Request body to be sent as json.
25
+ def update params = {}, body = {}
26
+ @connection.request(method: :post, path: build_path("/swarm/update", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
27
+ end
16
28
 
17
- def unlock_key
18
- @connection.get(build_path("/swarm/unlockkey"))
19
- end
29
+ ##
30
+ # Inspect swarm.
31
+ #
32
+ # Docker API: GET /swarm
33
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmInspect
34
+ def details
35
+ @connection.get("/swarm")
36
+ end
20
37
 
21
- def unlock body = {}
22
- @connection.request(method: :post, path: build_path("/swarm/unlock"), headers: {"Content-Type": "application/json"}, body: body.to_json)
23
- end
38
+ ##
39
+ # Get the unlock key.
40
+ #
41
+ # Docker API: GET /swarm/unlockkey
42
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmUnlockkey
43
+ def unlock_key
44
+ @connection.get("/swarm/unlockkey")
45
+ end
24
46
 
25
- def join body = {}
26
- @connection.request(method: :post, path: build_path("/swarm/join"), headers: {"Content-Type": "application/json"}, body: body.to_json)
27
- end
47
+ ##
48
+ # Unlock a locked manager.
49
+ #
50
+ # Docker API: POST /swarm/unlock
51
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmUnlock
52
+ #
53
+ # @param body [Hash]: Request body to be sent as json.
54
+ def unlock body = {}
55
+ @connection.request(method: :post, path: "/swarm/unlock", headers: {"Content-Type": "application/json"}, body: body.to_json)
56
+ end
28
57
 
29
- def leave params = {}
30
- @connection.post(build_path("/swarm/leave", params))
31
- end
58
+ ##
59
+ # Join an existing swarm.
60
+ #
61
+ # Docker API: POST /swarm/join
62
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmJoin
63
+ #
64
+ # @param body [Hash]: Request body to be sent as json.
65
+ def join body = {}
66
+ @connection.request(method: :post, path: "/swarm/join", headers: {"Content-Type": "application/json"}, body: body.to_json)
67
+ end
32
68
 
33
- #################################################
34
- # Items in this area to be removed before 1.0.0 #
35
- #################################################
36
- def inspect
37
- caller.each { | el | return super.inspect if el.match(/inspector/) }
38
- warn "WARNING: #inspect is deprecated and will be removed in the future, please use #details instead."
39
- details
40
- end
41
- #################################################
42
-
43
- end
69
+ ##
70
+ # Leave a swarm.
71
+ #
72
+ # Docker API: POST /swarm/leave
73
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/SwarmLeave
74
+ #
75
+ # @param params [Hash]: Parameters that are appended to the URL.
76
+ def leave params = {}
77
+ @connection.post(build_path("/swarm/leave", params))
44
78
  end
45
79
  end
@@ -1,14 +1,12 @@
1
1
  # This class represents the Docker API endpoints regarding tasks.
2
2
  #
3
- # @see https://docs.docker.com/engine/api/v1.40/#tag/Task
4
- #
5
3
  # A task is a container running on a swarm. It is the atomic scheduling unit of swarm. Swarm mode must be enabled for these endpoints to work.
4
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Task
6
5
  class Docker::API::Task < Docker::API::Base
7
6
 
8
7
  # List tasks
9
8
  #
10
9
  # Docker API: GET /tasks
11
- #
12
10
  # @see https://docs.docker.com/engine/api/v1.40/#operation/TaskList
13
11
  #
14
12
  # @param params [Hash]: Parameters that are appended to the URL.
@@ -19,7 +17,6 @@ class Docker::API::Task < Docker::API::Base
19
17
  # Inspect a task
20
18
  #
21
19
  # Docker API: GET /tasks/{id}
22
- #
23
20
  # @see https://docs.docker.com/engine/api/v1.40/#operation/TaskInspect
24
21
  #
25
22
  # @param name [String]: The ID or name of the task.
@@ -29,16 +26,19 @@ class Docker::API::Task < Docker::API::Base
29
26
 
30
27
  # Get stdout and stderr logs from a task.
31
28
  #
32
- # Note: This endpoint works only for services with the local, json-file or journald logging drivers.
33
- #
34
29
  # Docker API: GET /tasks/{id}/logs
35
- #
36
30
  # @see https://docs.docker.com/engine/api/v1.40/#operation/TaskLogs
37
31
  #
38
32
  # @param name (String) : The ID or name of the task.
39
- #
40
33
  # @param params (Hash) : Parameters that are appended to the URL.
41
- def logs name, params = {}
42
- @connection.get(build_path("/tasks/#{name}/logs", params))
34
+ # @param &block: Replace the default output to stdout behavior.
35
+ def logs name, params = {}, &block
36
+ path = build_path("/tasks/#{name}/logs", params)
37
+
38
+ if [true, 1 ].include? params[:follow]
39
+ @connection.request(method: :get, path: path , response_block: block_given? ? block.call : default_streamer)
40
+ else
41
+ @connection.get(path)
42
+ end
43
43
  end
44
44
  end
@@ -1,6 +1,6 @@
1
1
  module Docker
2
2
  module API
3
- GEM_VERSION = "0.15.0"
3
+ GEM_VERSION = "0.16.0"
4
4
 
5
5
  API_VERSION = "1.40"
6
6
 
@@ -25,8 +25,12 @@ require "docker/api/plugin"
25
25
  module Docker
26
26
  module API
27
27
 
28
+ ##
29
+ # This variable controls output verbosity.
28
30
  PRINT_TO_STDOUT = true
29
31
 
32
+ ##
33
+ # Valid values for parameter validations.
30
34
  VALID_PARAMS = {
31
35
  "Docker::API::Image" => {
32
36
  "build" => [:dockerfile, :t, :extrahosts, :remote, :q, :nocache, :cachefrom, :pull, :rm, :forcerm, :memory, :memswap, :cpushares, :cpusetcpus, :cpuperiod, :cpuquota, :buildargs, :shmsize, :squash, :labels, :networkmode, :platform, :target, :outputs],
@@ -114,6 +118,8 @@ module Docker
114
118
  }
115
119
  }
116
120
 
121
+ ##
122
+ # Valid values for request body validations.
117
123
  VALID_BODY = {
118
124
  "Docker::API::Image" => {
119
125
  "commit" => [:Hostname, :Domainname, :User, :AttachStdin, :AttachStdout, :AttachStderr, :ExposedPorts, :Tty, :OpenStdin, :StdinOnce, :Env, :Cmd, :HealthCheck, :ArgsEscaped, :Image, :Volumes, :WorkingDir, :Entrypoint, :NetworkDisabled, :MacAddress, :OnBuild, :Labels, :StopSignal, :StopTimeout, :Shell]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockerapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alysson A. Costa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-07 00:00:00.000000000 Z
11
+ date: 2020-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon