forked from localstack/localstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.py
More file actions
1376 lines (1240 loc) · 51.1 KB
/
docker.py
File metadata and controls
1376 lines (1240 loc) · 51.1 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
import io
import json
import logging
import os
import shlex
import socket
import subprocess
import tarfile
import tempfile
from abc import ABCMeta, abstractmethod
from enum import Enum, unique
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union
import docker
from docker import DockerClient
from docker.errors import APIError, ContainerError, DockerException, ImageNotFound, NotFound
from docker.models.containers import Container
from docker.utils.socket import STDERR, STDOUT, frames_iter
from localstack import config
from localstack.utils.common import TMP_FILES, rm_rf, safe_run, save_file, short_uid, to_bytes
from localstack.utils.run import to_str
LOG = logging.getLogger(__name__)
SDK_ISDIR = 1 << 31
@unique
class DockerContainerStatus(Enum):
DOWN = -1
NON_EXISTENT = 0
UP = 1
class ContainerException(Exception):
def __init__(self, message=None, stdout=None, stderr=None) -> None:
self.message = message or "Error during the communication with the docker daemon"
self.stdout = stdout
self.stderr = stderr
class NoSuchObject(ContainerException):
def __init__(self, object_id: str, message=None, stdout=None, stderr=None) -> None:
message = message or f"Docker object {object_id} not found"
super().__init__(message, stdout, stderr)
self.object_id = object_id
class NoSuchContainer(ContainerException):
def __init__(self, container_name_or_id: str, message=None, stdout=None, stderr=None) -> None:
message = message or f"Docker container {container_name_or_id} not found"
super().__init__(message, stdout, stderr)
self.container_name_or_id = container_name_or_id
class NoSuchImage(ContainerException):
def __init__(self, image_name: str, message=None, stdout=None, stderr=None) -> None:
message = message or f"Docker image {image_name} not found"
super().__init__(message, stdout, stderr)
self.image_name = image_name
class PortMappings(object):
"""Maps source to target port ranges for Docker port mappings."""
class HashableList(list):
def __hash__(self):
result = 0
for i in self:
result += hash(i)
return result
def __init__(self, bind_host=None):
self.bind_host = bind_host if bind_host else ""
self.mappings = {}
def add(self, port, mapped=None, protocol="tcp"):
mapped = mapped or port
if isinstance(port, list):
for i in range(port[1] - port[0] + 1):
self.add(port[0] + i, mapped[0] + i)
return
if port is None or int(port) <= 0:
raise Exception("Unable to add mapping for invalid port: %s" % port)
if self.contains(port):
return
for from_range, to_range in self.mappings.items():
if not self.in_expanded_range(port, from_range):
continue
if not self.in_expanded_range(mapped, to_range):
continue
self.expand_range(port, from_range)
self.expand_range(mapped, to_range)
return
protocol = str(protocol or "tcp").lower()
self.mappings[self.HashableList([port, port, protocol])] = [mapped, mapped]
def to_str(self) -> str:
bind_address = f"{self.bind_host}:" if self.bind_host else ""
def entry(k, v):
protocol = "/%s" % k[2] if k[2] != "tcp" else ""
if k[0] == k[1] and v[0] == v[1]:
return "-p %s%s:%s%s" % (bind_address, k[0], v[0], protocol)
return "-p %s%s-%s:%s-%s%s" % (bind_address, k[0], k[1], v[0], v[1], protocol)
return " ".join([entry(k, v) for k, v in self.mappings.items()])
def to_list(self) -> List[str]: # TODO test
bind_address = f"{self.bind_host}:" if self.bind_host else ""
def entry(k, v):
protocol = "/%s" % k[2] if k[2] != "tcp" else ""
if k[0] == k[1] and v[0] == v[1]:
return ["-p", f"{bind_address}{k[0]}:{v[0]}{protocol}"]
return ["-p", f"{bind_address}{k[0]}-{k[1]}:{v[0]}-{v[1]}{protocol}"]
return [item for k, v in self.mappings.items() for item in entry(k, v)]
def to_dict(self) -> Dict[str, Union[Tuple[str, int], int]]:
bind_address = self.bind_host or ""
def entry(k, v):
protocol = "/%s" % k[2]
return [
(
f"{container_port}{protocol}",
(bind_address, host_port) if bind_address else host_port,
)
for container_port, host_port in zip(range(v[0], v[1] + 1), range(k[0], k[1] + 1))
]
items = [item for k, v in self.mappings.items() for item in entry(k, v)]
return dict(items)
def contains(self, port):
for from_range, to_range in self.mappings.items():
if self.in_range(port, from_range):
return True
def in_range(self, port, range):
return port >= range[0] and port <= range[1]
def in_expanded_range(self, port, range):
return port >= range[0] - 1 and port <= range[1] + 1
def expand_range(self, port, range):
if self.in_range(port, range):
return
if port == range[0] - 1:
range[0] = port
elif port == range[1] + 1:
range[1] = port
else:
raise Exception("Unable to add port %s to existing range %s" % (port, range))
class ContainerClient(metaclass=ABCMeta):
@abstractmethod
def get_container_status(self, container_name: str) -> DockerContainerStatus:
"""Returns the status of the container with the given name"""
pass
@abstractmethod
def get_network(self, container_name: str) -> str:
"""Returns the network mode of the container with the given name"""
pass
@abstractmethod
def stop_container(self, container_name: str):
"""Stops container with given name"""
pass
@abstractmethod
def remove_container(self, container_name: str, force=True, check_existence=False) -> None:
"""Removes container with given name"""
pass
@abstractmethod
def list_containers(self, filter: Union[List[str], str, None] = None, all=True) -> List[dict]:
"""List all containers matching the given filters
:return: A list of dicts with keys id, image, name, labels, status
"""
pass
def get_running_container_names(self) -> List[str]:
"""Returns a list of the names of all running containers"""
result = self.list_containers(all=False)
result = list(map(lambda container: container["name"], result))
return result
def is_container_running(self, container_name: str) -> bool:
"""Checks whether a container with a given name is currently running"""
return container_name in self.get_running_container_names()
@abstractmethod
def copy_into_container(
self, container_name: str, local_path: str, container_path: str
) -> None:
"""Copy contents of the given local path into the container"""
pass
@abstractmethod
def copy_from_container(
self, container_name: str, local_path: str, container_path: str
) -> None:
"""Copy contents of the given container to the host"""
pass
@abstractmethod
def pull_image(self, docker_image: str) -> None:
"""Pulls a image with a given name from a docker registry"""
pass
@abstractmethod
def get_docker_image_names(self, strip_latest=True, include_tags=True) -> List[str]:
"""
Get all names of docker images available to the container engine
:param strip_latest: return images both with and without :latest tag
:param include_tags: Include tags of the images in the names
:return: List of image names
"""
pass
@abstractmethod
def get_container_logs(self, container_name_or_id: str, safe=False) -> str:
"""Get all logs of a given container"""
pass
@abstractmethod
def inspect_container(self, container_name_or_id: str) -> Dict[str, Union[Dict, str]]:
"""Get detailed attributes of an container.
:return: Dict containing docker attributes as returned by the daemon
"""
pass
@abstractmethod
def inspect_image(self, image_name: str) -> Dict[str, Union[Dict, str]]:
"""Get detailed attributes of an image.
:return: Dict containing docker attributes as returned by the daemon
"""
pass
def get_container_name(self, container_id: str) -> str:
"""Get the name of a container by a given identifier"""
return self.inspect_container(container_id)["Name"].lstrip("/")
def get_container_id(self, container_name: str) -> str:
"""Get the id of a container by a given name"""
return self.inspect_container(container_name)["Id"]
@abstractmethod
def get_container_ip(self, container_name_or_id: str) -> str:
"""Get the IP address of a given container
If container has multiple networks, it will return the IP of the first
"""
pass
def get_image_cmd(self, docker_image: str) -> str:
"""Get the command for the given image"""
cmd_list = self.inspect_image(docker_image)["Config"]["Cmd"] or []
return " ".join(cmd_list)
def get_image_entrypoint(self, docker_image: str) -> str:
"""Get the entry point for the given image"""
LOG.debug("Getting the entrypoint for image: %s", docker_image)
entrypoint_list = self.inspect_image(docker_image)["Config"]["Entrypoint"] or []
return " ".join(entrypoint_list)
@abstractmethod
def has_docker(self) -> bool:
"""Check if system has docker available"""
pass
@abstractmethod
def create_container(
self,
image_name: str,
*,
name: Optional[str] = None,
entrypoint: Optional[str] = None,
remove: bool = False,
interactive: bool = False,
tty: bool = False,
detach: bool = False,
command: Optional[Union[List[str], str]] = None,
mount_volumes: Optional[List[Tuple[str, str]]] = None,
ports: Optional[PortMappings] = None,
env_vars: Optional[Dict[str, str]] = None,
user: Optional[str] = None,
cap_add: Optional[str] = None,
network: Optional[str] = None,
dns: Optional[str] = None,
additional_flags: Optional[str] = None,
) -> str:
"""Creates a container with the given image
:return: Container ID
"""
pass
@abstractmethod
def run_container(
self,
image_name: str,
stdin: bytes = None,
*,
name: Optional[str] = None,
entrypoint: Optional[str] = None,
remove: bool = False,
interactive: bool = False,
tty: bool = False,
detach: bool = False,
command: Optional[Union[List[str], str]] = None,
mount_volumes: Optional[List[Tuple[str, str]]] = None,
ports: Optional[PortMappings] = None,
env_vars: Optional[Dict[str, str]] = None,
user: Optional[str] = None,
cap_add: Optional[str] = None,
network: Optional[str] = None,
dns: Optional[str] = None,
additional_flags: Optional[str] = None,
) -> Tuple[bytes, bytes]:
"""Creates and runs a given docker container
:return: A tuple (stdout, stderr)
"""
pass
@abstractmethod
def exec_in_container(
self,
container_name_or_id: str,
command: Union[List[str], str],
interactive: bool = False,
detach: bool = False,
env_vars: Optional[Dict[str, str]] = None,
stdin: Optional[bytes] = None,
user: Optional[str] = None,
) -> Tuple[bytes, bytes]:
"""Execute a given command in a container
:return: A tuple (stdout, stderr)
"""
pass
@abstractmethod
def start_container(
self,
container_name_or_id: str,
stdin: bytes = None,
interactive: bool = False,
attach: bool = False,
flags: Optional[str] = None,
) -> Tuple[bytes, bytes]:
"""Start a given, already created container
:return: A tuple (stdout, stderr) if attach or interactive is set, otherwise a tuple (b"container_name_or_id", b"")
"""
pass
class CmdDockerClient(ContainerClient):
"""Class for managing docker containers using the command line executable"""
def _docker_cmd(self) -> List[str]:
"""Return the string to be used for running Docker commands."""
return config.DOCKER_CMD.split()
def get_container_status(self, container_name: str) -> DockerContainerStatus:
cmd = self._docker_cmd()
cmd += [
"ps",
"-a",
"--filter",
f"name={container_name}",
"--format",
"{{ .Status }} - {{ .Names }}",
]
cmd_result = safe_run(cmd)
# filter empty / invalid lines from docker ps output
cmd_result = next((line for line in cmd_result.splitlines() if container_name in line), "")
container_status = cmd_result.strip().lower()
if len(container_status) == 0:
return DockerContainerStatus.NON_EXISTENT
elif container_status.startswith("up "):
return DockerContainerStatus.UP
else:
return DockerContainerStatus.DOWN
def get_network(self, container_name: str) -> str:
LOG.debug("Getting container network: %s", container_name)
cmd = self._docker_cmd()
cmd += [
"inspect",
container_name,
"--format",
"{{ .HostConfig.NetworkMode }}",
]
try:
cmd_result = safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
container_network = cmd_result.strip()
return container_network
def stop_container(self, container_name: str) -> None:
cmd = self._docker_cmd()
cmd += ["stop", "-t0", container_name]
LOG.debug("Stopping container with cmd %s", cmd)
try:
safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def remove_container(self, container_name: str, force=True, check_existence=False) -> None:
if check_existence and container_name not in self.get_running_container_names():
return
cmd = self._docker_cmd() + ["rm"]
if force:
cmd.append("-f")
cmd.append(container_name)
LOG.debug("Removing container with cmd %s", cmd)
try:
safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def list_containers(self, filter: Union[List[str], str, None] = None, all=True) -> List[dict]:
filter = [filter] if isinstance(filter, str) else filter
cmd = self._docker_cmd()
cmd.append("ps")
if all:
cmd.append("-a")
options = []
if filter:
options += [y for filter_item in filter for y in ["--filter", filter_item]]
cmd += options
cmd.append("--format")
cmd.append(
'{"id":"{{ .ID }}","image":"{{ .Image }}","name":"{{ .Names }}",'
'"labels":"{{ .Labels }}","status":"{{ .State }}"}'
)
try:
cmd_result = safe_run(cmd).strip()
except subprocess.CalledProcessError as e:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
container_list = []
if cmd_result:
container_list = [json.loads(line) for line in cmd_result.splitlines()]
return container_list
def copy_into_container(
self, container_name: str, local_path: str, container_path: str
) -> None:
cmd = self._docker_cmd()
cmd += ["cp", local_path, f"{container_name}:{container_path}"]
LOG.debug("Copying into container with cmd: %s", cmd)
try:
safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name)
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def copy_from_container(
self, container_name: str, local_path: str, container_path: str
) -> None:
cmd = self._docker_cmd()
cmd += ["cp", f"{container_name}:{container_path}", local_path]
LOG.debug("Copying from container with cmd: %s", cmd)
try:
safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name)
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def pull_image(self, docker_image: str) -> None:
cmd = self._docker_cmd()
cmd += ["pull", docker_image]
LOG.debug("Pulling image with cmd: %s", cmd)
try:
safe_run(cmd)
except subprocess.CalledProcessError as e:
if "pull access denied" in to_str(e.stdout):
raise NoSuchImage(docker_image)
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def get_docker_image_names(self, strip_latest=True, include_tags=True):
format_string = "{{.Repository}}:{{.Tag}}" if include_tags else "{{.Repository}}"
cmd = self._docker_cmd()
cmd += ["images", "--format", format_string]
try:
output = safe_run(cmd)
image_names = output.splitlines()
if strip_latest:
Util.append_without_latest(image_names)
return image_names
except Exception as e:
LOG.info('Unable to list Docker images via "%s": %s' % (cmd, e))
return []
def get_container_logs(self, container_name_or_id: str, safe=False) -> str:
cmd = self._docker_cmd()
cmd += ["logs", container_name_or_id]
try:
return safe_run(cmd)
except subprocess.CalledProcessError as e:
if safe:
return ""
if "No such container" in to_str(e.stdout):
raise NoSuchContainer(container_name_or_id, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def _inspect_object(self, object_name_or_id: str) -> Dict[str, Union[Dict, str]]:
cmd = self._docker_cmd()
cmd += ["inspect", "--format", "{{json .}}", object_name_or_id]
try:
cmd_result = safe_run(cmd)
except subprocess.CalledProcessError as e:
if "No such object" in to_str(e.stdout):
raise NoSuchObject(object_name_or_id, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
image_data = json.loads(cmd_result.strip())
return image_data
def inspect_container(self, container_name_or_id: str) -> Dict[str, Union[Dict, str]]:
try:
return self._inspect_object(container_name_or_id)
except NoSuchObject as e:
raise NoSuchContainer(container_name_or_id=e.object_id)
def inspect_image(self, image_name: str) -> Dict[str, Union[Dict, str]]:
try:
return self._inspect_object(image_name)
except NoSuchObject as e:
raise NoSuchImage(image_name=e.object_id)
def get_container_ip(self, container_name_or_id: str) -> str:
cmd = self._docker_cmd()
cmd += [
"inspect",
"--format",
"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}",
container_name_or_id,
]
try:
return safe_run(cmd).strip()
except subprocess.CalledProcessError as e:
if "No such object" in to_str(e.stdout):
raise NoSuchContainer(container_name_or_id, stdout=e.stdout, stderr=e.stderr)
else:
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def has_docker(self) -> bool:
try:
safe_run(self._docker_cmd() + ["ps"])
return True
except subprocess.CalledProcessError:
return False
def create_container(self, image_name: str, **kwargs) -> str:
cmd, env_file = self._build_run_create_cmd("create", image_name, **kwargs)
LOG.debug("Create container with cmd: %s", cmd)
try:
container_id = safe_run(cmd)
# Note: strip off Docker warning messages like "DNS setting (--dns=127.0.0.1) may fail in containers"
container_id = container_id.strip().split("\n")[-1]
return container_id.strip()
except subprocess.CalledProcessError as e:
if "Unable to find image" in to_str(e.stdout):
raise NoSuchImage(image_name, stdout=e.stdout, stderr=e.stderr)
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
finally:
Util.rm_env_vars_file(env_file)
def run_container(self, image_name: str, stdin=None, **kwargs) -> Tuple[bytes, bytes]:
cmd, env_file = self._build_run_create_cmd("run", image_name, **kwargs)
LOG.debug("Run container with cmd: %s", cmd)
result = self._run_async_cmd(cmd, stdin, kwargs.get("name") or "", image_name)
Util.rm_env_vars_file(env_file)
return result
def exec_in_container(
self,
container_name_or_id: str,
command: Union[List[str], str],
interactive=False,
detach=False,
env_vars: Optional[Dict[str, str]] = None,
stdin: Optional[bytes] = None,
user: Optional[str] = None,
) -> Tuple[bytes, bytes]:
env_file = None
cmd = self._docker_cmd()
cmd.append("exec")
if interactive:
cmd.append("--interactive")
if detach:
cmd.append("--detach")
if user:
cmd += ["--user", user]
if env_vars:
env_flag, env_file = Util.create_env_vars_file_flag(env_vars)
cmd += env_flag
cmd.append(container_name_or_id)
cmd += command if isinstance(command, List) else [command]
LOG.debug("Execute in container cmd: %s", cmd)
result = self._run_async_cmd(cmd, stdin, container_name_or_id)
Util.rm_env_vars_file(env_file)
return result
def start_container(
self,
container_name_or_id: str,
stdin=None,
interactive: bool = False,
attach: bool = False,
flags: Optional[str] = None,
) -> Tuple[bytes, bytes]:
cmd = self._docker_cmd() + ["start"]
if flags:
cmd.append(flags)
if interactive:
cmd.append("--interactive")
if attach:
cmd.append("--attach")
cmd.append(container_name_or_id)
LOG.debug("Start container with cmd: %s", cmd)
return self._run_async_cmd(cmd, stdin, container_name_or_id)
def _run_async_cmd(
self, cmd: List[str], stdin: bytes, container_name: str, image_name=None
) -> Tuple[bytes, bytes]:
kwargs = {
"inherit_env": True,
"asynchronous": True,
"stderr": subprocess.PIPE,
"outfile": subprocess.PIPE,
}
if stdin:
kwargs["stdin"] = True
try:
process = safe_run(cmd, **kwargs)
stdout, stderr = process.communicate(input=stdin)
if process.returncode != 0:
raise subprocess.CalledProcessError(
process.returncode,
cmd,
stdout,
stderr,
)
else:
return stdout, stderr
except subprocess.CalledProcessError as e:
stderr_str = to_str(e.stderr)
if "Unable to find image" in stderr_str:
raise NoSuchImage(image_name or "", stdout=e.stdout, stderr=e.stderr)
if "No such container" in stderr_str:
raise NoSuchContainer(container_name, stdout=e.stdout, stderr=e.stderr)
raise ContainerException(
"Docker process returned with errorcode %s" % e.returncode, e.stdout, e.stderr
)
def _build_run_create_cmd(
self,
action: str,
image_name: str,
*,
name: Optional[str] = None,
entrypoint: Optional[str] = None,
remove: bool = False,
interactive: bool = False,
tty: bool = False,
detach: bool = False,
command: Optional[Union[List[str], str]] = None,
mount_volumes: Optional[List[Tuple[str, str]]] = None,
ports: Optional[PortMappings] = None,
env_vars: Optional[Dict[str, str]] = None,
user: Optional[str] = None,
cap_add: Optional[str] = None,
network: Optional[str] = None,
dns: Optional[str] = None,
additional_flags: Optional[str] = None,
) -> Tuple[List[str], str]:
env_file = None
cmd = self._docker_cmd() + [action]
if remove:
cmd.append("--rm")
if name:
cmd += ["--name", name]
if entrypoint is not None: # empty string entrypoint can be intentional
cmd += ["--entrypoint", entrypoint]
if mount_volumes:
cmd += [
volume
for host_path, docker_path in mount_volumes
for volume in ["-v", f"{host_path}:{docker_path}"]
]
if interactive:
cmd.append("--interactive")
if tty:
cmd.append("--tty")
if detach:
cmd.append("--detach")
if ports:
cmd += ports.to_list()
if env_vars:
env_flags, env_file = Util.create_env_vars_file_flag(env_vars)
cmd += env_flags
if user:
cmd += ["--user", user]
if cap_add:
cmd += ["--cap-add", cap_add]
if network:
cmd += ["--network", network]
if dns:
cmd += ["--dns", dns]
if additional_flags:
cmd += shlex.split(additional_flags)
cmd.append(image_name)
if command:
cmd += command if isinstance(command, List) else [command]
return cmd, env_file
class Util:
MAX_ENV_ARGS_LENGTH = 20000
@classmethod
def create_env_vars_file_flag(cls, env_vars: Dict) -> Tuple[List[str], Optional[str]]:
if not env_vars:
return [], None
result = []
env_vars = dict(env_vars)
env_file = None
if len(str(env_vars)) > cls.MAX_ENV_ARGS_LENGTH:
# default ARG_MAX=131072 in Docker - let's create an env var file if the string becomes too long...
env_file = cls.mountable_tmp_file()
env_content = ""
for name, value in dict(env_vars).items():
if len(value) > cls.MAX_ENV_ARGS_LENGTH:
# each line in the env file has a max size as well (error "bufio.Scanner: token too long")
continue
env_vars.pop(name)
value = value.replace("\n", "\\")
env_content += "%s=%s\n" % (name, value)
save_file(env_file, env_content)
result += ["--env-file", env_file]
env_vars_res = [item for k, v in env_vars.items() for item in ["-e", "{}={}".format(k, v)]]
result += env_vars_res
return result, env_file
@staticmethod
def rm_env_vars_file(env_vars_file) -> None:
if env_vars_file:
return rm_rf(env_vars_file)
@staticmethod
def mountable_tmp_file():
f = os.path.join(config.TMP_FOLDER, short_uid())
TMP_FILES.append(f)
return f
@staticmethod
def append_without_latest(image_names):
suffix = ":latest"
for image in list(image_names):
if image.endswith(suffix):
image_names.append(image[: -len(suffix)])
@staticmethod
def tar_path(path, target_path, is_dir: bool):
f = tempfile.NamedTemporaryFile()
with tarfile.open(mode="w", fileobj=f) as t:
abs_path = os.path.abspath(path)
arcname = (
os.path.basename(path)
if is_dir
else (os.path.basename(target_path) or os.path.basename(path))
)
t.add(abs_path, arcname=arcname)
f.seek(0)
return f
@staticmethod
def untar_to_path(tardata, target_path):
target_path = Path(target_path)
with tarfile.open(mode="r", fileobj=io.BytesIO(b"".join(b for b in tardata))) as t:
if target_path.is_dir():
t.extractall(path=target_path)
else:
member = t.next()
if member:
member.name = target_path.name
t.extract(member, target_path.parent)
else:
LOG.debug("File to copy empty, ignoring...")
@staticmethod
def parse_additional_flags(
additional_flags: str,
env_vars: Dict[str, str] = None,
ports: PortMappings = None,
mounts: List[Tuple[str, str]] = None,
) -> Tuple[Dict[str, str], PortMappings, List[Tuple[str, str]], Optional[Dict[str, str]]]:
"""Parses environment, volume and port flags passed as string
:param additional_flags: String which contains the flag definitions
:param env_vars: Dict with env vars. Will be modified in place.
:param ports: PortMapping object. Will be modified in place.
:param mounts: List of mount tuples (host_path, container_path). Will be modified in place.
:return: A tuple containing the env_vars, ports and mount objects. Will return new objects if respective
parameters were None and additional flags contained a flag for that object, the same which are passed
otherwise.
"""
cur_state = None
extra_hosts = None
# TODO Use argparse to simplify this logic
for flag in shlex.split(additional_flags):
if not cur_state:
if flag in ["-v", "--volume"]:
cur_state = "volume"
elif flag in ["-p", "--publish"]:
cur_state = "port"
elif flag in ["-e", "--env"]:
cur_state = "env"
elif flag == "--add-host":
cur_state = "add-host"
else:
raise NotImplementedError(
"Flag %s is currently not supported by this docker client."
)
else:
if cur_state == "volume":
mounts = mounts if mounts is not None else []
mounts.append(tuple(flag.split(":")))
elif cur_state == "port":
port_split = flag.split(":")
protocol = "tcp"
if len(port_split) == 2:
host_port, container_port = port_split
elif len(port_split) == 3:
LOG.warning(
"Host part of port mappings are ignored currently in additional flags"
)
_, host_port, container_port = port_split
else:
raise ValueError("Invalid port string provided: %s", flag)
if "/" in container_port:
container_port, protocol = container_port.split("/")
ports = ports if ports is not None else PortMappings()
ports.add(int(host_port), int(container_port), protocol)
elif cur_state == "env":
env_split = flag.split("=")
env_vars = env_vars if env_vars is not None else {}
env_vars[env_split[0]] = env_split[1]
elif cur_state == "add-host":
extra_hosts = extra_hosts if extra_hosts is not None else {}
hosts_split = flag.split(":")
extra_hosts[hosts_split[0]] = hosts_split[1]
cur_state = None
return env_vars, ports, mounts, extra_hosts
@staticmethod
def convert_mount_list_to_dict(
mount_volumes: List[Tuple[str, str]]
) -> Dict[str, Dict[str, str]]:
"""Converts a List of (host_path, container_path) tuples to a Dict suitable as volume argument for docker sdk"""
return dict(
map(
lambda paths: (str(paths[0]), {"bind": paths[1], "mode": "rw"}),
mount_volumes,
)
)
class SdkDockerClient(ContainerClient):
"""Class for managing docker using the python docker sdk"""
docker_client: Optional[DockerClient]
def __init__(self):
try:
self.docker_client = docker.from_env()
logging.getLogger("urllib3").setLevel(logging.INFO)
except DockerException:
self.docker_client = None
def client(self):
if self.docker_client:
return self.docker_client
else:
raise ContainerException("Docker not available")
def _read_from_sock(self, sock: socket, tty: bool):
"""Reads multiplexed messages from a socket returned by attach_socket.
Uses the protocol specified here: https://docs.docker.com/engine/api/v1.41/#operation/ContainerAttach
"""
stdout = b""
stderr = b""
for frame_type, frame_data in frames_iter(sock, tty):
if frame_type == STDOUT:
stdout += frame_data
elif frame_type == STDERR:
stderr += frame_data
else:
raise ContainerException("Invalid frame type when reading from socket")
return stdout, stderr
def _container_path_info(self, container: Container, container_path: str):
"""
Get information about a path in the given container
:param container: Container to be inspected
:param container_path: Path in container
:return: Tuple (path_exists, path_is_directory)
"""
# Docker CLI copy uses go FileMode to determine if target is a dict or not
# https://github.com/docker/cli/blob/e3dfc2426e51776a3263cab67fbba753dd3adaa9/cli/command/container/cp.go#L260
# The isDir Bit is the most significant bit in the 32bit struct:
# https://golang.org/src/os/types.go?s=2650:2683
stats = {}
try:
_, stats = container.get_archive(container_path)
target_exists = True
except APIError:
target_exists = False
target_is_dir = target_exists and bool(stats["mode"] & SDK_ISDIR)
return target_exists, target_is_dir
def get_container_status(self, container_name: str) -> DockerContainerStatus:
# LOG.debug("Getting container status for container: %s", container_name) # too verbose
try:
container = self.client().containers.get(container_name)
if container.status == "running":
return DockerContainerStatus.UP
else:
return DockerContainerStatus.DOWN
except NotFound:
return DockerContainerStatus.NON_EXISTENT
except APIError:
raise ContainerException()
def get_network(self, container_name: str) -> str:
LOG.debug("Getting network type for container: %s", container_name)
try:
container = self.client().containers.get(container_name)
return container.attrs["HostConfig"]["NetworkMode"]
except NotFound:
raise NoSuchContainer(container_name)
except APIError:
raise ContainerException()