dockerapi 0.8.1 → 0.13.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.
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  # Specify which files should be added to the gem when it is released.
20
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
21
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|resources)/}) }
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|resources)/}) }.append("doc")
23
23
  end
24
24
  spec.bindir = "exe"
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
@@ -1,38 +1,66 @@
1
- module Docker
2
- module API
3
- class Base
1
+ class Docker::API::Base
2
+
4
3
 
5
- def initialize connection = nil
6
- raise Docker::API::Error.new("Expect connection to be a Docker::API::Connection class") if connection != nil && !connection.is_a?(Docker::API::Connection)
7
- @connection = connection || Docker::API::Connection.new
8
- end
9
-
10
- private
4
+ def initialize connection = nil
5
+ raise Docker::API::Error.new("Expected connection to be a Docker::API::Connection class") if connection != nil && !connection.is_a?(Docker::API::Connection)
6
+ @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
11
 
12
- def base_path # TODO: this method to be removed?
13
- "/"
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)
14
16
  end
17
+ end
18
+ end
19
+
20
+ private
15
21
 
16
- def validate error, permitted, params
17
- return if params[:skip_validation]
18
- unpermitted = params.keys.map(&:to_s) - permitted.map(&:to_s)
19
- raise error.new(permitted, unpermitted) if unpermitted.size > 0
20
- end
22
+ def default_streamer
23
+ streamer = lambda do |chunk, remaining_bytes, total_bytes|
24
+ p chunk.to_s.encode('UTF-8', invalid: :replace, undef: :replace, replace: '?') if Docker::API::PRINT_TO_STDOUT
25
+ end
26
+ streamer
27
+ end
21
28
 
22
- ## Converts Ruby Hash into query parameters
23
- ## In general, the format is key=value
24
- ## If value is another Hash, it should keep a json syntax {key:value}
25
- def hash_to_params h
26
- p = []
27
- 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}") }
28
- p.join("&").gsub(" ","")
29
- end
29
+ def default_writer path
30
+ streamer = lambda do |chunk, remaining_bytes, total_bytes|
31
+ return if "#{chunk}".match(/(No such image)/)
32
+ file = File.open(File.expand_path(path), "wb+")
33
+ file.write(chunk)
34
+ file.close
35
+ end
36
+ streamer
37
+ end
30
38
 
31
- def build_path path, params = {}
32
- p = path.is_a?(Array) ? ([base_path] << path).join("/") : path # TODO: this line to be removed?
33
- params.size > 0 ? [p, hash_to_params(params)].join("?") : p
34
- end
39
+ def default_reader path, url, header = {"Content-Type" => "application/x-tar"}, &block
40
+ file = File.open(File.expand_path(path), "r")
41
+ 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 )
42
+ file.close
43
+ response
44
+ end
35
45
 
36
- end
46
+ def validate error, permitted, params
47
+ return if params[:skip_validation]
48
+ unpermitted = params.keys.map(&:to_s) - permitted.map(&:to_s)
49
+ raise error.new(permitted, unpermitted) if unpermitted.size > 0
50
+ end
51
+
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
56
+ 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}") }
58
+ p.join("&").gsub(" ","")
59
+ end
60
+
61
+ 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
37
64
  end
65
+
38
66
  end
