blob: 7196ec6f492d2103726b1ec4e23b2ac7522e5e34 (
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
|
import { Item } from 'ancient-funicular';
import createDebug from 'debug';
import Docker from 'dockerode';
import { statSync } from 'fs';
/**
* @class Container
*/
export default class Container extends Item {
/**
* @constructs Container
* @param {Object=} [options] - Service connection settings
*/
constructor(options) {
super();
/**
* @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 = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
var stats = statSync(socket);
if (!stats.isSocket()) {
throw new Error('Local service is not found.');
}
this._docker = new Docker({
socketPath: socket
});
}
}
/**
* @protected
* @param {Object|String} options - Container parameters
* @description Creates and registers a container in the system.
*/
create(options) {
var self = this;
if (!self._isString(options) && !self._isObject(options)) {
throw new Error('The variable \'options\' is not correct.');
}
if (self._isString(options)) {
var Image = options;
options = { Image };
}
self._pullImage(options.Image, (error) => {
self._handlerError(error);
self._createContainer(options);
});
}
/**
* @protected
* @description Stops and destroys the container.
*/
destroy() {}
/**
* @protected
* @param {String} image - Name of the image
* @param {Function} callback - Called function
* @description Pulls out the image of the container.
*/
_pullImage(image, callback) {
var self = this;
self._docker.pull(image).then(stream => {
self._docker.modem.followProgress(stream, callback, self.debug);
}).catch(error => self._handlerError(error));
}
/**
* @protected
* @param {Object} options - Container settings
* @description Creating a container and registering management.
*/
_createContainer(options) {
var self = this;
self._docker.createContainer(options).then(container => {
/* @todo Class registration */
self.debug(container);
}).catch(error => self._handlerError(error));
}
/**
* @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';
}
/**
* @protected
* @param {*} error - A string with an error
* @description Handles the error if present.
*/
_handlerError(error) {
if (error) {
throw new Error(error);
}
}
/**
* @param {*} Any variables
* @description A simple debugger.
*/
debug() {
var args = Array.prototype.slice.call(arguments);
var debug = createDebug('container');
debug.apply(null, args);
}
}
|