dockerapi 0.12.0 → 0.17.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.
@@ -1,170 +1,310 @@
1
- module Docker
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
- class Container < Docker::API::Base
7
-
8
- def list params = {}
9
- validate Docker::API::InvalidParameter, [:all, :limit, :size, :filters], params
10
- @connection.get(build_path(["json"], params))
11
- end
12
-
13
- def details name, params = {}
14
- validate Docker::API::InvalidParameter, [:size], params
15
- @connection.get(build_path([name, "json"], params))
16
- end
17
-
18
- def top name, params = {}
19
- validate Docker::API::InvalidParameter, [:ps_args], params
20
- @connection.get(build_path([name, "top"], params))
21
- end
22
-
23
- def changes name
24
- @connection.get(build_path([name, "changes"]))
25
- end
26
-
27
- def start name, params = {}
28
- validate Docker::API::InvalidParameter, [:detachKeys], params
29
- @connection.post(build_path([name, "start"], params))
30
- end
31
-
32
- def stop name, params = {}
33
- validate Docker::API::InvalidParameter, [:t], params
34
- @connection.post(build_path([name, "stop"], params))
35
- end
36
-
37
- def restart name, params = {}
38
- validate Docker::API::InvalidParameter, [:t], params
39
- @connection.post(build_path([name, "restart"], params))
40
- end
41
-
42
- def kill name, params = {}
43
- validate Docker::API::InvalidParameter, [:signal], params
44
- @connection.post(build_path([name, "kill"], params))
45
- end
46
-
47
- def wait name, params = {}
48
- validate Docker::API::InvalidParameter, [:condition], params
49
- @connection.post(build_path([name, "wait"], params))
50
- end
51
-
52
- def update name, body = {}
53
- validate Docker::API::InvalidRequestBody, Docker::API::UpdateBody, body
54
- @connection.request(method: :post, path: build_path([name, "update"]), headers: {"Content-Type": "application/json"}, body: body.to_json)
55
- end
56
-
57
- def rename name, params = {}
58
- validate Docker::API::InvalidParameter, [:name], params
59
- @connection.post(build_path([name, "rename"], params))
60
- end
61
-
62
- def resize name, params = {}
63
- validate Docker::API::InvalidParameter, [:w, :h], params
64
- @connection.post(build_path([name, "resize"], params))
65
- end
66
-
67
- def prune params = {}
68
- validate Docker::API::InvalidParameter, [:filters], params
69
- @connection.post(build_path(["prune"], params))
70
- end
71
-
72
- def pause name
73
- @connection.post(build_path([name, "pause"]))
74
- end
75
-
76
- def unpause name
77
- @connection.post(build_path([name, "unpause"]))
78
- end
79
-
80
- def remove name, params = {}
81
- validate Docker::API::InvalidParameter, [:v, :force, :link], params
82
- @connection.delete(build_path([name]))
83
- end
84
-
85
- def logs name, params = {}
86
- validate Docker::API::InvalidParameter, [:follow, :stdout, :stderr, :since, :until, :timestamps, :tail], params
87
-
88
- path = build_path([name, "logs"], params)
89
-
90
- if params[:follow] == true || params[:follow] == 1
91
- @connection.request(method: :get, path: path , response_block: lambda { |chunk, remaining_bytes, total_bytes| puts chunk.inspect })
92
- else
93
- @connection.get(path)
94
- end
95
- end
96
-
97
- def attach name, params = {}
98
- validate Docker::API::InvalidParameter, [:detachKeys, :logs, :stream, :stdin, :stdout, :stderr], params
99
- @connection.request(method: :post, path: build_path([name, "attach"], params) , response_block: lambda { |chunk, remaining_bytes, total_bytes| puts chunk.inspect })
100
- end
101
-
102
- def create params = {}, body = {}
103
- validate Docker::API::InvalidParameter, [:name], params
104
- validate Docker::API::InvalidRequestBody, Docker::API::CreateBody, body
105
- @connection.request(method: :post, path: build_path(["create"], params), headers: {"Content-Type": "application/json"}, body: body.to_json)
106
- end
107
-
108
- def stats name, params = {}
109
- validate Docker::API::InvalidParameter, [:stream], params
110
- path = build_path([name, "stats"], params)
111
-
112
- if params[:stream] == true || params[:stream] == 1
113
- streamer = lambda do |chunk, remaining_bytes, total_bytes|
114
- puts chunk
115
- end
116
- @connection.request(method: :get, path: path , response_block: streamer)
117
- else
118
- @connection.get(path)
119
- end
120
- end
121
-
122
- def export name, path = "exported_container"
123
- response = self.details(name)
124
- if response.status == 200
125
- file = File.open(File.expand_path(path), "wb")
126
- streamer = lambda do |chunk, remaining_bytes, total_bytes|
127
- file.write(chunk)
128
- end
129
- response = @connection.request(method: :get, path: build_path([name, "export"]) , response_block: streamer)
130
- file.close
131
- end
132
- response
133
- end
134
-
135
- def archive name, file, params = {}
136
- validate Docker::API::InvalidParameter, [:path, :noOverwriteDirNonDir, :copyUIDGID], params
137
-
138
- begin # File exists on disk, send it to container
139
- file = File.open( File.expand_path( file ) , "r")
140
- response = @connection.request(method: :put, path: build_path([name, "archive"], params) , request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
141
- file.close
142
- rescue Errno::ENOENT # File doesnt exist, get it from container
143
- response = @connection.head(build_path([name, "archive"], params))
144
- if response.status == 200 # file exists in container
145
- file = File.open( File.expand_path( file ) , "wb")
146
- response = @connection.request(method: :get, path: build_path([name, "archive"], params) , response_block: lambda { |chunk, remaining_bytes, total_bytes| file.write(chunk) })
147
- file.close
148
- end
149
- end
150
- response
151
- end
152
-
153
- #################################################
154
- # Items in this area to be removed before 1.0.0 #
155
- #################################################
156
- def base_path
157
- "/containers"
158
- end
159
-
160
- def inspect *args
161
- return super.inspect if args.size == 0
162
- warn "WARNING: #inspect is deprecated and will be removed in the future, please use #details instead."
163
- name, params = args[0], args[1] || {}
164
- details(name, params)
165
- end
166
- #################################################
1
+ ##
2
+ # This class represents the Docker API endpoints regarding containers.
3
+ # @see https://docs.docker.com/engine/api/v1.40/#tag/Container
4
+ class Docker::API::Container < Docker::API::Base
167
5
 
6
+ ##
7
+ # Returns a list of containers.
8
+ #
9
+ # Docker API: GET /containers/json
10
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerList
11
+ #
12
+ # @param params [Hash]: Parameters that are appended to the URL.
13
+ def list params = {}
14
+ @connection.get(build_path("/containers/json", params))
15
+ end
16
+
17
+ ##
18
+ # Return low-level information about a container.
19
+ #
20
+ # Docker API: GET /containers/{id}/json
21
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerInspect
22
+ #
23
+ # @param name [String]: The ID or name of the container.
24
+ # @param params [Hash]: Parameters that are appended to the URL.
25
+ def details name, params = {}
26
+ @connection.get(build_path("/containers/#{name}/json", params))
27
+ end
28
+
29
+ ##
30
+ # List processes running inside a container.
31
+ #
32
+ # Docker API: GET /containers/{id}/top
33
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerTop
34
+ #
35
+ # @param name [String]: The ID or name of the container.
36
+ # @param params [Hash]: Parameters that are appended to the URL.
37
+ def top name, params = {}
38
+ @connection.get(build_path("/containers/#{name}/top", params))
39
+ end
40
+
41
+ ##
42
+ # Get changes on a container’s filesystem.
43
+ #
44
+ # Docker API: GET /containers/{id}/changes
45
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerChanges
46
+ #
47
+ # @param name [String]: The ID or name of the container.
48
+ def changes name
49
+ @connection.get("/containers/#{name}/changes")
50
+ end
51
+
52
+ ##
53
+ # Start a container.
54
+ #
55
+ # Docker API: POST /containers/{id}/start
56
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerStart
57
+ #
58
+ # @param name [String]: The ID or name of the container.
59
+ # @param params [Hash]: Parameters that are appended to the URL.
60
+ def start name, params = {}
61
+ @connection.post(build_path("/containers/#{name}/start", params))
62
+ end
63
+
64
+ ##
65
+ # Stop a container.
66
+ #
67
+ # Docker API: POST /containers/{id}/stop
68
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerStop
69
+ #
70
+ # @param name [String]: The ID or name of the container.
71
+ # @param params [Hash]: Parameters that are appended to the URL.
72
+ def stop name, params = {}
73
+ @connection.post(build_path("/containers/#{name}/stop", params))
74
+ end
75
+
76
+ ##
77
+ # Restart a container.
78
+ #
79
+ # Docker API: POST /containers/{id}/restart
80
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerRestart
81
+ #
82
+ # @param name [String]: The ID or name of the container.
83
+ # @param params [Hash]: Parameters that are appended to the URL.
84
+ def restart name, params = {}
85
+ @connection.post(build_path("/containers/#{name}/restart", params))
86
+ end
87
+
88
+ ##
89
+ # Send a POSIX signal to a container, defaulting to killing to the container.
90
+ #
91
+ # Docker API: POST /containers/{id}/kill
92
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerKill
93
+ #
94
+ # @param name [String]: The ID or name of the container.
95
+ # @param params [Hash]: Parameters that are appended to the URL.
96
+ def kill name, params = {}
97
+ @connection.post(build_path("/containers/#{name}/kill", params))
98
+ end
99
+
100
+ ##
101
+ # Block until a container stops, then returns the exit code.
102
+ #
103
+ # Docker API: POST /containers/{id}/wait
104
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerWait
105
+ #
106
+ # @param name [String]: The ID or name of the container.
107
+ # @param params [Hash]: Parameters that are appended to the URL.
108
+ def wait name, params = {}
109
+ @connection.post(build_path("/containers/#{name}/wait", params))
110
+ end
111
+
112
+ ##
113
+ # Change various configuration options of a container without having to recreate it.
114
+ #
115
+ # Docker API: POST /containers/{id}/update
116
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerUpdate
117
+ #
118
+ # @param name [String]: The ID or name of the container.
119
+ # @param body [Hash]: Request body to be sent as json.
120
+ def update name, body = {}
121
+ @connection.request(method: :post, path: "/containers/#{name}/update", headers: {"Content-Type": "application/json"}, body: body.to_json)
122
+ end
123
+
124
+ ##
125
+ # Rename a container.
126
+ #
127
+ # Docker API: POST /containers/{id}/rename
128
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerRename
129
+ #
130
+ # @param name [String]: The ID or name of the container.
131
+ # @param params [Hash]: Parameters that are appended to the URL.
132
+ def rename name, params = {}
133
+ @connection.post(build_path("/containers/#{name}/rename", params))
134
+ end
135
+
136
+ ##
137
+ # Resize the TTY for a container.
138
+ #
139
+ # Docker API: POST /containers/{id}/resize
140
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerResize
141
+ #
142
+ # @param name [String]: The ID or name of the container.
143
+ # @param params [Hash]: Parameters that are appended to the URL.
144
+ def resize name, params = {}
145
+ @connection.post(build_path("/containers/#{name}/resize", params))
146
+ end
147
+
148
+ ##
149
+ # Delete stopped containers.
150
+ #
151
+ # Docker API: POST /containers/prune
152
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerPrune
153
+ #
154
+ # @param params [Hash]: Parameters that are appended to the URL.
155
+ def prune params = {}
156
+ @connection.post(build_path("/containers/prune", params))
157
+ end
158
+
159
+ ##
160
+ # Pause a container.
161
+ #
162
+ # Docker API: POST /containers/{id}/pause
163
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerPause
164
+ #
165
+ # @param name [String]: The ID or name of the container.
166
+ def pause name
167
+ @connection.post("/containers/#{name}/pause")
168
+ end
169
+
170
+ ##
171
+ # Resume a container which has been paused.
172
+ #
173
+ # Docker API: POST /containers/{id}/unpause
174
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerUnpause
175
+ #
176
+ # @param name [String]: The ID or name of the container.
177
+ def unpause name
178
+ @connection.post("/containers/#{name}/unpause")
179
+ end
180
+
181
+ ##
182
+ # Remove a container.
183
+ #
184
+ # Docker API: DELETE /containers/{id}
185
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerDelete
186
+ #
187
+ # @param name [String]: The ID or name of the container.
188
+ # @param params [Hash]: Parameters that are appended to the URL.
189
+ def remove name, params = {}
190
+ @connection.delete(build_path("/containers/#{name}", params))
191
+ end
192
+
193
+ ##
194
+ # Get stdout and stderr logs from a container.
195
+ #
196
+ # Docker API: GET /containers/{id}/logs
197
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerLogs
198
+ #
199
+ # @param name [String]: The ID or name of the container.
200
+ # @param params [Hash]: Parameters that are appended to the URL.
201
+ # @param &block: Replace the default output to stdout behavior.
202
+ def logs name, params = {}, &block
203
+
204
+ path = build_path("/containers/#{name}/logs", params)
205
+
206
+ if [true, 1 ].include? params[:follow]
207
+ @connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
208
+ else
209
+ @connection.get(path)
168
210
  end
169
211
  end
212
+
213
+ ##
214
+ # Attach to a container.
215
+ #
216
+ # Docker API: POST /containers/{id}/attach
217
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerAttach
218
+ #
219
+ # @param name [String]: The ID or name of the container.
220
+ # @param params [Hash]: Parameters that are appended to the URL.
221
+ # @param &block: Replace the default output to stdout behavior.
222
+ def attach name, params = {}, &block
223
+ @connection.request(method: :post, path: build_path("/containers/#{name}/attach", params) , response_block: block_given? ? block : default_streamer)
224
+ end
225
+
226
+ ##
227
+ # Create a container.
228
+ #
229
+ # Docker API: POST /containers/create
230
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerCreate
231
+ #
232
+ # @param params [Hash]: Parameters that are appended to the URL.
233
+ # @param body [Hash]: Request body to be sent as json.
234
+ def create params = {}, body = {}
235
+ @connection.request(method: :post, path: build_path("/containers/create", params), headers: {"Content-Type": "application/json"}, body: body.to_json)
236
+ end
237
+
238
+ ##
239
+ # Get container stats based on resource usage.
240
+ #
241
+ # Docker API: GET /containers/{id}/stats
242
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerStats
243
+ #
244
+ # @param name [String]: The ID or name of the container.
245
+ # @param params [Hash]: Parameters that are appended to the URL.
246
+ # @param &block: Replace the default output to stdout behavior.
247
+ def stats name, params = {}, &block
248
+ path = build_path("/containers/#{name}/stats", params)
249
+ if [true, 1 ].include? params[:stream]
250
+ @connection.request(method: :get, path: path , response_block: block_given? ? block : default_streamer)
251
+ else
252
+ @connection.get(path)
253
+ end
254
+ end
255
+
256
+ ##
257
+ # Export the contents of a container as a tarball.
258
+ #
259
+ # Docker API: GET /containers/{id}/export
260
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerExport
261
+ #
262
+ # @param name [String]: The ID or name of the container.
263
+ # @param path [String]: Path to the exportated file.
264
+ # @param &block: Replace the default file writing behavior.
265
+ def export name, path, &block
266
+ response = self.details(name)
267
+ return response unless response.status == 200
268
+ @connection.request(method: :get, path: "/containers/#{name}/export" , response_block: block_given? ? block : default_writer(path))
269
+ end
270
+
271
+ ##
272
+ # Get an archive of a filesystem resource in a container.
273
+ #
274
+ # Get a tar archive of a resource in the filesystem of container id.
275
+ #
276
+ # Docker API: GET /containers/{id}/archive
277
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/ContainerArchive
278
+ #
279
+ # @param name [String]: The ID or name of the container.
280
+ # @param path [String]: Path to the exportated file.
281
+ # @param params [Hash]: Parameters that are appended to the URL.
282
+ # @param &block: Replace the default file writing behavior.
283
+ def get_archive name, path, params = {}, &block
284
+ response = @connection.head(build_path("/containers/#{name}/archive", params))
285
+ return response unless response.status == 200
286
+
287
+ file = File.open( File.expand_path( path ) , "wb")
288
+ response = @connection.request(method: :get, path: build_path("/containers/#{name}/archive", params) , response_block: block_given? ? block : lambda { |chunk, remaining_bytes, total_bytes| file.write(chunk) })
289
+ file.close
290
+ response
291
+ end
292
+
293
+ ##
294
+ # Extract an archive of files or folders to a directory in a container.
295
+ #
296
+ # Upload a tar archive to be extracted to a path in the filesystem of container id.
297
+ #
298
+ # Docker API: PUT /containers/{id}/archive
299
+ # @see https://docs.docker.com/engine/api/v1.40/#operation/PutContainerArchive
300
+ #
301
+ # @param name [String]: The ID or name of the container.
302
+ # @param path [String]: Path of the tar file.
303
+ # @param params [Hash]: Parameters that are appended to the URL.
304
+ def put_archive name, path, params = {}
305
+ file = File.open( File.expand_path( path ) , "r")
306
+ response = @connection.request(method: :put, path: build_path("/containers/#{name}/archive", params) , request_block: lambda { file.read(Excon.defaults[:chunk_size]).to_s} )
307
+ file.close
308
+ response
309
+ end
170
310
  end