@@ -0,0 +1,66 @@
1
+ # This class represents the Docker API endpoints regarding configs.
2
+ #
3
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Config
4
+ #
5
+ # Configs are application configurations that can be used by services. Swarm mode must be enabled for these endpoints to work.
6
+ class Docker::API::Config < Docker::API::Base
7
+
8
+ # List configs
9
+ #
10
+ # Docker API: GET /configs
11
+ #
12
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ConfigList
13
+ #
14
+ # @param params [Hash]: Parameters that are appended to the URL.
15
+ def list params = {}
16
+ @connection.get(build_path("/configs",params))
17
+ end
18
+
19
+ # Create a config
20
+ #
21
+ # Docker API: POST /configs/create
22
+ #
23
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ConfigCreate
24
+ #
25
+ # @param body [Hash]: Request body to be sent as json.
26
+ def create body = {}
27
+ @connection.request(method: :post, path: "/configs/create", headers: {"Content-Type": "application/json"}, body: body.to_json)
28
+ end
29
+
30
+ # Inspect a config
31
+ #
32
+ # Docker API: GET /configs/{id}
33
+ #
34
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ConfigInspect
35
+ #
36
+ # @param name [String]: The ID or name of the config.
37
+ def details name
38
+ @connection.get("/configs/#{name}")
39
+ end
40
+
41
+ # Update a config
42
+ #
43
+ # Docker API: POST /configs/{id}/update
44
+ #
45
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ConfigUpdate
46
+ #
47
+ # @param name [String]: The ID or name of the config.
48
+ #
49
+ # @param params [Hash]: Parameters that are appended to the URL.
50
+ #
51
+ # @param body [Hash]: Request body to be sent as json.
52
+ def update name, params = {}, body = {}
53
+ @connection.request(method: :post, path: build_path("/configs/#{name}/update",params), headers: {"Content-Type": "application/json"}, body: body.to_json)
54
+ end
55
+
56
+ # Delete a config
57
+ #
58
+ # Docker API: DELETE /configs/{id}
59
+ #
60
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ConfigDelete
61
+ #
62
+ # @param name [String]: The ID or name of the config.
63
+ def delete name
64
+ @connection.delete("/configs/#{name}")
65
+ end
66
+ end
@@ -1,28 +1,16 @@
1
1
  module Docker
2
2
  module API
3
- CreateBody = [: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,:HostConfig,:NetworkingConfig]
4
- UpdateBody = [:CpuShares, :Memory, :CgroupParent, :BlkioWeight, :BlkioWeightDevice, :BlkioWeightReadBps, :BlkioWeightWriteBps, :BlkioWeightReadOps, :BlkioWeightWriteOps, :CpuPeriod, :CpuQuota, :CpuRealtimePeriod, :CpuRealtimeRuntime, :CpusetCpus, :CpusetMems, :Devices, :DeviceCgroupRules, :DeviceRequest, :Kernel, :Memory, :KernelMemoryTCP, :MemoryReservation, :MemorySwap, :MemorySwappiness, :NanoCPUs, :OomKillDisable, :Init, :PidsLimit, :ULimits, :CpuCount, :CpuPercent, :IOMaximumIOps, :IOMaximumBandwidth, :RestartPolicy]
5
-
6
3
  class Container < Docker::API::Base
7
4
 
8
- def base_path
9
- "/containers"
5
+ def list params = {}
6
+ @connection.get(build_path(["json"], params))
10
7
  end
11
8
 
12
- def inspect *args
13
- return super.inspect if args.size == 0
14
- name, params = args[0], args[1] || {}
15
- validate Docker::API::InvalidParameter, [:size], params
9
+ def details name, params = {}
16
10
  @connection.get(build_path([name, "json"], params))
17
11
  end
18
12
 
19
- def list params = {}
20
- validate Docker::API::InvalidParameter, [:all, :limit, :size, :filters], params
21
- @connection.get(build_path(["json"], params))
22
- end
23
-
24
13
  def top name, params = {}
25
- validate Docker::API::InvalidParameter, [:ps_args], params
26
14
  @connection.get(build_path([name, "top"], params))
27
15
  end
28
16
 
@@ -31,47 +19,38 @@ module Docker
31
19
  end
32
20
 
33
21
  def start name, params = {}
34
- validate Docker::API::InvalidParameter, [:detachKeys], params
35
22
  @connection.post(build_path([name, "start"], params))
36
23
  end
37
24
 
38
25
  def stop name, params = {}
39
- validate Docker::API::InvalidParameter, [:t], params
40
26
  @connection.post(build_path([name, "stop"], params))
41
27
  end
42
28
 
43
29
  def restart name, params = {}
44
- validate Docker::API::InvalidParameter, [:t], params
45
30
  @connection.post(build_path([name, "restart"], params))
