UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

170 lines 6.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HelmDeploymentManager = void 0; const jsyaml = require("js-yaml"); class HelmDeploymentManager { constructor(session, release) { this.release = release; this.session = session; this.cleanDeployments(); this.sortDeployments(); this.latestDeployment = this.getLatestDeployment(); this.activeDeployment = this.getActiveDeployment(); } // Public Methods // createFirstDeployment(chart, description, config, values, valuesFiles, gvc) { return { gvc: gvc, revision: 1, updated: new Date().toISOString(), status: 'pending-install', chart: chart.name, appVersion: chart.appVersion, description: description, config: config, values: values, valuesFiles: valuesFiles, resources: [], }; } createNextDeployment(status, chart, description, config, values, valuesFiles, gvc) { // Create a new deployment const deployment = { gvc: gvc, revision: this.latestDeployment.revision + 1, updated: new Date().toISOString(), status: status, chart: chart.name, appVersion: chart.appVersion, description: description, config: config, values: values, valuesFiles: valuesFiles, resources: [], }; // Remove one of the deployments that are not deployed if (this.release.deployments.length > 9) { // Find the index of the first deployment whose status is not 'deployed' const indexToRemove = this.release.deployments.findIndex((deployment) => deployment.status !== 'deployed'); // Check if such deployment is found and remove it, most likely such deployment is found if (indexToRemove !== -1) { this.release.deployments.splice(indexToRemove, 1); } } return deployment; } updateDeploymentStatus(deployment, status, errorMessage) { if (!deployment.description) { let description = ''; switch (status) { case 'deployed': description = deployment.status == 'pending-install' ? 'Install complete' : 'Upgrade complete'; break; case 'failed': description = deployment.status == 'pending-install' ? 'Install failed' : 'Upgrade failed'; if (errorMessage) { description = `${description}: ${errorMessage}`; } break; } deployment.description = description; } deployment.status = status; deployment.updated = new Date().toISOString(); } getDeployment(revision) { return this.release.deployments.find((deployment) => deployment.revision == revision); } findDeployment(revision) { let deployment; // Find the deployment with the specified revision if (revision) { const foundDeployment = this.release.deployments.find((deployment) => deployment.revision == revision); if (!foundDeployment) { this.session.abort({ message: `ERROR: Deployment with revision '${revision}' does not exit.` }); } deployment = foundDeployment; } else { if (!this.activeDeployment) { throw new Error(`ERROR: No active deployment was found.`); } deployment = this.activeDeployment; } return deployment; } findLatestSupersededDeployment() { // Iterate from the end of the array to the beginning since it is sorted for (let i = this.release.deployments.length - 1; i >= 0; i--) { if (this.release.deployments[i].status === 'superseded') { // Return the first 'superseded' deployment encountered return this.release.deployments[i]; } } // Return undefined if no 'superseded' deployment is found return undefined; } findLatestDeployedDeployment() { // Iterate from the end of the array to the beginning since it is sorted for (let i = this.release.deployments.length - 1; i >= 0; i--) { if (this.release.deployments[i].status === 'deployed') { // Return the first 'deployed' deployment encountered return this.release.deployments[i]; } } // Return undefined if no 'deployed' deployment is found return undefined; } // Private Methods // cleanDeployments() { if (this.release.deployments.length == 0) { return; } for (const deployment of this.release.deployments) { switch (deployment.status) { case 'pending-install': case 'pending-upgrade': case 'pending-rollback': case 'uninstalling': deployment.status = 'failed'; break; } if (deployment.values === undefined) { deployment.values = {}; } else if (typeof deployment.values === 'string') { try { deployment.values = jsyaml.load(deployment.values, { json: true }); } catch (e) { } } if (deployment.valuesFiles === undefined) { deployment.valuesFiles = []; } } } sortDeployments() { this.release.deployments.sort((a, b) => a.revision - b.revision); } getActiveDeployment() { let deployment = this.release.deployments.find((deployment) => deployment.status === 'deployed'); // Return deployment if found if (deployment) { return deployment; } // Throw an error if active deployment is not found and there were more than 1 deployment if (this.release.deployments.length > 1) { // We should never get to this line of code unless the user manually manipulated the secret payload this.session.abort({ message: 'ERROR: Could not find an active deployment. Please, uninstall and re-install the release again.' }); } return this.getLatestDeployment(); } getLatestDeployment() { if (this.release.deployments.length == 0) { return undefined; } return this.release.deployments[this.release.deployments.length - 1]; } } exports.HelmDeploymentManager = HelmDeploymentManager; //# sourceMappingURL=helmDeploymentManager.js.map