mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
lxc: simplify to a single find_image(aliases) method
Replace the two-function find_relay_image / _find_relay_image pair with Incus.find_image(aliases), which returns the first alias that exists in the local image store, or None. Container.launch() passes [RELAY_IMAGE_ALIAS, BASE_IMAGE_ALIAS] and ensure_base_image() passes [BASE_IMAGE_ALIAS].
This commit is contained in:
@@ -4,7 +4,7 @@ import os
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from ..util import get_git_hash, get_version_string, shell
|
from ..util import get_git_hash, get_version_string, shell
|
||||||
from .incus import Incus, RelayContainer
|
from .incus import RELAY_IMAGE_ALIAS, Incus, RelayContainer
|
||||||
|
|
||||||
RELAY_NAMES = ("test0", "test1")
|
RELAY_NAMES = ("test0", "test1")
|
||||||
|
|
||||||
@@ -228,7 +228,7 @@ def lxc_test_cmd(args, out):
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
# Snapshot the first relay so subsequent ones launch pre-deployed
|
# Snapshot the first relay so subsequent ones launch pre-deployed
|
||||||
if not ix.find_relay_image():
|
if not ix.find_image([RELAY_IMAGE_ALIAS]):
|
||||||
with out.section("lxc-test: caching relay image"):
|
with out.section("lxc-test: caching relay image"):
|
||||||
ct.publish_as_relay_image()
|
ct.publish_as_relay_image()
|
||||||
|
|
||||||
|
|||||||
@@ -178,19 +178,15 @@ class Incus:
|
|||||||
return None
|
return None
|
||||||
return result.stdout.strip()
|
return result.stdout.strip()
|
||||||
|
|
||||||
def _find_image(self, alias):
|
def find_image(self, aliases):
|
||||||
"""Return *alias* if an image with that alias exists, else None."""
|
"""Return the first alias from *aliases* that exists, else None."""
|
||||||
images = self.run_json(["image", "list"], check=False) or []
|
images = self.run_json(["image", "list"], check=False) or []
|
||||||
for img in images:
|
existing = {a.get("name") for img in images for a in img.get("aliases", [])}
|
||||||
for a in img.get("aliases", []):
|
for alias in aliases:
|
||||||
if a.get("name") == alias:
|
if alias in existing:
|
||||||
return alias
|
return alias
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def find_relay_image(self):
|
|
||||||
"""Return the relay image alias if it exists, else None."""
|
|
||||||
return self._find_image(RELAY_IMAGE_ALIAS)
|
|
||||||
|
|
||||||
def delete_images(self):
|
def delete_images(self):
|
||||||
"""Delete the cached base and relay images."""
|
"""Delete the cached base and relay images."""
|
||||||
for alias in (RELAY_IMAGE_ALIAS, BASE_IMAGE_ALIAS):
|
for alias in (RELAY_IMAGE_ALIAS, BASE_IMAGE_ALIAS):
|
||||||
@@ -229,7 +225,7 @@ class Incus:
|
|||||||
slow apt-get install step.
|
slow apt-get install step.
|
||||||
Returns the image alias.
|
Returns the image alias.
|
||||||
"""
|
"""
|
||||||
if self._find_image(BASE_IMAGE_ALIAS):
|
if self.find_image([BASE_IMAGE_ALIAS]):
|
||||||
self.out.print(f" Base image '{BASE_IMAGE_ALIAS}' already cached.")
|
self.out.print(f" Base image '{BASE_IMAGE_ALIAS}' already cached.")
|
||||||
return BASE_IMAGE_ALIAS
|
return BASE_IMAGE_ALIAS
|
||||||
|
|
||||||
@@ -282,7 +278,7 @@ class Incus:
|
|||||||
class Container:
|
class Container:
|
||||||
"""The base container handle wraps all interactions with incus."""
|
"""The base container handle wraps all interactions with incus."""
|
||||||
|
|
||||||
def __init__(self, incus, name, domain=None, memory="100MiB"):
|
def __init__(self, incus, name, domain=None, memory="200MiB"):
|
||||||
self.incus = incus
|
self.incus = incus
|
||||||
self.out = incus.out
|
self.out = incus.out
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -322,7 +318,12 @@ class Container:
|
|||||||
|
|
||||||
def launch(self):
|
def launch(self):
|
||||||
"""Launch from the best available image, return the alias used."""
|
"""Launch from the best available image, return the alias used."""
|
||||||
image = self.incus.find_relay_image() or self.incus.ensure_base_image()
|
image = self.incus.find_image([RELAY_IMAGE_ALIAS, BASE_IMAGE_ALIAS])
|
||||||
|
if not image:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"No base image '{BASE_IMAGE_ALIAS}' found. "
|
||||||
|
"Call ensure_base_image() before launching containers."
|
||||||
|
)
|
||||||
self.out.print(f" Launching from '{image}' image ...")
|
self.out.print(f" Launching from '{image}' image ...")
|
||||||
cfg = []
|
cfg = []
|
||||||
cfg += ("-c", f"{LABEL_KEY}=true")
|
cfg += ("-c", f"{LABEL_KEY}=true")
|
||||||
@@ -412,7 +413,7 @@ class RelayContainer(Container):
|
|||||||
incus,
|
incus,
|
||||||
f"{name}-localchat",
|
f"{name}-localchat",
|
||||||
domain=f"_{name}{DOMAIN_SUFFIX}",
|
domain=f"_{name}{DOMAIN_SUFFIX}",
|
||||||
memory="500MiB",
|
memory="600MiB",
|
||||||
)
|
)
|
||||||
self.sname = name
|
self.sname = name
|
||||||
self.ini = incus.lxconfigs_dir / f"chatmail-{name}.ini"
|
self.ini = incus.lxconfigs_dir / f"chatmail-{name}.ini"
|
||||||
@@ -456,7 +457,7 @@ class RelayContainer(Container):
|
|||||||
|
|
||||||
Stops the container, 'publishes' it as 'localchat-relay', then restarts it.
|
Stops the container, 'publishes' it as 'localchat-relay', then restarts it.
|
||||||
"""
|
"""
|
||||||
if self.incus.find_relay_image():
|
if self.incus.find_image([RELAY_IMAGE_ALIAS]):
|
||||||
return
|
return
|
||||||
self.out.print(
|
self.out.print(
|
||||||
f" Locally caching {self.name!r} as '{RELAY_IMAGE_ALIAS}' image ..."
|
f" Locally caching {self.name!r} as '{RELAY_IMAGE_ALIAS}' image ..."
|
||||||
@@ -484,11 +485,7 @@ class RelayContainer(Container):
|
|||||||
return shell(cmd, timeout=15).returncode == 0
|
return shell(cmd, timeout=15).returncode == 0
|
||||||
|
|
||||||
def configure_dns(self, dns_ip):
|
def configure_dns(self, dns_ip):
|
||||||
"""Point this container's resolver at *dns_ip* and verify it works.
|
"""Point this container's resolver at *dns_ip* and verify it works."""
|
||||||
|
|
||||||
Disables systemd-resolved, writes a static /etc/resolv.conf,
|
|
||||||
and configures unbound to forward .localchat queries to *dns_ip*.
|
|
||||||
"""
|
|
||||||
self.bash(f"""\
|
self.bash(f"""\
|
||||||
systemctl disable --now systemd-resolved 2>/dev/null || true
|
systemctl disable --now systemd-resolved 2>/dev/null || true
|
||||||
rm -f /etc/resolv.conf
|
rm -f /etc/resolv.conf
|
||||||
|
|||||||
@@ -228,7 +228,6 @@ The Incus image store retains the following snapshot images:
|
|||||||
cached after the first successful ``cmdeploy run``.
|
cached after the first successful ``cmdeploy run``.
|
||||||
Subsequent relay containers launch from this image
|
Subsequent relay containers launch from this image
|
||||||
so the deploy step is mostly no-ops (roughly 3× faster than a fresh deploy).
|
so the deploy step is mostly no-ops (roughly 3× faster than a fresh deploy).
|
||||||
Relay containers are limited to **500 MiB RAM** and the DNS container to **100 MiB**.
|
|
||||||
|
|
||||||
|
|
||||||
.. _lxc-tls:
|
.. _lxc-tls:
|
||||||
|
|||||||
Reference in New Issue
Block a user