46
31
  end
47
32
 
48
33
  def kill name, params = {}
49
- validate Docker::API::InvalidParameter, [:signal], params
50
34
  @connection.post(build_path([name, "kill"], params))
51
35
  end
52
36
 
53
37
  def wait name, params = {}
54
- validate Docker::API::InvalidParameter, [:condition], params
55
38
  @connection.post(build_path([name, "wait"], params))
56
39
  end
57
40
 
58
41
  def update name, body = {}
59
- validate Docker::API::InvalidRequestBody, Docker::API::UpdateBody, body
60
42
  @connection.request(method: :post, path: build_path([name, "update"]), headers: {"Content-Type": "application/json"}, body: body.to_json)
61
43
  end
62
44
 
63
45
  def rename name, params = {}
64
- validate Docker::API::InvalidParameter, [:name], params
65
46
  @connection.post(build_path([name, "rename"], params))
66
47
  end
67
48
 
68
49
  def resize name, params = {}
69
- validate Docker::API::InvalidParameter, [:w, :h], params
70
50
  @connection.post(build_path([name, "resize"], params))
71
51
  end
72
52
 
73
53
  def prune params = {}
74
- validate Docker::API::InvalidParameter, [:filters], params
75
54
  @connection.post(build_path(["prune"], params))
76
55
  end
77
56
 
@@ -84,12 +63,10 @@ module Docker
84
63
  end
85
64
 
86
65
  def remove name, params = {}
87
- validate Docker::API::InvalidParameter, [:v, :force, :link], params
88
66
  @connection.delete(build_path([name]))
89
67
  end
90
68
 
91
69
  def logs name, params = {}
92
- validate Docker::API::InvalidParameter, [:follow, :stdout, :stderr, :since, :until, :timestamps, :tail], params
93
70
 
94
71
  path = build_path([name, "logs"], params)
95
72
 
@@ -101,18 +78,14 @@ module Docker
101
78
  end
102
79
 
103
80
  def attach name, params = {}
104
- validate Docker::API::InvalidParameter, [:detachKeys, :logs, :stream, :stdin, :stdout, :stderr], params
105
81
  @connection.request(method: :post, path: build_path([name, "attach"], params) , response_block: lambda { |chunk, remaining_bytes, total_bytes| puts chunk.inspect })
106
82
  end
107
83
 
108
84
  def create params = {}, body = {}
109
- validate Docker::API::InvalidParameter, [:name], params
110
- validate Docker::API::InvalidRequestBody, Docker::API::CreateBody, body
111
85
  @connection.request(method: :post, path: build_path(["create"], params), headers: {"Content-Type": "application/json"}, body: body.to_json)
112
86
  end
113
87
 
114
88
  def stats name, params = {}
115
- validate Docker::API::InvalidParameter, [:stream], params
116
89
  path = build_path([name, "stats"], params)
117
90
 
118
91
  if params[:stream] == true || params[:stream] == 1
@@ -126,7 +99,7 @@ module Docker
126
99
  end
127
100
 
128
101
  def export name, path = "exported_container"
129
- response = self.inspect(name)
102
+ response = self.details(name)
130
103
  if response.status == 200
131
104
  file = File.open(File.expand_path(path), "wb")
132
105
  streamer = lambda do |chunk, remaining_bytes, total_bytes|
@@ -139,7 +112,6 @@ module Docker
139
112
  end
140
113
 
141
114
  def archive name, file, params = {}
142
- validate Docker::API::InvalidParameter, [:path, :noOverwriteDirNonDir, :copyUIDGID], params
143
115
 
144
116
  begin # File exists on disk, send it to container
145
117
  file = File.open( File.expand_path( file ) , "r")
@@ -156,6 +128,21 @@ module Docker
156
128
  response
157
129
  end
158
130
 
