aboutsummaryrefslogtreecommitdiff
path: root/src/lib/index.js
blob: 7ef814bd7c42ee019ad3be2df76593f4f81d5468 (plain) (blame)
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
import { normalize } from 'path';
import Docker from 'dockerode';
import { statSync } from 'fs';

/**
 * @class Container
 */
export default class Containers {
    /**
     * @constructs Container
     * @param {Object=} [options] - Service connection settings
     */
    constructor(options) {
        /**
         * @protected
         * @type {Object}
         * @description Docker class service management.
         * https://github.com/apocas/dockerode
         */
        this._docker = null;

        if (this._isObject(options)) {
            this._docker = new Docker(options);
        }
        else {
            var socket = this._getSocket();
            var stats = statSync(socket);

            if (!stats.isSocket()) {
                throw new Error('');
            }

            this._docker = new Docker({
                socketPath: socket
            });
        }
    }

    /**
     * @protected
     * @param {String|Object} options - The name of the image or options
     * @returns {Promise} Returns the container object
     * @description Creates a container by options or name, returning the management interface.
     */
    create(options) {
        var self = this;

        if (!self._isString(options) && !self._isObject(options)) {
            throw new Error('');
        }

        if (self._isString(options)) {
            var Image = options;
            options = { Image };
        }

        return new Promise((resolve, reject) => {
            self._getImage(options.Image, (error) => {
                if (error) {
                    reject(error);
                }

                self._createContainer(options, (error, container) => {
                    if (error) {
                        reject(error);
                    }

                    resolve(container);
                });
            });
        });
    }

    /**
     * @protected
     * @param {String=} [id] - Container ID
     * @returns {Promise} Returns the container or container object
     * @description Returns the container management interface.
     */
    get(id) {
        var self = this;

        if (id && !self._isString(id)) {
            throw new Error('');
        }

        return new Promise((resolve, reject) => {
            self._getContainers((error, list) => {
                if (error) {
                    reject(error);
                }

                if (self._isString(id)) {
                    self._findContainer(list, id, (result) => {
                        resolve(self._getContainer(result.Id));
                    });
                }
                else {
                    self._createListContainers(list, (result) => {
                        resolve(result);
                    });
                }
            });
        });
    }

    /**
     * @protected
     * @param {String=} [id] - Container ID
     * @returns {Promise} Returns the container or container object
     * @description Searches in existing containers.
     */
    info(id) {
        var self = this;

        if (id && !self._isString(id)) {
            throw new Error('');
        }

        return new Promise((resolve, reject) => {
            self._getContainers((error, list) => {
                if (error) {
                    reject(error);
                }

                if (self._isString(id)) {
                    self._findContainer(list, id, (result) => {
                        resolve(result);
                    });
                }
                else {
                    self._createInformationList(list, (result) => {
                        resolve(result);
                    });
                }
            });
        });
    }

    /**
     * @protected
     * @param {Object} options - Settings created by the container
     * @param {Function} callback - Callback after creation
     * @description Creates a container and returns its interface.
     */
    _createContainer(options, callback) {
        this._docker.createContainer(options, (error, container) => {
            callback(error, container);
        });
    }

    /**
     * @protected
     * @param {Array} list - List of containers
     * @param {Function} callback - Callback after building the list
     * @description Creates a list of containers for the user.
     */
    _createInformationList(list, callback) {
        callback(list.reduce((result, item) => {
            result[item.Id] = item;
            return result;
        }, {}));
    }

    /**
     * @protected
     * @param {Array} list - List of containers
     * @param {Function} callback - Callback after building the list
     * @description Creates a list of container interfaces.
     */
    _createListContainers(list, callback) {
        var self = this;

        callback(list.reduce((result, item) => {
            result[item.Id] = self._getContainer(item.Id);
            return result;
        }, {}));
    }

    /**
     * @protected
     * @returns {String} Socket
     * @description Getting the system socket.
     */
    _getSocket() {
        return process.env.DOCKER_SOCKET || normalize('/var/run/docker.sock');
    }

    /**
     * @protected
     * @param {Function} callback - Callback after receiving the list
     * @description Getting a list of all available containers in the system.
     */
    _getContainers(callback) {
        this._docker.listContainers({ all: true }, (error, list) => {
            callback(error, list);
        });
    }

    /**
     * @protected
     * @param {String} id - Container ID
     * @returns {Object} Container interface
     * @description Getting the interface of the container.
     */
    _getContainer(id) {
        return this._docker.getContainer(id);
    }

    /**
     * @protected
     * @param {String} image - The name of the image
     * @param {Function} callback - Callback after download
     * @description Load the image needed for the container.
     */
    _getImage(image, callback) {
        var self = this;

        self._docker.pull(image, (error, stream) => {
            if (error) {
                callback(error);
            }

            self._docker.modem.followProgress(stream, callback);
        });
    }

    /**
     * @protected
     * @param {Array} list - List of containers
     * @param {String} id - Container ID
     * @param {Function} callback - Callback with the search result
     * @description Search for a specific container from the general list of containers.
     */
    _findContainer(list, id, callback) {
        list.find(item => {
            if (item.Id == id) {
                callback(item);
            }
        });
    }

    /**
     * @protected
     * @param {*} value - The variable to check
     * @returns {Boolean} Result of checking
     * @description Checks the type of the variable.
     */
    _isString(value) {
        return typeof value === 'string';
    }

    /**
     * @protected
     * @param {*} value - The variable to check
     * @returns {Boolean} Result of checking
     * @description Checks the type of the variable.
     */
    _isObject(value) {
        return typeof value === 'object';
    }
}