UNPKG

containr

Version:

Docker Build scripts for npm

172 lines (133 loc) 4.51 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildImage = exports.runContainer = exports.pushImage = exports.tagContainer = exports.imageExists = undefined; var _assign = require('babel-runtime/core-js/object/assign'); var _assign2 = _interopRequireDefault(_assign); var _shelljs = require('shelljs'); var _child_process = require('child_process'); var _logger = require('./logger'); var _logger2 = _interopRequireDefault(_logger); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const isImageHash = /[a-f0-9]{12}/igm; const imageExists = exports.imageExists = imageName => { const commandString = `docker images -q ${imageName}`; const eh = (0, _shelljs.exec)(commandString, { silent: true }); const hashValue = eh.stdout.trim(); if (eh.code === 0 && hashValue.match(isImageHash)) { return true; } return false; }; const tagContainer = exports.tagContainer = (options = {}) => { const userOptions = (0, _assign2.default)({ verbose: false }, options); const { to: toTag, from: fromTag, verbose } = userOptions; const commandString = `docker tag ${fromTag} ${toTag}`; _logger2.default.debug(`${commandString}`); const buildExec = (0, _shelljs.exec)(commandString, { silent: true }); if (verbose && buildExec.stdout.length) { _logger2.default.debug(buildExec.stdout); } if (buildExec.code === 0) { // const [, buildId] = buildExec.stdout.match(/Successfully built ([a-f0-9]{12})/im); return { success: true }; } return { success: false, message: buildExec.stderr.trim() }; }; const pushImage = exports.pushImage = (options = {}) => { const userOptions = (0, _assign2.default)({ verbose: false }, options); const { tag = 'latest', verbose } = userOptions; const commandString = `docker push ${tag}`; _logger2.default.debug(`${commandString}`); const buildExec = (0, _shelljs.exec)(commandString, { silent: false }); if (verbose && buildExec.stdout.length) { _logger2.default.debug(buildExec.stdout); } if (buildExec.code === 0) { // const [, buildId] = buildExec.stdout.match(/Successfully built ([a-f0-9]{12})/im); return { success: true }; } return { success: false, message: buildExec.stderr.trim() }; }; const runContainer = exports.runContainer = (options = {}) => { const userOptions = (0, _assign2.default)({ verbose: false }, options); const { tag } = userOptions; const argString = `run --rm -P -it ${tag}`; const buildExec = (0, _child_process.spawn)('docker', argString.split(' '), { stdio: 'inherit' }); buildExec.on('close', code => { _logger2.default.banner(`Exited test process (${code})`); }); }; const buildImage = exports.buildImage = (options = {}) => { const userOptions = (0, _assign2.default)({}, { dockerfile: 'Dockerfile', cmdOptions: ['--force-rm'], name: 'temp-container', version: '', context: '.', verbose: false }, options); const { dockerfile, cmdOptions, version, context, verbose } = userOptions; let { name } = userOptions; _logger2.default.debug(`Building - version is ${version} name is ${name}`); _logger2.default.debug(`options.version ${options.version}`); _logger2.default.debug(`userOptions.version ${userOptions.version}`); _logger2.default.debug(`version - ${version}`); if (version.length) { name = `${name}:${version}`; } _logger2.default.debug(`Building - name is now ${name}`); let optionsStr; if (Array.isArray(cmdOptions) && cmdOptions.length) { optionsStr = userOptions.cmdOptions.join(' '); } else { optionsStr = ''; } const commandString = `docker build -t ${name} ${optionsStr} -f ${dockerfile} ${context}`; _logger2.default.debug(`Building: ${commandString}`); const buildExec = (0, _shelljs.exec)(commandString, { silent: true }); if (buildExec.stdout.length) { _logger2.default.info(buildExec.stdout); } if (buildExec.code === 0) { const [, buildId] = buildExec.stdout.match(/Successfully built ([a-f0-9]{12})/im); return { success: true, hash: buildId, name, version }; } const errMsg = buildExec.stderr.split('\n').filter(x => x.length > 0).join(' '); return { success: false, message: errMsg }; }; exports.default = { imageExists, tagContainer, pushImage, runContainer, buildImage }; //# sourceMappingURL=docker.js.map