131
+ #################################################
132
+ # Items in this area to be removed before 1.0.0 #
133
+ #################################################
134
+ def base_path
135
+ "/containers"
136
+ end
137
+
138
+ def inspect *args
139
+ return super.inspect if args.size == 0
140
+ warn "WARNING: #inspect is deprecated and will be removed in the future, please use #details instead."
141
+ name, params = args[0], args[1] || {}
142
+ details(name, params)
143
+ end
144
+ #################################################
145
+
159
146
  end
160
147
  end
161
148
  end
@@ -2,20 +2,11 @@ module Docker
2
2
  module API
3
3
  class Exec < Docker::API::Base
4
4
 
5
- def inspect *args
6
- return super.inspect if args.size == 0
7
- name = args[0]
8
- @connection.get("/exec/#{name}/json")
9
- end
10
-
11
5
  def create name, body = {}
12
- validate Docker::API::InvalidRequestBody, [:AttachStdin, :AttachStdout, :AttachStderr, :DetachKeys, :Tty, :Env, :Cmd, :Privileged, :User, :WorkingDir], body
13
6
  @connection.request(method: :post, path: "/containers/#{name}/exec", headers: {"Content-Type": "application/json"}, body: body.to_json )
14
7
  end
15
8
 
16
9
  def start name, body = {}
17
- validate Docker::API::InvalidRequestBody, [:Detach, :Tty], body
18
-
19
10
  stream = ""
