1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const base_1 = tslib_1.__importStar(require("../base"));
|
5 | const CustomErrors_1 = require("../errors/CustomErrors");
|
6 | const env_1 = require("../constants/env");
|
7 | const get_docker_1 = tslib_1.__importDefault(require("../utils/get-docker"));
|
8 |
|
9 | exports.getOps = async (opName, teamId, accessToken, api) => {
|
10 | return api.find('/private/ops', {
|
11 | query: {
|
12 | name: opName,
|
13 | team_id: teamId,
|
14 | },
|
15 | headers: {
|
16 | Authorization: accessToken,
|
17 | },
|
18 | });
|
19 | };
|
20 |
|
21 | exports.formImageName = (nameOrId, teamName, registryHost) => {
|
22 | return `${registryHost}/${teamName}/${nameOrId}`;
|
23 | };
|
24 |
|
25 | exports.removeImage = async (docker, imageName) => {
|
26 | await docker
|
27 | .getImage(imageName)
|
28 | .remove()
|
29 | .catch(err => {
|
30 | this.debug('%O', err);
|
31 | throw new CustomErrors_1.ImageNotFoundError(imageName);
|
32 | });
|
33 | };
|
34 | class Cleanup extends base_1.default {
|
35 | async run() {
|
36 | const { args: { opName }, } = this.parse(Cleanup);
|
37 | try {
|
38 | this.docker = await get_docker_1.default(this, 'publish');
|
39 | await this.isLoggedIn();
|
40 | if (!this.docker)
|
41 | return;
|
42 | if (opName === 'all' || !opName) {
|
43 |
|
44 | await this.docker.pruneImages();
|
45 | this.log(`\n Successfully removed unused images`);
|
46 | process.exit();
|
47 | }
|
48 | const ops = await exports.getOps(opName, this.team.id, this.accessToken, this.services.api).catch(err => {
|
49 | this.debug('%O', err);
|
50 | throw new Error('API error');
|
51 | });
|
52 | if (!ops.data) {
|
53 | throw new CustomErrors_1.ImageNotFoundError(opName);
|
54 | }
|
55 |
|
56 | const { id, name } = ops.data[0];
|
57 | const imagebyId = exports.formImageName(id, this.team.name, env_1.OPS_REGISTRY_HOST);
|
58 | const imagebyName = exports.formImageName(name, this.team.name, env_1.OPS_REGISTRY_HOST);
|
59 | await exports.removeImage(this.docker, imagebyId);
|
60 | await exports.removeImage(this.docker, imagebyName);
|
61 | this.services.analytics.track({
|
62 | userId: this.user.email,
|
63 | teamId: this.team.id,
|
64 | cliEvent: 'Ops CLI Cleanup',
|
65 | event: 'Ops CLI Cleanup',
|
66 | properties: {
|
67 | email: this.user.email,
|
68 | username: this.user.username,
|
69 | },
|
70 | }, this.accessToken);
|
71 | this.log(`\n Successfully removed images for ${opName}`);
|
72 | }
|
73 | catch (err) {
|
74 | this.debug('%O', err);
|
75 | this.config.runHook('error', { err, accessToken: this.accessToken });
|
76 | }
|
77 | }
|
78 | }
|
79 | exports.Cleanup = Cleanup;
|
80 | Cleanup.description = 'Clean up locally cached docker images.';
|
81 | Cleanup.flags = {
|
82 | help: base_1.flags.help({ char: 'h' }),
|
83 | };
|
84 | Cleanup.args = [
|
85 | {
|
86 | name: 'opName',
|
87 | description: 'Name of the op to be cleaned up',
|
88 | },
|
89 | ];
|