aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2017-10-06 22:51:11 +0300
committerValentin Popov <info@valentineus.link>2017-10-06 22:51:11 +0300
commitb431dbe252bcc573f4037dae4ba979d4ff2065b8 (patch)
treebd8985e245ccad3d04d68520fe063cbbc02915d4 /src
parent2c2d12f5f62384421aea1ad5bcdf75c9574cc5e0 (diff)
downloadsimple-container-b431dbe252bcc573f4037dae4ba979d4ff2065b8.tar.xz
simple-container-b431dbe252bcc573f4037dae4ba979d4ff2065b8.zip
The first working beta version
Diffstat (limited to 'src')
-rw-r--r--src/lib/index.js215
1 files changed, 175 insertions, 40 deletions
diff --git a/src/lib/index.js b/src/lib/index.js
index 7196ec6..9e86cc7 100644
--- a/src/lib/index.js
+++ b/src/lib/index.js
@@ -1,4 +1,3 @@
-import { Item } from 'ancient-funicular';
import createDebug from 'debug';
import Docker from 'dockerode';
import { statSync } from 'fs';
@@ -6,14 +5,12 @@ import { statSync } from 'fs';
/**
* @class Container
*/
-export default class Container extends Item {
+export default class Containers {
/**
* @constructs Container
* @param {Object=} [options] - Service connection settings
*/
constructor(options) {
- super();
-
/**
* @protected
* @type {Object}
@@ -26,11 +23,11 @@ export default class Container extends Item {
this._docker = new Docker(options);
}
else {
- var socket = process.env.DOCKER_SOCKET || '/var/run/docker.sock';
+ var socket = this._getSocket();
var stats = statSync(socket);
if (!stats.isSocket()) {
- throw new Error('Local service is not found.');
+ throw new Error('');
}
this._docker = new Docker({
@@ -41,14 +38,15 @@ export default class Container extends Item {
/**
* @protected
- * @param {Object|String} options - Container parameters
- * @description Creates and registers a container in the system.
+ * @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('The variable \'options\' is not correct.');
+ throw new Error('');
}
if (self._isString(options)) {
@@ -56,42 +54,190 @@ export default class Container extends Item {
options = { Image };
}
- self._pullImage(options.Image, (error) => {
- self._handlerError(error);
- self._createContainer(options);
+ 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
- * @description Stops and destroys the container.
+ * @param {String=} [id] - Container ID
+ * @returns {Promise} Returns the container or container object
+ * @description Returns the container management interface.
*/
- destroy() {}
+ 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} image - Name of the image
- * @param {Function} callback - Called function
- * @description Pulls out the image of the container.
+ * @param {String=} [id] - Container ID
+ * @returns {Promise} Returns the container or container object
+ * @description Searches in existing containers.
*/
- _pullImage(image, callback) {
+ info(id) {
var self = this;
- self._docker.pull(image).then(stream => {
- self._docker.modem.followProgress(stream, callback, self.debug);
- }).catch(error => self._handlerError(error));
+
+ 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 {Object} options - Container settings
- * @description Creating a container and registering management.
+ * @param {Array} list - List of containers
+ * @param {Function} callback - Callback after building the list
+ * @description Creates a list of container interfaces.
*/
- _createContainer(options) {
+ _createListContainers(list, callback) {
var self = this;
- self._docker.createContainer(options).then(container => {
- /* @todo Class registration */
- self.debug(container);
- }).catch(error => self._handlerError(error));
+
+ 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 || '/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, self.debug);
+ });
+ }
+
+ /**
+ * @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);
+ }
+ });
}
/**
@@ -115,23 +261,12 @@ export default class Container extends Item {
}
/**
- * @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');
+ var debug = createDebug('containers');
debug.apply(null, args);
}
} \ No newline at end of file