20
11
  response = @connection.request(method: :post,
21
12
  path: "/exec/#{name}/start",
@@ -28,10 +19,24 @@ module Docker
28
19
  end
29
20
 
30
21
  def resize name, params = {}
31
- validate Docker::API::InvalidParameter, [:w, :h], params
32
22
  @connection.post(build_path("/exec/#{name}/resize", params))
33
23
  end
34
24
 
25
+ def details name
26
+ @connection.get("/exec/#{name}/json")
27
+ end
28
+
29
+ #################################################
30
+ # Items in this area to be removed before 1.0.0 #
31
+ #################################################
32
+ def inspect *args
33
+ return super.inspect if args.size == 0
34
+ warn "WARNING: #inspect is deprecated and will be removed in the future, please use #details instead."
35
+ name = args[0]
36
+ details(name)
37
+ end
38
+ #################################################
39
+
35
40
  end
36
41
  end
37
42
  end
@@ -1,140 +1,190 @@
1
- require "base64"
2
- require "json"
3
- require 'fileutils'
4
- module Docker
5
- module API
6
- CommitBody = [: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]
7
- BuildParams = [:dockerfile, :t, :extrahosts, :remote, :q, :nocache, :cachefrom, :pull, :rm, :forcerm, :memory, :memswap, :cpushares, :cpusetcpus, :cpuperiod, :cpuquota, :buildargs, :shmsize, :squash, :labels, :networkmode, :platform, :target, :outputs]
8
- class Image < Docker::API::Base
9
-
10
- def base_path
11
- "/images"
12
- end
13
-
14
- def inspect *args
15
- return super.inspect if args.size == 0
16
- name = args[0]
17
- @connection.get(build_path([name, "json"]))
18
- end
19
-
20
- def history name
21
- @connection.get(build_path([name, "history"]))
22
- end
23
-
24
- def list params = {}
25
- validate Docker::API::InvalidParameter, [:all, :filters, :digests], params
26
- @connection.get(build_path(["json"], params))
27
- end
28
-
29
- def search params = {}
30
- validate Docker::API::InvalidParameter, [:term, :limit, :filters], params
31
- @connection.get(build_path(["search"], params))
32
- end
33
-
34
- def tag name, params = {}
35
- validate Docker::API::InvalidParameter, [:repo, :tag], params
36
- @connection.post(build_path([name, "tag"], params))
37
- end
38
-
39
- def prune params = {}
40
- validate Docker::API::InvalidParameter, [:filters], params
41
- @connection.post(build_path(["prune"], params))
42
- end
43
-
44
- def remove name, params = {}
45
- validate Docker::API::InvalidParameter, [:force, :noprune], params
46
- @connection.delete(build_path([name], params))
47
- end
48
-
49
- def export name, path = "exported_image"
50
- file = File.open("/tmp/exported-image", "wb")
51
- streamer = lambda do |chunk, remaining_bytes, total_bytes|
52
- file.write(chunk)
53
- end
54
- response = @connection.request(method: :get, path: build_path([name, "get"]) , response_block: streamer)
55
- file.close
56
- response.status == 200 ? FileUtils.mv("/tmp/exported-image", File.expand_path(path)) : FileUtils.rm("/tmp/exported-image")
57
- response
58
- end
59
-
60
- def import path, params = {}
61
- validate Docker::API::InvalidParameter, [:quiet], params
62
- file = File.open(File.expand_path(path), "r")
63
- response = @connection.request(method: :post, path: build_path(["load"], params) , headers: {"Content-Type" => "application/x-tar"}, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
64
- file.close
65
- response
66
- end
67
-
68
- def push name, params = {}, authentication = {}
69
- validate Docker::API::InvalidParameter, [:tag], params
70
-
71
- if authentication.keys.size > 0
72
- @connection.request(method: :post, path: build_path([name, "push"], params), headers: { "X-Registry-Auth" => Base64.urlsafe_encode64(authentication.to_json.to_s).chomp } )
73
- else
74
- raise Docker::API::Error.new("Provide authentication parameters to push an image")
75
- end
76
- end
77
-
78
- def commit params = {}, body = {}
79
- validate Docker::API::InvalidParameter, [:container, :repo, :tag, :comment, :author, :pause, :changes], params
80
- validate Docker::API::InvalidRequestBody, Docker::API::CommitBody, body
81
- container = Docker::API::Container.new.inspect(params[:container])
82
- return container if [404, 301].include? container.status
83
- body = JSON.parse(container.body)["Config"].merge(body)
84
- @connection.request(method: :post, path: build_path("/commit", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
85
- end
86
-
87
- def create params = {}, authentication = {}
88
- validate Docker::API::InvalidParameter, [:fromImage, :fromSrc, :repo, :tag, :message, :platform], params
89
-
90
- if authentication.keys.size > 0
91
- auth = Docker::API::System.new.auth(authentication)
92
- return auth unless [200, 204].include? auth.status
93
- @connection.request(method: :post, path: build_path(["create"], params), headers: { "X-Registry-Auth" => Base64.encode64(authentication.to_json.to_s).chomp } )
94
- elsif params.has_key? :fromSrc
95
- if params[:fromSrc].match(/^(http|https)/)
96
- @connection.request(method: :post, path: build_path(["create"], params))
97
- else
98
- file = File.open(File.expand_path(params[:fromSrc]), "r")
99
- params[:fromSrc] = "-"
100
- response = @connection.request(method: :post, path: build_path(["create"], params) , headers: {"Content-Type" => "application/x-tar"}, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
101
- file.close
102
- response
103
- end
104
- else
105
- @connection.post(build_path(["create"], params))
106
- end
107
- end
108
-
109
- def build path, params = {}, authentication = {}
110
- raise Docker::API::Error.new("Expected path or params[:remote]") unless path || params[:remote]
111
- validate Docker::API::InvalidParameter, Docker::API::BuildParams, params
112
-
113
- header = {"Content-type": "application/x-tar"}
114
- if authentication.keys.size > 0
115
- authentication.each_key do |server|
116
- auth = Docker::API::System.new.auth({username: authentication[server][:username] ,password:authentication[server][:password], serveraddress: server})
117
- return auth unless [200, 204].include? auth.status
118
- end
119
- header.merge!({"X-Registry-Config": Base64.urlsafe_encode64(authentication.to_json.to_s).chomp})
120
- end
121
-
122
- begin
123
- file = File.open( File.expand_path( path ) , "r")
124
- response = @connection.request(method: :post, path: build_path("/build", params), headers: header, request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s})
125
- file.close
126
- rescue
127
- response = @connection.request(method: :post, path: build_path("/build", params), headers: header)
128
- end
129
- response
130
- end
131
-
132
- def delete_cache params = {}
133
- validate Docker::API::InvalidParameter, [:all, "keep-storage", :filters], params
134
- @connection.post(build_path("/build/prune", params))
135
- end
1
+ ##
2
+ # This class represents the Docker API endpoints regarding images.
3
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Image
4
+ class Docker::API::Image < Docker::API::Base
5
+
6
+ ##
7
+ # Return low-level information about an image.
8
+ # Docker API: GET /images/{name}/json
9
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageInspect
10
+ #
11
+ # @param name [String]: The ID or name of the image.
12
+ def details name
13
+ @connection.get("/images/#{name}/json")
14
+ end
15
+
16
+ ##
17
+ # Return image digest and platform information by contacting the registry.
18
+ # Docker API: GET /distribution/{name}/json
19
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Distribution
20
+ #
21
+ # @param name [String]: The ID or name of the image.
22
+ def distribution name
23
+ @connection.get("/distribution/#{name}/json")
24
+ end
25
+
26
+ ##
27
+ # Return parent layers of an image.
28
+ # Docker API: GET /images/{name}/history
29
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageHistory
30
+ #
31
+ # @param name [String]: The ID or name of the image.
32
+ def history name
33
+ @connection.get("/images/#{name}/history")
34
+ end
35
+
36
+ ##
37
+ # Returns a list of images on the server. Note that it uses a different, smaller representation of an image than inspecting a single image.
38
+ # Docker API: GET /images/json
39
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageList
40
+ #
41
+ # @param params [Hash]: Parameters that are appended to the URL.
42
+ def list params = {}
43
+ @connection.get(build_path("/images/json", params))
44
+ end
136
45
 
46
+ ##
47
+ # Search for an image on Docker Hub.
48
+ # Docker API: GET /images/search
49
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageSearch
50
+ #
51
+ # @param params [Hash]: Parameters that are appended to the URL.
52
+ def search params = {}
53
+ @connection.get(build_path("/images/search", params))
54
+ end
55
+
56
+ ##
57
+ # Tag an image so that it becomes part of a repository.
58
+ # Docker API: POST /images/{name}/tag
59
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageTag
60
+ #
61
+ # @param name [String]: The ID or name of the image.
62
+ # @param params [Hash]: Parameters that are appended to the URL.
63
+ def tag name, params = {}
64
+ @connection.post(build_path("/images/#{name}/tag", params))
65
+ end
66
+
67
+ ##
68
+ # Delete unused images.
69
+ # Docker API: POST /images/prune
70
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImagePrune
71
+ #
72
+ # @param params [Hash]: Parameters that are appended to the URL.
73
+ def prune params = {}
74
+ @connection.post(build_path("/images/prune", params))
75
+ end
76
+
77
+ ##
78
+ # Remove an image, along with any untagged parent images that were referenced by that image.
79
+ # Images can't be removed if they have descendant images, are being used by a running container or are being used by a build.
80
+ # Docker API: DELETE /images/{name}
81
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageDelete
82
+ #
83
+ # @param name [String]: The ID or name of the image.
84
+ # @param params [Hash]: Parameters that are appended to the URL.
85
+ def remove name, params = {}
86
+ @connection.delete(build_path("/images/#{name}", params))
87
+ end
88
+
89
+ ##
90
+ # Export an image.
91
+ # Docker API: GET /images/{name}/get
92
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageGet
93
+ #
94
+ # @param name [String]: The ID or name of the image.
95
+ # @param path [Hash]: Parameters that are appended to the URL.
96
+ # @param block [Hash]: Request body to be sent as json.
97
+ # @block: Replace the default file writing method.
98
+ def export name, path = "exported_image", &block
99
+ @connection.request(method: :get, path: build_path("/images/#{name}/get") , response_block: block_given? ? block.call : default_writer(path))
100
+ end
101
+
102
+ ##
103
+ # Import images.
104
+ # Docker API: POST /images/load
105
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageLoad
106
+ #
107
+ # @param name [String]: The ID or name of the image.
108
+ # @param params [Hash]: Parameters that are appended to the URL.
109
+ def import path, params = {}
110
+ default_reader(path, build_path("/images/load", params))
111
+ end
112
+
113
+ ##
114
+ # Push an image to a registry.
115
+ # Docker API: POST /images/{name}/push
116
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImagePush
117
+ #
118
+ # @param name [String]: The ID or name of the image.
119
+ # @param params [Hash]: Parameters that are appended to the URL.
120
+ # @param authentication [Hash]: Authentication parameters.
121
+ def push name, params = {}, authentication = {}
122
+ raise Docker::API::Error.new("Provide authentication parameters to push an image") unless authentication.keys.size > 0
123
+ @connection.request(method: :post, path: build_path("/images/#{name}/push", params), headers: { "X-Registry-Auth" => Base64.urlsafe_encode64(authentication.to_json.to_s).chomp } )
124
+ end
125
+
126
+ ##
127
+ # Create a new image from a container.
128
+ # Docker API: POST /commit
129
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageCommit
130
+ #
131
+ # @param params [Hash]: Parameters that are appended to the URL.
132
+ # @param body [Hash]: Request body to be sent as json.
133
+ def commit params = {}, body = {}
134
+ container = Docker::API::Container.new.details(params[:container])
135
+ return container if [404, 301].include? container.status
136
+ @connection.request(method: :post, path: build_path("/commit", params), headers: {"Content-Type": "application/json"}, body: container.json["Config"].merge(body).to_json)
137
+ end
138
+
139
+ ##
140
+ # Create an image by either pulling it from a registry or importing it.
141
+ # Docker API: POST /images/create
142
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageCreate
143
+ #
144
+ # @param params [Hash]: Parameters that are appended to the URL.
145
+ # @param authentication [Hash]: Authentication parameters.
146
+ # @param &block: Replace the default output to stdout behavior.
147
+ def create params = {}, authentication = {}, &block
148
+ request = {method: :post, path: build_path("/images/create", params), response_block: block_given? ? block.call : default_streamer }
149
+ if params.has_key? :fromSrc and !params[:fromSrc].match(/^(http|https)/) # then it's using a tar file
150
+ path = params[:fromSrc]
151
+ params[:fromSrc] = "-"
152
+ default_reader(path, build_path("/images/create", params))
153
+ else
154
+ request[:headers] = { "X-Registry-Auth" => Base64.encode64(authentication.to_json.to_s).chomp } if authentication.keys.size > 0
155
+ @connection.request(request)
156
+ end
157
+ end
158
+
159
+ ##
160
+ # Build an image from a tar archive with a Dockerfile in it.
161
+ # Docker API: POST /build
162
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ImageBuild
163
+ #
164
+ # @param path [String]: Path to the tar file.
165
+ # @param params [Hash]: Parameters that are appended to the URL.
166
+ # @param authentication [Hash]: Authentication parameters.
167
+ # @param &block: Replace the default output to stdout behavior.
168
+ def build path, params = {}, authentication = {}, &block
169
+ raise Docker::API::Error.new("Expected path or params[:remote]") unless path || params[:remote]
170
+
171
+ headers = {"Content-type": "application/x-tar"}
172
+ headers.merge!({"X-Registry-Config": Base64.urlsafe_encode64(authentication.to_json.to_s).chomp}) if authentication.keys.size > 0
173
+
174
+ if path == nil and params.has_key? :remote
175
+ response = @connection.request(method: :post, path: build_path("/build", params), headers: headers, response_block: block_given? ? block.call : default_streamer)
176
+ else
177
+ default_reader(path, build_path("/build", params), headers)
137
178
  end
179
+ end
138
180
 
181
+ ##
182
+ # Delete builder cache.
183
+ # Docker API: POST /build/prune
184
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/BuildPrune
185
+ #
186
+ # @param params [Hash]: Parameters that are appended to the URL.
187
+ def delete_cache params = {}
188
+ @connection.post(build_path("/build/prune", params))
139
189
  end
140
190
  end