UNPKG

3.59 kBJavaScriptView Raw
1var util = require('./util');
2
3/**
4 * Represents an Service
5 * @param {Object} modem docker-modem
6 * @param {String} id Service's ID
7 */
8var Service = function(modem, id) {
9 this.modem = modem;
10 this.id = id;
11};
12
13Service.prototype[require('util').inspect.custom] = function() { return this; };
14
15/**
16 * Query Docker for service details.
17 *
18 * @param {function} callback
19 */
20Service.prototype.inspect = function(callback) {
21 var self = this;
22
23 var optsf = {
24 path: '/services/' + this.id,
25 method: 'GET',
26 statusCodes: {
27 200: true,
28 404: 'no such service',
29 500: 'server error'
30 }
31 };
32
33 if(callback === undefined) {
34 return new this.modem.Promise(function(resolve, reject) {
35 self.modem.dial(optsf, function(err, data) {
36 if (err) {
37 return reject(err);
38 }
39 resolve(data);
40 });
41 });
42 } else {
43 this.modem.dial(optsf, function(err, data) {
44 callback(err, data);
45 });
46 }
47};
48
49/**
50 * Delete Service
51 *
52 * @param {function} callback
53 */
54Service.prototype.remove = function(callback) {
55 var self = this;
56
57 var optsf = {
58 path: '/services/' + this.id,
59 method: 'DELETE',
60 statusCodes: {
61 200: true,
62 204: true,
63 404: 'no such service',
64 500: 'server error'
65 }
66 };
67
68 if(callback === undefined) {
69 return new this.modem.Promise(function(resolve, reject) {
70 self.modem.dial(optsf, function(err, data) {
71 if (err) {
72 return reject(err);
73 }
74 resolve(data);
75 });
76 });
77 } else {
78 this.modem.dial(optsf, function(err, data) {
79 callback(err, data);
80 });
81 }
82};
83
84/**
85 * Update service
86 *
87 * @param {object} auth
88 * @param {object} opts
89 * @param {function} callback
90 */
91Service.prototype.update = function(auth, opts, callback) {
92 var self = this;
93 if (!callback) {
94 var t = typeof opts;
95 if(t === 'function'){
96 callback = opts;
97 opts = auth;
98 auth = opts.authconfig || undefined;
99 } else if (t === 'undefined'){
100 opts = auth;
101 auth = opts.authconfig || undefined;
102 }
103 }
104
105 var optsf = {
106 path: '/services/' + this.id + '/update?',
107 method: 'POST',
108 statusCodes: {
109 200: true,
110 404: 'no such service',
111 500: 'server error'
112 },
113 authconfig: auth,
114 options: opts
115 };
116
117 if(callback === undefined) {
118 return new this.modem.Promise(function(resolve, reject) {
119 self.modem.dial(optsf, function(err, data) {
120 if (err) {
121 return reject(err);
122 }
123 resolve(data);
124 });
125 });
126 } else {
127 this.modem.dial(optsf, function(err, data) {
128 callback(err, data);
129 });
130 }
131};
132
133
134
135/**
136 * Service logs
137 * @param {Object} opts Logs options. (optional)
138 * @param {Function} callback Callback with data
139 */
140Service.prototype.logs = function(opts, callback) {
141 var self = this;
142 var args = util.processArgs(opts, callback, {});
143
144 var optsf = {
145 path: '/services/' + this.id + '/logs?',
146 method: 'GET',
147 isStream: args.opts.follow || false,
148 statusCodes: {
149 200: true,
150 404: 'no such service',
151 500: 'server error',
152 503: 'node is not part of a swarm'
153 },
154 options: args.opts
155 };
156
157 if(args.callback === undefined) {
158 return new this.modem.Promise(function(resolve, reject) {
159 self.modem.dial(optsf, function(err, data) {
160 if (err) {
161 return reject(err);
162 }
163 resolve(data);
164 });
165 });
166 } else {
167 this.modem.dial(optsf, function(err, data) {
168 args.callback(err, data);
169 });
170 }
171};
172
173
174
175module.exports = Service;