-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
859 lines (728 loc) · 26.6 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
import contextlib
import enum
import functools
import json
import os
import sys
from argparse import REMAINDER
from pathlib import Path
from argcmdr import Local, LocalRoot, localmethod
from descriptors import cachedproperty
from plumbum import colors
ROOT_PATH = Path(__file__).parent.resolve()
SRC_PATH = ROOT_PATH / 'src'
class StrEnum(str, enum.Enum):
def __bool__(self):
return bool(self.value)
def __str__(self):
return str(self.value)
class Appy(LocalRoot):
"""appy management"""
@Appy.register
class Stack(Local):
"""manage infrastructure (excluding elastic beanstalk)"""
checkmethod = localmethod(
'-y', '--yes',
action='store_true',
default=False,
dest='pre_approved',
help='do not prompt for confirmation',
)
def exec_terraform(self, command, *flags):
flags += ('-auto-approve',) if getattr(self.args, 'pre_approved', False) else ()
yield self.local.FG, self.local['terraform'][command, flags]
@localmethod
def check(self):
"""check state of stack via: terraform plan"""
yield from self.exec_terraform('plan')
@checkmethod
def sync(self):
"""sync stack via: terraform apply"""
yield from self.exec_terraform('apply')
@checkmethod
def destroy(self, args):
"""tear down stack via: terraform destroy"""
if not args.pre_approved:
print(
colors.bold |
"If you have initialized the hub's kubernetes cluster, "
"you might want to destroy that first!"
)
print('\n\t', 'manage hub destroy', '\n')
input(colors.bold | 'Press enter to continue or ctrl+c to abort...\n')
yield from self.exec_terraform('destroy')
@Appy.register
class Env(Local):
"""manage elastic beanstalk environment"""
default_env = 'appy-reviews-pro'
def __init__(self, parser):
parser.add_argument(
'-n', '--name',
default=self.default_env,
help=f"target environment (default: {self.default_env})",
)
@localmethod('config',
help="saved configuration from which to create environment")
def create(self, args):
"""create environment from saved configuration"""
yield self.local['eb'][
'create',
args.name,
'--cfg', args.config,
]
@localmethod('config', nargs='?', help="label for saved configuration")
@localmethod('--no-save', dest='save', default=True, action='store_false')
def destroy(self, args, parser):
"""tear down environment (having saved its configuration)"""
if args.save:
if not args.config:
parser.error('config name required')
yield self.local['eb'][
'config',
'save',
args.name,
'--cfg', args.config,
]
yield self.local['eb'][
'terminate',
args.name,
]
@Appy.register
class DNS(Local):
"""manage site DNS"""
SET_STYLE = 'alias' # alias or cname
class Domain(StrEnum):
zone = 'dssg.io.'
fqdn = f'review.{zone}'
fqdn_s = f's.{fqdn}'
fqdn_a = f'appy.{fqdn}'
class CName(StrEnum):
live = 'pro-reviews-dssg.us-west-2.elasticbeanstalk.com'
static = 'd2va83k0l3phq8.cloudfront.net'
class profile_hint(contextlib.AbstractContextManager):
"""Print a helpful hint about using the correct AWS profile in
the event of an execution error.
Note that the exception is not suppressed.
"""
def __exit__(self, _exc_type, exc_value, _exc_tb):
if isinstance(exc_value, Local.local.ProcessExecutionError):
print(
'Hint: specify your AWS profile for the DSSG account:\n'
'\n'
'\tAWS_PROFILE=dssg manage dns ...\n',
file=sys.stderr,
)
@classmethod
def prepare_hosted_zone(cls):
# AWS_PROFILE=dssg
return cls.local['aws'][
'route53',
'list-hosted-zones',
'--query', f"HostedZones[?Name == '{cls.Domain.zone}']",
] | cls.local['jq'][
'-r',
'.[0].Id',
]
def __init__(self, parser):
parser.add_argument(
'-p', '--prefix',
default='production',
choices=('staging', 'production'),
help="domain prefix (default: production prefix)",
)
@property
def fqdn(self):
return self.Domain.fqdn_s if self.args.prefix == 'staging' else self.Domain.fqdn
@localmethod
def check(self):
"""look up the site's current DNS setting"""
with self.profile_hint():
(_retcode, output, _error) = yield (
self.prepare_hosted_zone() |
self.local['xargs'][
'-I', '{}',
'aws',
'route53',
'list-resource-record-sets',
'--hosted-zone-id', '{}',
'--query', f"ResourceRecordSets[?Name == '{self.fqdn}']",
] | self.local['jq']['-r'][
'.[0].AliasTarget.DNSName'
if self.SET_STYLE == 'alias' else
'.[0].ResourceRecords[0].Value'
]
)
if output is None:
# dry run
return
cname = output.strip('. \n')
for value in self.CName:
if value == cname:
print(value.name.upper() | colors.info)
break
else:
print('UNKNOWN' | colors.warn)
@localmethod('target', choices=CName.__members__, help="the CNAME value to set")
def set(self, args):
"""set the site's DNS to either the live server or the static
content bucket
"""
target_cname = self.CName[args.target]
if target_cname.endswith('.elasticbeanstalk.com'):
alias_target_zone_id = 'Z38NKT9BP95V3O'
elif target_cname.endswith('.cloudfront.net'):
alias_target_zone_id = 'Z2FDTNDATAQYW2'
else:
raise ValueError("HostedZoneId for target unknown", target_cname)
with self.profile_hint():
yield self.prepare_hosted_zone() | self.local['xargs'][
'-I', '{}',
'aws',
'route53',
'change-resource-record-sets',
'--hosted-zone-id', '{}',
'--change-batch', ('''{
"Changes": [{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "%%s",
%s
}
}]
}''' % (
'''"Type": "A",
"AliasTarget": {
"HostedZoneId": "%s",
"DNSName": "%%s",
"EvaluateTargetHealth": false
}''' % alias_target_zone_id
if self.SET_STYLE == 'alias' else
'''"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [{"Value": "%s"}]'''
)
) % (self.fqdn, target_cname),
]
class Leader(Local):
"""manage utility instance DNS"""
@localmethod
def show(self):
"""look up autoscaling group leader's public IP address"""
yield self.local['aws'][
'elasticbeanstalk',
'describe-environment-resources',
'--environment-name', 'appy-reviews-pro',
] | self.local['jq'][
'-r',
'.EnvironmentResources.Instances[].Id',
] | self.local['sort'] | self.local['head']['-1'] | self.local['xargs'][
'aws',
'ec2',
'describe-instances',
'--instance-ids', # (xargs instance)
] | self.local['jq'][
'-r',
'.Reservations[].Instances[].PublicIpAddress',
]
@localmethod('ip_address', metavar='instance-ip',
help='public IP address of the leader instance (see "show")')
def set(self, args):
"""set the utility instance DNS name to the leader IP address"""
# NOTE: show and set do *not* compose only because the former must
# query the application account and the latter the DNS account.
#
# This is lame.
#
# (It could perhaps be improved but will do for now....)
#
with self[-1].profile_hint():
(_retcode, output, _error) = yield self[-1].prepare_hosted_zone()
hosted_zone = output.strip() if output is not None else 'DRY-RUN'
yield self.local['aws'][
'route53',
'change-resource-record-sets',
'--hosted-zone-id', hosted_zone,
'--change-batch', '''{
"Changes": [{
"Action": "UPSERT",
"ResourceRecordSet": {
"Name": "%(name)s",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{"Value": "%(target)s"}]
}
}]
}''' % {
'name': self[-1].Domain.fqdn_a,
'target': args.ip_address,
},
]
@Appy.register
class Build(Local):
"""build container image"""
class EnvEnum(StrEnum):
__env_default__ = ''
def __new__(cls, key):
value = os.getenv(key, cls.__env_default__)
obj = str.__new__(cls, value)
obj.envname = key
obj._value_ = value
return obj
class EnvDefault(EnvEnum):
registry = 'AP_CONTAINER_REGISTRY'
image = 'AP_IMAGE_PATH'
def add_help_text(self, help_text):
value_display = str(self) if self else 'none'
return f"{help_text} (default populated from {self.envname}: {value_display})"
def __init__(self, parser):
parser.add_argument(
'--registry',
default=self.EnvDefault.registry,
help=self.EnvDefault.registry.add_help_text(
"Container registry's fully-qualified domain"
),
metavar='DOMAIN',
)
parser.add_argument(
'--name',
default=self.EnvDefault.image,
help=self.EnvDefault.image.add_help_text("Image repository name (path)"),
)
parser.add_argument(
'--label',
action='append',
help='Tags to label image; the first of these, '
'if any, is treated as the "version"',
)
parser.add_argument(
'--not-latest',
action='store_false',
default=True,
dest='latest',
help='Do NOT tag the image "latest"',
)
parser.add_argument(
'--target',
choices=('development', 'production'),
default='production',
help="Target environment (default: production)",
)
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_tag(self, tag=None, registry=False):
if not self.args.name:
self.args.__parser__.error(
f'specify image name/path via flag --name '
f'or env var {self.EnvDefault.image.envname}'
)
full_tag = self.args.name
if registry:
if not self.args.registry:
self.args.__parser__.error(
f'specify container registry domain via flag --registry '
f'or env var {self.EnvDefault.registry.envname}'
)
full_tag = '/'.join((self.args.registry, full_tag))
if tag:
full_tag = ':'.join((full_tag, tag))
return full_tag
def prepare(self, args, parser):
if args.login and not args.push:
parser.error("will not log in outside of push operation")
command = self.local['docker'][
'build',
'--build-arg', f'TARGET={args.target}',
]
if args.latest:
command = command[
'-t', self.get_full_tag('latest'),
'-t', self.get_full_tag('latest', registry=True),
]
if args.label:
for label in args.label:
command = command[
'-t', self.get_full_tag(label),
'-t', self.get_full_tag(label, registry=True),
]
yield command[ROOT_PATH]
if args.push:
yield from self['push'].delegate()
if args.deploy:
yield self['deploy'].delegate()
@localmethod('-l', '--login', action='store_true', help="log in to AWS ECR")
def push(self, args):
"""push already-built image to registry"""
if args.login:
login_command = self.local['aws'][
'ecr',
'get-login',
'--no-include-email',
'--region', 'us-west-2',
]
if args.show_commands or not args.execute_commands:
print('>', login_command)
if args.execute_commands:
full_command = login_command()
(executable, *arguments) = full_command.split()
assert executable == 'docker'
yield self.local[executable][arguments]
if args.label:
for label in args.label:
yield self.local['docker'][
'push',
self.get_full_tag(label, registry=True),
]
if args.latest:
yield self.local['docker'][
'push',
self.get_full_tag('latest', registry=True),
]
else:
yield self.local['docker'][
'push',
self.get_full_tag(registry=True),
]
@localmethod
def deploy(self, args, parser):
"""deploy an image container"""
command = self.local['eb']['deploy']
# specify environment
if args.target == 'production':
command = command['appy-reviews-pro']
else:
command = command['appy-reviews-dev']
command = command['--nohang']
if args.label:
tag = args.label[0]
command = command['-l', tag]
elif args.latest:
tag = 'latest'
else:
parser.error("unable to determine image tag to deploy")
if args.execute_commands:
# modify eb metadata to pull appropriate image
metafile = ROOT_PATH / 'Dockerrun.aws.json'
with metafile.open() as fd:
metadata = json.load(fd)
metadata_image = metadata.setdefault('Image', {})
metadata_image['Name'] = self.get_full_tag(tag, registry=True)
with metafile.open('w') as fd:
json.dump(metadata, fd, indent=2)
return command
# #
# Base classes for management of Docker container #
# #
class LocalContainer(Local):
@classmethod
def run(cls, *args, **kwargs):
docker = cls.local['docker']
return docker[
'run',
'--rm',
'--net=host',
'-v', f'{SRC_PATH}:/app',
].bound_command(
*args
).bound_command(
*(
f'-e{key}' if value is None else f'-e{key}={value}'
for (key, value) in kwargs.items()
)
)['appyreviews_web']
class DbLocal(LocalContainer):
EXAMPLE_DATABASE_URL = 'postgres://appy_reviews:PASSWORD@localhost:5433/appy_reviews'
@cachedproperty
def database_url(self):
if self.args.database_url:
return self.args.database_url
if os.getenv('DATABASE_URL'):
return None
self.args.__parser__.error(
'DATABASE_URL must be specified by argument or environment\n\n'
'For example:\n'
f'\tmanage db --database-url={self.EXAMPLE_DATABASE_URL}\n'
'or:\n'
f'\tDATABASE_URL={self.EXAMPLE_DATABASE_URL} manage db'
)
def manage(self, *args, user='webapp', **kwargs):
return self.run(
'--user', user,
*args,
DATABASE_URL=self.database_url,
**kwargs
)['./manage.py']
def __init__(self, parser):
parser.add_argument(
'-v', '--verbosity',
type=int,
default=1,
)
parser.add_argument(
'--db', '--database-url',
dest='database_url',
metavar='database-url',
help=f"Database URL (e.g.: {self.EXAMPLE_DATABASE_URL})",
)
@Appy.register
class Develop(DbLocal):
"""create the appy-reviews container in development mode and run it
in the background
"""
DEFAULT_NAMETAG = 'appyreviews_web_1'
CONTROLS = ('start', 'stop', 'restart')
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",
)
def prepare(self, args):
if args.build:
yield self.local['docker'][
'build',
'--build-arg', 'TARGET=development',
'-t', 'appyreviews_web',
ROOT_PATH,
]
try:
yield self.local['docker'][
'stop',
args.name,
]
except self.local.ProcessExecutionError:
pass
yield self.run(
'-d',
'--name', args.name,
DATABASE_URL=self.database_url,
APPY_DEBUG=1,
REVIEW_WHITELIST=None,
SLACK_CHANNEL_URL=None,
SLACK_JOIN_URL=None,
SMTP_USER=None,
SMTP_PASSWORD=None,
WUFOO_API_KEY=None,
)
def _container_hint(func):
"""Decorator to catch `exec`-based commands' "no such container"
error and print helpful hint.
"""
@functools.wraps(func)
def wrapped(self, args):
try:
yield from func(self, args)
except self.local.ProcessExecutionError:
# check whether development container running
(_retcode, output, _error) = yield self.local['docker'][
'ps',
'--filter', f'name={args.name}',
'--quiet',
]
if output:
# ...it appears to be running, so this is some other issue
raise
# not running; provide hint:
print(
"Hint: Start the development container "
"(see `manage develop --help`)",
file=sys.stderr,
)
return wrapped
@localmethod('mcmd', metavar='command', choices=CONTROLS,
help="{{{}}}".format(', '.join(CONTROLS)))
@_container_hint
def control(self, args):
"""control the appy-reviews supervisor process"""
yield self.local['docker'][
'exec',
args.name,
'supervisorctl',
'-c',
'/etc/supervisor/supervisord.conf',
args.mcmd,
'webapp',
]
@localmethod('remainder', metavar='command arguments', nargs=REMAINDER)
@localmethod('mcmd', metavar='command', help="django management command")
@localmethod('-u', '--user', default='webapp', help='container user (default: webapp)')
@localmethod('--no-tty', dest='tty', action='store_false', default=True,
help="do NOT supply a pseudo-tty")
@_container_hint
def djmanage(self, args):
"""manage the appy-reviews django project"""
yield (
# foreground command to fully support shell
self.local.FG,
self.local['docker'][
'exec',
(('-it',) if args.tty else ()),
# users have no $HOME, so tell ipython, et al to look elsewhere:
'-e', 'XDG_CACHE_HOME=/tmp/xdg-cache/',
'--user', args.user,
args.name,
'./manage.py',
args.mcmd,
args.remainder,
]
)
_container_hint = staticmethod(_container_hint)
@Appy.register
class Static(LocalContainer):
"""generate static pages"""
@localmethod
def closed(self):
"""render closed.html and upload it to the S3 bucket as index.html"""
return (
# render to stdout
self.run()[
'./manage.py',
'makestatic',
'closed',
'program_year={settings.REVIEW_PROGRAM_YEAR}',
] |
# pipe rendering to awscli
# and upload to bucket
self.local['aws'][
's3',
'cp',
'-',
's3://review.dssg.io/index.html',
'--acl', 'public-read',
'--content-type', 'text/html',
]
)
@Appy.register
class Db(DbLocal):
"""manage database via local container"""
commands = (
'showmigrations',
'sqlmigrate',
'makemigrations',
'migrate',
)
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'mcmd',
metavar='command',
choices=self.commands,
help="{{{}}}".format(', '.join(self.commands)),
)
parser.add_argument(
'remainder',
metavar='command arguments',
nargs=REMAINDER,
)
def prepare(self, args):
return self.manage('-it')[args.mcmd, args.remainder]
@Appy.register
class Etl(DbLocal):
"""manage survey data"""
def __init__(self, parser):
super().__init__(parser)
parser.add_argument(
'--year',
type=int,
help=f"Program year",
)
parser.add_argument(
'--stage',
choices=('application', 'review'),
help="configuration preset depending on whether the application "
"period is still open or applications are now under review "
"(and which reads year from settings)",
)
parser.add_argument(
'--all',
action='store_true',
help="perform all operations (subcommands wufoo & apps)",
)
def prepare(self, args, parser):
if not args.all:
super().prepare(args)
return
yield self['wufoo'].delegate()
yield self['apps'].delegate()
@localmethod('--csv', action='store_true', default=False, dest='csv_cache',
help="cache data in local CSV files")
@localmethod('-n', '--no-database', action='store_false', default=True, dest='write_to_db',
help="Do not write data to database")
def wufoo(self, args, parser):
"""load initial survey data"""
if not os.getenv('WUFOO_API_KEY'):
parser.error(
'WUFOO_API_KEY must be specified by environment\n\n'
'For example:\n'
'\tWUFOO_API_KEY=xx-xx-xx DATABASE_URL=... manage etl bootstrap'
)
return self.manage(WUFOO_API_KEY=None)[
'loadwufoo',
'-v', args.verbosity,
(('-f', f'^{args.year} ') if args.year else ()),
(('--stage', args.stage) if args.stage else ()),
('--no-database' if not args.write_to_db else ()),
('output' if args.csv_cache else '-'),
]
@localmethod('subcommand',
choices=('execute', 'inspect',), default='execute', nargs='?',
help="either execute command, or inspect state of system "
"only, do not load applications (default: execute)")
@localmethod('-d', '--dry-run', action='store_true',
help="do not commit database transactions so as to test effect "
"of command")
@localmethod('--invite-only', action='append', metavar='EMAIL',
help="consider only *these* reviewer records, indicated by email "
"address, and do not process any other dataset")
@localmethod('--email-ignore', action='append', metavar='EMAIL',
help="Do not email these reviewers, indicated by email address, "
"(and do still process other datasets)")
def apps(self, args):
"""map wufoo data into appy"""
return self.manage(
SLACK_CHANNEL_URL=None,
SLACK_JOIN_URL=None,
SMTP_USER=None,
SMTP_PASSWORD=None,
)[
'loadapps',
(('-s', f'_{args.year}') if args.year else ()),
(('--year', args.year) if args.year else ()),
(('--closed',) if args.stage == 'review' else ()),
('--dry-run' if args.dry_run else ()),
([f'--invite-only={email}' for email in args.invite_only] if args.invite_only else ()),
([f'--email-ignore={email}' for email in args.email_ignore] if args.email_ignore else ()),
args.subcommand,
]
@Appy.register
class Reviewer(DbLocal):
"""manage reviewers"""
@localmethod('remainder', metavar='command arguments', nargs='*',
help="arguments to pass to sendconfirm (preceeded by --)")
def invite(self, args):
"""invite a new or existing reviewer"""
return self.manage(
SMTP_USER=None,
SMTP_PASSWORD=None,
)[
'sendinvite',
args.remainder,
]