mirror of
https://github.com/Red5d/docker-autocompose
synced 2026-09-06 03:31:47 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
356cc632a3 |
@@ -43,7 +43,7 @@ jobs:
|
||||
uses: docker/setup-buildx-action@v1.3.0
|
||||
|
||||
- name: Cache Docker layers
|
||||
uses: actions/cache@v4
|
||||
uses: actions/cache@v2.1.6
|
||||
with:
|
||||
path: /tmp/.buildx-cache
|
||||
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||
|
||||
Generated
+10
-10
@@ -1,4 +1,4 @@
|
||||
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.
|
||||
# This file is automatically @generated by Poetry 2.1.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
@@ -152,18 +152,18 @@ websockets = ["websocket-client (>=1.3.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.15"
|
||||
version = "3.10"
|
||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
python-versions = ">=3.6"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "idna-3.15-py3-none-any.whl", hash = "sha256:048adeaf8c2d788c40fee287673ccaa74c24ffd8dcf09ffa555a2fbb59f10ac8"},
|
||||
{file = "idna-3.15.tar.gz", hash = "sha256:ca962446ea538f7092a95e057da437618e886f4d349216d2b1e294abfdb65fdc"},
|
||||
{file = "idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3"},
|
||||
{file = "idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
all = ["mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"]
|
||||
all = ["flake8 (>=7.1.1)", "mypy (>=1.11.2)", "pytest (>=8.3.2)", "ruff (>=0.6.2)"]
|
||||
|
||||
[[package]]
|
||||
name = "pyaml"
|
||||
@@ -277,19 +277,19 @@ files = [
|
||||
|
||||
[[package]]
|
||||
name = "requests"
|
||||
version = "2.32.3"
|
||||
version = "2.32.4"
|
||||
description = "Python HTTP for Humans."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "requests-2.32.3-py3-none-any.whl", hash = "sha256:70761cfe03c773ceb22aa2f671b4757976145175cdfca038c02654d061d6dcc6"},
|
||||
{file = "requests-2.32.3.tar.gz", hash = "sha256:55365417734eb18255590a9ff9eb97e9e1da868d4ccd6402399eaf68af20a760"},
|
||||
{file = "requests-2.32.4-py3-none-any.whl", hash = "sha256:27babd3cda2a6d50b30443204ee89830707d396671944c998b5975b031ac2b2c"},
|
||||
{file = "requests-2.32.4.tar.gz", hash = "sha256:27d0316682c8a29834d3264820024b62a36942083d52caf2f14c0591336d3422"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
certifi = ">=2017.4.17"
|
||||
charset-normalizer = ">=2,<4"
|
||||
charset_normalizer = ">=2,<4"
|
||||
idna = ">=2.5,<4"
|
||||
urllib3 = ">=1.21.1,<3"
|
||||
|
||||
|
||||
+8
-69
@@ -13,57 +13,6 @@ pyaml.add_representer(bool,lambda s,o: s.represent_scalar('tag:yaml.org,2002:boo
|
||||
IGNORE_VALUES = [None, "", [], "null", {}, "default", 0, ",", "no"]
|
||||
|
||||
|
||||
def format_compose_duration(value):
|
||||
if isinstance(value, int):
|
||||
return f"{value}ns"
|
||||
return value
|
||||
|
||||
|
||||
def build_service_networks(network_settings, default_networks):
|
||||
custom_networks = {}
|
||||
|
||||
for network_name, network_attributes in network_settings.items():
|
||||
if network_name in default_networks:
|
||||
continue
|
||||
|
||||
network_values = {}
|
||||
aliases = network_attributes.get("Aliases")
|
||||
if aliases:
|
||||
network_values["aliases"] = aliases
|
||||
|
||||
custom_networks[network_name] = network_values
|
||||
|
||||
if not custom_networks:
|
||||
return None, set()
|
||||
|
||||
has_network_config = any(network_values for network_values in custom_networks.values())
|
||||
if has_network_config:
|
||||
return custom_networks, set(custom_networks.keys())
|
||||
|
||||
return sorted(custom_networks.keys()), set(custom_networks.keys())
|
||||
|
||||
|
||||
def build_healthcheck(config):
|
||||
healthcheck = config.get("Healthcheck")
|
||||
if not isinstance(healthcheck, dict):
|
||||
return None
|
||||
|
||||
values = {}
|
||||
|
||||
if healthcheck.get("Test") not in IGNORE_VALUES:
|
||||
values["test"] = healthcheck.get("Test")
|
||||
if healthcheck.get("Interval") not in IGNORE_VALUES:
|
||||
values["interval"] = format_compose_duration(healthcheck.get("Interval"))
|
||||
if healthcheck.get("Timeout") not in IGNORE_VALUES:
|
||||
values["timeout"] = format_compose_duration(healthcheck.get("Timeout"))
|
||||
if healthcheck.get("Retries") not in IGNORE_VALUES:
|
||||
values["retries"] = healthcheck.get("Retries")
|
||||
if healthcheck.get("StartPeriod") not in IGNORE_VALUES:
|
||||
values["start_period"] = format_compose_duration(healthcheck.get("StartPeriod"))
|
||||
|
||||
return values
|
||||
|
||||
|
||||
def shell_escape_string(input_string):
|
||||
# Currently known issues:
|
||||
# - Basic Auth strings (e.g. set via Træfik labels) contain $ characters, which must be doubled. See https://stackoverflow.com/a/40621373/5885325
|
||||
@@ -220,8 +169,6 @@ def generate(cname, createvolumes=False):
|
||||
ct = cfile[cattrs.get("Name")[1:]]
|
||||
|
||||
default_networks = ["bridge", "host", "none"]
|
||||
network_settings = cattrs.get("NetworkSettings", {}).get("Networks", {})
|
||||
service_networks, attached_network_names = build_service_networks(network_settings, default_networks)
|
||||
|
||||
values = {
|
||||
"cap_drop": cattrs.get("HostConfig", {}).get("CapDrop", None),
|
||||
@@ -241,8 +188,9 @@ def generate(cname, createvolumes=False):
|
||||
"driver": cattrs.get("HostConfig", {}).get("LogConfig", {}).get("Type", None),
|
||||
"options": cattrs.get("HostConfig", {}).get("LogConfig", {}).get("Config", None),
|
||||
},
|
||||
"networks": service_networks,
|
||||
"healthcheck": build_healthcheck(cattrs.get("Config", {})),
|
||||
"networks": {
|
||||
x for x in cattrs.get("NetworkSettings", {}).get("Networks", {}).keys() if x not in default_networks
|
||||
},
|
||||
"security_opt": cattrs.get("HostConfig", {}).get("SecurityOpt"),
|
||||
"ulimits": cattrs.get("HostConfig", {}).get("Ulimits"),
|
||||
# the line below would not handle type bind
|
||||
@@ -256,16 +204,7 @@ def generate(cname, createvolumes=False):
|
||||
"domainname": cattrs.get("Config", {}).get("Domainname", None),
|
||||
"hostname": cattrs.get("Config", {}).get("Hostname", None),
|
||||
"ipc": cattrs.get("HostConfig", {}).get("IpcMode", None),
|
||||
"mac_address": cattrs.get("NetworkSettings", {}).get(
|
||||
"MacAddress"
|
||||
) or next(
|
||||
(
|
||||
net.get("MacAddress")
|
||||
for net in cattrs.get("NetworkSettings", {}).get("Networks", {}).values()
|
||||
if net.get("MacAddress")
|
||||
),
|
||||
None,
|
||||
),
|
||||
"mac_address": cattrs.get("NetworkSettings", {}).get("MacAddress", None),
|
||||
"privileged": cattrs.get("HostConfig", {}).get("Privileged", None),
|
||||
"restart": cattrs.get("HostConfig", {}).get("RestartPolicy", {}).get("Name", None),
|
||||
"read_only": cattrs.get("HostConfig", {}).get("ReadonlyRootfs", None),
|
||||
@@ -280,17 +219,17 @@ def generate(cname, createvolumes=False):
|
||||
]
|
||||
|
||||
networks = {}
|
||||
if not attached_network_names:
|
||||
if values["networks"] == set():
|
||||
del values["networks"]
|
||||
|
||||
if len(network_settings.keys()) > 0:
|
||||
assumed_default_network = list(network_settings.keys())[0]
|
||||
if len(cattrs.get("NetworkSettings", {}).get("Networks", {}).keys()) > 0:
|
||||
assumed_default_network = list(cattrs.get("NetworkSettings", {}).get("Networks", {}).keys())[0]
|
||||
values["network_mode"] = assumed_default_network
|
||||
networks = None
|
||||
else:
|
||||
networklist = c.networks.list()
|
||||
for network in networklist:
|
||||
if network.attrs["Name"] in attached_network_names:
|
||||
if network.attrs["Name"] in values["networks"]:
|
||||
networks[network.attrs["Name"]] = {
|
||||
"external": (not network.attrs["Internal"]),
|
||||
"name": network.attrs["Name"],
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from src import autocompose
|
||||
|
||||
|
||||
class FakeContainerSummary:
|
||||
name = "svc"
|
||||
short_id = "abc123"
|
||||
|
||||
|
||||
class FakeContainerWithAttrs:
|
||||
def __init__(self, attrs):
|
||||
self.attrs = attrs
|
||||
|
||||
|
||||
class FakeContainers:
|
||||
def __init__(self, attrs):
|
||||
self._attrs = attrs
|
||||
self._summary = FakeContainerSummary()
|
||||
|
||||
def list(self, **_kwargs):
|
||||
return [self._summary]
|
||||
|
||||
def get(self, _cid):
|
||||
return FakeContainerWithAttrs(self._attrs)
|
||||
|
||||
|
||||
class FakeNetwork:
|
||||
def __init__(self, name, internal=False):
|
||||
self.attrs = {"Name": name, "Internal": internal}
|
||||
|
||||
|
||||
class FakeNetworks:
|
||||
def __init__(self, networks):
|
||||
self._networks = [FakeNetwork(name, internal) for name, internal in networks]
|
||||
|
||||
def list(self):
|
||||
return self._networks
|
||||
|
||||
|
||||
class FakeDockerClient:
|
||||
def __init__(self, attrs, networks):
|
||||
self.containers = FakeContainers(attrs)
|
||||
self.networks = FakeNetworks(networks)
|
||||
|
||||
|
||||
def make_attrs(networks, healthcheck=None):
|
||||
return {
|
||||
"Name": "/svc",
|
||||
"HostConfig": {
|
||||
"CapDrop": None,
|
||||
"CgroupParent": None,
|
||||
"Dns": None,
|
||||
"DnsSearch": None,
|
||||
"ExtraHosts": None,
|
||||
"Links": None,
|
||||
"LogConfig": {"Type": None, "Config": None},
|
||||
"SecurityOpt": None,
|
||||
"Ulimits": None,
|
||||
"VolumeDriver": None,
|
||||
"VolumesFrom": None,
|
||||
"IpcMode": None,
|
||||
"Privileged": None,
|
||||
"RestartPolicy": {"Name": None},
|
||||
"ReadonlyRootfs": None,
|
||||
"Devices": None,
|
||||
"PortBindings": {},
|
||||
},
|
||||
"Config": {
|
||||
"Env": None,
|
||||
"Image": "test:latest",
|
||||
"Labels": {},
|
||||
"Entrypoint": None,
|
||||
"User": None,
|
||||
"WorkingDir": None,
|
||||
"Domainname": None,
|
||||
"Hostname": None,
|
||||
"OpenStdin": None,
|
||||
"Tty": None,
|
||||
"Cmd": None,
|
||||
"ExposedPorts": {},
|
||||
"Healthcheck": healthcheck,
|
||||
},
|
||||
"NetworkSettings": {"Networks": networks, "MacAddress": None},
|
||||
"Mounts": [],
|
||||
}
|
||||
|
||||
|
||||
class GenerateTests(unittest.TestCase):
|
||||
def test_preserves_network_aliases(self):
|
||||
attrs = make_attrs(
|
||||
{
|
||||
"custom_net": {
|
||||
"Aliases": ["svc", "db"],
|
||||
}
|
||||
}
|
||||
)
|
||||
fake_client = FakeDockerClient(attrs, networks=[("custom_net", False)])
|
||||
|
||||
with patch("src.autocompose.docker.from_env", return_value=fake_client):
|
||||
cfile, c_networks, _ = autocompose.generate("svc")
|
||||
|
||||
self.assertEqual(cfile["svc"]["networks"], {"custom_net": {"aliases": ["svc", "db"]}})
|
||||
self.assertEqual(
|
||||
c_networks,
|
||||
{"custom_net": {"external": True, "name": "custom_net"}},
|
||||
)
|
||||
|
||||
def test_serializes_healthcheck_config(self):
|
||||
attrs = make_attrs(
|
||||
{"custom_net": {}},
|
||||
healthcheck={
|
||||
"Test": ["CMD-SHELL", "echo ok"],
|
||||
"Interval": 1000000000,
|
||||
"Timeout": 2000000000,
|
||||
"Retries": 3,
|
||||
"StartPeriod": 3000000000,
|
||||
},
|
||||
)
|
||||
fake_client = FakeDockerClient(attrs, networks=[("custom_net", False)])
|
||||
|
||||
with patch("src.autocompose.docker.from_env", return_value=fake_client):
|
||||
cfile, _, _ = autocompose.generate("svc")
|
||||
|
||||
self.assertEqual(
|
||||
cfile["svc"]["healthcheck"],
|
||||
{
|
||||
"test": ["CMD-SHELL", "echo ok"],
|
||||
"interval": "1000000000ns",
|
||||
"timeout": "2000000000ns",
|
||||
"retries": 3,
|
||||
"start_period": "3000000000ns",
|
||||
},
|
||||
)
|
||||
|
||||
def test_simple_custom_network_uses_list_form(self):
|
||||
attrs = make_attrs({"custom_net": {}})
|
||||
fake_client = FakeDockerClient(attrs, networks=[("custom_net", True)])
|
||||
|
||||
with patch("src.autocompose.docker.from_env", return_value=fake_client):
|
||||
cfile, c_networks, _ = autocompose.generate("svc")
|
||||
|
||||
self.assertEqual(cfile["svc"]["networks"], ["custom_net"])
|
||||
self.assertEqual(
|
||||
c_networks,
|
||||
{"custom_net": {"external": False, "name": "custom_net"}},
|
||||
)
|
||||
|
||||
def test_default_network_still_uses_network_mode(self):
|
||||
attrs = make_attrs({"bridge": {}})
|
||||
fake_client = FakeDockerClient(attrs, networks=[("bridge", False)])
|
||||
|
||||
with patch("src.autocompose.docker.from_env", return_value=fake_client):
|
||||
cfile, c_networks, _ = autocompose.generate("svc")
|
||||
|
||||
self.assertEqual(cfile["svc"]["network_mode"], "bridge")
|
||||
self.assertNotIn("networks", cfile["svc"])
|
||||
self.assertIsNone(c_networks)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user