UNPKG

@vroomlabs/gsdk-deploy

Version:

Google Cloud deployment script for kubernetes clusters using Global Load Balancer

96 lines (79 loc) 6.68 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.GoogleSvcManager=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 _fs=require('fs');var fs=_interopRequireWildcard(_fs); var _yamljs=require('yamljs');var YAML=_interopRequireWildcard(_yamljs); var _logger=require('../util/logger'); var _shell=require('../util/shell');var shell=_interopRequireWildcard(_shell);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 GoogleSvcManager=exports.GoogleSvcManager=function(){ /** * @param {GoogleApi} gauth */ function GoogleSvcManager(gauth){_classCallCheck(this,GoogleSvcManager); this.gauth=gauth; } /** * @param {string[]} fileNames */_createClass(GoogleSvcManager,[{key:'deployEndpoint',value:function(){var _ref=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee( fileNames){var endpoint,loadedYaml,loadedJson,json,api;return regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0: endpoint={hasApi:false,name:undefined,version:undefined,grpc:undefined};if(!( fileNames.length>0)){_context.next=14;break} endpoint.hasApi=true; _logger.logger.info('Updating endpoint...'); loadedYaml=fileNames.filter(function(p){return p.match(/.ya?ml$/)}).map(function(p){return YAML.load(p)}); loadedJson=fileNames.filter(function(p){return p.match(/.json$/)}).map(function(p){return JSON.parse(fs.readFileSync(p))}); endpoint.grpc=loadedYaml.concat(loadedJson).filter(function(p){return p.type==='google.api.Service'&&p.name}).length>0;_context.next=9;return( shell.exec(this.gauth.gcloud+' endpoints services deploy '+fileNames.join(' ')+' '+('--project '+ this.gauth.project+' --format=json'),{direct:'debug',stdout:function stdout(){}}));case 9:json=_context.sent; api=JSON.parse(json); process.env.ENDPOINT_NAME=endpoint.name=api.serviceConfig.name; process.env.ENDPOINT_VERSION=endpoint.version=api.serviceConfig.id; _logger.logger.info('Endpoint version '+endpoint.version+' created for '+endpoint.name);case 14:return _context.abrupt('return', endpoint);case 15:case'end':return _context.stop();}}},_callee,this)}));function deployEndpoint(_x){return _ref.apply(this,arguments)}return deployEndpoint}() /** * Returns the latest endpoint descriptor {name: string, version: string} * @param {string[]} fileNames */},{key:'getLatestEndpoint',value:function(){var _ref2=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee2( fileNames){var loadedYaml,loadedJson,endpoint,isGrpc,json,api;return regeneratorRuntime.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:if(!( fileNames.length===0)){_context2.next=2;break}return _context2.abrupt('return', {hasApi:false});case 2: loadedYaml=fileNames.filter(function(p){return p.match(/.ya?ml$/)}).map(function(p){return YAML.load(p)}); loadedJson=fileNames.filter(function(p){return p.match(/.json$/)}).map(function(p){return JSON.parse(fs.readFileSync(p))}); endpoint=loadedYaml.concat(loadedJson). filter(function(p){return p.type==='google.api.Service'&&p.name||p.swagger&&p.swagger.toString().match(/^\d/)}). filter(function(p){return p.name&&p.name.length>0||p.host&&p.host.length>0})[0];if( endpoint){_context2.next=7;break}throw( new Error('Unable to locate name in any yaml api config.'));case 7: isGrpc=endpoint.type==='google.api.Service'; endpoint={name:endpoint.name||endpoint.host};_context2.next=11;return( shell.exec(this.gauth.gcloud+' endpoints services describe '+endpoint.name+' '+('--project '+ this.gauth.project+' --format=json')));case 11:json=_context2.sent; api=JSON.parse(json); _logger.logger.verbose('Using endpoint version '+api.serviceConfig.id+' of '+api.serviceConfig.name); process.env.ENDPOINT_NAME=api.serviceConfig.name; process.env.ENDPOINT_VERSION=api.serviceConfig.id;return _context2.abrupt('return', { hasApi:true, name:api.serviceConfig.name, version:api.serviceConfig.id, grpc:isGrpc});case 17:case'end':return _context2.stop();}}},_callee2,this)}));function getLatestEndpoint(_x2){return _ref2.apply(this,arguments)}return getLatestEndpoint}()}]);return GoogleSvcManager}();