UNPKG

@cto.ai/ops-rc

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

86 lines (85 loc) • 3.19 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 exports.getOps = async (opName, teamId, accessToken, api) => { return api.find('/private/ops', { query: { name: opName, team_id: teamId, }, headers: { Authorization: accessToken, }, }); }; // form docker image name given the name/id and team name exports.formImageName = (nameOrId, teamName, registryHost) => { return `${registryHost}/${teamName}/${nameOrId}`; }; // remove the docker images exports.removeImage = async (docker, imageName) => { await docker .getImage(imageName) .remove() .catch(err => { throw new CustomErrors_1.ImageNotFoundError(imageName); }); }; class Cleanup extends base_1.default { async run() { const { args: { opName }, } = this.parse(Cleanup); const config = await this.isLoggedIn(); try { this.docker = await get_docker_1.default(this, 'publish'); if (!this.docker) return; if (opName === 'all' || !opName) { // prune all unused images await this.docker.pruneImages(); this.log(`\n Successfully removed unused images`); process.exit(); } const ops = await exports.getOps(opName, 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(opName); } // remove both the images for the matching op name const { id, name } = ops.data[0]; const imagebyId = exports.formImageName(id, config.team.name, env_1.OPS_REGISTRY_HOST); const imagebyName = exports.formImageName(name, config.team.name, env_1.OPS_REGISTRY_HOST); await exports.removeImage(this.docker, imagebyId); await exports.removeImage(this.docker, imagebyName); this.services.analytics.track('Ops CLI Cleanup', { username: config.user.username, }, config); this.log(`\n Successfully removed images for ${opName}`); } 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: 'opName', description: 'Name of the op to be cleaned up', }, ];