-
Notifications
You must be signed in to change notification settings - Fork 13
/
destroy.ts
60 lines (51 loc) · 2.2 KB
/
destroy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { CliUx, Flags } from '@oclif/core';
import chalk from 'chalk';
import AccountUtils from '../architect/account/account.utils';
import Deployment from '../architect/deployment/deployment.entity';
import { EnvironmentUtils, GetEnvironmentOptions } from '../architect/environment/environment.utils';
import PipelineUtils from '../architect/pipeline/pipeline.utils';
import { DeployCommand } from './deploy';
export default class Destroy extends DeployCommand {
async auth_required(): Promise<boolean> {
return true;
}
static description = 'Destroy components from an environment';
static examples = [
'architect destroy --account=myaccount --auto-approve',
'architect destroy --account=myaccount --environment=myenvironment --auto-approve',
];
static args = [];
static flags = {
...DeployCommand.flags,
...AccountUtils.flags,
...EnvironmentUtils.flags,
components: Flags.string({
char: 'c',
description: 'Component(s) to destroy',
multiple: true,
sensitive: false,
}),
};
async run(): Promise<void> {
const { flags } = await this.parse(Destroy);
const account = await AccountUtils.getAccount(this.app, flags.account);
const get_environment_options: GetEnvironmentOptions = { environment_name: flags.environment };
const environment = await EnvironmentUtils.getEnvironment(this.app.api, account, get_environment_options);
CliUx.ux.action.start(chalk.blue('Creating pipeline'));
let instance_ids;
if (flags.components) {
const { data: instances_to_destroy } = await this.app.api.get(`/environments/${environment.id}/instances`, { params: { component_versions: flags.components } });
instance_ids = instances_to_destroy.map((instance: Deployment) => instance.instance_id);
}
const { data: pipeline } = await this.app.api.delete(`/environments/${environment.id}/instances`, { data: { instance_ids } });
CliUx.ux.action.stop();
const approved = await this.approvePipeline(pipeline);
if (!approved) {
return;
}
CliUx.ux.action.start(chalk.blue('Deploying'));
await PipelineUtils.pollPipeline(this.app, pipeline.id);
this.log(chalk.green(`Deployed`));
CliUx.ux.action.stop();
}
}