-
Notifications
You must be signed in to change notification settings - Fork 22
/
delete-stack.py
68 lines (53 loc) · 2.14 KB
/
delete-stack.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
#!/usr/bin/env python
# coding: utf-8
import os
import json
import logging
import argparse
import boto3
import urllib2
import botocore
import sys
import time
import signal
from urlparse import urlparse, parse_qs
from utils import make_cloudformation_client, load_config, get_log_level
LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(funcName) '
'-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--name', type=str, required=True,
help='the name of the stack to create.')
parser.add_argument('--retain', type=str, required=False,
help='the names (comma separated) of the resources to retain.')
parser.add_argument('--log', type=str, default="INFO", required=False,
help='which log level. DEBUG, INFO, WARNING, CRITICAL')
parser.add_argument('--config', type=str, required=False,
help='the config file used for the application.')
args = parser.parse_args()
# init LOGGER
logging.basicConfig(level=get_log_level(args.log), format=LOG_FORMAT)
#load the client using app config or default
client = make_cloudformation_client(args.config)
try:
retained_resources = []
if args.retain and len(args.retain)>0:
retained_respources = args.retain.split(",")
response = client.delete_stack(
StackName=args.name,
RetainResources=retained_resources
)
# we expect a response, if its missing on non 200 then show response
if 'ResponseMetadata' in response and \
response['ResponseMetadata']['HTTPStatusCode'] < 300:
logging.info("succeed. response: {0}".format(json.dumps(response)))
else:
logging.critical("There was an Unexpected error. response: {0}".format(json.dumps(response)))
except ValueError as e:
logging.critical("Value error caught: {0}".format(e))
except:
# catch any failure
logging.critical("Unexpected error: {0}".format(sys.exc_info()[0]))
if __name__ == '__main__':
main()