UNPKG

ring-websites-toolbelt

Version:

Ring Publishing Platform tool to work with Ring Websites

104 lines (88 loc) 3.92 kB
const fs = require('fs'); const fse = require('fs-extra'); const path = require('path'); const Jasmine = require('jasmine'); const retryPromise = require(`./../../utils/retryPromise`); const RouterScriptAbstract = require(`../../RouterScriptAbstract`); class Router extends RouterScriptAbstract { constructor(options) { super(options); this.compressedThemeFileName = 'compressedRouter.zip'; } async execute() { await this.validateRouterJson(); await this.selectApiConfig(); await this.setUpProviders(); await this.deployRouter(); } async deployRouter() { console.info('Deploying router to PROD'); try { await this.files.compressDir(this.paths.router, 'router', this.compressedThemeFileName, ['.git', 'node_modules']); const ocdnUrl = await retryPromise(this.uploadCompressedRouterToOcdn.bind(this), 3); console.info('Compressed router uploaded to ocdn:', ocdnUrl); await fse.remove(this.compressedThemeFileName); const deploymentId = await retryPromise(this.triggerDeployment.bind(this, ocdnUrl), 3); await this.checkIfRouterDeployed(deploymentId); console.info(`Router has been deployed: ${this.routerJson.name}@${this.routerJson.version}`); } catch(err) { if (this.options.verbose) { console.warn('Error while deploying router:', (err.message || err)); } throw err; }; } async uploadCompressedRouterToOcdn() { console.info('Uploading compressed router to ocdn'); try { return (await this.s3.uploadFinalRouter(this.compressedThemeFileName, this.routerJson.name, this.routerJson.version)).Location; } catch (error) { if (this.options.verbose) { console.warn('Error while uploading router to ocdn:', (error.message || error)); } throw error; } } async triggerDeployment(ocdnUrl, version) { console.info('Deploying...'); try { return await this.api.deployRouter(this.routerJson.name, this.routerJson.version, ocdnUrl); } catch (error) { if (this.options.verbose) { console.error('Error while getting deployment ID:', (error.message || error)); } throw error; } } async checkIfRouterDeployed(deploymentId) { console.info('Checking if router has been deployed...', this.options.verbose ? `(deploymentId:${deploymentId})` : ''); return new Promise((resolve, reject) => { const maxAttempts = 60; let currentAttemptsCount = 0; let result; let intervalId = setInterval(async () => { ++currentAttemptsCount; try { result = await this.api.checkIfRouterDeployed(this.routerJson.name, deploymentId); } catch (err) {} if (result && result.finished) { clearInterval(intervalId); if (!result.error) { resolve(); } else { const errorMessage = '\n==================== Error in router\'s deployment ======================' + '\n' + result.error + '\n' + '========================================================================' reject(new Error(errorMessage)); } } else if (currentAttemptsCount > maxAttempts) { clearInterval(intervalId); reject(new Error('Error while checking if router deployed - max attempts count exceeded.')); } else { console.info('Router not yet deployed... Waiting...'); } }, 5000); }); } } module.exports = Router;