-
Notifications
You must be signed in to change notification settings - Fork 8
/
destroy
executable file
·183 lines (157 loc) · 5.21 KB
/
destroy
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# region copyright
# Copyright 2023 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# endregion
import os
import shutil
import sys
from pathlib import Path
import click
from src.python.azure import azure_login
from src.python.config import c as config
from src.python.debug import debug_break # noqa
from src.python.utils import (
colorize_info,
colorize_prompt,
colorize_result,
deployments,
format_cloud_name,
gcp_login,
shell_command,
)
@click.command()
@click.option(
"--debug/--no-debug",
default=False,
show_default=True,
)
@click.option(
"--dryrun/--no-dryrun",
default=False,
show_default=True,
)
@click.argument(
"deployment_name",
required=True,
type=click.Choice(deployments()),
)
@click.option(
"--yes/--ask",
default=False,
show_default=True,
help="Destroy without confirmation?",
prompt=colorize_prompt("Are you sure?"),
)
@click.option(
"--login/--no-login",
default=True,
show_default=True,
help="Login to Azure?",
)
def main(
yes: bool,
debug: bool,
login: bool,
dryrun: bool,
deployment_name: str,
):
if not yes:
click.echo(colorize_info("Aborted!"))
return
if dryrun:
print(colorize_info("* Dry run - not destroying anything"))
tfstate_file = Path(f"{config['state_dir']}/{deployment_name}/.tfstate").absolute()
tfvars_file = Path(f"{config['state_dir']}/{deployment_name}/.tfvars").absolute()
# determine what cloud the deployment is for
cloud = "none"
if Path(tfstate_file).exists():
text = Path(tfstate_file).read_text()
if "registry.terraform.io/hashicorp/aws" in text:
cloud = "aws"
elif "registry.terraform.io/hashicorp/azurerm" in text:
cloud = "azure"
elif "registry.terraform.io/hashicorp/google" in text:
cloud = "gcp"
elif "registry.terraform.io/hashicorp/alicloud" in text:
cloud = "alicloud"
if cloud == "none":
print(colorize_info(f'* No cloud resources found for "{deployment_name}"'))
# login if needed
if login:
if "azure" == cloud:
azure_login(verbose=debug)
elif "gcp" == cloud:
gcp_login(verbose=debug)
if cloud != "none":
# do `terraform init` in case we didn't map /app dir
# (the case when prebuilt image is used)
click.echo(colorize_info("* Initializing terraform modules..."))
shell_command(
f"terraform init -upgrade -no-color -input=false {' > /dev/null' if not debug else ''}",
verbose=debug,
cwd=f"{config['terraform_dir']}/{cloud.lower()}",
)
if dryrun:
# only show what would be destroyed by terraform
click.echo(
colorize_info(
f'* Showing changes to {format_cloud_name(cloud)} resources used by "{deployment_name}"...'
)
)
shell_command(
command=f'terraform plan -destroy -var-file="{tfvars_file}" -state="{tfstate_file}"',
cwd=f"{config['terraform_dir']}/{cloud.lower()}",
exit_on_error=False,
verbose=debug,
)
else:
# destroy cloud resources
click.echo(
colorize_info(
f'* Destroying {format_cloud_name(cloud)} resources used by "{deployment_name}"...'
)
)
shell_command(
command=f'terraform destroy -auto-approve -var-file="{tfvars_file}" -state="{tfstate_file}"',
cwd=f"{config['terraform_dir']}/{cloud.lower()}",
exit_on_error=True,
verbose=debug,
)
# remove tfstate, tfvars, keys, etc
deployment_data_dir = f"{config['state_dir']}/{deployment_name}"
deployment_data_dir_relative = os.path.relpath(
deployment_data_dir, config["app_dir"]
)
if not dryrun:
# remove deployment data dir
shutil.rmtree(deployment_data_dir, ignore_errors=True)
click.echo(
colorize_info(f'* Removed "{deployment_data_dir_relative}" directory')
)
else:
# only show what would be removed on real run
click.echo(
colorize_info(f'* Would remove "{deployment_data_dir_relative}" directory')
)
# ta-da
if not dryrun:
click.echo(colorize_result(f'* Deployment "{deployment_name}" destroyed'))
if __name__ == "__main__":
if os.path.exists("/.dockerenv"):
# we're in docker, run command
main()
else:
# we're outside, start docker container first
shell_command(f"./run '{' '.join(sys.argv)}'", verbose=True)