-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdelete.ts
157 lines (114 loc) · 4.33 KB
/
delete.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { flags } from "@oclif/command"
import dotenv from "dotenv-flow"
import ux from "cli-ux"
import { remove } from "fs-extra"
import chalk from "chalk"
import execa from "execa"
import BaseInfrastructureCommand from "../../BaseInfrastructureCommand"
import InfrastructureFactory from "../../lib/infrastructures/InfrastructureFactory"
import environmentsManager from "../../lib/environments"
import AWSS3Backend from "../../lib/backends/AWSS3Backend"
import confirmEnvironmentDeletionPrompt from "../../lib/prompts/confirmEnvironmentDeletionPrompt"
import IAWSInfrastructureEnvVars from "../../lib/infrastructures/interfaces/IAWSInfrastructureEnvVars"
/**
* Represents the "scaffold env:delete {environment}" command
* used to delete an environment.
*/
class EnvDelete extends BaseInfrastructureCommand {
static args = [{
name: "environment",
required: true,
}]
static description = "delete passed environment"
static flags = {
help: flags.help({
char: "h",
}),
}
async run() {
const { args } = this.parse(EnvDelete)
await this.ensureAllRequirements()
const infrastructurePath = await this.infrastructurePath()
if (!infrastructurePath) {
this.error("No infrastructures found.")
}
const infrastructurePackage = await this.infrastructurePackage(infrastructurePath)
if (!infrastructurePackage) {
this.error("Invalid scaffold.json file.")
}
const infrastructure = InfrastructureFactory.infrastructure(infrastructurePackage.type)
if (!infrastructure) {
this.error("Unknown infrastructure.")
}
const environmentToDelete = args.environment as string
const environments = await environmentsManager.findAll(infrastructurePath)
if (!environments.all.includes(environmentToDelete)) {
this.error(`"${chalk.bold(environmentToDelete)}" environment doesn't exist.`)
}
if (environments.sandboxed.includes(environmentToDelete)) {
this.error(`Sandbox found. Run "${chalk.bold(chalk.cyan("scaffold") + " sandbox:delete " + environmentToDelete)}" first.`)
}
const cdktfPath = this.cdktfPath(infrastructurePath)
const terraformPlanPath = await this.terraformPlanPath(infrastructurePath)
const terraformPath = BaseInfrastructureCommand.terraformBinaryPath()
if (environments.configured.includes(environmentToDelete)) {
ux.action.start("Checking out Terraform state")
await execa(cdktfPath, ["synth"], {
cwd: infrastructurePath,
env: {
NODE_ENV: environmentToDelete,
},
})
try {
await execa(terraformPath, ["init", "-reconfigure"], {
cwd: terraformPlanPath,
})
const { stdout } = await execa(terraformPath, ["state", "pull"], {
cwd: terraformPlanPath,
})
const state = JSON.parse(stdout)
ux.action.stop()
if (state.resources.length > 0) {
this.error(`Non-empty Terraform state. Run "${chalk.bold(chalk.cyan("scaffold") + " destroy " + environmentToDelete)}" first.`)
}
this.log("")
} catch (error) {
if (!error.message.match(/S3 bucket does not exist/)) {
throw error
}
}
}
const confirmDeletion = await confirmEnvironmentDeletionPrompt(environmentToDelete)
if (!confirmDeletion) {
this.log("\nDeletion cancelled.")
this.exit(0)
}
this.log("")
ux.action.start("Removing environment")
const {
global: globalEnvPath,
specific: specificEnvPath,
local: localEnvPath,
} = environmentsManager.getPaths(infrastructurePath, environmentToDelete)
if (environments.configured.includes(environmentToDelete)) {
const envVars = dotenv.parse([
globalEnvPath,
specificEnvPath,
localEnvPath,
]) as any as IAWSInfrastructureEnvVars
const s3Backend = new AWSS3Backend(
envVars.SCAFFOLD_AWS_REGION,
envVars.SCAFFOLD_AWS_PROFILE
)
const backendEnvVars = s3Backend.generateBackendEnvVarsFromInfraEnvVars(
envVars
)
await s3Backend.destroy(backendEnvVars)
await remove(localEnvPath)
}
await remove(specificEnvPath)
ux.action.stop()
this.log(`\n${chalk.bold.green("Success!")} Deleted "${chalk.bold(environmentToDelete)}" environment at ${chalk.bold(infrastructurePath)}`)
}
}
export default EnvDelete