-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
1083 lines (901 loc) · 37.3 KB
/
manage.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
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
import datetime
import dateutil.parser
import enum
import functools
import itertools
import json
import os
import re
import socket
import sys
import textwrap
import time
from argparse import _StoreAction, REMAINDER
from pathlib import Path
from urllib.parse import urljoin
import requests
from argcmdr import Local, LocalRoot, localmethod
from descriptors import cachedproperty, cachedclassproperty
from plumbum.commands import ExecutionModifier
from terminaltables import AsciiTable
ROOT_PATH = Path(__file__).parent.resolve()
SRC_PATH = ROOT_PATH / 'src'
class PathPrefixSession(requests.Session):
def __init__(self, prefix_url=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.prefix_url = prefix_url
def request(self, method, url, *args, **kwargs):
# NOTE: urljoin can be kind of annoying/surprising in its operation
# https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin
url = urljoin(self.prefix_url, url)
return super().request(method, url, *args, **kwargs)
def get(self, url='', *args, **kwargs):
return super().get(url, *args, **kwargs)
def post(self, url='', *args, **kwargs):
return super().post(url, *args, **kwargs)
def delete(self, url='', *args, **kwargs):
return super().delete(url, *args, **kwargs)
def spincycle(chars='|/–\\', file=sys.stdout, wait=1):
"""A generator which writes a "spinner" to file, cycling through
the given characters, with the given wait between writes.
The last written character is yielded to the iterator, such that a
polling procedure may be executed with the given wait, while the
file (stdout) is updated.
"""
for char in itertools.cycle(chars):
file.write(char)
file.flush()
yield char
time.sleep(wait)
file.write('\b')
class EnvDefault(str):
pass
def store_env_override(option_strings,
dest,
envvar,
nargs=None,
default=None,
type=None,
choices=None,
description=None,
addendum=None,
help=None,
metavar=None):
"""Construct an argparse action which stores the value of a command
line option to override a corresponding value in the process
environment.
If the environment variable is not empty, then no override is
required. If the environment variable is empty, and no default is
provided, then the "option" is required.
In the case of a default value which is a *transformation* of the
single environment variable, this default may be provided as a
callable, (*e.g.* as a lambda function).
Rather than have to fully explain the relationship of this
environment-backed option, help text may be generated from a
provided description. (And an addendum may be optionally appended to
the end of the generated text.)
To aide in the differentiation of whether the resulting value
originated from the command line or the process environment, the
environment-derived default (and its optional transformation) are
wrapped in the ``str`` subclass: ``EnvDefault``.
"""
if envvar == '':
raise ValueError("unsupported environment variable name", envvar)
envvalue = os.getenv(envvar)
if callable(default):
default_value = EnvDefault(default(envvalue))
elif envvalue:
default_value = EnvDefault(envvalue)
else:
default_value = default
if description and help:
raise ValueError(
"only specify help to override its optional generation from "
"description -- not both"
)
elif description:
if default_value:
help = '{} (default {} envvar {}: {})'.format(
description,
'provided by' if default is None else 'derived from',
envvar,
default_value,
)
if addendum:
help += f' {addendum}'
else:
help = (f'{description} (required because '
f'envvar {envvar} is empty)')
elif addendum:
raise ValueError("addendum intended for use in conjunction with description")
return _StoreAction(
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=None,
default=default_value,
type=type,
choices=choices,
required=(not default_value),
help=help,
metavar=metavar,
)
class _SHH(ExecutionModifier):
"""plumbum execution modifier to ensure output is not echoed to terminal
essentially a no-op, this may be used to override argcmdr settings
and cli flags controlling this feature, on a line-by-line basis, to
hide unnecessary or problematic (e.g. highly verbose) command output.
"""
__slots__ = ('retcode', 'timeout')
def __init__(self, retcode=0, timeout=None):
self.retcode = retcode
self.timeout = timeout
def __rand__(self, cmd):
return cmd.run(retcode=self.retcode, timeout=self.timeout)
SHH = _SHH()
class EnvironmentMixin:
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'target',
choices=('staging', 'production',),
help="target environment",
)
class ClusterServiceMixin:
environmental = False
@cachedclassproperty
def _environmental(cls):
return cls.environmental or issubclass(cls, EnvironmentMixin)
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'--cluster',
action=store_env_override,
envvar='ECS_CLUSTER_NAME',
description="short name or full Amazon Resource Name (ARN) "
"of the cluster that your service is running on",
addendum='(default extended for staging with suffix: -staging)'
if self._environmental else None,
)
parser.add_argument(
'--service',
action=store_env_override,
envvar='ECS_SERVICE_NAME',
description="name of the service to update",
addendum='(default extended for staging with suffix: -staging)'
if self._environmental else None,
)
parser.set_defaults(
resolve_cluster=functools.lru_cache()(self.resolve_cluster),
resolve_service=functools.lru_cache()(self.resolve_service),
)
def resolve_cluster(self):
if (
self._environmental and
self.args.target == 'staging' and
isinstance(self.args.cluster, EnvDefault)
):
return self.args.cluster + '-staging'
return self.args.cluster
def resolve_service(self):
if (
self._environmental and
self.args.target == 'staging' and
isinstance(self.args.service, EnvDefault)
):
return self.args.service + '-staging'
return self.args.service
def describe_service(self, suppress=True):
modifier = SHH if suppress else None
(_retcode, stdout, _stderr) = yield modifier, self.local['aws'][
'ecs',
'describe-services',
'--cluster', self.args.resolve_cluster(),
'--services', self.args.resolve_service(),
]
if stdout is None:
return None
result = json.loads(stdout)
(service,) = result['services']
return service
class DeploymentMixin:
GITHUB_DEPLOYMENT_API = 'https://api.github.com/repos/{REPO_SPEC}/deployments'
class BearerAuth(requests.auth.AuthBase):
"""Attaches Bearer Authentication to Requests."""
username = '<no-user>'
def __init__(self, token):
self.token = token
def __call__(self, request):
request.headers['Authorization'] = f"Bearer {self.token}"
return request
class TagSpec(enum.Enum):
staging = re.compile(r'^\d+\.\d+.*$', re.M)
production = re.compile(r'^\d+\.\d+\.\d+$', re.M)
__delegates__ = {'pattern', 'search'}
def __getattr__(self, name):
if name in self.__delegates__:
return getattr(self.value, name)
return super().__getattr__(name)
@cachedproperty
def github_auth(self):
github_auth = os.getenv('GITHUB_AUTH', '')
if not github_auth:
raise LookupError(
"GitHub API credentials not found under environment variable GITHUB_AUTH "
"({USERNAME}:{TOKEN} for Basic or {TOKEN} for Bearer authentication)"
)
try:
(username, token) = github_auth.split(':', 1)
except ValueError:
return self.BearerAuth(github_auth)
else:
return requests.auth.HTTPBasicAuth(username, token)
@cachedproperty
def github_deployments(self):
session = PathPrefixSession(self.GITHUB_DEPLOYMENT_API
.format(REPO_SPEC=self.args.github_repo))
session.auth = self.github_auth
# https://docs.github.com/en/rest/reference/repos#deployments
# https://developer.github.com/v3/previews/#deployment-statuses
# NOTE: required for "in_progress" status -- application/vnd.github.flash-preview+json
# session.headers.update({'Accept': 'application/vnd.github+json'})
# session.headers.update({'Accept': 'application/vnd.github.v3+json'})
# session.headers.update({'Accept': 'application/vnd.github.ant-man-preview+json'})
session.headers.update({'Accept': 'application/vnd.github.flash-preview+json'})
# session.headers.update({'Accept': 'application/vnd.github.v3+json, '
# 'application/vnd.github.flash-preview+json'})
return session
def check_latest(self):
if not self.args.if_latest:
return
tag_spec = self.TagSpec[self.args.target]
label = self.args.label[0] if self.args.label else None
if not label or not tag_spec.search(label):
self.args.__parser__.error(
f"a version label matching the pattern /{tag_spec.pattern}/ is "
f"required for {self.args.target} when flag --if-latest is provided"
)
(_retcode, stdout, _stderr) = yield SHH, self.local['git'][
'tag',
'--sort=version:refname',
]
tags = stdout.splitlines() if stdout is not None else ()
for latest_tag in reversed(tags):
if tag_spec.search(latest_tag):
if label != latest_tag:
self.args.__parser__.error(
f"the provided version label {label} is not the latest {latest_tag}"
)
break
def get_record_description(self, action):
return (f'{action} initiated by {__file__} on behalf of '
f'{self.github_auth.username} from {socket.gethostname()}')
def record_deployment(self, deployment_id=None, status='pending', current_action='BUILD'):
if self.args.record_deployment and not self.args.label:
self.args.__parser__.error('version label required to record deployment '
'(try --no-record to skip)')
elif self.args.label and not self.args.record_deployment:
print('warning: deployment of',
self.args.label[0],
'to',
self.args.target,
'will NOT be recorded')
elif self.args.label and self.args.record_deployment:
# check for an extant deployment record
if deployment_id:
response = self.github_deployments.get(f'deployments/{deployment_id}')
response.raise_for_status()
deployment = response.json()
else:
response = self.github_deployments.get(params={
'ref': self.args.label[0],
'environment': self.args.target,
})
response.raise_for_status()
try:
(deployment,) = response.json()
except ValueError:
# record a new deployment (pending)
if self.args.execute_commands:
response = self.github_deployments.post(json={
'ref': self.args.label[0],
'auto_merge': False,
'environment': self.args.target,
'description': self.get_record_description('DEPLOYMENT'),
})
response.raise_for_status()
deployment = response.json()
else:
deployment = None
else:
if self.args.show_warnings:
self.args.__parser__.error(
f"deployment record for {self.args.label[0]} already exists "
f"(try --force to override): {deployment['url']}"
)
if deployment:
self.record_status(
deployment['id'],
status,
self.get_record_description(current_action),
)
return deployment
def record_status(self, deployment_id, state, description=''):
if not self.args.execute_commands:
return
response = self.github_deployments.post(
f"deployments/{deployment_id}/statuses",
json={
'state': state,
'description': description,
'auto_inactive': False,
},
)
response.raise_for_status()
print(f'info: status "{state}" recorded:', response.json()['url'])
class ContainerRegistryMixin:
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'--repository-uri',
action=store_env_override,
envvar='IMAGE_REPOSITORY_URI',
description='image repository URI',
)
parser.add_argument(
'--repository-name',
action=store_env_override,
envvar='IMAGE_REPOSITORY_NAME',
description='image repository name',
addendum='(default extended for staging to: …/staging)'
if isinstance(self, EnvironmentMixin) else None,
)
parser.set_defaults(
resolve_repository_name=functools.lru_cache()(self.resolve_repository_name),
)
def resolve_repository_name(self):
if (
isinstance(self, EnvironmentMixin) and
self.args.target == 'staging' and
isinstance(self.args.repository_name, EnvDefault)
):
return self.args.repository_name + '/staging'
return self.args.repository_name
class Marketplace(LocalRoot):
"""marketplace app management"""
@Marketplace.register
class Db(Local):
"""manage databases"""
default_production_id = 'marketplace-db'
default_staging_id = 'marketplace-staging-db'
@localmethod('--name', default=default_staging_id,
help=f"database instance name to apply (default: {default_staging_id})")
@localmethod('--from', dest='from_name', metavar='NAME', default=default_production_id,
help=f"database instance whose snapshot to use (production) "
f"(default: {default_production_id})")
def build_staging(self, args, parser):
"""restore most recent production snapshot to NEW staging database"""
# Look up production instance info (vpc subnet group, etc.)
(_retcode, stdout, _stderr) = yield SHH, self.local['aws'][
'rds',
'describe-db-instances',
'--db-instance-identifier',
args.from_name,
]
if stdout is None:
subnet_group_name = 'DRY-RUN'
else:
try:
(description,) = json.loads(stdout)['DBInstances']
subnet_group_name = description['DBSubnetGroup']['DBSubnetGroupName']
except (KeyError, TypeError, ValueError):
print(stdout)
raise ValueError('unexpected response')
def snapshot_datetime(data):
timestamp = data['SnapshotCreateTime']
return dateutil.parser.parse(timestamp)
(_retcode, stdout, _stderr) = yield SHH, self.local['aws'][
'rds',
'describe-db-snapshots',
'--snapshot-type', 'automated',
'--db-instance-identifier', args.from_name,
]
if stdout is None:
snapshot_id = 'DRY-RUN'
else:
try:
snapshots = json.loads(stdout)['DBSnapshots']
snapshots_available = (snapshot for snapshot in snapshots
if snapshot['Status'] == 'available')
snapshots_sorted = sorted(snapshots_available, key=snapshot_datetime, reverse=True)
snapshot_id = snapshots_sorted[0]['DBSnapshotIdentifier']
except IndexError:
parser.error(f"{args.from_name} has no snapshots available")
except (KeyError, TypeError, ValueError):
print(stdout)
raise ValueError('unexpected response')
yield self.local['aws'][
'rds',
'restore-db-instance-from-db-snapshot',
'--no-publicly-accessible',
'--db-subnet-group-name', subnet_group_name,
'--db-instance-identifier', args.name,
'--db-snapshot-identifier', snapshot_id,
]
build_staging.__name__ = 'build-staging'
@Marketplace.register
class Build(ContainerRegistryMixin, DeploymentMixin, EnvironmentMixin, Local):
"""build app container image for deployment"""
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'--github-repo',
action=store_env_override,
envvar='GITHUB_REPO',
metavar='OWNER/NAME',
description='GitHub repository for which Deployments are tracked',
)
parser.add_argument(
'-n', '--name',
help='image name:tag (default derived from repository name: '
'{REPOSITORY_NAME}:latest)',
)
parser.add_argument(
'--label',
action='append',
help='additional name/tags to label image; the first of these, '
'if any, is treated as the "version"',
)
parser.add_argument(
'--if-latest',
action='store_true',
help="ensure that the label provided is the most recent repository tag appropriate "
"to the target environment (x.y.z for production and x.y* for staging)",
)
parser.add_argument(
'--no-record',
action='store_false',
dest='record_deployment',
default=True,
help="disable Github Deployment record and "
"disable check that deployment has not already been recorded",
)
parser.add_argument(
'-f', '--force',
dest='show_warnings',
action='store_false',
default=True,
help="ignore warnings",
)
parser.add_argument(
'-l', '--login',
action='store_true',
help="log in to AWS ECR",
)
parser.add_argument(
'-p', '--push',
action='store_true',
help="push image once built",
)
parser.add_argument(
'-d', '--deploy',
action='store_true',
help="deploy the container once the image is pushed",
)
def get_full_name(self, name):
return '/'.join((self.args.repository_uri, name))
def prepare(self, args, parser):
if args.login and not args.push:
parser.error("will not log in outside of push operation")
yield from self.check_latest()
if args.deploy:
deployment = self.record_deployment()
else:
deployment = None
repository_tag = args.name or (args.resolve_repository_name() + ':latest')
command = self.local['docker'][
'build',
'--build-arg', 'TARGET=production',
'-t', repository_tag,
'-t', self.get_full_name(repository_tag),
]
if args.label:
# treat first label/tag as version to be handled specially by build
command = command[
'--build-arg', f'APPVERSION={args.label[0]}',
]
for label in args.label:
name = args.resolve_repository_name() + ':' + label
command = command[
'-t', name,
'-t', self.get_full_name(name),
]
elif args.show_warnings and args.target == 'production':
parser.error(textwrap.dedent("""\
at least the standard versioning label is recommended for builds intended for production
for example – 0.1.1 –
manage build --label 0.1.1 production
ensure that you have pulled the latest from the Git repository, and consult –
git tag -l --sort version:refname
– for the tags currently in use. and, ensure that you apply (and push) the same tag
to the source in the Git repository as to the Docker image here, for example –
git tag -a 0.1.1
(to suppress this warning, see: `manage build --force`)\
"""))
yield command[ROOT_PATH]
if args.push:
if deployment:
self.record_status(
deployment['id'],
'pending',
self.get_record_description('PUSH'),
)
yield from self['push'].delegate()
if args.deploy:
if deployment:
args.deployment_id = deployment['id']
yield from self['deploy'].delegate()
@localmethod('-l', '--login', action='store_true', help="log in to AWS ECR")
def push(self, args):
"""push image(s) to registry"""
if args.login:
login_command = self.local['aws'][
'ecr',
'get-login',
'--no-include-email',
'--region', 'us-west-2',
]
self.print_command(login_command)
if args.execute_commands:
full_command = login_command()
(executable, *arguments) = full_command.split()
assert executable == 'docker'
yield self.local[executable][arguments]
yield self.local['docker'][
'push',
self.get_full_name(args.resolve_repository_name()),
]
class Deploy(ClusterServiceMixin, DeploymentMixin, Local):
"""deploy the latest image container to the cluster service"""
environmental = True # flag for ClusterServiceMixin
TASK_START_PATTERN = re.compile(r'\(service (?P<service_name>[\w-]+)\) '
r'has started (?P<number_tasks>\d+) '
r'tasks: \(task (?P<task_id>[\w-]+)\)\.')
class UpdateServiceColumns(str, enum.Enum):
id = 'ID'
status = 'Status'
desiredCount = 'Desired'
pendingCount = 'Pending'
createdAt = 'Created'
updatedAt = 'Updated'
def __str__(self):
return self.value.__str__()
def get_string(self, data):
value = data[self.name]
if self is self.createdAt or self is self.updatedAt:
return datetime.datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S')
return str(value)
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'--deployment-id',
help="Github Deployment record to update",
)
parser.add_argument(
'--static',
action='store_true',
help="collect and deploy static assets upon deployment",
)
parser.add_argument(
'--migrate',
action='store_true',
help="migrate the database upon deployment",
)
parser.add_argument(
'--ssh',
action=store_env_override,
default='ssh',
envvar='ECS_SSH',
metavar='COMMAND',
description='ssh command through which to execute post-deployment tasks',
)
# Note: This override only works when command is called
# outright, not when delegated:
parser.add_argument(
'--no-quiet',
action='store_true',
default=False,
dest='foreground',
help="print command output",
)
parser.add_argument(
'-qq',
action='store_false',
default=True,
dest='report',
help="do not print deployment result",
)
def prepare(self, args, parser, local):
# NOTE: because, for simplicity, this just updates the service by
# NOTE: telling it to pull the "latest" from the image repo, the
# NOTE: versioned deployment record *could* be inaccurate (for now)
yield from self.check_latest()
deployment = self.record_deployment(
args.deployment_id,
'in_progress',
'CLUSTER-UPDATE',
)
last_event = None
if args.static or args.migrate:
# AWS CLI and ECS CLI provide no great way of tying deployments
# to containers and instances, which makes this quite annoying.
#
# Retrieve a baseline event:
service = yield from self.describe_service(suppress=False)
if service is not None:
last_event = service['events'][0]
# update service (deploy)
modifier = SHH if args.__command__ is not self else None
(_retcode, stdout, _stderr) = yield modifier, local['aws'][
'ecs',
'update-service',
'--force-new-deployment',
'--cluster', args.resolve_cluster(),
'--service', args.resolve_service(),
]
# report on deployments
if args.report and stdout is not None:
try:
result = json.loads(stdout)
service_name = result['service']['serviceName']
deployments = result['service']['deployments']
data = [self.UpdateServiceColumns]
data.extend(
[
column.get_string(deployment)
for column in self.UpdateServiceColumns
]
for deployment in deployments
)
except (KeyError, ValueError):
print('unexpected command output:', stdout, file=sys.stderr, sep='\n')
else:
table = AsciiTable(data, title=f"{service_name} deployments")
print(table.table)
# post-deployment actions
if last_event:
# there are post-deployment actions and this is not a dry run
#
# poll event stream to discover newly-started task reflecting
# deployed version
spinner = spincycle(['']) if args.show_commands else spincycle()
for _cycle in spinner:
service = yield from self.describe_service(suppress=False)
events = tuple(itertools.takewhile(
lambda event: event['id'] != last_event['id'],
service['events']
))
for event in reversed(events):
task_start_match = self.TASK_START_PATTERN.match(event['message'])
if task_start_match:
task_id = task_start_match.group('task_id')
break
else:
# no match -- skip cycle
continue
# match found -- break loop
print('\b')
break
# trace task to its EC2 instance
# (AWS CLI you are terrible)
(_retcode, stdout, _stderr) = yield local['aws'][
'ecs',
'describe-tasks',
'--cluster', args.resolve_cluster(),
'--tasks', task_id,
]
result = json.loads(stdout)
(task,) = result['tasks']
(_retcode, stdout, _stderr) = yield local['aws'][
'ecs',
'describe-container-instances',
'--cluster', args.resolve_cluster(),
'--container-instances', task['containerInstanceArn'],
]
result = json.loads(stdout)
(container_instance,) = result['containerInstances']
(_retcode, stdout, _stderr) = yield local['aws'][
'ec2',
'describe-instances',
'--instance-ids', container_instance['ec2InstanceId'],
]
result = json.loads(stdout)
(ec2_reservation,) = result['Reservations']
(ec2_instance,) = ec2_reservation['Instances']
public_ip = ec2_instance['PublicIpAddress']
(ssh_exec, *ssh_args) = args.ssh.split()
ssh = local[ssh_exec].bound_command(*ssh_args)[public_ip]
image_path = '/'.join((args.repository_uri, args.resolve_repository_name()))
for _cycle in spinner:
# FIXME: We've seen it not here tho it should be: investigate
(_retcode, stdout, _stderr) = yield ssh['docker'][
'ps',
'--filter', f'ancestor={image_path}',
'--format', '"{{.Names}}"',
]
try:
(container_name,) = stdout.splitlines()
except ValueError:
continue
else:
print('\b')
break
container = ssh['docker']['exec', '-u', 'webapp', container_name]
# Grab target environment's entrypoint
#
# NOTE: This might change; but currently, the Dockerfile leaves
# the default entrypoint, and sets the default command to
# supervisor/webapp. However, *in staging/production*, the
# entrypoint is set to `chamber`, to populate
# configuration/secrets.
#
# We'll set our own command here to execute, of course; but, it
# won't succeed without reinstituting the target environment's
# entrypoint, (which `exec` won't do for us).
#
# (It's conceivable that this could be resolved by baking
# `chamber` into the Dockerfile entrypoint, with a null backend
# when environment not established by the environ. But,
# investigation is warranted.)
#
# Rather than build the logic of that entrypoint here, we'll
# simply read it from the running container, and prepend it to
# our command.
(_retcode, stdout, _stderr) = yield ssh['docker'][
'inspect',
'--format', '"{{json .Config.Entrypoint}}"',
container_name,
]
entrypoint = json.loads(stdout)
container = container[entrypoint]
if args.migrate:
yield (
local.FG(retcode=None),
container['./manage.py', 'migrate']
)
if args.static:
yield (
local.FG(retcode=None),
container['./manage.py', 'collectstatic', '--no-input', '-v', '1']
)
if deployment:
self.record_status(deployment['id'], 'success')
@Marketplace.register
class Develop(Local):
"""build, run and manage a Docker development container"""
DEFAULT_NAMETAG = 'marketplace_web'
@classmethod
def run(cls, *args, name=None, **kwargs):
docker = cls.local['docker']
return docker[
'run',
'-v', f'{SRC_PATH}:/app',
'--env-file', str(ROOT_PATH / '.env'),
].bound_command(
*args
).bound_command(
*(
f'-e{key}' if value is None else f'-e{key}={value}'
for (key, value) in kwargs.items()
)
)[name or cls.DEFAULT_NAMETAG]
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'-n', '--name',
default=self.DEFAULT_NAMETAG,
help=f'Image name/tag (default: {self.DEFAULT_NAMETAG})',
)
parser.add_argument(
'-b', '--build',
action='store_true',
help="(re-)build image before container creation",
)
parser.add_argument(
'--net',
choices=('host',),
help="specify a non-default networking mode",
)
parser.add_argument(
'-e', '--env',
action='append',
help='set environment variables (as for docker)',
)
parser.set_defaults(
resolve_environ=functools.lru_cache()(self.resolve_environ),
)
def resolve_environ(self):
return dict(pair.split('=') if '=' in pair else (pair, None)
for pair in (self.args.env or ()))
def exec(self, user='webapp', interactive=True, tty=True, **environ):
command = self.local['docker']['exec']
if user:
command = command['-u', user]
if interactive:
command = command['-i']
if tty:
command = command['-t']