-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfn_launch.py
455 lines (366 loc) · 17.1 KB
/
cfn_launch.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/python
# -*- coding:utf8 -*-
"""
Name: cfn_launch.py
Dependencies:
pip install requests
pip install boto3
IAM -> USERS -> Add user
aws configure -> access key + secret key
C:\\Users\\kwang1\\.aws\\credentials
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
pip install awscli
pip install PyYAML
Functions:
1. Create, describe and delete AWS CloudFormation Stackes using boto3.
2. Provide local IP address to enable EC2 access to local laptop only.
3. When creating stacks, if not provided, stack name will be appended with current timestamp.
4. Describe stack will generate Xshell access config file for stacks in "CREATE_COMPLETE" status.
5. Describe stack will show stack information until "DELETE_COMPLETE".
6. Delete stack will delete Xshell access config file of the stack.
7. Serialize stack info data to cfn-StackInfo.json.
8. Serialize stack parameters to cfn-parameters.json with local ip information.
History:
---------
03/04/2019 Kaiyuan Wang 0.1
Functions using awscli for stack creation. Stack describe and delete with boto3.
03/07/2019 Kaiyuan Wang 1
Change stack create to boto3. Encapsulation stack management into class CfnClient. Documentation.
TODO:
open interfaces
"""
__author__ = "Kaiyuan Wang"
import requests
import re
import os
import json
import yaml
import boto3
import botocore
import sys
import signal
import logging
import logging.handlers
import datetime
import subprocess
import shutil
import platform, getpass
try:
import ConfigParser
except:
import configparser as ConfigParser
from argparse import ArgumentParser
from warnings import filterwarnings
"""
TODO: currently check on stacks created by this script, add option to check all stacks in region
TODO: retrieve server ip from instance instead of output
"""
LOG_LEVELS = { 'debug':logging.DEBUG,
'info':logging.INFO,
'warning':logging.WARNING,
'error':logging.ERROR,
'critical':logging.CRITICAL
}
def exception_hook(exc_type, exc_value, exc_traceback):
logger.error(
"Uncaught exception",
exc_info=(exc_type, exc_value, exc_traceback)
)
sys.excepthook = exception_hook
def log():
def decorator(func):
def wrapper(*args, **kw):
logger.info("Running {0}()...".format(func.__name__))
a=func(*args, **kw)
return a
return wrapper
return decorator
def set_up_logging(log_level):
"""Set up logger."""
logger = logging.getLogger('ConfigAnsibleLogger')
logger.setLevel(LOG_LEVELS[log_level])
if os.path.exists(os.path.join(os.getcwd(), 'logs')):
log_file=os.path.join(os.getcwd(), 'logs', 'cfn_launch.log')
else:
log_file=os.path.join(os.getcwd(), 'cfn_launch.log')
formatter = logging.Formatter('[%(asctime)s %(levelname)s] %(message)s')
handler = logging.handlers.TimedRotatingFileHandler(log_file, when='D', backupCount=10)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
logger = set_up_logging('info')
def fetch_local_ip():
response = requests.get("http://txt.go.sohu.com/ip/soip")
ip = re.findall(r'\d+.\d+.\d+.\d+',response.text)
pub_ip = ip[0]
local_ip_param = [
{
"ParameterKey": "SSHLocation",
"ParameterValue": pub_ip+"/32",
"ResolvedValue": "string"
}
]
with open(os.path.join('cfn_template','cfn-parameters.json'), 'w') as fh:
json.dump(local_ip_param, fh)
return local_ip_param
def yaml2json(yaml_file, json_file):
with open(yaml_file, 'r', encoding='utf8') as fh:
datastream = yaml.load(fh)
with open(json_file, 'w') as fh:
json.dump(datastream, fh)
class MyConfigParser(ConfigParser.ConfigParser):
def optionxform(self, optionstr):
return optionstr
"""Virtually identical to the original method, but delimit keys and values with '=' instead of ' = '"""
def write(self, fp):
if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT)
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
for section in self._sections:
fp.write("[%s]\n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continue
if (value is not None) or (self._optcre == self.OPTCRE):
# This is the important departure from ConfigParser for what you are looking for
key = "=".join((key, str(value).replace('\n', '\n\t')))
fp.write("%s\n" % (key))
fp.write("\n")
class XshellAccess(object):
@staticmethod
def create(stack_name, control_ip):
config_basename = stack_name+'.xsh'
config_file = os.path.join('C:\\Users', getpass.getuser(), 'Desktop', config_basename)
if os.path.exists(config_file):
return 0
aws_config_template = 'C:\\Users\\kwang1\\Documents\\NetSarang\\Xshell\\Sessions\\AWS\\template.xsh'
config_dir = os.path.dirname(aws_config_template)
config_dict = {}
cf = MyConfigParser(allow_no_value=True)
cf.read(aws_config_template)
cf['CONNECTION']['Host'] = control_ip
with open(config_file, 'w') as fh:
cf.write(fh)
os.system(config_file)
@staticmethod
def delete(stack_name):
stack_name = os.path.join('C:\\Users', getpass.getuser(), 'Desktop', stack_name)
os.remove(stack_name+'.xsh')
class CfnClient(object):
def __init__(self, cfn_dir, region='ap-southeast-2', ):
self.region = region
self.cfn_conn = boto3.client('cloudformation', region_name=self.region)
self.stack_info_json = os.path.join(cfn_dir,'cfn-StackInfo.json')
self.stack_info_dict = {}
#with open(self.template_yaml) as yh:
# self.cfn_template = yaml.load(yh)
#self.cfn_template = json.dumps(yh)
def load_stack_info(self):
try:
with open(self.stack_info_json) as fh:
self.stack_info_dict = json.load(fh)
except Exception as e:
logger.error("{0} loading error. {1}".format(self.stack_info_json, e))
self.stack_info_dict = {}
def dump_stack_info(self):
with open(self.stack_info_json, 'w') as fh:
json.dump(self.stack_info_dict, fh)
@log()
def query_cfn_status(self, stack_id_list, connect=False):
# change to use *list
logger.info(stack_id_list)
stack_dict_tmp = {}
for stack_id in stack_id_list:
boto_resp = self.cfn_conn.describe_stacks(StackName=stack_id)
#output_dict = {d["OutputKey"]:d["OutputValue"] for d in boto_resp['Stacks'][0]['Outputs']}
stack_details = boto_resp['Stacks'][0]
stack_name = stack_id.split('/')[1]
stack_status = stack_details['StackStatus']
stack_parameters = stack_details['Parameters']
logger.info("stack_details:\n{0}".format(stack_details))
logger.info("stack_details['StackStatus']: {0}".format(stack_details['StackStatus']))
logger.info("stack_details['Parameters']:\n{0}".format(stack_details['Parameters']))
stack_info = {"stack_name":stack_name, "stack_status":stack_status, "stack_parameters":stack_parameters}
if stack_status == "DELETE_COMPLETE":
logger.info(" ".join([stack_name, stack_status]))
continue
if 'Outputs' in stack_details.keys():
stack_output = {d["OutputKey"]:d["OutputValue"] for d in stack_details['Outputs']}
logger.info("stack_details['Outputs']:\n{0}".format(stack_details['Outputs']))
#print("ControlPublicIp: "+stack_output['ControlPublicIp'])
if not stack_status.startswith("DELETE") and connect and platform.system() == 'Windows':
try:
XshellAccess.create(stack_name, stack_output['ControlPublicIp'])
except:
pass
stack_info.update({"stack_output":stack_output})
stack_dict_tmp.update({stack_id:stack_info})
return stack_dict_tmp
@log()
def build_cfn(self, cfn_template, stack_name):
''' aws cli connection. AWS cli supports JASON only
# yaml2json for aws cli
if re.split("\.", options.cfn_template)[-1] in ["yml", "yaml"]:
cfn_template = re.split("\.", options.cfn_template)[0]+'.json'
yaml2json(options.cfn_template, cfn_template)
else:
cfn_template = options.cfn_template
# cfn-StackId.json: [{"StackId": "arn:aws:cloudformation:ap-southeast-2:813556749890:stack/AnsibleTest-20190302115934/73f8a4f0-3c86-11e9-9b63-02181cf5d610"}]
#aws cloudformation create-stack --stack-name example-cli-stack --template-body file://cfn_ansible_test_one_server.json --parameters file://cfn-parameters.json --region ap-southeast-2
aws_cmd = "aws cloudformation create-stack --stack-name {0} --template-body file://{1} --parameters file://{2} --region {3}".format(stack_name, cfn_template, "cfn-parameters.json", options.region)
p = subprocess.Popen(aws_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logger.info(p.communicate())
stack_id_dict = json.loads(p.communicate()[0].decode('utf-8'))
'''
self.cfn_parameters = []
self.cfn_template = cfn_template
self.stack_name = stack_name
self.validate_cfn()
self.cfn_parameters = fetch_local_ip()
self.load_stack_info()
logger.debug(self.stack_info_dict.keys())
self.stack_info_dict = self.query_cfn_status(self.stack_info_dict.keys())
logger.debug(self.stack_info_dict.keys())
if self.stack_info_dict:
create_confirm=input("Stacks {0} in system. \nAre you sure you want to create another stack from template {1} (y or n)?\n".format(list(self.stack_info_dict.keys()),self.cfn_template))
if create_confirm != "y":
sys.exit("Stack creation aborted. Script exit now!")
try:
with open(self.cfn_template, 'r', encoding='utf8') as fh:
stack_id = self.cfn_conn.create_stack(
StackName=self.stack_name,
TemplateBody=fh.read(),
Parameters=self.cfn_parameters,
# when creating IAM
Capabilities=['CAPABILITY_IAM']
)['StackId']
except Exception as e:
logger.error("{0} loading error. {1}".format(self.cfn_template, e))
sys.exit("{0} loading error. {1}".format(self.cfn_template, e))
logger.debug(stack_id)
stack_id_list = list(self.stack_info_dict.keys())+[stack_id]
logger.debug(stack_id_list)
self.stack_info_dict= self.query_cfn_status(stack_id_list)
self.dump_stack_info()
logger.info("""Create cloudformation stack finished. """)
sys.exit("Stack creation initiated. Exit now!")
@log()
def describe_cfn(self, input_stack_id=""):
if input_stack_id:
logger.info(input_stack_id)
self.print_cfn_desc([input_stack_id])
else:
self.load_stack_info()
self.stack_info_dict = self.print_cfn_desc(self.stack_info_dict.keys())
self.dump_stack_info()
if not self.stack_info_dict:
logger.info("""Describe cloudformation stack finished. \nNo cloudformatation stack can be found. Exit now!""")
sys.exit("No cloudformatation stack can be found. Exit now!")
else:
logger.info("""Describe cloudformation stack finished. """)
sys.exit("""Describe cloudformation stack finished. """)
def print_cfn_desc(self, stack_id_list):
stack_info_dict = self.query_cfn_status(stack_id_list, connect=True)
if not stack_info_dict:
return stack_info_dict
for si in stack_info_dict.keys():
print(si)
logger.info(stack_info_dict[si])
print("Stack Status: "+stack_info_dict[si].get('stack_status'))
output = stack_info_dict[si].get("stack_output")
if output:
for k,v in sorted(output.items()):
print(': '.join([k,v]))
return stack_info_dict
@log()
def delete_cfn(self,input_stack_id=""):
if input_stack_id:
stack_info_dict = self.query_cfn_status([input_stack_id])
self.del_cfn_action(stack_info_dict)
else:
self.load_stack_info()
self.stack_info_dict = self.query_cfn_status(self.stack_info_dict)
if not self.stack_info_dict:
sys.exit("No cloudformatation stack created by {0} is in the system. Exit now!".format(sys.argv[0]))
else:
self.del_cfn_action(self.stack_info_dict)
def del_cfn_action(self, stack_info_dict):
stack_list_remain, stack_list_delete = [], []
if not stack_info_dict:
sys.exit('Stack already DELETE_COMPLETE. Exit now!')
for stack_id in stack_info_dict.keys():
stack_status = stack_info_dict[stack_id].get('stack_status')
print(stack_id, stack_status)
if stack_status.startswith('DELETE'):
print('Already initiated DELETE action.')
continue
delete_confirm=input("Are you sure you want to delete this stack (y or n)?\n")
if delete_confirm == "y":
try:
self.cfn_conn.delete_stack(StackName=stack_id)
logger.info(stack_info_dict)
if platform.system() == 'Windows':
XshellAccess.delete(stack_info_dict[stack_id].get('stack_name'))
except:
pass
finally:
stack_list_delete.append(stack_id)
else:
stack_list_remain.append(stack_id)
logger.info("""Delete cloudformation stack initiation finished. """)
if stack_list_delete:
sys.exit("Stack deleting initiated. Exit now!")
else:
sys.exit("Stack deleting aborted. Exit now!")
def validate_cfn(self):
try:
with open(self.cfn_template, 'r', encoding='utf8') as fh:
response = self.cfn_conn.validate_template(TemplateBody=fh.read())
except Exception as e:
sys.exit(': '.join([self.cfn_template,str(e)]))
if __name__ == '__main__':
parser = ArgumentParser(description="Launch, describe and delete cloudformation stacks")
parser.add_argument('-t', '--template', dest='cfn_template',
help='Cloudformation template. Default: cfn_test.yaml. 3 server template: cfn_ansible_test.yaml',
default='cfn_test.yaml', action='store')
parser.add_argument('-d', '--dir', dest='cfn_dir',
help='Cloudformation dir. Default: cfn_template. ',
default='cfn_template', action='store')
parser.add_argument('-n', '--stack-name', dest='stack_name',
help='Cloudformation stack name', action='store')
parser.add_argument('-i', '--stack-id', dest='stack_id',
help='Cloudformation stack id. Used by stack-describe, stack-delete',
default='', action='store')
parser.add_argument('-p', '--parameter-file', dest='parameter_file',
help='Cloudformation parameter file. default: cfn-parameter.json',
default='cfn-parameter.json', action='store')
parser.add_argument('-r', '--region', dest='region',
help='Cloudformation parameter file. default: ap-southeast-2',
default="ap-southeast-2", action='store')
parser.add_argument('-m', '--mode', dest='mode',
help='Cloudformation management mode. Available options: create, delete, describe.',
default="describe", action='store')
parser.add_argument('-l', '--log-level', dest='log_level',
help='Availalbe log_level: debug, info, warning, error, critical.',
default="info", action='store')
args = parser.parse_args()
logger.info("""script start. \n{0}""".format(args))
# CREATE CfnClient INSTANCE
cfn_client = CfnClient(args.cfn_dir)
# CREATE STACK
if args.mode == "create":
if args.stack_name:
stack_name = args.stack_name
else:
stack_name = '-'.join(re.findall("([0-9a-zA-Z]+)",args.cfn_template)[:-1]+[datetime.datetime.now().strftime("%Y%m%d%H%M%S")])
cfn_client.build_cfn(os.path.join(args.cfn_dir,args.cfn_template), stack_name)
elif args.mode == "describe":
cfn_client.describe_cfn(args.stack_id)
elif args.mode == "delete":
cfn_client.delete_cfn(args.stack_id)
else:
sys.exit("Not a valid mode option!")