UNPKG

7.89 kBJavaScriptView Raw
1var util = require('./util');
2
3/**
4 * Represents a plugin
5 * @param {Object} modem docker-modem
6 * @param {String} name Plugin's name
7 */
8var Plugin = function(modem, name, remote) {
9 this.modem = modem;
10 this.name = name;
11 this.remote = remote || name;
12};
13
14Plugin.prototype[require('util').inspect.custom] = function() { return this; };
15
16/**
17 * Inspect
18 * @param {Function} callback Callback, if specified Docker will be queried.
19 * @return {Object} Name only if callback isn't specified.
20 */
21Plugin.prototype.inspect = function(callback) {
22 var self = this;
23
24 var optsf = {
25 path: '/plugins/' + this.name,
26 method: 'GET',
27 statusCodes: {
28 200: true,
29 404: 'plugin is not installed',
30 500: 'server error'
31 }
32 };
33
34 if(callback === undefined) {
35 return new this.modem.Promise(function(resolve, reject) {
36 self.modem.dial(optsf, function(err, data) {
37 if (err) {
38 return reject(err);
39 }
40 resolve(data);
41 });
42 });
43 } else {
44 this.modem.dial(optsf, function(err, data) {
45 callback(err, data);
46 });
47 }
48};
49
50/**
51 * Removes the plugin
52 * @param {[Object]} opts Remove options (optional)
53 * @param {Function} callback Callback
54 */
55Plugin.prototype.remove = function(opts, callback) {
56 var self = this;
57 var args = util.processArgs(opts, callback);
58
59 var optsf = {
60 path: '/plugins/' + this.name + '?',
61 method: 'DELETE',
62 statusCodes: {
63 200: true,
64 404: 'plugin is not installed',
65 500: 'server error'
66 },
67 options: args.opts
68 };
69
70 if(args.callback === undefined) {
71 return new this.modem.Promise(function(resolve, reject) {
72 self.modem.dial(optsf, function(err, data) {
73 if (err) {
74 return reject(err);
75 }
76 resolve(data);
77 });
78 });
79 } else {
80 this.modem.dial(optsf, function(err, data) {
81 if (err) return args.callback(err, data);
82 args.callback(err, data);
83 });
84 }
85};
86
87/**
88 * get privileges
89 * @param {Function} callback Callback
90 * @return {Object} Name only if callback isn't specified.
91 */
92Plugin.prototype.privileges = function(callback) {
93 var self = this;
94 var optsf = {
95 path: '/plugins/privileges?',
96 method: 'GET',
97 options: {
98 'remote': this.remote
99 },
100 statusCodes: {
101 200: true,
102 500: 'server error'
103 }
104 };
105
106 if(callback === undefined) {
107 return new this.modem.Promise(function(resolve, reject) {
108 self.modem.dial(optsf, function(err, data) {
109 if (err) {
110 return reject(err);
111 }
112 resolve(data);
113 });
114 });
115 } else {
116 this.modem.dial(optsf, function(err, data) {
117 callback(err, data);
118 });
119 }
120};
121
122
123/**
124 * Installs a new plugin
125 * @param {Object} opts Create options
126 * @param {Function} callback Callback
127 */
128Plugin.prototype.pull = function(opts, callback) {
129 var self = this;
130 var args = util.processArgs(opts, callback);
131
132 if(args.opts._query && !args.opts._query.name) {
133 args.opts._query.name = this.name;
134 }
135 if(args.opts._query && !args.opts._query.remote) {
136 args.opts._query.remote = this.remote;
137 }
138
139 var optsf = {
140 path: '/plugins/pull?',
141 method: 'POST',
142 isStream: true,
143 options: args.opts,
144 statusCodes: {
145 200: true, // unofficial, but proxies may return it
146 204: true,
147 500: 'server error'
148 }
149 };
150
151 if(args.callback === undefined) {
152 return new this.modem.Promise(function(resolve, reject) {
153 self.modem.dial(optsf, function(err, data) {
154 if (err) {
155 return reject(err);
156 }
157 resolve(data);
158 });
159 });
160 } else {
161 this.modem.dial(optsf, function(err, data) {
162 args.callback(err, data);
163 });
164 }
165};
166
167
168/**
169 * Enable
170 * @param {Object} opts Plugin enable options (optional)
171 * @param {Function} callback Callback
172 */
173Plugin.prototype.enable = function(opts, callback) {
174 var self = this;
175 var args = util.processArgs(opts, callback);
176
177 var optsf = {
178 path: '/plugins/' + this.name + '/enable?',
179 method: 'POST',
180 statusCodes: {
181 200: true,
182 500: 'server error'
183 },
184 options: args.opts
185 };
186
187 if(args.callback === undefined) {
188 return new this.modem.Promise(function(resolve, reject) {
189 self.modem.dial(optsf, function(err, data) {
190 if (err) {
191 return reject(err);
192 }
193 resolve(data);
194 });
195 });
196 } else {
197 this.modem.dial(optsf, function(err, data) {
198 args.callback(err, data);
199 });
200 }
201};
202
203/**
204 * Disable
205 * @param {Object} opts Plugin disable options (optional)
206 * @param {Function} callback Callback
207 */
208Plugin.prototype.disable = function(opts, callback) {
209 var self = this;
210 var args = util.processArgs(opts, callback);
211
212 var optsf = {
213 path: '/plugins/' + this.name + '/disable',
214 method: 'POST',
215 statusCodes: {
216 200: true,
217 500: 'server error'
218 },
219 options: args.opts
220 };
221
222 if(args.callback === undefined) {
223 return new this.modem.Promise(function(resolve, reject) {
224 self.modem.dial(optsf, function(err, data) {
225 if (err) {
226 return reject(err);
227 }
228 resolve(data);
229 });
230 });
231 } else {
232 this.modem.dial(optsf, function(err, data) {
233 args.callback(err, data);
234 });
235 }
236};
237
238/**
239 * Push
240 * @param {Object} opts Plugin push options (optional)
241 * @param {Function} callback Callback
242 */
243Plugin.prototype.push = function(opts, callback) {
244 var self = this;
245 var args = util.processArgs(opts, callback);
246
247 var optsf = {
248 path: '/plugins/' + this.name + '/push',
249 method: 'POST',
250 statusCodes: {
251 200: true,
252 404: 'plugin not installed',
253 500: 'server error'
254 },
255 options: args.opts
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
274/**
275 * COnfigure
276 * @param {Object} opts Plugin configure options (optional)
277 * @param {Function} callback Callback
278 */
279Plugin.prototype.configure = function(opts, callback) {
280 var self = this;
281 var args = util.processArgs(opts, callback);
282
283 var optsf = {
284 path: '/plugins/' + this.name + '/set',
285 method: 'POST',
286 statusCodes: {
287 200: true,
288 404: 'plugin not installed',
289 500: 'server error'
290 },
291 options: args.opts
292 };
293
294 if(args.callback === undefined) {
295 return new this.modem.Promise(function(resolve, reject) {
296 self.modem.dial(optsf, function(err, data) {
297 if (err) {
298 return reject(err);
299 }
300 resolve(data);
301 });
302 });
303 } else {
304 this.modem.dial(optsf, function(err, data) {
305 args.callback(err, data);
306 });
307 }
308};
309
310
311/**
312 * Upgrade plugin
313 *
314 * @param {object} auth
315 * @param {object} opts
316 * @param {function} callback
317 */
318Plugin.prototype.upgrade = function(auth, opts, callback) {
319 var self = this;
320 if (!callback && typeof opts === 'function') {
321 callback = opts;
322 opts = auth;
323 auth = opts.authconfig || undefined;
324 }
325
326 var optsf = {
327 path: '/plugins/' + this.name + '/upgrade?',
328 method: 'POST',
329 statusCodes: {
330 200: true,
331 204: true,
332 404: 'plugin not installed',
333 500: 'server error'
334 },
335 authconfig: auth,
336 options: opts
337 };
338
339 if(callback === undefined) {
340 return new this.modem.Promise(function(resolve, reject) {
341 self.modem.dial(optsf, function(err, data) {
342 if (err) {
343 return reject(err);
344 }
345 resolve(data);
346 });
347 });
348 } else {
349 this.modem.dial(optsf, function(err, data) {
350 callback(err, data);
351 });
352 }
353};
354
355
356module.exports = Plugin;