| 1 |
# This code is part of the Ansible collection community.docker, but is an independent component. |
| 2 |
# This particular file, and this file only, is based on the Docker SDK for Python (https://github.com/docker/docker-py/) |
| 3 |
# |
| 4 |
# Copyright (c) 2016-2022 Docker, Inc. |
| 5 |
# |
| 6 |
# It is licensed under the Apache 2.0 license (see LICENSES/Apache-2.0.txt in this collection) |
| 7 |
# SPDX-License-Identifier: Apache-2.0 |
| 8 |
|
| 9 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 10 |
# Do not use this from other collections or standalone plugins/modules! |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import re |
| 15 |
import typing as t |
| 16 |
|
| 17 |
if t.TYPE_CHECKING: |
| 18 |
from collections.abc import Collection, Sequence |
| 19 |
|
| 20 |
|
| 21 |
PORT_SPEC = re.compile( |
| 22 |
"^" # Match full string |
| 23 |
"(" # External part |
| 24 |
r"(\[?(?P<host>[a-fA-F\d.:]+)\]?:)?" # Address |
| 25 |
r"(?P<ext>[\d]*)(-(?P<ext_end>[\d]+))?:" # External range |
| 26 |
")?" |
| 27 |
r"(?P<int>[\d]+)(-(?P<int_end>[\d]+))?" # Internal range |
| 28 |
"(?P<proto>/(udp|tcp|sctp))?" # Protocol |
| 29 |
"$" # Match full string |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def add_port_mapping( |
| 34 |
port_bindings: dict[str, list[str | tuple[str, str | None] | None]], |
| 35 |
internal_port: str, |
| 36 |
external: str | tuple[str, str | None] | None, |
| 37 |
) -> None: |
| 38 |
if internal_port in port_bindings: |
| 39 |
port_bindings[internal_port].append(external) |
| 40 |
else: |
| 41 |
port_bindings[internal_port] = [external] |
| 42 |
|
| 43 |
|
| 44 |
def add_port( |
| 45 |
port_bindings: dict[str, list[str | tuple[str, str | None] | None]], |
| 46 |
internal_port_range: list[str], |
| 47 |
external_range: list[str] | list[tuple[str, str | None]] | None, |
| 48 |
) -> None: |
| 49 |
if external_range is None: |
| 50 |
for internal_port in internal_port_range: |
| 51 |
add_port_mapping(port_bindings, internal_port, None) |
| 52 |
else: |
| 53 |
for internal_port, external_port in zip(internal_port_range, external_range): |
| 54 |
# mypy loses the exact type of eternal_port elements for some reason... |
| 55 |
add_port_mapping(port_bindings, internal_port, external_port) # type: ignore |
| 56 |
|
| 57 |
|
| 58 |
def build_port_bindings( |
| 59 |
ports: Collection[str], |
| 60 |
) -> dict[str, list[str | tuple[str, str | None] | None]]: |
| 61 |
port_bindings: dict[str, list[str | tuple[str, str | None] | None]] = {} |
| 62 |
for port in ports: |
| 63 |
internal_port_range, external_range = split_port(port) |
| 64 |
add_port(port_bindings, internal_port_range, external_range) |
| 65 |
return port_bindings |
| 66 |
|
| 67 |
|
| 68 |
def _raise_invalid_port(port: str) -> t.NoReturn: |
| 69 |
raise ValueError( |
| 70 |
f'Invalid port "{port}", should be ' |
| 71 |
"[[remote_ip:]remote_port[-remote_port]:]" |
| 72 |
"port[/protocol]" |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
@t.overload |
| 77 |
def port_range( |
| 78 |
start: str, |
| 79 |
end: str | None, |
| 80 |
proto: str, |
| 81 |
randomly_available_port: bool = False, |
| 82 |
) -> list[str]: ... |
| 83 |
|
| 84 |
|
| 85 |
@t.overload |
| 86 |
def port_range( |
| 87 |
start: str | None, |
| 88 |
end: str | None, |
| 89 |
proto: str, |
| 90 |
randomly_available_port: bool = False, |
| 91 |
) -> list[str] | None: ... |
| 92 |
|
| 93 |
|
| 94 |
def port_range( |
| 95 |
start: str | None, |
| 96 |
end: str | None, |
| 97 |
proto: str, |
| 98 |
randomly_available_port: bool = False, |
| 99 |
) -> list[str] | None: |
| 100 |
if start is None: |
| 101 |
return start |
| 102 |
if end is None: |
| 103 |
return [f"{start}{proto}"] |
| 104 |
if randomly_available_port: |
| 105 |
return [f"{start}-{end}{proto}"] |
| 106 |
return [f"{port}{proto}" for port in range(int(start), int(end) + 1)] |
| 107 |
|
| 108 |
|
| 109 |
def split_port( |
| 110 |
port: str | int, |
| 111 |
) -> tuple[list[str], list[str] | list[tuple[str, str | None]] | None]: |
| 112 |
port = str(port) |
| 113 |
match = PORT_SPEC.match(port) |
| 114 |
if match is None: |
| 115 |
_raise_invalid_port(port) |
| 116 |
parts = match.groupdict() |
| 117 |
|
| 118 |
host: str | None = parts["host"] |
| 119 |
proto: str = parts["proto"] or "" |
| 120 |
int_p: str = parts["int"] |
| 121 |
ext_p: str = parts["ext"] |
| 122 |
internal: list[str] = port_range(int_p, parts["int_end"], proto) # type: ignore |
| 123 |
external = port_range(ext_p or None, parts["ext_end"], "", len(internal) == 1) |
| 124 |
|
| 125 |
if host is None: |
| 126 |
if (external is not None and len(internal) != len(external)) or ext_p == "": |
| 127 |
raise ValueError("Port ranges don't match in length") |
| 128 |
return internal, external |
| 129 |
external_or_none: Sequence[str | None] |
| 130 |
if not external: |
| 131 |
external_or_none = [None] * len(internal) |
| 132 |
else: |
| 133 |
external_or_none = external |
| 134 |
if len(internal) != len(external_or_none): |
| 135 |
raise ValueError("Port ranges don't match in length") |
| 136 |
return internal, [(host, ext_port) for ext_port in external_or_none] |