UNPKG

10.8 kBPlain TextView Raw
1'use strict'
2
3import Modem = require('docker-modem')
4import fs = require('fs')
5
6/**
7 * Class representing an image
8 */
9export class Image {
10 modem: Modem
11 id: String
12 data: Object = {}
13
14 /**
15 * Creates a new image
16 * @param {Modem} modem Modem to connect to the remote service
17 * @param {string} id Container id
18 */
19 constructor (modem: Modem, id: String) {
20 this.modem = modem
21 this.id = id
22 }
23
24 /**
25 * Get low-level information on an image
26 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-an-image
27 * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
28 * @param {Object} opts Query params in the request (optional)
29 * @return {Promise} Promise return the image
30 */
31 status (opts?: Object): Promise<Image> {
32 const call = {
33 path: `/images/${this.id}/json?`,
34 method: 'GET',
35 options: opts,
36 statusCodes: {
37 200: true,
38 404: 'no such image',
39 500: 'server error'
40 }
41 }
42
43 return new Promise((resolve, reject) => {
44 this.modem.dial(call, (err, conf) => {
45 if (err) return reject(err)
46 const image = new Image(this.modem, this.id)
47 image.data = conf
48 resolve(image)
49 })
50 })
51 }
52
53 /**
54 * History of the image
55 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-the-history-of-an-image
56 * @param {Object} opts Query params in the request (optional)
57 * @param {String} id ID of the image to inspect, if it's not set, use the id of the object (optional)
58 * @return {Promise} Promise return the events in the history
59 */
60 history (opts?: Object): Promise<Array<Object>> {
61 const call = {
62 path: `/images/${this.id}/history?`,
63 method: 'GET',
64 options: opts,
65 statusCodes: {
66 200: true,
67 404: 'no such image',
68 500: 'server error'
69 }
70 }
71
72 return new Promise((resolve, reject) => {
73 this.modem.dial(call, (err, events: Array<Object>) => {
74 if (err) return reject(err)
75 resolve(events)
76 })
77 })
78 }
79
80 /**
81 * Push an image to the registry
82 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/push-an-image-on-the-registry
83 * @param {Object} auth Authentication (optional)
84 * @param {Object} opts Query params in the request (optional)
85 * @return {Promise} Promise return the resulting stream
86 */
87 push (auth?: Object, opts?: Object): Promise<Object> {
88 const call = {
89 path: `/images/${this.id}/push?`,
90 method: 'POST',
91 options: opts,
92 isStream: true,
93 authconfig: auth,
94 statusCodes: {
95 200: true,
96 404: 'no such image',
97 500: 'server error'
98 }
99 }
100
101 return new Promise((resolve, reject) => {
102 this.modem.dial(call, (err, stream: Object) => {
103 if (err) return reject(err)
104 resolve(stream)
105 })
106 })
107 }
108
109 /**
110 * Tag the image into the registry
111 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/tag-an-image-into-a-repository
112 * @param {Object} opts Query params in the request (optional)
113 * @return {Promise} Promise return the image
114 */
115 tag (opts?: Object): Promise<Image> {
116 const call = {
117 path: `/images/${this.id}/tag?`,
118 method: 'POST',
119 options: opts,
120 statusCodes: {
121 201: true,
122 400: 'bad parameter',
123 404: 'no such image',
124 409: 'conflict',
125 500: 'server error'
126 }
127 }
128
129 return new Promise((resolve, reject) => {
130 this.modem.dial(call, (err, res) => {
131 if (err) return reject(err)
132 const image = new Image(this.modem, this.id)
133 resolve(image)
134 })
135 }).then((image: Image) => image.status())
136 }
137
138 /**
139 * Remove an image from the filesystem
140 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/remove-an-image
141 * @param {Object} opts Query params in the request (optional)
142 * @return {Promise} Promise return the result
143 */
144 remove (opts?: Object): Promise<Array<Object>> {
145 const call = {
146 path: `/images/${this.id}?`,
147 method: 'DELETE',
148 options: opts,
149 statusCodes: {
150 200: true,
151 404: 'no such image',
152 409: 'conflict',
153 500: 'server error'
154 }
155 }
156
157 return new Promise((resolve, reject) => {
158 this.modem.dial(call, (err, res: Array<Object>) => {
159 if (err) return reject(err)
160 resolve(res)
161 })
162 })
163 }
164
165 /**
166 * Get an image in a tarball
167 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/get-a-tarball-containing-all-images-in-a-repository
168 * @param {Object} opts Query params in the request (optional)
169 * @return {Promise} Promise return the stream with the tarball
170 */
171 get (opts?: Object): Promise<Object> {
172 const call = {
173 path: `/images/${this.id}/get?`,
174 method: 'GET',
175 options: opts,
176 isStream: true,
177 statusCodes: {
178 200: true,
179 500: 'server error'
180 }
181 }
182
183 return new Promise((resolve, reject) => {
184 this.modem.dial(call, (err, stream: Object) => {
185 if (err) return reject(err)
186 resolve(stream)
187 })
188 })
189 }
190}
191
192export default class {
193 modem: Modem
194
195 constructor(modem: Modem) {
196 this.modem = modem
197 }
198
199 /**
200 * Get a Image object
201 * @param {id} string ID of the secret
202 * @return {Image}
203 */
204 get (id: String): Image {
205 return new Image(this.modem, id)
206 }
207
208 /**
209 * Search an image on Docker Hub
210 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/search-images
211 * @param {Object} opts Query params in the request (optional)
212 * @return {Promise} Promise return the images
213 */
214 search (opts?: Object): Promise<Array<Object>> {
215 const call = {
216 path: `/images/search?`,
217 method: 'GET',
218 options: opts,
219 statusCodes: {
220 200: true,
221 500: 'server error'
222 }
223 }
224
225 return new Promise((resolve, reject) => {
226 this.modem.dial(call, (err, images) => {
227 if (err) return reject(err)
228 resolve(images)
229 })
230 })
231 }
232
233 /**
234 * Get the list of images
235 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-images
236 * @param {Object} opts Query params in the request (optional)
237 * @return {Promise} Promise returning the result as a list of images
238 */
239 list (opts?: Object): Promise<Array<Image>> {
240 const call = {
241 path: '/images/json?',
242 method: 'GET',
243 options: opts,
244 statusCodes: {
245 200: true,
246 400: 'bad request',
247 500: 'server error'
248 }
249 }
250
251 return new Promise((resolve, reject) => {
252 this.modem.dial(call, (err, images) => {
253 if (err) return reject(err)
254 resolve(images.map((conf) => {
255 const image = new Image(this.modem, conf.Id)
256 image.data = conf
257 return image
258 }))
259 })
260 })
261 }
262
263 /**
264 * Build image from dockerfile
265 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/build-image-from-a-dockerfile
266 * @file {File} file Dockerfile to build
267 * @param {Object} opts Query params in the request (optional)
268 * @return {Promise} Promise return the resulting stream
269 */
270 build (file: fs.ReadStream, opts?: Object): Promise<Object> {
271 const call = {
272 path: '/build?',
273 method: 'POST',
274 options: opts,
275 file: file,
276 isStream: true,
277 statusCodes: {
278 200: true,
279 500: 'server error'
280 }
281 }
282
283 return new Promise((resolve, reject) => {
284 this.modem.dial(call, (err, stream: Object) => {
285 if (err) return reject(err)
286 resolve(stream)
287 })
288 })
289 }
290
291 /**
292 * Create an image
293 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/create-an-image
294 * @param {Object} auth Authentication (optional)
295 * @param {Object} opts Query params in the request (optional)
296 * @return {Promise} Promise return the resulting stream
297 */
298 create (auth: Object, opts?: Object): Promise<Object> {
299 const call = {
300 path: '/images/create?',
301 method: 'POST',
302 options: opts,
303 isStream: true,
304 authconfig: auth,
305 statusCodes: {
306 200: true,
307 500: 'server error'
308 }
309 }
310
311 return new Promise((resolve, reject) => {
312 this.modem.dial(call, (err, stream: Object) => {
313 if (err) return reject(err)
314 resolve(stream)
315 })
316 })
317 }
318
319 /**
320 * Get all images in a tarball
321 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/get-a-tarball-containing-all-images
322 * @param {Object} opts Query params in the request (optional)
323 * @return {Promise} Promise return the stream with the tarball
324 */
325 getAll (opts?: Object): Promise<Object> {
326 const call = {
327 path: `/images/get?`,
328 method: 'GET',
329 options: opts,
330 isStream: true,
331 statusCodes: {
332 200: true,
333 500: 'server error'
334 }
335 }
336
337 return new Promise((resolve, reject) => {
338 this.modem.dial(call, (err, stream: Object) => {
339 if (err) return reject(err)
340 resolve(stream)
341 })
342 })
343 }
344
345 /**
346 * Load image from tarball
347 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/load-a-tarball-with-a-set-of-images-and-tags-into-docker
348 * @file {File} file Tarball to load
349 * @param {Object} opts Query params in the request (optional)
350 * @return {Promise} Promise return the stream with the process
351 */
352 load (file: fs.ReadStream, opts?: Object): Promise<Object> {
353 const call = {
354 path: '/images/load?',
355 method: 'POST',
356 options: opts,
357 file: file,
358 isStream: true,
359 statusCodes: {
360 200: true,
361 500: 'server error'
362 }
363 }
364
365 return new Promise((resolve, reject) => {
366 this.modem.dial(call, (err, stream: Object) => {
367 if (err) return reject(err)
368 resolve(stream)
369 })
370 })
371 }
372
373 /**
374 * Prune images
375 * https://docs.docker.com/engine/api/v1.25/#operation/ImagePrune
376 * @param {Object} opts Query params in the request (optional)
377 * @return {Promise} Promise returning the container
378 */
379 prune (opts?: Object): Promise<String> {
380 const call = {
381 path: `/images/prune`,
382 method: 'POST',
383 options: opts,
384 statusCodes: {
385 200: true,
386 500: 'server error'
387 }
388 }
389
390 return new Promise((resolve, reject) => {
391 this.modem.dial(call, (err, res: String) => {
392 if (err) return reject(err)
393 resolve(res)
394 })
395 })
396 }
397}