UNPKG

@vroomlabs/gsdk-deploy

Version:

Google Cloud deployment script for kubernetes clusters using Global Load Balancer

140 lines (115 loc) 8.21 kB
'use strict'; /****************************************************************************** * MIT License * Copyright (c) 2017 https://github.com/vroomlabs * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * Created by rogerk on 7/3/17. ******************************************************************************/Object.defineProperty(exports,'__esModule',{value:true});exports.GoogleDockerRepo=undefined;var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if('value'in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}(); var _logger=require('../util/logger'); var _shell=require('../util/shell');var shell=_interopRequireWildcard(_shell); var _templateArg=require('../util/templateArg');function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj}else{var newObj={};if(obj!=null){for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key))newObj[key]=obj[key]}}newObj.default=obj;return newObj}}function _asyncToGenerator(fn){return function(){var gen=fn.apply(this,arguments);return new Promise(function(resolve,reject){function step(key,arg){try{var info=gen[key](arg);var value=info.value}catch(error){reject(error);return}if(info.done){resolve(value)}else{return Promise.resolve(value).then(function(value){step('next',value)},function(err){step('throw',err)})}}return step('next')})}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function')}}var GoogleDockerRepo=exports.GoogleDockerRepo=function(){ /** * @param {GoogleApi} gauth * @param {string=} dockerCmd */ function GoogleDockerRepo(gauth,dockerCmd){_classCallCheck(this,GoogleDockerRepo); this.gauth=gauth; this.docker=dockerCmd||process.env.DOCKER_COMMAND||'docker'; } /** * @param {string} project * @param {string} repo * @param {string=} tag */_createClass(GoogleDockerRepo,[{key:'getUri',value:function getUri( project,repo,tag){ return'us.gcr.io/'+project+'/'+repo+(tag?':'+tag:''); } /** * @param {string} uri * @param {number} limit * @param {string} filter */},{key:'getImages',value:function(){var _ref=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee( uri,limit,filter){var response;return regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_context.next=2;return( shell.exec( this.gauth.gcloud+' --quiet container images list-tags '+uri+' --format=json'+( limit>0?' --limit='+limit:'')+( filter?' --filter=tags:'+filter:'')));case 2:response=_context.sent;return _context.abrupt('return', JSON.parse(response));case 4:case'end':return _context.stop();}}},_callee,this)}));function getImages(_x,_x2,_x3){return _ref.apply(this,arguments)}return getImages}() /** * Returns the most recent image url for the given repo/branch * @param {string} repo * @param {string} branch */},{key:'getLatestImage',value:function(){var _ref2=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee2( repo,branch){var uribase,images,img,imgTag,uri;return regeneratorRuntime.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0: uribase=this.getUri(this.gauth.project,repo);_context2.next=3;return( this.getImages(uribase,1,branch+'-'));case 3:images=_context2.sent;if( images.length>0){_context2.next=6;break}throw( new Error('No images found in: '+uribase));case 6: img=images[0]; imgTag=img.tags&&img.tags.length>0?img.tags[0]:img.digest;if( imgTag){_context2.next=10;break}throw( new Error('Image missing tags: '+JSON.stringify(img)));case 10: uri=this.getUri(this.gauth.project,repo,imgTag); _logger.logger.verbose('Using docker image: '+uri);return _context2.abrupt('return', uri);case 13:case'end':return _context2.stop();}}},_callee2,this)}));function getLatestImage(_x4,_x5){return _ref2.apply(this,arguments)}return getLatestImage}() /** * @param {string} image * @param {string} repo * @param {string} branch * @param {string} tagFormat */},{key:'pushLocalImage',value:function(){var _ref3=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee3( image,repo,branch,tagFormat){var targetImage,latestTag,args_dockerhost,args_tls;return regeneratorRuntime.wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0: tagFormat=(0,_templateArg.replaceInText)(tagFormat); targetImage=this.getUri(this.gauth.project,repo,tagFormat); latestTag=this.getUri(this.gauth.project,repo,branch+'-latest'); _logger.logger.verbose('pushing docker image',{source:image,target:targetImage,latest:latestTag}); _logger.logger.info('Tagging docker image '+image+' as '+targetImage);_context3.next=7;return( shell.exec(this.docker+' tag '+image+' '+targetImage));case 7: /* * --docker-host=$DOCKER_HOST * -- * --tlsverify --tlscacert $DOCKER_CERT_PATH/ca.pem * --tlscert $DOCKER_CERT_PATH/cert.pem * --tlskey $DOCKER_CERT_PATH/key.pem */ args_dockerhost=''; if(process.env.DOCKER_HOST){ _logger.logger.verbose('DOCKER_HOST is set'); args_dockerhost=' --docker-host='+process.env.DOCKER_HOST; } args_tls=''; if(process.env.DOCKER_CERT_PATH){ _logger.logger.verbose('DOCKER_CERT_PATH is set'); args_tls=args_tls+' --tlsverify'; args_tls=args_tls+' --tlscacert '+process.env.DOCKER_CERT_PATH+'/ca.pem'; args_tls=args_tls+' --tlscert '+process.env.DOCKER_CERT_PATH+'/cert.pem'; args_tls=args_tls+' --tlskey '+process.env.DOCKER_CERT_PATH+'/key.pem'; } _logger.logger.info('Pushing docker image: '+targetImage);_context3.next=14;return( shell.exec(this.gauth.gcloud+' docker '+args_dockerhost+' -- '+args_tls+' push '+targetImage,{direct:true}));case 14: _logger.logger.info('Applying image label: '+latestTag);_context3.next=17;return( shell.exec(this.gauth.gcloud+' --quiet container images add-tag '+targetImage+' '+latestTag,{direct:true}));case 17:return _context3.abrupt('return', targetImage);case 18:case'end':return _context3.stop();}}},_callee3,this)}));function pushLocalImage(_x6,_x7,_x8,_x9){return _ref3.apply(this,arguments)}return pushLocalImage}() /** * @param {string} image */},{key:'isLocalImage',value:function(){var _ref4=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee4( image){var text;return regeneratorRuntime.wrap(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.next=2;return( shell.exec(this.docker+' images -q '+image));case 2:text=_context4.sent;return _context4.abrupt('return', !!text.trim());case 4:case'end':return _context4.stop();}}},_callee4,this)}));function isLocalImage(_x10){return _ref4.apply(this,arguments)}return isLocalImage}()}]);return GoogleDockerRepo}();