UNPKG

@cto.ai/ops

Version:

💻 CTO.ai - The CLI built for Teams 🚀

89 lines (88 loc) • 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Cleanup = exports.removeImage = exports.formImageName = exports.getOps = void 0; const tslib_1 = require("tslib"); const base_1 = tslib_1.__importStar(require("../base")); const CustomErrors_1 = require("../errors/CustomErrors"); const env_1 = require("../constants/env"); const get_docker_1 = tslib_1.__importDefault(require("./../utils/get-docker")); // get ops matching the provided name const getOps = async (opName, teamId, accessToken, api) => { return api.find('/private/ops', { query: { name: opName, team_id: teamId, }, headers: { Authorization: accessToken, }, }); }; exports.getOps = getOps; // form docker image name given the name/id and team name const formImageName = (nameOrId, teamName, registryHost) => { return `${registryHost}/${teamName}/${nameOrId}`; }; exports.formImageName = formImageName; // remove the docker images const removeImage = async (docker, imageName) => { await docker .getImage(imageName) .remove() .catch(err => { throw new CustomErrors_1.ImageNotFoundError(imageName); }); }; exports.removeImage = removeImage; class Cleanup extends base_1.default { async run() { const { args: { workflow }, } = this.parse(Cleanup); const config = await this.isLoggedIn(); try { this.docker = await (0, get_docker_1.default)(this, 'publish'); if (!this.docker) return; if (workflow === 'all' || !workflow) { // prune all unused images await this.docker.pruneImages(); this.log(`\n Successfully removed unused images`); process.exit(); } const ops = await (0, exports.getOps)(workflow, this.team.id, this.accessToken, this.services.api).catch(err => { this.debug('%O', err); throw new Error('API error'); }); if (!ops.data) { throw new CustomErrors_1.ImageNotFoundError(workflow); } // remove both the images for the matching workflow name const { id, name } = ops.data[0]; const imagebyId = (0, exports.formImageName)(id, config.team.name, env_1.OPS_REGISTRY_HOST); const imagebyName = (0, exports.formImageName)(name, config.team.name, env_1.OPS_REGISTRY_HOST); await (0, exports.removeImage)(this.docker, imagebyId); await (0, exports.removeImage)(this.docker, imagebyName); this.services.analytics.track('Ops CLI Cleanup', { username: config.user.username, }, config); this.log(`\n Successfully removed images for ${workflow}`); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: config.tokens.accessToken, }); } } } exports.Cleanup = Cleanup; Cleanup.description = 'Clean up locally cached docker images.'; Cleanup.flags = { help: base_1.flags.help({ char: 'h' }), }; Cleanup.args = [ { name: 'workflow', description: 'Name of the workflow to be cleaned up', }, ];