UNPKG

@vroomlabs/gsdk-deploy

Version:

Google Cloud deployment script for kubernetes clusters using Global Load Balancer

100 lines (85 loc) 5.14 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.GoogleAsyncTask=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');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 GoogleAsyncTask=exports.GoogleAsyncTask=function(){ /** * @param {GoogleApi} gauth */ function GoogleAsyncTask(gauth){_classCallCheck(this,GoogleAsyncTask); this.gauth=gauth; }_createClass(GoogleAsyncTask,[{key:'completeTask',value:function(){var _ref=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function _callee( operation){var self;return regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:if( operation){_context.next=2;break}return _context.abrupt('return');case 2: _logger.logger.silly('Waiting for operation',{selfLink:operation.selfLink,targetLink:operation.targetLink}); self=this;if(!( operation.kind==='compute#operation')){_context.next=8;break}return _context.abrupt('return', new Promise(function(accept,reject){ self._completeOperation(operation,'compute',function(err,op){ if(err)return reject(err); accept(op); }); }));case 8:if(! operation.selfLink.match(/https:\/\/container.googleapis.com\//)){_context.next=12;break}return _context.abrupt('return', new Promise(function(accept,reject){ self._completeOperation(operation,'container',function(err,op){ if(err)return reject(err); accept(op); }); }));case 12: _logger.logger.silly('malformed operation',operation);throw( new Error('Unknown operation type: '+operation.kind));case 14:case'end':return _context.stop();}}},_callee,this)}));function completeTask(_x){return _ref.apply(this,arguments)}return completeTask}()},{key:'_completeOperation',value:function _completeOperation( op,type,callback){ var self=this; var waitOn=self.gauth.compute.globalOperations; var opQuery={project:this.gauth.project,operation:op.name}; if(type==='container'){ waitOn=self.gauth.container.projects.zones.operations; opQuery={projectId:this.gauth.project,zone:op.zone,operationId:op.name}; } var iteration=0; _logger.logger.debug('begin async wait on global',opQuery); function waitForComplete(cb){ waitOn.get(opQuery,function(err,status){ if(err)return cb(err); if(status.status==='RUNNING'||status.status==='PENDING'){ if(iteration>900){// 15m return cb(new Error('Timeout exceeded waiting for async operation '+op.name+'.')); } if(++iteration%30===0){ _logger.logger.verbose('Waiting for async operation '+op.name); } return setTimeout(function(){return waitForComplete(cb)},1000); } opQuery.status=status.status; _logger.logger.verbose('completed wait on global',opQuery); if(status.status==='DONE'){ cb(); }else { cb(new Error('Operation did not complete successfully.')); } }); } setTimeout(function(){return waitForComplete(callback)},1000); }}]);return GoogleAsyncTask}();