-
Notifications
You must be signed in to change notification settings - Fork 5
/
destroy-citc.py
executable file
·70 lines (57 loc) · 2.58 KB
/
destroy-citc.py
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
#! /usr/bin/env python
from __future__ import print_function, unicode_literals
import argparse
import os
import os.path
import stat
import tarfile
from subprocess import check_call, CalledProcessError
try:
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
try:
# Python 2/3 compatibility
input = raw_input
except NameError:
pass
def main():
parser = argparse.ArgumentParser()
parser.add_argument("csp", help="Which cloud provider to install into")
parser.add_argument("ip", help="The IP address of the cluster's management node")
parser.add_argument("key", help="Path of the SSH key from cluster creation")
parser.add_argument("--dry-run", help="Perform a dry run", action="store_true")
args = parser.parse_args()
# Check that the user really meant it
if not args.dry_run:
proceed = input("Are you sure you want to destroy the cluster at {}? [y/N]: ".format(args.ip))
if proceed.lower() != "y":
exit(1)
# Download the Terraform configuration from the cluster
tf_zip_filename = "citc-terraform.tar.gz"
print("Downloading the Terraform configuration from {}".format(args.ip))
check_call(["scp", "-i", args.key, "-o", "IdentitiesOnly=yes", "citc@{}:{}".format(args.ip, tf_zip_filename), "."])
tf_tar = tarfile.open(tf_zip_filename)
dir_name = tf_tar.getnames()[0]
tf_tar.extractall()
# Shut down any running compute nodes and delete associated DNS entries
if not args.dry_run:
try:
print("Connecting to the cluster to destroy lingering compute nodes...")
check_call(["ssh", "-i", args.key, "-o", "IdentitiesOnly=yes", "citc@{}".format(args.ip), "/usr/local/bin/kill_all_nodes --force"])
except CalledProcessError:
print("/usr/local/bin/kill_all_nodes failed to run. You may have lingering compute nodes. You must kill these manually.")
os.chdir(dir_name)
os.chmod("terraform", stat.S_IRWXU)
check_call(["./terraform", "-chdir={}".format(args.csp), "init"])
if not args.dry_run:
try:
print("Destroying cluster...")
check_call(["./terraform", "-chdir={}".format(args.csp), "apply", "-destroy", "-auto-approve"])
except CalledProcessError:
print("Terraform destroy failed. Try again with:")
print(" cd {}".format(dir_name))
print(" ./terraform -chdir={} apply -destroy ".format(args.csp))
print("You may need to manually clean up any remaining running instances or DNS entries")
if __name__ == "__main__":
main()