UNPKG

@cto.ai/ops

Version:

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

90 lines (89 loc) • 3.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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 => { this.debug('%O', err); throw new CustomErrors_1.ImageNotFoundError(imageName); }); }; class Cleanup extends base_1.default { async run() { const { args: { opName }, } = this.parse(Cleanup); try { this.docker = await get_docker_1.default(this, 'publish'); await this.isLoggedIn(); 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, this.team.name, env_1.OPS_REGISTRY_HOST); const imagebyName = exports.formImageName(name, this.team.name, env_1.OPS_REGISTRY_HOST); await exports.removeImage(this.docker, imagebyId); await exports.removeImage(this.docker, imagebyName); this.services.analytics.track({ userId: this.user.email, teamId: this.team.id, cliEvent: 'Ops CLI Cleanup', event: 'Ops CLI Cleanup', properties: { email: this.user.email, username: this.user.username, }, }, this.accessToken); this.log(`\n Successfully removed images for ${opName}`); } catch (err) { this.debug('%O', err); this.config.runHook('error', { err, accessToken: this.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', }, ];