Skip to content

Commit d5c0e6d

Browse files
hpk42j4n
authored andcommitted
docker/lxc: replace CHATMAIL_NO* env vars with systemd-detect-virt detection
Cherry-pick runtime container detection from the hpk/lxcdeploy branch for the dovecot sysctl block and apply the same pattern to the port-availability check in deployers.py, and drop both CHATMAIL_NOSYSCTL and CHATMAIL_NOPORTCHECK from the container startup script and Dockerfile; the code now self-detects the context.
1 parent 289420b commit d5c0e6d

4 files changed

Lines changed: 21 additions & 20 deletions

File tree

cmdeploy/src/cmdeploy/deployers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Chat Mail pyinfra deploy.
33
"""
44

5-
import os
65
import shutil
76
import subprocess
87
import sys
@@ -14,6 +13,7 @@
1413
from pyinfra.api import FactBase
1514
from pyinfra.facts import hardware
1615
from pyinfra.facts.files import Sha256File
16+
from pyinfra.facts.server import Command
1717
from pyinfra.facts.systemd import SystemdEnabled
1818
from pyinfra.operations import apt, files, pip, server, systemd
1919

@@ -589,7 +589,7 @@ def deploy_chatmail(config_path: Path, disable_mail: bool, website_only: bool) -
589589
Out().red(f"Deploy failed: mtail_address {config.mtail_address} is not available (VPN up?).\n")
590590
exit(1)
591591

592-
if not os.environ.get("CHATMAIL_NOPORTCHECK"):
592+
if host.get_fact(Command, "systemd-detect-virt -c || true") == "none":
593593
port_services = [
594594
(["master", "smtpd"], 25),
595595
("unbound", 53),

cmdeploy/src/cmdeploy/dovecot/deployer.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import os
21
import urllib.request
32

43
from chatmaild.config import Config
54
from pyinfra import host
65
from pyinfra.facts.deb import DebPackages
7-
from pyinfra.facts.server import Arch, Sysctl
6+
from pyinfra.facts.server import Arch, Command, Sysctl
87
from pyinfra.operations import apt, files, server, systemd
98

109
from cmdeploy.basedeploy import (
@@ -153,19 +152,25 @@ def _configure_dovecot(config: Config, debug: bool = False) -> (bool, bool):
153152

154153
# as per https://doc.dovecot.org/2.3/configuration_manual/os/
155154
# it is recommended to set the following inotify limits
156-
if not os.environ.get("CHATMAIL_NOSYSCTL"):
157-
for name in ("max_user_instances", "max_user_watches"):
158-
key = f"fs.inotify.{name}"
159-
if host.get_fact(Sysctl)[key] > 65535:
160-
# Skip updating limits if already sufficient
161-
# (enables running in incus containers where sysctl readonly)
162-
continue
163-
server.sysctl(
164-
name=f"Change {key}",
165-
key=key,
166-
value=65535,
167-
persist=True,
155+
can_modify = host.get_fact(Command, "systemd-detect-virt -c || true") == "none"
156+
for name in ("max_user_instances", "max_user_watches"):
157+
key = f"fs.inotify.{name}"
158+
value = host.get_fact(Sysctl)[key]
159+
if value > 65534:
160+
continue
161+
if not can_modify:
162+
print(
163+
"\n!!!! refusing to attempt sysctl setting in shared-kernel containers\n"
164+
f"!!!! dovecot: sysctl {key!r}={value}, should be >65534 for production setups\n"
165+
"!!!!"
168166
)
167+
continue
168+
server.sysctl(
169+
name=f"Change {key}",
170+
key=key,
171+
value=65535,
172+
persist=True,
173+
)
169174

170175
timezone_env = files.line(
171176
name="Set TZ environment variable",

docker/chatmail-init.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
set -euo pipefail
44
export CHATMAIL_INI="${CHATMAIL_INI:-/etc/chatmail/chatmail.ini}"
5-
export CHATMAIL_NOSYSCTL=True
6-
export CHATMAIL_NOPORTCHECK=True
75

86
CMDEPLOY=/opt/cmdeploy/bin/cmdeploy
97

docker/chatmail_relay.dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ RUN printf '[params]\nmail_domain = build.local\n' > /tmp/chatmail.ini
5252

5353
RUN CMDEPLOY_STAGES=install \
5454
CHATMAIL_INI=/tmp/chatmail.ini \
55-
CHATMAIL_NOSYSCTL=True \
56-
CHATMAIL_NOPORTCHECK=True \
5755
/opt/cmdeploy/bin/pyinfra @local \
5856
/opt/chatmail/cmdeploy/src/cmdeploy/run.py -y
5957

0 commit comments

Comments
 (0)