-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
271 lines (244 loc) · 9.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'use strict';
var draap = require('docker-remote-api-as-promised');
var Promise = require('native-or-bluebird');
var DockerObj = require('./lib/docker-object');
var dockerRegistry = 'https://index.docker.io/v1';
/**
* Create a new instance of Nodöcker
* @class
* @param {string} host the docker host or socket
* @param {object} auth an AuthConfig object
* @param {string} [auth.username] username to use for authed queries
* @param {string} [auth.passwod] password to use for authed queries
* @param {string} [auth.serveraddress=https://index.docker.io/v1] the registry
* @param {string} auth.email your email address
*/
function Nodoecker(host, auth){
auth = auth || {};
this.host = host;
this.auth = auth;
this.dkr = draap(host);
this.authStr = this._makeAuth(auth);
}
/**
* Create a base64 auth string from an AuthConfig object
* @memberOf Nodoecker
* @method _makeAuth
* @private
* @param {object} auth AuthConfig object
* @returns {string} base64 encoded JSON.stringify'ed representation of AuthConfig
*/
Nodoecker.prototype._makeAuth = function(auth){
var a = {
username: auth.username || '',
password: auth.password || '',
serveraddress: auth.serveraddress || dockerRegistry,
email: auth.email
};
return new Buffer(JSON.stringify(a)).toString('base64');
};
/**
* A convience method that takes a containerName, an image and optional details
* about the container to create and returns a running container
* @method run
* @param {string} containerName what to call your new container
* @param {string|object} image either a string identifying the image, or an instance of the image object
* @param {object} details [Container creation details](https://docs.docker.com/reference/api/docker_remote_api_v1.17/#create-a-container)
* @returns {Promise} resolves to a container object representing the running container
*/
Nodoecker.prototype.run = function(containerName, image, details) {
if (typeof image === 'string') {
image = this.image(image, {delay: true});
}
return image.run(containerName, details);
};
/**
* Stop a running container
* @method stop
* @param {string|object} container Either a string identifying the container or a container instance
* @param {number} time The amount of time you want to delay the action
* @returns {Promise} resolves to `true` on success
*/
Nodoecker.prototype.stop = function(container, time) {
return this._runState('stop', container, time);
};
/**
* Restart a running container
* @method restart
* @param {string|object} container Either a string identifying the container or a container instance
* @param {number} time The amount of time you want to delay the action
* @returns {Promise} resolves to `true` on success
*/
Nodoecker.prototype.restart = function(container, time) {
return this._runState('restart', container, time);
};
/**
* Starts a running container
* @method start
* @param {string|object} container Either a string identifying the container or a container instance
* @param {number} time The amount of time you want to delay the action
* @returns {Promise} resolves to `true` on success
*/
Nodoecker.prototype.start = function(container, time) {
return this._runState('start', container, time);
};
/**
* Actually perform the run state action on the container
* @method _runState
* @private
* @memberOf Nodoecker
* @param {string} method start|stop|restart
* @param {string|object} container Either a string identifying the container or a container instance
* @param {number} time The amount of time you want to delay the action
* @returns {Promise} resolves to `true` on success
*/
Nodoecker.prototype._runState = function(method, container, time) {
if (typeof container === 'string') {
container = this.container(container, {delay: true});
}
if (!(container instanceof DockerObj)) {
throw new Error('Container should either be a string or a container instance');
}
return container[method].call(container, time);
};
/**
* Create a new image object and pull it from the remote docker registry
* @method pull
* @param {string} image identifying text for image
* @param {object} opts options for pulling the image
* @returns {Promise} resolves to a new Image object
*/
Nodoecker.prototype.pull = function(image, opts) {
if (typeof image === 'string') {
opts.delay = true;
image = this.image(image, opts);
}
if (!(image instanceof DockerObj)) {
throw new Error('image must either be a string or an image instance');
}
image.pull(opts);
};
/**
* List all images in docker instance
* @method _list
* @memberOf Nodoedocker
* @private
* @promise
* @param {object} params parameters for sorting/filtering
* @param {boolean} [params.all=false] true = show all images, false = show only running
* @param {string} [params.limit] show only x number of last created images, including non-running
* @param {string} [params.since] show only images created since container id
* @param {string} [params.before] show only images created before container id
* @param {boolean} [params.size=false] show the container sizes
* @param {object} [params.filters] a set of filters to apply to the response
* @param {integer} [params.filters.exited] list images with the specified exit code
* @param {string} [params.filters.status] list images with status: restarting, running, paused, exiting
* @returns {container[]} an array of container objects when the promise fulfills
*/
Nodoecker.prototype._list = function(type, params) {
var listUrl = '/' + type + 's/json';
params = params || {};
if (params.filters) {
params.filters = JSON.stringify(params.filters);
}
var opts = {
qs: params,
json: true
};
return this.dkr
.get(listUrl, opts)
.bind(this)
.then(function(items) {
var imgOpts = {
host: this.host,
authStr: this.authStr,
delay: true
};
return items.map(function(item) {
return this[type](item, imgOpts);
});
})
.catch(function(err) {
throw err;
});
};
/**
* Returns an instance of an Image.
* @promise
* @memberOf Nodoedocker
* @param {string} name Name or ID of image
* @param {object} opts options
* @param {boolean} [opts.delay] Don't call `img.Inspect()`
* @return {object} Promise -> new Image
*/
Nodoecker.prototype.image = function(name, opts) {
opts = opts || {};
opts.host = this.host;
opts.authStr = this.authStr;
var img = new DockerObj(name, 'image', opts);
if (opts.delay) {
return Promise.resolve(img);
} else {
return img.Inspect();
}
};
/**
* Returns an instance of a container
* @method container
* @promise
* @memberOf Nodoedocker
* @param {string} name name of the container you want to create
* @param {object} opts options for creating
* @param {boolean} [opts.delay] dont' call `container.Inspect()`
* @returns {object} Promise -> new container
*/
Nodoecker.prototype.container = function(name, opts) {
opts = opts || {};
opts.host = this.host;
opts.authStr = this.authStr;
var container = new DockerObj(name, 'container', opts);
if (opts.delay) {
return Promise.resolve(container);
} else {
return container.Inspect();
}
};
/**
* Return a list of all the running containers on a docker dameon
* @method ps
* @promise
* @memberOf Nodoedocker
* @param {object} params parameters for sorting/filtering
* @param {boolean} [params.all=false] true = show all images, false = show only running
* @param {string} [params.limit] show only x number of last created images, including non-running
* @param {string} [params.since] show only images created since container id
* @param {string} [params.before] show only images created before container id
* @param {boolean} [params.size=false] show the container sizes
* @param {object} [params.filters] a set of filters to apply to the response
* @param {integer} [params.filters.exited] list images with the specified exit code
* @param {string} [params.filters.status] list images with status: restarting, running, paused, exiting
* @returns {container[]} an array of container objects when the promise fulfills
*/
Nodoecker.prototype.ps = function(params) {
return this._list('container', params);
};
/**
* Return a list of all the running containers on a docker dameon
* @method images
* @promise
* @memberOf Nodoedocker
* @param {object} params parameters for sorting/filtering
* @param {boolean} [params.all=false] true = show all images, false = show only running
* @param {string} [params.limit] show only x number of last created images, including non-running
* @param {string} [params.since] show only images created since container id
* @param {string} [params.before] show only images created before container id
* @param {boolean} [params.size=false] show the container sizes
* @param {object} [params.filters] a set of filters to apply to the response
* @param {integer} [params.filters.exited] list images with the specified exit code
* @param {string} [params.filters.status] list images with status: restarting, running, paused, exiting
* @returns {container[]} an array of container objects when the promise fulfills
*/
Nodoecker.prototype.images = function(params) {
return this._list('image', params);
};
module.exports = Nodoecker;