UNPKG

6.24 kBJavaScriptView Raw
1var util = require('./util');
2
3/**
4 * Represents an image
5 * @param {Object} modem docker-modem
6 * @param {String} name Image's name
7 */
8var Image = function(modem, name) {
9 this.modem = modem;
10 this.name = name;
11};
12
13Image.prototype[require('util').inspect.custom] = function() { return this; };
14
15/**
16 * Inspect
17 * @param {Function} callback Callback, if specified Docker will be queried.
18 * @return {Object} Name only if callback isn't specified.
19 */
20Image.prototype.inspect = function(callback) {
21 var self = this;
22
23 var opts = {
24 path: '/images/' + this.name + '/json',
25 method: 'GET',
26 statusCodes: {
27 200: true,
28 404: 'no such image',
29 500: 'server error'
30 }
31 };
32
33 if(callback === undefined) {
34 return new this.modem.Promise(function(resolve, reject) {
35 self.modem.dial(opts, function(err, data) {
36 if (err) {
37 return reject(err);
38 }
39 resolve(data);
40 });
41 });
42 } else {
43 this.modem.dial(opts, function(err, data) {
44 if (err) return callback(err, data);
45 callback(err, data);
46 });
47 }
48};
49
50/**
51 * Distribution
52 * @param {Object} opts
53 * @param {Function} callback Callback, if specified Docker will be queried.
54 * @return {Object} Name only if callback isn't specified.
55 */
56Image.prototype.distribution = function(opts, callback) {
57 var args = util.processArgs(opts, callback);
58 var self = this;
59
60 var fopts = {
61 path: '/distribution/' + this.name + '/json',
62 method: 'GET',
63 statusCodes: {
64 200: true,
65 401: 'no such image',
66 500: 'server error'
67 },
68 authconfig: (args.opts) ? args.opts.authconfig : undefined
69 };
70
71 if(args.callback === undefined) {
72 return new this.modem.Promise(function(resolve, reject) {
73 self.modem.dial(fopts, function(err, data) {
74 if (err) {
75 return reject(err);
76 }
77 resolve(data);
78 });
79 });
80 } else {
81 this.modem.dial(fopts, function(err, data) {
82 if (err) return args.callback(err, data);
83 args.callback(err, data);
84 });
85 }
86};
87
88/**
89 * History
90 * @param {Function} callback Callback
91 */
92Image.prototype.history = function(callback) {
93 var self = this;
94 var opts = {
95 path: '/images/' + this.name + '/history',
96 method: 'GET',
97 statusCodes: {
98 200: true,
99 404: 'no such image',
100 500: 'server error'
101 }
102 };
103
104 if(callback === undefined) {
105 return new this.modem.Promise(function(resolve, reject) {
106 self.modem.dial(opts, function(err, data) {
107 if (err) {
108 return reject(err);
109 }
110 resolve(data);
111 });
112 });
113 } else {
114 this.modem.dial(opts, function(err, data) {
115 if (err) return callback(err, data);
116 callback(err, data);
117 });
118 }
119};
120
121/**
122 * Get
123 * @param {Function} callback Callback with data stream.
124 */
125Image.prototype.get = function(callback) {
126 var self = this;
127 var opts = {
128 path: '/images/' + this.name + '/get',
129 method: 'GET',
130 isStream: true,
131 statusCodes: {
132 200: true,
133 500: 'server error'
134 }
135 };
136
137 if(callback === undefined) {
138 return new this.modem.Promise(function(resolve, reject) {
139 self.modem.dial(opts, function(err, data) {
140 if (err) {
141 return reject(err);
142 }
143 resolve(data);
144 });
145 });
146 } else {
147 this.modem.dial(opts, function(err, data) {
148 if (err) return callback(err, data);
149 callback(err, data);
150 });
151 }
152};
153
154/**
155 * Push
156 * @param {Object} opts Push options, like 'registry' (optional)
157 * @param {Function} callback Callback with stream.
158 * @param {Object} auth Registry authentication
159 */
160Image.prototype.push = function(opts, callback, auth) {
161 var self = this;
162 var args = util.processArgs(opts, callback);
163 var isStream = true;
164 if (args.opts.stream === false) {
165 isStream = false;
166 }
167 var optsf = {
168 path: '/images/' + this.name + '/push?',
169 method: 'POST',
170 options: args.opts,
171 authconfig: args.opts.authconfig || auth,
172 isStream: isStream,
173 statusCodes: {
174 200: true,
175 404: 'no such image',
176 500: 'server error'
177 }
178 };
179
180 delete optsf.options.authconfig;
181
182 if(callback === undefined) {
183 return new this.modem.Promise(function(resolve, reject) {
184 self.modem.dial(optsf, function(err, data) {
185 if (err) {
186 return reject(err);
187 }
188 resolve(data);
189 });
190 });
191 } else {
192 this.modem.dial(optsf, function(err, data) {
193 callback(err, data);
194 });
195 }
196};
197
198/**
199 * Tag
200 * @param {Object} opts Tag options, like 'repo' (optional)
201 * @param {Function} callback Callback
202 */
203Image.prototype.tag = function(opts, callback) {
204 var self = this;
205 var optsf = {
206 path: '/images/' + this.name + '/tag?',
207 method: 'POST',
208 options: opts,
209 statusCodes: {
210 200: true, // unofficial, but proxies may return it
211 201: true,
212 400: 'bad parameter',
213 404: 'no such image',
214 409: 'conflict',
215 500: 'server error'
216 }
217 };
218
219 if(callback === undefined) {
220 return new this.modem.Promise(function(resolve, reject) {
221 self.modem.dial(optsf, function(err, data) {
222 if (err) {
223 return reject(err);
224 }
225 resolve(data);
226 });
227 });
228 } else {
229 this.modem.dial(optsf, function(err, data) {
230 callback(err, data);
231 });
232 }
233};
234
235/**
236 * Removes the image
237 * @param {[Object]} opts Remove options (optional)
238 * @param {Function} callback Callback
239 */
240Image.prototype.remove = function(opts, callback) {
241 var self = this;
242 var args = util.processArgs(opts, callback);
243
244
245 var optsf = {
246 path: '/images/' + this.name + '?',
247 method: 'DELETE',
248 statusCodes: {
249 200: true,
250 404: 'no such image',
251 409: 'conflict',
252 500: 'server error'
253 },
254 options: args.opts
255 };
256
257
258 if(args.callback === undefined) {
259 return new this.modem.Promise(function(resolve, reject) {
260 self.modem.dial(optsf, function(err, data) {
261 if (err) {
262 return reject(err);
263 }
264 resolve(data);
265 });
266 });
267 } else {
268 this.modem.dial(optsf, function(err, data) {
269 args.callback(err, data);
270 });
271 }
272};
273
274module.exports = Image;