cdep.py
56.7 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
#!/usr/bin/env python
#
# AWS cloud deployment tool for autoscaled applications.
# Usage:
# $ ./cdep.py -h
#
# Requires:
# boto3 >= 1.6.6
# interruptingcow = 0.8
# pytz
#
import boto3
import socket
import time
import subprocess
import re
import os
import json
import logging
import sys
import glob
import argparse
import logging as log
import pytz
import itertools
from shutil import copy
from interruptingcow import timeout
from datetime import datetime
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# define boto3 clients
asg_client = boto3.client('autoscaling')
CDEP_VERSION = '1.2.2'
TMP_DIR = '/tmp'
DEFAULT_REGION = 'us-west-2'
DEFAULT_VER = '$Default'
DEFAULT_ASG_WAIT = 300
STATE_PENDING = 0
ASG_MIN_MINSIZE = 2
ASG_MIN_MAXSIZE = 3
SSH_KEYWORD_HOST = 'TARGET_IP'
SSH_KEYWORD_PORT = 'TARGET_PORT'
SSH_KEYWORD_COMMENT = 'TARGET_COMMENT'
class Cleanable(object):
"""
An interface for classes that need to perform
some kind of cleanup process at the end of the script run.
"""
def _get_removable_files(self):
"""
Return a string or a list of strings (file pathnames).
"""
return []
def _get_cleanup_funs(self):
"""
Return a dict or a list of dicts with the following keys/values:
[
{
'fun': <function>,
'args': <list>,
'kwargs': <dict>,
}
]
"""
return []
class Cleaner(object):
"""
Just a class that gathers filename paths and deletes them.
And also executes functions, supposedly as part of cleanup.
"""
def __init__(self):
self.files = []
self.funs = []
def mark(self, cleanable):
"""
Tag all deletable files from a Cleanable object.
Also get the cleanup functions to be executed later.
"""
if not isinstance(cleanable, Cleanable):
log.error(
'Cleaner.mark(cleanable): Argument is '
'not an instance of Cleanable class.'
)
raise RuntimeError('Arg not an instance of Cleanable')
v = cleanable._get_removable_files()
if isinstance(v, list):
self.files += v
elif isinstance(v, str):
self.files.append(v)
v = cleanable._get_cleanup_funs()
if isinstance(v, list):
self.funs += v
elif isinstance(v, dict):
self.funs.append(v)
def cleanup(self):
for f in self.files:
log.debug('(cleaner) Removing file %s', f)
try: os.remove(f)
except OSError: pass
for f in self.funs:
f['fun'](*f['args'], **f['kwargs'])
class AppDeployer(Cleanable):
"""
Enables custom deploy methods on nodes.
"""
def __init__(self, **kwargs):
self.ident = None
self.app_project_dir = kwargs.get('app_project_dir', '.')
self.mark_file_path = kwargs.get(
'mark_file_path', TMP_DIR + '/cd-app-deployed'
)
log.debug(
'[%s] app_project_dir: %s',
self.__class__.__name__,
self.app_project_dir
)
def deploy(self, ident=None):
self.ident = ident
if self.ident:
log.debug(
'%s idempotent deploy with id: %s',
self.__class__.__name__, self.ident
)
mark_file = self.mark_file_path + '_' + self.ident
if os.path.isfile(mark_file):
log.debug('Deploy mark file found: %s', mark_file)
log.info(
'Skipping deploy. Existing deploy mark found for %s',
self.ident
)
else:
log.debug('%s deploying now', self.__class__.__name__)
if self._do_deploy():
log.debug('Writing deploy mark file to: %s', mark_file)
open(mark_file, 'w').close()
else:
log.debug('%s non-idempotent deploy', self.__class__.__name__)
self._do_deploy()
def _do_deploy(self):
"""
Please override this implementation in the subclass.
Return True if successful.
"""
return True
def _get_removable_files(self):
if self.ident:
return self.mark_file_path + '_' + self.ident
return []
class TestDeployer(AppDeployer):
"""
A test deployer that does nothing
"""
def __init__(self, **kwargs):
self.ssh = kwargs.get('ssh')
self.target_hostname = kwargs.get('target_hostname', 'test_target')
super(TestDeployer, self).__init__(**kwargs)
def _do_deploy(self):
if subprocess.call(['ssh', self.target_hostname, 'uname -a']) != 0:
raise RuntimeError('Unable to uname -a')
return True
class CapDeployer(AppDeployer):
"""
Customize deploy as Capistrano for Ruby-on-Rails.
"""
def __init__(self, **kwargs):
self.ssh = kwargs.get('ssh')
self.cap_env = kwargs.get('cap_env', 'production')
log.debug('[%s] env: %s', self.__class__.__name__, self.cap_env)
super(CapDeployer, self).__init__(**kwargs)
def _do_deploy(self):
with SSHAgentEnv([self.ssh.id_rsa_file_path]) as senv:
com = ['bundle', 'exec', 'cap', self.cap_env]
com_1 = com + ['linked_files:upload_files']
log.debug('Working dir: %s', self.app_project_dir)
log.debug('Executing command: %s', ' '.join(com_1))
if subprocess.call(com_1, cwd=self.app_project_dir, env=senv) != 0:
log.error(
'%s non-zero return value on: %s',
self.__class__.__name__, ' '.join(com_1)
)
raise RuntimeError('Unable to upload linked_files')
com_2 = com + ['deploy']
log.debug('Working dir: %s', self.app_project_dir)
log.debug('Executing command: %s', ' '.join(com_2))
if subprocess.call(com_2, cwd=self.app_project_dir, env=senv) != 0:
log.error(
'%s non-zero return value on: %s',
self.__class__.__name__, ' '.join(com_2)
)
raise RuntimeError('Unable to deploy')
return True
class SSHAgentEnv(object):
"""
Takes care of deleting and renewing the SSH agent process.
Use this when doing some SSH-agent-dependent RPC on a remote IP.
"""
def __init__(self, keys):
self.keys = keys
self.environ = None
def __enter__(self):
self.environ = os.environ.copy()
subprocess.call(['pkill', 'ssh-agent'])
try:
stro = subprocess.check_output(['ssh-agent', '-s'])
except subprocess.CalledProcessError as e:
log.exception('Error occurred while doing command: ssh-agent -s')
raise RuntimeError(
'ssh-agent -s error: {0}: {1}'.format(e.returncode, e.output)
)
for name, val in re.findall(r'([A-Z_]+)=([^;]+);', stro):
self.environ[name] = val
for key in self.keys:
subprocess.call(['ssh-add', key], env=self.environ)
return self.environ
def __exit__(self, type, value, traceback):
subprocess.call(['ssh-agent', '-k'], env=self.environ)
class SSHInfo(object):
"""
Contains information about current SSH config setup.
Also contains methods for configuring/reconfiguring user SSH client
for use when new remote nodes are created.
"""
def __init__(self, **kwargs):
config_dir = kwargs.get('config_dir', os.path.expanduser('~/.ssh'))
log.debug(
'[%s] config_dir: %s',
self.__class__.__name__, config_dir
)
# Some default SSH client values
self.port = kwargs.get('port', 22)
self.wait_timeout = kwargs.get('wait_timeout', 60)
self.wait_interval = kwargs.get('wait_interval', 5)
self.wait_gap = kwargs.get('wait_gap', 3)
log.debug(
'[%s] port: %i',
self.__class__.__name__, self.port
)
log.debug(
'[%s] wait_timeout: %i',
self.__class__.__name__, self.wait_timeout
)
log.debug(
'[%s] wait_interval: %i',
self.__class__.__name__, self.wait_interval
)
log.debug(
'[%s] wait_gap: %i',
self.__class__.__name__, self.wait_gap
)
# Client SSH files location
self.config_file_path = kwargs.get(
'config_file_path', config_dir + '/config'
)
self.config_template_file_path = kwargs.get(
'config_template_file_path', config_dir + '/config.template'
)
self.id_rsa_file_path = kwargs.get(
'id_rsa_file_path', config_dir + '/id_rsa'
)
self.known_hosts_file_path = kwargs.get(
'known_hosts_file_path', config_dir + '/known_hosts'
)
log.debug(
'[%s] config_file_path: %s',
self.__class__.__name__, self.config_file_path
)
log.debug(
'[%s] config_template_file_path: %s',
self.__class__.__name__, self.config_template_file_path
)
log.debug(
'[%s] id_rsa_file_path: %s',
self.__class__.__name__, self.id_rsa_file_path
)
log.debug(
'[%s] known_hosts_file_path: %s',
self.__class__.__name__, self.known_hosts_file_path
)
# Used for the SSH config template (def: ~/.ssh/config.template)
self.host_keyword = kwargs.get('host_keyword', SSH_KEYWORD_HOST)
self.port_keyword = kwargs.get('port_keyword', SSH_KEYWORD_PORT)
self.comment_keyword = kwargs.get('comment_keyword', SSH_KEYWORD_COMMENT)
log.debug(
'[%s] host_keyword: %s',
self.__class__.__name__, self.host_keyword
)
log.debug(
'[%s] port_keyword: %s',
self.__class__.__name__, self.port_keyword
)
log.debug(
'[%s] comment_keyword: %s',
self.__class__.__name__, self.comment_keyword
)
def wait_for_ssh(self, ip_address):
"""
Wait for SSH port on IP address to be open and accessible.
"""
try:
with timeout(self.wait_timeout, exception=SystemError):
while True:
try:
log.info('Trying SSH to %s', ip_address)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with timeout(self.wait_interval, exception=RuntimeError):
s.connect((ip_address, self.port))
break;
except RuntimeError:
log.debug(
'(Socket timeout: %i second/s)',
self.wait_interval
)
pass
except socket.error as e:
log.debug('(Socket error: %s)', e)
pass
finally:
s.close()
log.debug('Waiting for %i second/s', self.wait_gap)
time.sleep(self.wait_gap)
except SystemError:
log.exception('SSH timeout error: %i second/s', self.wait_timeout)
raise
def generate_ssh_config(self, ip_address, comment='node deploy'):
"""
Add public RSA keys of IP address to current known hosts.
Generate new .ssh/config file.
"""
if os.path.exists(self.known_hosts_file_path):
log.debug(
'Removing %s from %s',
ip_address, self.known_hosts_file_path
)
subprocess.call([
'ssh-keygen', '-R', ip_address,
'-f', self.known_hosts_file_path
])
with open(self.known_hosts_file_path, 'a') as f:
log.debug(
'Adding %s to %s',
ip_address, self.known_hosts_file_path
)
subprocess.call(['ssh-keyscan', '-H', ip_address], stdout=f)
log.debug(
'Edit %s to pipe. Replace "%s" with "%s".',
self.config_template_file_path,
self.comment_keyword, comment
)
sedcomment = subprocess.Popen(
[
'sed',
's/{0}/{1}/'.format(self.comment_keyword, comment),
self.config_template_file_path
],
stdout=subprocess.PIPE
)
log.debug(
'Edit %s to pipe. Replace "%s" with "%s".',
self.config_template_file_path,
self.host_keyword, ip_address
)
sedhost = subprocess.Popen(
['sed', 's/{0}/{1}/'.format(self.host_keyword, ip_address)],
stdin=sedcomment.stdout,
stdout=subprocess.PIPE
)
log.debug(
'Edit %s to %s. Replace "%s" with "%s".',
self.config_template_file_path,
self.config_file_path,
self.port_keyword, self.port
)
with open(self.config_file_path, 'w') as f:
subprocess.call(
['sed', 's/{0}/{1}/'.format(self.port_keyword, self.port)],
stdin=sedhost.stdout,
stdout=f
)
log.debug('Waiting on OS sed pipes')
sedcomment.wait()
sedhost.wait()
class AWSDefault(object):
def __init__(self, **kwargs):
self.ident = kwargs.get('Ident', '')
self.profile_name = kwargs.get('ProfileName', None)
self.region = kwargs.get('Region', DEFAULT_REGION)
log.debug(
'[%s] profile_name: %s',
self.__class__.__name__, self.profile_name
)
log.debug(
'[%s] region: %s',
self.__class__.__name__, self.region
)
self.sess = boto3.session.Session(
region_name=self.region, profile_name=self.profile_name
)
self.ec2 = self.sess.resource('ec2')
self.asg = self.sess.client('autoscaling')
self.ecc = self.sess.client('ec2')
def _suff_id(self, string):
"""
Just suffix ident to a string if ident is not an empty string.
"""
if len(self.ident) > 0:
return string + '_' + self.ident
else:
return string
class AppNode(AWSDefault, Cleanable):
"""
The application server node. Plus the AMI, snapshot,
and Launch Templates involved.
"""
def __init__(self, **kwargs):
super(AppNode, self).__init__(**kwargs)
log.debug('Now using %s ident: %s',
self.__class__.__name__, self.ident
)
self.instance_id = None
self.ami_id = None
self.snapshot_id = None
# Mandatory args
self.subnet = kwargs.get('Subnet')
self.launch_template_id = kwargs.get('LaunchTemplateId')
log.debug(
'[%s] subnet: %s',
self.__class__.__name__, self.subnet
)
log.debug(
'[%s] launch_template_id: %s',
self.__class__.__name__, self.launch_template_id
)
# Optional args (with defaults)
self.launch_template_version = kwargs.get(
'LaunchTemplateVersion', DEFAULT_VER
)
self.ami_name = kwargs.get('AmiName', 'code_deploy')
self.ami_desc = kwargs.get(
'AmiDesc', 'Generated by cloud_deploy script'
)
self.ami_wait_timeout = kwargs.get('AmiWaitTimeout', 300)
self.ami_wait_interval = kwargs.get('AmiWaitInterval', 7)
log.debug(
'[%s] launch_template_version: %s',
self.__class__.__name__, self.launch_template_version
)
log.debug(
'[%s] ami_name: %s',
self.__class__.__name__, self.ami_name
)
log.debug(
'[%s] ami_desc: %s',
self.__class__.__name__, self.ami_desc
)
log.debug(
'[%s] ami_wait_timeout: %i',
self.__class__.__name__, self.ami_wait_timeout
)
log.debug(
'[%s] ami_wait_interval: %i',
self.__class__.__name__, self.ami_wait_interval
)
self.instance_id_file_path = kwargs.get(
'InstanceIdFilePath',
self._suff_id(TMP_DIR + '/cd-instance-id')
)
self.ami_id_file_path = kwargs.get(
'AmiIdFilePath',
self._suff_id(TMP_DIR + '/cd-ami-id')
)
self.target_lt_base_file_path = kwargs.get(
'TargetLTBaseFilePath',
self._suff_id(TMP_DIR + '/cd-target-lt')
)
def launch_ec2(self):
"""
Launch an EC2 instance from the given Launch Template and
return once the instance is at "running" state.
If there's already an instance running, use that instead.
Populates self.instance_id.
"""
try:
with open(self.instance_id_file_path) as f:
log.debug('EC2 id file found: %s', self.instance_id_file_path)
log.info('Existing EC2 found')
self.instance_id = f.read().strip()
except IOError:
try:
log.info('Launching new EC2 from template %s version %s',
self.launch_template_id,
self.launch_template_version
)
insts = self.ec2.create_instances(
MaxCount=1, MinCount=1, SubnetId=self.subnet,
LaunchTemplate={
'LaunchTemplateId': self.launch_template_id,
'Version': self.launch_template_version
}
)
self.instance_id = insts[0].id
log.debug(
'Writing EC2 id to file: %s',
self.instance_id_file_path
)
with open(self.instance_id_file_path, 'w') as f:
f.write(self.instance_id)
except ClientError:
log.exception('Error launching EC2')
raise
log.info('EC2 instance id: %s. Waiting to run.', self.instance_id)
self.ec2.Instance(self.instance_id).wait_until_running()
def generate_ami(self):
"""
Create an AMI of self.instance_id, and tag both AMI and its snapshot.
Populates self.ami_id and self.snapshot_id.
"""
self._create_image()
self._tag_ami_snaps()
def _create_image(self):
"""
Create an AMI from deployed node.
return once the AMI has been fully built.
If there's already an existing AMI, use that instead.
"""
if self.instance_id is None:
log.error('No EC2 instance_id found')
raise RuntimeError('No EC2 instance found')
try:
with open(self.ami_id_file_path) as f:
log.debug('AMI id file found: %s', self.ami_id_file_path)
log.info('Existing AMI found')
self.ami_id = f.read().strip()
except IOError:
try:
log.info('Creating AMI from instance %s', self.instance_id)
i = self.ec2.Instance(self.instance_id)
image = i.create_image(
BlockDeviceMappings=[self._get_block_device_mapping()],
Description=self.ami_desc,
Name=self._suff_id(self.ami_name)
)
self.ami_id = image.id
log.debug('Writing AMI id to file: %s', self.ami_id_file_path)
with open(self.ami_id_file_path, 'w') as f:
f.write(self.ami_id)
except ClientError:
log.exception('Error creating AMI')
raise
log.info('Image id: %s. Waiting to exist.', self.ami_id)
self.ec2.Image(self.ami_id).wait_until_exists()
try:
with timeout(self.ami_wait_timeout, exception=RuntimeError):
while True:
log.info('Waiting for AMI to be available')
ami_state = self.ec2.Image(self.ami_id).state
if ami_state == 'available': break
log.debug('AMI %s state: %s', self.ami_id, ami_state)
log.debug('Waiting for %i second/s', self.ami_wait_interval)
time.sleep(self.ami_wait_interval)
except RuntimeError:
log.exception(
'AMI completion timeout error: %i seconds/s',
self.ami_wait_timeout
)
raise
self.snapshot_id = (
self.ec2.Image(self.ami_id)
.block_device_mappings[0]['Ebs']['SnapshotId']
)
log.info('Obtained snapshot id: %s', self.snapshot_id)
def _get_block_device_mapping(self):
i = self.ec2.Instance(self.instance_id)
ebs = i.block_device_mappings[0]['Ebs']
vol = self.ec2.Volume(ebs['VolumeId'])
log.debug(
'(BDMap) DeviceName: %s',
i.block_device_mappings[0]['DeviceName']
)
log.debug(
'(BDMap) Ebs.DeleteOnTermination: %s',
ebs['DeleteOnTermination']
)
log.debug('(BDMap) Ebs.VolumeSize: %s', vol.size)
log.debug('(BDMap) Ebs.VolumeType: %s', vol.volume_type)
return {
'DeviceName': i.block_device_mappings[0]['DeviceName'],
'Ebs': {
'DeleteOnTermination': ebs['DeleteOnTermination'],
'VolumeSize': vol.size,
'VolumeType': vol.volume_type
}
}
def _tag_ami_snaps(self):
"""
Tag both snapshot and AMI with Name tags.
"""
name_val = self._suff_id(self.ami_name)
tags = [{
'Key': 'Name',
'Value': name_val
}]
log.debug('Tagging AMI with: Name => %s', name_val)
self.ec2.Image(self.ami_id).create_tags(Tags=tags)
log.debug('Tagging snapshot with: Name => %s', name_val)
self.ec2.Snapshot(self.snapshot_id).create_tags(Tags=tags)
def create_new_lt_version(
self, lt_id, lt_source_ver=DEFAULT_VER, lt_desc='deployed'
):
"""
Create a new version for the target 'deployed' launch template
basing off the given source version. The new version
will now contain the new AMI of the previously deployed EC2 server.
The new LT version will be made the $Default version.
"""
new_ver = None
new_ver_file_path = self.target_lt_base_file_path + '_' + lt_id
try:
with open(new_ver_file_path) as f:
log.debug('Version marker file found: %s', new_ver_file_path)
log.info('New version already created for LT: %s', lt_id)
new_ver = int(f.read().strip())
except IOError:
log.debug(
'Inserting snapshot id %s into block device mapping',
self.snapshot_id
)
bdmap = self._get_block_device_mapping()
bdmap['Ebs']['SnapshotId'] = self.snapshot_id
if len(self.ident) > 0:
lt_desc += ' ' + self.ident
try:
log.info('Creating new version for LT: %s', lt_id)
res = self.ecc.create_launch_template_version(
LaunchTemplateId=lt_id,
SourceVersion=lt_source_ver,
VersionDescription=lt_desc,
LaunchTemplateData={
'ImageId': self.ami_id,
'BlockDeviceMappings': [bdmap]
}
)
new_ver = res['LaunchTemplateVersion']['VersionNumber']
log.debug(
'Writing new LT version to file: %s',
new_ver_file_path
)
with open(new_ver_file_path, 'w') as f:
f.write(str(new_ver))
except ClientError:
log.exception('Error creating new launch template version')
raise
log.info('New launch template version: %i', new_ver)
try:
log.info('Updating designated %s version', DEFAULT_VER)
res = self.ecc.modify_launch_template(
LaunchTemplateId=lt_id,
DefaultVersion=str(new_ver)
)
if new_ver != res['LaunchTemplate']['DefaultVersionNumber']:
raise RuntimeError('Client result mismatch')
except ClientError:
log.exception('Error modifying launch template')
raise
def get_ip_address(self, address_type='private'):
if self.instance_id:
i = self.ec2.Instance(self.instance_id)
if address_type == 'private':
return i.private_ip_address
if address_type == 'public':
return i.public_ip_address
return None
def _get_removable_files(self):
return [
self.instance_id_file_path,
self.ami_id_file_path,
] + glob.glob(self.target_lt_base_file_path + '_*')
def _get_cleanup_funs(self):
return [
{
'fun': log.info,
'args': ['Terminating EC2 instance id: %s', self.instance_id],
'kwargs': {},
},
{
'fun': self.ec2.Instance(self.instance_id).terminate,
'args': [],
'kwargs': {},
},
]
class ASGroup(AWSDefault, Cleanable):
"""
The AWS Autoscaling Group. This class contains the
methods to perform and automated rolling termination deploy.
"""
def __init__(self, **kwargs):
super(ASGroup, self).__init__(**kwargs)
log.debug('Now using %s ident: %s',
self.__class__.__name__, self.ident
)
# Optional args (with defaults)
self.wait_interval = kwargs.get('WaitInterval', 5)
log.debug(
'[%s] wait_interval: %i',
self.__class__.__name__, self.wait_interval
)
self.props_file_path = kwargs.get(
'PropsFilePath',
self._suff_id(TMP_DIR + '/cd-asg-props')
)
self.ins_file_path = kwargs.get(
'InstancesFilePath',
self._suff_id(TMP_DIR + '/cd-asg-instances')
)
self.mark_file_path = kwargs.get(
'MarkFilePath',
self._suff_id(TMP_DIR + '/cd-asg-deployed')
)
def get_asg(self, asg_name):
"""
Get the details of an autoscaling group.
"""
try:
log.debug('Describe ASG details for %s', asg_name)
res = self.asg.describe_auto_scaling_groups(
AutoScalingGroupNames=[asg_name]
)
except ClientError:
log.exception('Error describing ASG %s', asg_name)
raise
asgs = res['AutoScalingGroups']
if len(asgs) != 1:
log.error(
'Number of results found for ASG %s: %i',
asg_name, len(asgs)
)
raise RuntimeError('Either ASG not found or found too many')
return asgs[0]
def get_ips_by_asg(self, asg_name, address_type='private'):
"""
Get the IP addresses of the instances currently under an ASG.
"""
asg = self.get_asg(asg_name)
ips = []
for i in asg['Instances']:
ins = self.ec2.Instance(i['InstanceId'])
if address_type == 'private':
ips.append(ins.private_ip_address)
elif address_type == 'public':
ips.append(ins.public_ip_address)
return ips
def rolling_termination_deploy(self, asg_name):
"""
Perform a rolling termination deploy for the ASG.
This process makes sure there is no downtime when
it comes to "InService" instances.
"""
asg = self.get_asg(asg_name)
if asg['DesiredCapacity'] == 0:
log.info('ASG DesiredCapacity is 0. Rolling deploy not needed.')
return True
# Data-gathering
minsize, maxsize, dessize, grace_prd = self._get_original_props(asg)
defunct_ins = self._get_defunct_instances(asg)
# In case we are resuming from a previous
# interrupted operation
all_ins = [i['InstanceId'] for i in asg['Instances']]
waiting_ins = list(set(all_ins) - set(defunct_ins))
if len(waiting_ins) > 0:
log.info(
'Pending instances before deployment:\n %s',
'\n '.join(waiting_ins)
)
self._wait_for_instances(waiting_ins, grace_prd)
# Adjust ASG sizes if they are too low
if asg['MinSize'] < ASG_MIN_MINSIZE:
if asg['MaxSize'] < ASG_MIN_MAXSIZE:
self._increase_to_floor_size(asg_name, increase_max=True)
else:
self._increase_to_floor_size(asg_name)
log.info('ASG size adjusted. Waiting for autoscaling events.')
self._wait_for_instances(
self._get_pending_instances(asg_name, grace_prd),
grace_prd
)
# Actual rolling termination deployment.
# But instead of termination, the instances are simply detached.
mark_file = self.mark_file_path + '_' + asg_name
try:
with open(mark_file) as f:
log.debug('ASG deploy mark file found: %s', mark_file)
stamp = f.read().strip()
log.info(
'Rolling deploy for ASG %s (%s) already completed on %s',
asg_name, self.ident, stamp
)
except IOError:
for i in defunct_ins:
is_attached = False
is_detach_ok = False
log.info('Removing old instance %s', i)
try:
log.debug('Lookup instance %s', i)
res = self.asg.describe_auto_scaling_instances(
InstanceIds=[i]
)
if len(res['AutoScalingInstances']) == 1:
is_attached = True
except ClientError as e:
log.warning('Error on instance %s lookup: %s', i, e)
if is_attached:
try:
log.debug('Detach instance %s', i)
self.asg.detach_instances(
AutoScalingGroupName=asg_name,
InstanceIds=[i],
ShouldDecrementDesiredCapacity=False
)
is_detach_ok = True
except ClientError as e:
log.warning('Error detaching instance %s: %s', i, e)
if is_detach_ok:
log.info('Waiting for replacement')
self._wait_for_instances(
self._get_pending_instances(asg_name, grace_prd),
grace_prd
)
# Terminate all defunct instances at once
for i in defunct_ins:
try:
log.debug('Terminate instance %s', i)
self.ec2.Instance(i).terminate()
except ClientError as e:
log.warning('Error terminating instance %s: %s', i, e)
# ASG deploy idempotency based on asg's name and deploy ident
stamp = datetime.now(pytz.utc).strftime('%Y-%b-%d %I:%M%p %Z')
log.info('%s - ASG %s rolling deploy done', stamp, asg_name)
log.debug('Marking ASG deploy as done: %s', mark_file)
with open(mark_file, 'w') as f:
f.write(stamp)
# Get penultimate state of ASG data
minsize_new = None
try:
log.debug('Describe ASG details for %s', asg_name)
res = self.asg.describe_auto_scaling_groups(
AutoScalingGroupNames=[asg_name]
)
minsize_new = res['AutoScalingGroups'][0]['MinSize']
except ClientError:
log.exception('Error describing ASG %s', asg_name)
raise
# Restore ASG sizes if they were modified
if minsize != minsize_new:
self._set_asg_size(asg_name, minsize, maxsize, dessize)
def _get_defunct_instances(self, asg):
"""
Get all currently healthy instances under an ASG.
Presumably, they will be terminated later on as part
of the rolling deploy.
If instances have already been determined in this way
through a previous operation, do not do it again.
"""
asg_name = asg['AutoScalingGroupName']
instances = asg['Instances']
ins_file = self.ins_file_path + '_' + asg_name
try:
with open(ins_file) as f:
log.debug('ASG instances file found: %s', ins_file)
log.info(
'Defunct ASG instances already obtained: %s', asg_name
)
defunct_ins = [s.strip() for s in f.readlines()]
except IOError:
defunct_ins = []
for i in instances:
if ( i['HealthStatus'] == 'Healthy' and
i['LifecycleState'] == 'InService' ):
defunct_ins.append(i['InstanceId'])
log.debug('Writing defunct ids to file: %s', ins_file)
with open(ins_file, 'w') as f:
f.writelines([s + '\n' for s in defunct_ins])
if len(defunct_ins) > 0:
log.info(
'Instances marked for removal:\n %s',
'\n '.join(defunct_ins)
)
return defunct_ins
def _get_original_props(self, asg):
"""
Obtain the original MinSize, MaxSize, and DesiredCapacity of ASG.
Also gets the ASG's HealthCheckGracePeriod.
"""
asg_name = asg['AutoScalingGroupName']
props_file = self.props_file_path + '_' + asg_name
try:
with open(props_file) as f:
log.debug('ASG properties file found: %s', props_file)
log.info('ASG sizes already obtained: %s', asg_name)
for prop in f.readlines():
x = prop.strip().split('=')
if x[0] == 'MinSize': minsize = int(x[1])
if x[0] == 'MaxSize': maxsize = int(x[1])
if x[0] == 'DesiredCapacity': dessize = int(x[1])
except IOError:
minsize = asg['MinSize']
maxsize = asg['MaxSize']
dessize = asg['DesiredCapacity']
log.debug(
'Writing original ASG properties to file: %s', props_file
)
props = [
'MinSize={}'.format(minsize),
'MaxSize={}'.format(maxsize),
'DesiredCapacity={}'.format(dessize),
]
with open(props_file, 'w') as f:
f.writelines([prop + '\n' for prop in props])
log.info(
'ASG %s original sizes:\n' +
' MinSize: %i\n MaxSize: %i\n DesiredCapacity: %i',
asg_name, minsize, maxsize, dessize
)
grace_prd = DEFAULT_ASG_WAIT
if asg['HealthCheckGracePeriod'] > 0:
grace_prd = asg['HealthCheckGracePeriod']
log.info('Internal wait timeout: %i', grace_prd)
return minsize, maxsize, dessize, grace_prd
def _get_pending_instances(self, asg_name, timelimit):
"""
Poll the ASG for any instances that are in a 'pending'
run state, indicating that they have just been created.
Return as soon as 1 or more pending instances are found in a poll.
Throw an exception if timeout is reached and no
pending instances are found.
"""
iids = []
try:
with timeout(timelimit, exception=RuntimeError):
while len(iids) == 0:
log.info('Polling for new ASG instances')
try:
log.debug('Describe ASG details for %s', asg_name)
res = self.asg.describe_auto_scaling_groups(
AutoScalingGroupNames=[asg_name]
)
for i in res['AutoScalingGroups'][0]['Instances']:
ins = self.ec2.Instance(i['InstanceId'])
if ins.state['Code'] == STATE_PENDING:
iids.append(i['InstanceId'])
except ClientError as e:
log.warning('Error describing ASG %s: %s', asg_name, e)
if len(iids) == 0:
log.debug('Waiting %i second/s', self.wait_interval)
time.sleep(self.wait_interval)
except RuntimeError:
log.exception(
'[%s] Timeout waiting for new instances: %i second/s',
self.__class__.__name__, timelimit
)
raise
return iids
def _wait_for_instances(self, iids, timelimit):
"""
Wait for instances to be available and InService under an ASG.
Time out after a certain period of time.
"""
for iid in iids:
log.info('Waiting for instance %s to be running', iid)
i = self.ec2.Instance(iid)
i.wait_until_running()
try:
with timeout(timelimit, exception=RuntimeError):
while len(iids) > 0:
try:
log.debug('Describe ASG instances')
res = self.asg.describe_auto_scaling_instances(
InstanceIds=iids
)
for i in res['AutoScalingInstances']:
if ( i['HealthStatus'] == 'HEALTHY' and
i['LifecycleState'] == 'InService' ):
iids.remove(i['InstanceId'])
else:
log.info(
'Waiting for instance %s to be InService',
i['InstanceId']
)
except ClientError as e:
log.warning(
'Error enumerating ASG instances: %s', e
)
if len(iids) > 0:
log.debug('Waiting %i second/s', self.wait_interval)
time.sleep(self.wait_interval)
except RuntimeError:
log.exception(
'[%s] Timeout waiting for instances: %i second/s',
self.__class__.__name__, timelimit
)
raise
def _increase_to_floor_size(self, asg_name, increase_max=False):
"""
Increase the MinSize and MaxSize to minimum values for rolling deploy.
"""
args = {
'AutoScalingGroupName': asg_name,
'MinSize': ASG_MIN_MINSIZE,
}
log.info(
'Temporarily increasing ASG %s MinSize to %i',
asg_name, ASG_MIN_MINSIZE
)
if increase_max:
args['MaxSize'] = ASG_MIN_MAXSIZE
log.info(
'Temporarily increasing ASG %s MaxSize to %i',
asg_name, ASG_MIN_MAXSIZE
)
try:
self.asg.update_auto_scaling_group(**args)
except ClientError:
log.exception('Error updating ASG settings')
raise
def _set_asg_size(self, asg_name, minsize, maxsize, dessize):
"""
Just set ASG's MinSize and MaxSize.
"""
log.info(
'Setting ASG %s MinSize (%i), MaxSize (%i), DesiredCapacity (%i)',
asg_name, minsize, maxsize, dessize
)
try:
self.asg.update_auto_scaling_group(
AutoScalingGroupName=asg_name,
MinSize=minsize,
MaxSize=maxsize,
DesiredCapacity=dessize
)
except ClientError:
log.exception('Error updating ASG settings')
raise
def _get_removable_files(self):
return glob.glob(self.props_file_path + '_*') \
+ glob.glob(self.ins_file_path + '_*') \
+ glob.glob(self.mark_file_path + '_*')
def main():
pr = argparse.ArgumentParser(
description=get_desc(), formatter_class=argparse.RawTextHelpFormatter
)
ga = pr.add_argument_group('Actions')
ga.add_argument(
'-V', '--version', action='version',
version='Cloud deploy v{}'.format(CDEP_VERSION)
)
ga.add_argument(
'--full-deploy', action='store_true',
help='Execute app deployment process. This is the default action.'
)
ga.add_argument(
'--rolling-termination', action='store_true',
help='Skip app deploy. Execute only rolling termination on ASGs.'
)
ga.add_argument(
'--get-asg-ips', action='store_true',
help='Get current IP addresses by ASG and exit'
)
gm = pr.add_argument_group('Modifiers')
gm.add_argument(
'-i', '--ident', help='Deployment identifier'
)
gm.add_argument(
'-v', '--verbose', action='store_true', help='Increase verbosity'
)
awso = pr.add_argument_group('AWS options')
awso.add_argument(
'-s', '--aws-subnet',
help='VPC subnet id. Required for full deploy.'
)
awso.add_argument(
'-l', '--aws-launch-template',
help='Source LT id. Required for full deploy.'
)
awso.add_argument(
'-p', '--aws-profile', help='Local IAM credentials (None)'
)
awso.add_argument(
'-r', '--aws-region', help='(us-east-1)'
)
awso.add_argument(
'--aws-launch-template-ver',
type=int, help='Source LT version ($Default)'
)
awso.add_argument(
'-L', '--aws-target-launch-template', nargs='*',
help='Target LT ids (None)'
)
awso.add_argument(
'--aws-target-launch-template-ver',
type=int, help='Target LT base version ($Default)'
)
awso.add_argument(
'-Y', '--aws-target-launch-template-desc',
help='Target LT description (deployed)'
)
awso.add_argument(
'-a', '--aws-autoscaling-group',
nargs='*', help='Autoscaling group names (None)'
)
awso.add_argument(
'-A', '--aws-ami-name', help='Generated AMI name (code_deploy)'
)
awso.add_argument(
'-X', '--aws-ami-desc', help='Generated AMI description (<boilerplate>)'
)
awso.add_argument(
'--aws-ami-wait-timeout',
type=int, help='Timeout for waiting on AMI (300)'
)
awso.add_argument(
'--aws-ami-wait-interval', type=int, help='Interval between checks (7)'
)
awso.add_argument(
'--aws-asg-wait-interval', type=int, help='Interval between checks (5)'
)
awso.add_argument(
'--aws-mark-ec2-file',
help='Marker file ({}/cd-instance-id)'.format(TMP_DIR)
)
awso.add_argument(
'--aws-mark-ami-file',
help='Marker file ({}/cd-ami-id)'.format(TMP_DIR)
)
awso.add_argument(
'--aws-mark-lt-file',
help='Marker file ({}/cd-target-lt)'.format(TMP_DIR)
)
awso.add_argument(
'--aws-mark-props-file',
help='Marker file ({}/cd-asg-props)'.format(TMP_DIR)
)
awso.add_argument(
'--aws-mark-instances-file',
help='Marker file ({}/cd-asg-instances)'.format(TMP_DIR)
)
awso.add_argument(
'--aws-mark-rolling-file',
help='Marker file ({}/cd-asg-rolling)'.format(TMP_DIR)
)
appo = pr.add_argument_group('App deployment options')
appo.add_argument(
'-d', '--app-project-dir', help='Location of project code (<cwd>)'
)
appo.add_argument(
'-e', '--app-cap-env', help='Capistrano env (production)'
)
appo.add_argument(
'-H', '--app-halt-after',
action='store_true', help='Halt script after application deploy'
)
appo.add_argument(
'--app-mark-file',
help='Marker file ({}/cd-app-deployed)'.format(TMP_DIR)
)
ssho = pr.add_argument_group('SSH options')
ssho.add_argument(
'--ssh-public', action='store_true', help='Target is a public IP'
)
ssho.add_argument(
'--ssh-port', type=int, help='Target SSH port (22)'
)
ssho.add_argument(
'--ssh-wait-timeout',
type=int, help='Overall timeout for SSH connection (60)'
)
ssho.add_argument(
'--ssh-wait-interval', type=int, help='Timeout for each SSH attempt (5)'
)
ssho.add_argument(
'--ssh-wait-gap', type=int, help='Wait time between SSH attempts (3)'
)
sshco = pr.add_argument_group('SSH client config options')
sshco.add_argument(
'--ssh-config-dir', help='Client SSH dir (~/.ssh)'
)
sshco.add_argument(
'--ssh-config-file', help='(<config_dir>/config)'
)
sshco.add_argument(
'--ssh-config-template-file', help='(<config_dir>/config.template)'
)
sshco.add_argument(
'--ssh-id-rsa-file', help='(<config_dir>/id_rsa)'
)
sshco.add_argument(
'--ssh-known-hosts-file', help='(<config_dir>/known_hosts)'
)
sshto = pr.add_argument_group('SSH config template options')
sshto.add_argument(
'--ssh-host-keyword', help='Replaceable keyword for Host (TARGET_IP)'
)
sshto.add_argument(
'--ssh-port-keyword', help='Replaceable keyword for Port (TARGET_PORT)'
)
sshto.add_argument(
'--ssh-comment-keyword',
help='Replaceable keyword as comments (TARGET_COMMENT)'
)
ar = pr.parse_args()
# Output verbosity settings
loglevel = log.INFO
if ar.verbose:
loglevel = log.DEBUG
log.basicConfig(
datefmt='%y-%m-%d %H:%M:%S',
format='%(asctime)s %(levelname)s:%(message)s',
level=loglevel,
)
cleaner = Cleaner()
# Target autoscaling groups
target_asgs = []
if ar.aws_autoscaling_group:
target_asgs = ar.aws_autoscaling_group
# AWS settings for ASGroup
asg_opts = options_asgroup(ar)
asgroup = ASGroup(**asg_opts)
# IP address type (relative to VPC)
add_type = 'private'
if ar.ssh_public:
add_type = 'public'
is_default_action = (not any([
ar.rolling_termination,
ar.get_asg_ips
])) or ar.full_deploy
### Action (default): Rolling Application Deploy ###
if is_default_action:
if not all([ar.aws_subnet, ar.aws_launch_template]):
pr.error('Missing argument/s: aws-subnet, aws-launch-template')
# SSH client settings
si_opts = options_sshinfo(ar)
ssh_info = SSHInfo(**si_opts)
# Capistrano application deployer settings
#ad_opts = options_capdeployer(ar)
#ad_opts['ssh'] = ssh_info
#cap = CapDeployer(**ad_opts)
# Test deployer
topts = {}
topts['ssh'] = ssh_info
td = TestDeployer(**topts)
# AWS settings for AppNode
aws_opts = options_appnode(ar)
aws_opts['Subnet'] = ar.aws_subnet
aws_opts['LaunchTemplateId'] = ar.aws_launch_template
node = AppNode(**aws_opts)
ident = 'none'
if ar.ident:
ident = ar.ident
log.info('Now using deploy identifier: %s', ident)
# Begin creating a new target EC2 node
node.launch_ec2()
log.info('New EC2 instance launched from Launch Template')
# Wait for the new EC2 server, then begin
# deploying the application (with Capistrano)
node_ip = node.get_ip_address(address_type=add_type)
if node_ip:
ssh_info.wait_for_ssh(node_ip)
ssh_info.generate_ssh_config(
node_ip, comment='Deployment Tag: {}'.format(ident)
)
deploy_ident = ident + '_ip' + node_ip
#cap.deploy(deploy_ident)
td.deploy(deploy_ident)
log.info('Application deployed to EC2 instance')
# If directed, stop the script
if ar.app_halt_after: non_graceful_halt()
# Begin creating a new AMI out of the EC2 node
node.generate_ami()
log.info('AMI generated from EC2 instance')
# Calculate target launch templates
target_lts = []
arg_lt_desc = ar.aws_target_launch_template_desc
if ar.aws_target_launch_template:
# If target LTs are provided, modify LTs.
# Deploy may proceed depending on ASGs provided.
for lt in ar.aws_target_launch_template:
x = { 'lt_id': lt }
if ar.aws_target_launch_template_ver:
x['lt_source_ver'] = ar.aws_target_launch_template_ver
if arg_lt_desc:
x['lt_desc'] = arg_lt_desc
target_lts.append(x)
else:
# If NO target LTs are provided, infer LTs
# from ASGs if ASGs are provided.
for asg_name in target_asgs:
asg = asgroup.get_asg(asg_name)
if asg['LaunchTemplate']:
xid = asg['LaunchTemplate']['LaunchTemplateId']
xver = asg['LaunchTemplate']['Version']
x = {
'lt_id': xid,
'lt_source_ver': xver,
}
if arg_lt_desc:
x['lt_desc'] = arg_lt_desc
log.info(
'Obtained target LT %s ver %s from ASG %s',
xid, xver, asg_name
)
target_lts.append(x)
# Create new versions of the Launch Templates.
# New versions will now use the generated AMI.
for lt_args in target_lts:
node.create_new_lt_version(**lt_args)
log.info('Target Launch Template modified to use new AMI')
if len(target_asgs) > 0:
log.info('Proceeding to termination deploy of autoscaling groups')
cleaner.mark(node)
#cleaner.mark(cap)
cleaner.mark(td)
### Action: Autoscaling Groups rolling termination ###
if is_default_action or ar.rolling_termination:
for asg_name in target_asgs:
asgroup.rolling_termination_deploy(asg_name)
log.info('Rolling deploy done for %s', asg_name)
### Action: Get IP addresses ###
if ar.get_asg_ips:
for asg_name in target_asgs:
print(('[{}]'.format(asg_name)))
for ip in asgroup.get_ips_by_asg(asg_name, address_type=add_type):
print(ip)
cleaner.mark(asgroup)
cleaner.cleanup()
sys.exit(0)
def non_graceful_halt():
log.info(
'---------------\n'
'Script execution paused by user argument.'
'You may SSH into the deploy target instance and '
'perform commands, and/or continue the process later.'
'\n\nTo resume execution, simply repeat the deploy command '
'without the "-H" or "--app-halt-after" flag.\n'
'---------------'
)
sys.exit(0)
def get_desc():
return (
'DESCRIPTION'
'\n\nPerform a rolling termination deployment on an AWS autoscaling '
'group. Works best under the following conditions:'
'\n\n- The autoscaling group (ASG) uses Launch Templates, instead of '
'Launch Configurations.'
'\n- The ASG uses the $Default version of its Launch Template.'
'\n- At the start of deploy, when a fresh EC2 is created, the script '
'will ask for a Launch Template, not an AMI directly.'
'\n\nPROCESS'
'\n\nGiven the source launch template ID, target launch template ID/s, '
'target autoscaling group name/s, and a Ruby on Rails app project '
'directory, this script will:'
'\n\n1) Create a temporary EC2 instance from source launch template.'
'\n2) Perform a Capistrano deploy on that instance. (Other app '
'deployment methods will be added in the future.)'
'\n3) Generate an AMI from that instance.'
'\n4) Create a new version of the target launch template/s which '
'now uses the new AMI ID obtained in (3).'
'\n5) Do a rolling termination on the existing instances of the '
'target autoscaling group/s, allowing them to be automatically '
'replaced by the ASG as they are being removed one by one.'
'\n6) Terminate the temporary instance created in (1).'
'\n\nCLIENT SSH CONFIG TEMPLATING'
'\n\nIn order to SSH to the new EC2 instance, this script utilizes '
'a template for the client SSH config (by default, it is located at '
'~/.ssh/config.template). This template contains a Host stub with '
'a designated keyword in place of an IP address. When the new EC2 '
'is created, the template is used to create a new client SSH config '
"file (~/.ssh/config), with the keyword replaced by the instance's "
"actual IP address. The project's Capistrano deploy configuration "
'should be made to reflect this setup.'
'\n\nEXAMPLE USAGE'
'\n\n$ ./cdep.py -s subnet-123abc -l lt-abc123xyz '
"-A myapp-ami -X 'Myapp server image' -Y 'Myapp LT' "
'-a mygroup-asg -d /home/john/myproj -i v1.0.0 -H'
'\n\nThe command above creates a new instance from launch template '
'lt-abc123xyz, then does a Capistrano production deploy to it. After '
'deployment, the script is halted so the user can SSH to the instance '
'and perform additional operations, if desired.'
'\n\nWhen ready, the command should be repeated without the -H flag. '
'If so, the deploy continues by creating an AMI from the finalized '
'instance. The target launch templates, inferred from the given '
'autoscaling group mygroup-asg, is updated to '
'use this new AMI. Finally, existing mygroup-asg instances are removed '
'one by one for auto-replacement to trigger with new instances.'
'\n\nFILES'
'\n\n/tmp/cd-*'
'\n~/.ssh/config.template'
'\n~/.ssh/config'
)
def options_sshinfo(args):
x = {}
if args.ssh_port:
x['port'] = args.ssh_port
if args.ssh_wait_timeout:
x['wait_timeout'] = args.ssh_wait_timeout
if args.ssh_wait_interval:
x['wait_interval'] = args.ssh_wait_interval
if args.ssh_wait_gap:
x['wait_gap'] = args.ssh_wait_gap
if args.ssh_host_keyword:
x['host_keyword'] = args.ssh_host_keyword
if args.ssh_port_keyword:
x['port_keyword'] = args.ssh_port_keyword
if args.ssh_comment_keyword:
x['comment_keyword'] = args.ssh_comment_keyword
if args.ssh_config_dir:
x['config_dir'] = args.ssh_config_dir
if args.ssh_config_file:
x['config_file_path'] = args.ssh_config_file
if args.ssh_config_template_file:
x['config_template_file_path'] = args.ssh_config_template_file
if args.ssh_id_rsa_file:
x['id_rsa_file_path'] = args.ssh_id_rsa_file
if args.ssh_known_hosts_file:
x['known_hosts_file_path'] = args.ssh_known_hosts_file
return x
def options_capdeployer(args):
x = {}
if args.app_project_dir:
x['app_project_dir'] = args.app_project_dir
if args.app_cap_env:
x['cap_env'] = args.app_cap_env
if args.app_mark_file:
x['mark_file_path'] = args.app_mark_file
return x
def options_appnode(args):
x = {}
if args.ident:
x['Ident'] = args.ident
if args.aws_profile:
x['ProfileName'] = args.aws_profile
if args.aws_region:
x['Region'] = args.aws_region
if args.aws_launch_template_ver:
x['LaunchTemplateVersion'] = args.aws_launch_template_ver
if args.aws_ami_name:
x['AmiName'] = args.aws_ami_name
if args.aws_ami_desc:
x['AmiDesc'] = args.aws_ami_desc
if args.aws_ami_wait_timeout:
x['AmiWaitTimeout'] = args.aws_ami_wait_timeout
if args.aws_ami_wait_interval:
x['AmiWaitInterval'] = args.aws_ami_wait_interval
if args.aws_mark_ec2_file:
x['InstanceIdFilePath'] = args.aws_mark_ec2_file
if args.aws_mark_ami_file:
x['AmiIdFilePath'] = args.aws_mark_ami_file
if args.aws_mark_lt_file:
x['TargetLTBaseFilePath'] = args.aws_mark_lt_file
return x
def options_asgroup(args):
x = {}
if args.ident:
x['Ident'] = args.ident
if args.aws_profile:
x['ProfileName'] = args.aws_profile
if args.aws_region:
x['Region'] = args.aws_region
if args.aws_ami_wait_interval:
x['WaitInterval'] = args.aws_ami_wait_interval
if args.aws_mark_props_file:
x['PropsFilePath'] = args.aws_mark_props_file
if args.aws_mark_instances_file:
x['InstancesFilePath'] = args.aws_mark_instances_file
if args.aws_mark_rolling_file:
x['MarkFilePath'] = args.aws_mark_rolling_file
return x
if __name__ == '__main__':
main()