This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdemo
executable file
·947 lines (823 loc) · 30.5 KB
/
demo
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
#!/usr/bin/env python3
"""
This is a script intended to make running a local version of the AllenNLP Demo easier.
The AllenNLP demo is a series of individual containers that together make the experience
available at https://demo.allennlp.org. Weaving these containers together locally to make
changes takes a surprising amount of acrobatics, so rather than expecting you to become
an expert in docker, we wrap the necessary incanations in a series of simple commands
provided by this script.
The easiest way to learn more about this script and how it works is likely by viewing
it's usage information, which you can see by running this command:
./demo --help
It's worth noting that this script isn't used anywhere other than local development
environments, and probably shouldn't be. This means it's safe to make changes without
worrying about breaking the house of cards that makes the live site work.
"""
import argparse
import subprocess
import os
import sys
import shlex
import time
import json
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Tuple, Optional
from pathlib import Path
from urllib.request import urlopen
from urllib.error import URLError
from urllib.parse import urlparse
from http.client import HTTPResponse, RemoteDisconnected
# When we spawn docker things they need a name that prevents them from colliding with other
# docker things. We use this prefix to ensure that.
prefix = "allennlp-demo-"
api_prefix = f"{prefix}api-"
# The name of the docker network used by this script.
network = "allennlp-demo"
# The path to the directory this script lives in.
root = Path(__file__).parent.resolve()
class Paths:
"""
This class provides access to a series of directories in the repository used throughout
this script.
"""
def __init__(self):
self.root = Path(__file__).parent.resolve()
self.api = self.root / "api"
self.api_modules = self.api / "allennlp_demo"
self.ui = self.root / "ui"
self.it = self.root / "it"
self.run = self.root / "run"
paths = Paths()
class ContainerIds:
"""
This class provides access to the identifiers for a few containers that are hardcoded.
"""
def __init__(self):
self.proxy = "proxy"
self.ui = "ui"
self.it = "it"
container_ids = ContainerIds()
def network_exists() -> bool:
"""
Returns true if the network this script manages exists.
"""
ls = subprocess.check_output(
shlex.split(f"docker network ls --quiet --filter name=^{network}$")
)
if len(ls.strip()) > 0:
return True
return False
def create_network():
"""
Creates a new network that allows containers started by this script to intercommunicate. If
the network already exists this method does nothing.
"""
if network_exists():
return
subprocess.check_call(
shlex.split(f"docker network create {network}"),
stdout=subprocess.DEVNULL
)
def printFlush(*args, **kwargs):
"""
Prints a message immediately to stdout that's meant to convey the start of an action.
Three trailing dots are appended as a suffix to indicate things are in flight.
"""
kw={ "end":"...", **kwargs, "flush":True }
print(*args, **kw)
def printLnFlush(*args, **kwargs):
"""
Print a message immediately to stdout with a trailing newline. Use this for regular output,
or following a `printFlush()` call to indicate that the previously started action has
completed.
"""
kw={ **kwargs, "flush":True }
print(*args, **kw)
class ContainerError(Exception):
def __init__(self, cid: str, msg: str, stdout: Optional[str], stderr: Optional[str]):
self.cid = cid
self.stdout = stdout
self.stderr = stderr
super().__init__(msg)
class BuildError(ContainerError):
def __init__(self, cid: str):
super().__init__(cid, f"{cid} failed to build", None, None)
class StartError(ContainerError):
def __init__(self, cid: str, stdout: str, stderr: str):
super().__init__(cid, f"{cid} failed to start", stdout, stderr)
class Status(Enum):
"""
An Enum reflecting the various states of a container.
"""
UP = "Up"
DOWN = "Down"
class Container:
"""
A single, runnable part of the AllenNLP Demo that's managed with Docker.
"""
def __init__(self, cid: str):
# The container id, which is unique ID for the thing within the scope of the demo.
self.cid = cid
# The "fully qualified name". Each thing we run has to have a name that's unique and
# doesn't collide with something else running within docker on the host machine, so
# we append a human readable prefix before the cid.
self.fqn = f"{prefix}{self.cid}"
# The docker tag to use when building images.
self.tag = f"{self.fqn}:latest"
def build_cmd(self) -> str:
"""
Returns the command, as a string, for building the container.
"""
raise NotImplementedError()
def build(self):
"""
Builds the container.
"""
printLnFlush(f"Building {self.cid}")
bp = subprocess.run(shlex.split(self.build_cmd()))
if bp.returncode != 0:
printLnFlush("Error")
raise BuildError(self.cid)
printLnFlush("Success")
def start_cmd(self) -> str:
"""
Returns the command, as a string, for starting the container.
"""
raise NotImplementedError()
def start(self):
"""
Starts the container.
"""
if self.status() == Status.UP:
printLnFlush(f"Container {self.cid} is already running.")
return
printFlush(f"Starting {self.cid}")
create_network()
p = subprocess.Popen(
shlex.split(self.start_cmd()),
start_new_session=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8"
)
# If `p.poll()` returns a value, the process has terminated. We intentionally don't timeout,
# we leave that to the user and Ctrl^C.
while p.poll() is None and not self.is_healthy():
time.sleep(5)
# If the container still isn't healthy, then it failed to start.
if not self.is_healthy():
stdout, stderr = p.communicate()
printLnFlush("Error")
raise StartError(self.cid, stdout, stderr)
printLnFlush("Success")
def exec_cmd(self) -> str:
"""
Returns the command used to execute arbitrary commands in the container.
"""
return f"docker exec {self.fqn}"
def run(self, entrypoint: str, *args: str):
"""
Runs the provided command in the container.
"""
if self.status() == Status.UP:
cmd = shlex.split(f"{self.exec_cmd()} {entrypoint}")
else:
cmd = shlex.split(self.start_cmd())
# The tag to run is always the last argument, so we insert the `--entrypoint`
# before then.
cmd = cmd[:len(cmd)-1] + [ "--entrypoint", entrypoint ] + cmd[len(cmd)-1:]
for a in args:
cmd.append(a)
subprocess.check_call(cmd)
def stop(self):
"""
Stops the container.
"""
if self.status() == Status.DOWN:
printLnFlush(f"Container {self.cid} isn't running.")
return
printFlush(f"Stopping {self.cid}")
subprocess.check_call(shlex.split(f"docker stop {self.fqn}"), stdout=subprocess.DEVNULL)
printLnFlush("Success")
def healthcheck_url(self) -> str:
"""
Returns a URL that can be used to determine if the container is healthy. The URL should
be one that's routable within the container.
"""
raise NotImplementedError()
def is_healthy(self) -> bool:
"""
Returns true if the container has been started and is healthy. Container health is
determined by making a HTTP request to the URL returned by healthcheck_url(). A non-200
response indicates that the container is not healthy.
"""
# We execute the command in the container since it might not be routable from the host.
p = subprocess.run(
shlex.split(f"docker exec {self.fqn} curl --fail {self.healthcheck_url()}"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return p.returncode == 0
def status(self) -> Status:
"""
Returns whether the container is up or down.
"""
out = subprocess.check_output(
shlex.split(f"docker ps --quiet --filter name=^{self.fqn}$"),
encoding="utf-8"
)
if len(out.strip()) > 0:
return Status.UP
return Status.DOWN
def logs(self, follow: bool = False):
"""
Outputs logs from the container. If follow is true logs are continuously streamed to
stdout.
"""
follow_arg = "--follow" if follow else ""
subprocess.check_call(shlex.split(f"docker logs {follow_arg} {self.fqn}"))
@staticmethod
def all() -> List[Tuple['Container', Status ]]:
"""
Returns a list of all containers that make up the AllenNLP Demo and their status.
"""
out = subprocess.check_output(
shlex.split(f"docker ps --format {{{{.Names}}}} --filter name=^{prefix}"),
encoding="utf-8"
)
running = set(out.splitlines())
containers = []
for container in [ UI(), Proxy() ]:
containers.append((container, Status.UP if container.fqn in running else Status.DOWN))
return [ *APIEndpoint.all(), *containers ]
@staticmethod
def from_id(cid: str) -> 'Container':
"""
Returns a Container instance from its ID.
"""
if cid == container_ids.proxy:
return Proxy()
if cid == container_ids.ui:
return UI()
if cid == container_ids.it:
return IT()
return APIEndpoint(cid)
class UI(Container):
"""
A special container that's used to serve the web GUI locally.
"""
def __init__(self):
super().__init__(container_ids.ui)
def build_cmd(self) -> str:
return f"""
docker build
--file {paths.ui / "Dockerfile.dev"}
--tag {self.tag}
{paths.ui}
"""
def start_cmd(self) -> str:
return f"""
docker run
--rm
--name {self.fqn}
--env NODE_ENV=development
--network {network}
--volume {paths.ui / "public"}:/ui/public
--volume {paths.ui / "src"}:/ui/src
{'--tty' if sys.stdout.isatty() else ''}
{self.tag}
"""
def healthcheck_url(self) -> str:
return "http://localhost:3000"
def exec_cmd(self) -> str:
return f"docker exec {'--tty' if sys.stdout.isatty() else ''} {self.fqn}"
class IT(Container):
"""
A special container that's used to run integration tests.
"""
def __init__(self):
super().__init__(container_ids.it)
def build_cmd(self) -> str:
return f"""
docker build
--file {paths.it / "Dockerfile"}
--tag {self.tag}
{paths.it}
"""
def start_cmd(self) -> str:
return f"""
docker run
--rm
--name {self.fqn}
--network {network}
--volume {paths.it / "cypress"}:/it/cypress
{'--tty' if sys.stdout.isatty() else ''}
{self.tag}
"""
def exec_cmd(self) -> str:
return f"docker exec {'--tty' if sys.stdout.isatty() else ''} {self.fqn}"
def healthcheck_url(self) -> str:
return ""
def is_healthy(self) -> bool:
return True
def start(self):
raise NotImplementedError(f"{self.cid} can't be started. Use './demo it' instead.")
class APIEndpoint(Container):
"""
The AllenNLP demo has a series of HTTP APIs, exposed at the `/api` path. Some of these
are model specific, and do things like servie predictions. Others return information about
the models and other miscellaneous metadata that's used by the demo.
"""
def __init__(self, cid: str):
super().__init__(cid)
self.path = paths.api_modules / cid
with open(self.path / "model.json", "r") as fh:
conf = json.load(fh)
self.model_id = conf["id"]
self.fqn = f"{api_prefix}{self.model_id}"
def urlpath(self) -> str:
"""
Returns the path portion of the URL that's routed to the endpoint.
"""
return f"/api/{self.model_id}"
def dockerfile(self) -> Path:
"""
Returns a path to the Dockerfile that's used to build the endpoint. Most endpoints use
the one that's in `api/`. Others have a `Dockerfile` that's a sibling to their code,
as to accomodate different AllenNLP versions and customized dependendencies.
"""
custom = self.path / "Dockerfile"
if custom.exists() and custom.is_file():
return custom
return paths.api / "Dockerfile"
def build_cmd(self) -> str:
return f"""
docker build
--file {self.dockerfile()}
--build-arg MODULE={self.cid}
--tag {self.tag}
{paths.api}
"""
def start_cmd(self) -> str:
home = Path.home()
return f"""
docker run
--rm
--name {self.fqn}
--env FLASK_ENV=development
--network {network}
--volume {self.path}:/app/allennlp_demo/{self.cid}
--volume {paths.api_modules / "common"}:/app/allennlp_demo/common
--volume {home / ".allennlp"}:/root/.allennlp
--volume {home / ".cache/huggingface"}:/root/.cache/huggingface
--volume {home / "nltk_data"}:/root/nltk_data
{self.tag}
"""
def healthcheck_url(self) -> str:
return "http://localhost:8000"
@staticmethod
def all() -> List[Tuple['APIEndpoint', Status]]:
"""
Returns a list of all APIEndpoints used by the demo and their status.
"""
out = subprocess.check_output(
shlex.split(f"docker ps --format {{{{.Names}}}} --filter name=^{api_prefix}"),
encoding="utf-8"
)
running = set(out.splitlines())
endpoints: List[Tuple['APIEndpoint', Status]] = []
for p in paths.api_modules.iterdir():
if not p.is_dir():
continue
if p.name == "common" or p.name.startswith(".") or p.name == "__pycache__":
continue
if len(list(p.iterdir())) == 0:
continue
endpoint = APIEndpoint(p.name)
status = Status.UP if endpoint.fqn in running else Status.DOWN
endpoints.append((endpoint, status))
return endpoints
@dataclass(frozen=True)
class ProxyRoute:
"""
A route that's handled by the reverse proxy.
"""
path: str
upstream: str
rewrites: List[str] = field(default_factory=list)
local_cid: Optional[str] = None
# The webserver that serves the UI locally uses websockets to tell the frontend about code
# changes and how to include them dynamically without refreshing the page. The reverse proxy
# has to include a special configuration snippet to support this.
enable_websockets: bool = False
hidden: bool = False
class InvalidHostnameError(Exception):
def __init__(self, hostname: str, message: str):
super().__init__(message)
self.hostname = hostname
class Proxy(Container):
"""
The AllenNLP Demo, as noted, is a collection of individual containers. This class captures
the functionality required to run a local reverse proxy that routes requests as appropriate.
In production we run something similar, but it's managed by the ReViz team as part of the
Skiff cluster.
"""
def __init__(self):
super().__init__(container_ids.proxy)
self.conf_dir = paths.run / "proxy"
self.conf_dir.mkdir(parents=True, exist_ok=True)
def build(self):
return
def start_cmd(self) -> str:
return f"""
docker run
--rm
--name {self.fqn}
--network {network}
--publish 8080:8080
--volume {self.conf_dir}:/etc/nginx/conf.d
nginx:1.17.10
"""
def healthcheck_url(self) -> str:
return "http://localhost:8080/health"
def is_healthy(self) -> bool:
# For the proxy we make requests from the host, since it's routable from there and
# we ultimately don't want to declare things healthy unless that's working.
try:
url = self.healthcheck_url()
resp: HTTPResponse = urlopen(url)
return resp.status == 204
except (URLError, RemoteDisconnected) as err:
return False
def routes(self) -> List[ProxyRoute]:
"""
Returns a list of routes that the proxy is responsible for.
"""
routes = []
for endpoint, status in APIEndpoint.all():
urlpath = endpoint.urlpath()
is_local = status == Status.UP
if is_local:
routes.append(ProxyRoute(
urlpath,
f"http://{endpoint.fqn}:8000",
[f"rewrite {urlpath}(/(.*))? /$2 break"],
local_cid=endpoint.cid
))
else:
# We can't use a single catch-all that forwards all requests to `/api`
# to production because some models share a prefix. For instance, `/api/bidaf`
# and `/api/bidaf-elmo` would both get sent to the same place if we made
# a rule matching `/api`. We could affix a trailing `/` to resolve this,
# but that makes NGINX behave in weird ways with respect to whether the
# trailing URL path is included in the forwarded request or not. So we avoid
# all of these headaches by simply having a route for everything that goes
# where we want it to.
routes.append(ProxyRoute(urlpath, "https://demo.allennlp.org"))
ui = UI()
if ui.status() == Status.UP:
routes.append(ProxyRoute(
"/sockjs-node",
f"http://{ui.fqn}:3000",
local_cid=ui.cid,
enable_websockets=True,
hidden=True
))
routes.append(ProxyRoute("/", f"http://{ui.fqn}:3000", local_cid=ui.cid))
return routes
def config(self, routes: List[ProxyRoute]) -> str:
"""
Returns the NGINX configuration for the provided sets of routes.
"""
locs = [
f"""
location /health {{
return 204;
}}
"""
]
for route in routes:
rewrites = ""
for rw in route.rewrites:
rewrites += f"{rw};\n"
extra = ""
if route.enable_websockets:
extra = f"""
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $remote_addr;
"""
# If we're proxying to something that uses SSL, set the server name to that of
# the remote resource. Otherwise SSL won't work.
upstream_url = urlparse(route.upstream)
host = upstream_url.netloc.split(":")[0]
# Make sure the hostname isn't longer than 63 characters:
# https://datatracker.ietf.org/doc/html/rfc1123#section-2
# TODO: There's probably other things about the hostname we should
# validate, but this is a good start.
if len(host) > 63:
raise InvalidHostnameError(host, f"{host} is too long, must be < 64 characters")
if upstream_url.scheme == "https":
extra += f"""
proxy_ssl_name {host};
proxy_ssl_server_name on;
"""
locs.append(f"""
location {route.path} {{
{rewrites}
proxy_pass {route.upstream};
proxy_set_header X-Forwarded-For $remote_addr;
# Add a header that clients can use to determine where a response is
# coming from.
add_header Via {route.upstream};
{extra}
}}
""")
l = "\n".join(locs)
return f"""
server {{
listen [::]:8080;
listen 8080;
charset utf-8;
# No HTTP caching.
expires -1;
{l}
}}
"""
def update_config(self, routes: List[ProxyRoute]):
"""
Updates the NGINX config on disk to reflect the provided list of routes.
"""
with open(self.conf_dir / "default.conf", "w") as fh:
fh.write(self.config(routes))
def reload(self):
"""
Reloads updated NGINX configuration settings by restarting the process.
"""
if self.status() != Status.UP:
raise RuntimeError(f"The proxy isn't up.")
printFlush("Updating proxy")
subprocess.check_call(
shlex.split(f"docker exec {self.fqn} nginx -s reload"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
printLnFlush("Success")
def upstart(self) -> List[ProxyRoute]:
"""
If the proxy isn't running, this method generates it's configuration files and restarts it.
If the proxy is running, this method updates the configuration files and applies them
without starting and stopping the container.
The name comes from the notion of an "upsert" in programs that use databases.
"""
routes = self.routes()
self.update_config(routes)
if self.status() == Status.UP:
self.reload()
else:
self.start()
return routes
def print_routes(self, routes: List[ProxyRoute]):
"""
Prints some information about the routes handled by the proxy and where they go.
"""
# Partition things into two buckets, those that go to local things and those that don't.
local = []
non_local = []
for r in routes:
if r.hidden:
continue
if r.local_cid is not None:
local.append((f"http://localhost:8080{r.path}", f"Local ({r.local_cid})"))
continue
non_local.append((f"http://localhost:8080{r.path}", r.upstream))
# Usually the user runs with 1 or 2 local routes, with everything else being pointed
# at production. This collapses things so that the non-local endpoints don't dominate
# the output.
if len(non_local) > 0:
non_local = [('http://localhost:8080/api/*', 'https://demo.allennlp.org')]
# This is a bit of code that figures out how to format things nicely given the length
# of the things we're outputting.
both = local + non_local
url_spaces = max(len(u) for u, _ in both)
target_spaces = max(len(t) for _, t in both)
fmt = "{:<" + str(url_spaces) + "}\t{:<" + str(target_spaces) + "}"
printLnFlush()
printLnFlush(fmt.format("Path", "Target"))
printLnFlush(fmt.format("-" * url_spaces, "-" * target_spaces))
for u, t in local:
printLnFlush(fmt.format(u, t))
for u, t in non_local:
printLnFlush(fmt.format(u, t))
def filter_cids(cids: List[str]) -> List[str]:
"""
Returns the provided ids without the proxy, since we manage the proxy explicitly.
"""
return [ cid for cid in cids if cid != container_ids.proxy ]
def start(args: argparse.Namespace):
"""
The start command is responsible for building and starting a list of containers, and
standing up a reverse proxy that sends requests to the right places.
"""
try:
for cid in filter_cids(args.container):
container = Container.from_id(cid)
if container.status() == Status.UP:
printLnFlush(f"Container {cid} is already running.")
continue
container.build()
container.start()
proxy = Proxy()
proxy.print_routes(proxy.upstart())
except StartError as err:
out = err.stdout + err.stderr
for line in out.splitlines():
printLnFlush(line)
sys.exit(1)
except BuildError:
sys.exit(1)
def stop(args: argparse.Namespace):
"""
The stop command stops containers. If no specific container is provided, all running
containers are stopped.
"""
cids: List[str] = args.container
# If no ids are provided, stop all running containers
if len(cids) == 0:
cids = [ c.cid for c, s in Container.all() if s == Status.UP ]
# Stop 'em
for cid in filter_cids(cids):
container = Container.from_id(cid)
container.stop()
proxy = Proxy()
# If only the proxy is left, stop it too, as it's pointless on it's own.
running = [ c for c, s in Container.all() if s == Status.UP ]
if len(running) == 1 and running[0].cid == container_ids.proxy:
proxy.stop()
return
proxy.print_routes(proxy.upstart())
def restart(args: argparse.Namespace):
"""
Restarts the specified containers by stopping them, building and then starting them.
"""
try:
cids: List[str] = args.container
# If no ids are provided, restart all running containers
if len(cids) == 0:
cids = [ c.cid for c, s in Container.all() if s == Status.UP ]
cids = filter_cids(cids)
# Stop everything first, so the user isn't confused by any interim, partial state.
for cid in cids:
container = Container.from_id(cid)
container.stop()
# Now build and start everything.
for cid in cids:
container = Container.from_id(cid)
container.build()
container.start()
proxy = Proxy()
proxy.print_routes(proxy.upstart())
except (StartError, BuildError):
sys.exit(1)
def status(args: argparse.Namespace):
"""
The status command prints some output about the containers that make up the AllenNLP
Demo and whether they're running or not.
"""
containers = Container.all()
spaces = max(len(container.cid) for container, _ in containers)
fmt = "{:<" + str(spaces) + "}\t{:<6}"
printLnFlush(fmt.format("Container", "Status"))
printLnFlush(fmt.format("-" * spaces, "------"))
for container, status in containers:
if args.all or status == Status.UP:
printLnFlush(fmt.format(container.cid, status.value))
proxy = Proxy()
proxy.print_routes(proxy.routes())
def view_logs(args: argparse.Namespace):
"""
Outputs logs for the specified container.
"""
try:
container = Container.from_id(args.container)
container.logs(args.follow)
except KeyboardInterrupt as err:
# We don't need to complain when this occurs.
return
def run_integration_tests(args: argparse.Namespace):
"""
Runs the integration tests.
"""
it = IT()
it.build()
it.run("yarn", "test:it")
def run(args: argparse.Namespace):
"""
Run a command in a container.
"""
container = Container.from_id(args.container)
container.run(args.entrypoint, *args.args)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="demo",
description="A utility for running the AllenNLP demo locally."
)
subparsers = parser.add_subparsers(
title="command",
description="the command to execute.",
dest="command"
)
start_parser = subparsers.add_parser(
"start",
help="starts a container"
)
start_parser.add_argument(
"container",
nargs="*",
help="the container or containers to start",
type=str,
default=[container_ids.ui]
)
start_parser.set_defaults(func=start)
stop_parser = subparsers.add_parser(
"stop",
help="stops a container"
)
stop_parser.add_argument(
"container",
nargs="*",
help="the container or containers to stop, if empty all running ones are stopped",
type=str
)
stop_parser.set_defaults(func=stop)
restart_parser = subparsers.add_parser(
"restart",
help="restarts a container"
)
restart_parser.add_argument(
"container",
nargs="*",
help="the container or containers to restart, if empty all running containers are restarted",
type=str
)
restart_parser.set_defaults(func=restart)
status_parser = subparsers.add_parser(
"status",
help="displays containers and their status"
)
status_parser.add_argument(
"--all",
"-a",
action="store_true",
help="show status for containers that are down too"
)
status_parser.set_defaults(func=status)
logs_parser = subparsers.add_parser(
"logs",
help="provides access to container logs"
)
logs_parser.add_argument(
"-f",
"--follow",
action="store_true",
help="don't stop at EOF"
)
logs_parser.add_argument(
"container",
help="the target container",
type=str
)
logs_parser.set_defaults(func=view_logs)
it_parser = subparsers.add_parser(
"it",
help="run integration tests"
)
it_parser.set_defaults(func=run_integration_tests)
run_parser = subparsers.add_parser(
"run",
help="run a command in a container"
)
run_parser.add_argument(
"container",
help="the container where you'd like to run the command",
type=str
)
run_parser.add_argument(
"entrypoint",
type=str,
help="the entrypoint to use"
)
run_parser.add_argument(
"args",
nargs="*",
type=str,
help="the command arguments"
)
run_parser.set_defaults(func=run)
args = parser.parse_args()
if 'func' not in args:
sys.stderr.write(f"Error: Unknown command.\n")
parser.print_usage()
sys.exit(1)
args.func(args)