mirror of
https://github.com/chatmail/relay.git
synced 2026-05-13 01:24:36 +00:00
Compare commits
22 Commits
j4n/docker
...
j4n/docker
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b50019aa2a | ||
|
|
7606612091 | ||
|
|
2b12ed1ca1 | ||
|
|
40051f7ac3 | ||
|
|
e45d2b99e4 | ||
|
|
38246ca8ea | ||
|
|
2635ac7e6d | ||
|
|
4fabfb31f8 | ||
|
|
36478dbfcf | ||
|
|
ff541b81ea | ||
|
|
ed9b4092a8 | ||
|
|
1b8ad3ca12 | ||
|
|
f85d304e65 | ||
|
|
4d1856d8f1 | ||
|
|
ae2ab52aa9 | ||
|
|
d0c396538b | ||
|
|
78a4e28408 | ||
|
|
2432d4f498 | ||
|
|
31301abb42 | ||
|
|
6b4edd8502 | ||
|
|
9c467ab3e8 | ||
|
|
774350778b |
2
.github/workflows/ci.yaml
vendored
2
.github/workflows/ci.yaml
vendored
@@ -15,7 +15,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
ref: ${{ github.event.pull_request.head.sha }}
|
ref: ${{ github.event.pull_request.head.sha }}
|
||||||
- name: download filtermail
|
- name: download filtermail
|
||||||
run: curl -L https://github.com/chatmail/filtermail/releases/download/v0.5.2/filtermail-x86_64 -o /usr/local/bin/filtermail && chmod +x /usr/local/bin/filtermail
|
run: curl -L https://github.com/chatmail/filtermail/releases/download/v0.6.0/filtermail-x86_64 -o /usr/local/bin/filtermail && chmod +x /usr/local/bin/filtermail
|
||||||
- name: run chatmaild tests
|
- name: run chatmaild tests
|
||||||
working-directory: chatmaild
|
working-directory: chatmaild
|
||||||
run: pipx run tox
|
run: pipx run tox
|
||||||
|
|||||||
375
.github/workflows/deploy.yaml
vendored
375
.github/workflows/deploy.yaml
vendored
@@ -1,375 +0,0 @@
|
|||||||
name: Deploy
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
- j4n/docker-pr
|
|
||||||
pull_request:
|
|
||||||
paths-ignore:
|
|
||||||
- 'scripts/**'
|
|
||||||
- '**/README.md'
|
|
||||||
- 'CHANGELOG.md'
|
|
||||||
- 'LICENSE'
|
|
||||||
|
|
||||||
env:
|
|
||||||
REGISTRY: ghcr.io
|
|
||||||
IMAGE_NAME: ${{ github.repository }}
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-docker:
|
|
||||||
name: Build Docker image
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
packages: write
|
|
||||||
outputs:
|
|
||||||
image: ${{ steps.image-ref.outputs.image }}
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Login to GHCR
|
|
||||||
if: github.event_name == 'push'
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ${{ env.REGISTRY }}
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata (tags, labels)
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
# Tagged releases: v1.2.3 -> :1.2.3, :1.2, :latest
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
# Branch pushes: foo/docker-pr -> :foo-docker-pr
|
|
||||||
type=ref,event=branch
|
|
||||||
# Always: :sha-<hash>
|
|
||||||
type=sha
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: docker/chatmail_relay.dockerfile
|
|
||||||
push: ${{ github.event_name == 'push' }}
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
build-args: |
|
|
||||||
GIT_HASH=${{ github.sha }}
|
|
||||||
|
|
||||||
- name: Output image reference
|
|
||||||
id: image-ref
|
|
||||||
run: |
|
|
||||||
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
|
||||||
IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
|
||||||
echo "image=${IMAGE}" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
name: Deploy to ${{ matrix.host }}
|
|
||||||
needs: build-docker
|
|
||||||
# dont do the regular tests on this branch
|
|
||||||
if: >-
|
|
||||||
!cancelled() && (
|
|
||||||
github.event_name == 'push' ||
|
|
||||||
(github.event_name == 'pull_request' && !startsWith(github.head_ref, 'j4n/'))
|
|
||||||
)
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 60
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
include:
|
|
||||||
- host: staging2.testrun.org
|
|
||||||
acme_dir: acme
|
|
||||||
dkim_dir: dkimkeys
|
|
||||||
zone_file: staging.testrun.org-default.zone
|
|
||||||
disable_ipv6: false
|
|
||||||
add_ssh_keys: true
|
|
||||||
- host: staging-ipv4.testrun.org
|
|
||||||
acme_dir: acme-ipv4
|
|
||||||
dkim_dir: dkimkeys-ipv4
|
|
||||||
zone_file: staging-ipv4.testrun.org-default.zone
|
|
||||||
disable_ipv6: true
|
|
||||||
add_ssh_keys: false
|
|
||||||
environment:
|
|
||||||
name: ${{ matrix.host }}
|
|
||||||
url: https://${{ matrix.host }}/
|
|
||||||
concurrency: ${{ matrix.host }}
|
|
||||||
steps:
|
|
||||||
# --- Common setup ---
|
|
||||||
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: prepare SSH and save ACME/DKIM
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ACME_DIR: ${{ matrix.acme_dir }}
|
|
||||||
DKIM_DIR: ${{ matrix.dkim_dir }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
mkdir ~/.ssh
|
|
||||||
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
|
||||||
chmod 600 ~/.ssh/id_ed25519
|
|
||||||
ssh-keyscan ${HOST} > ~/.ssh/known_hosts
|
|
||||||
# save previous acme & dkim state (trailing slash = copy contents)
|
|
||||||
rsync -avz root@${HOST}:/var/lib/acme/ ${ACME_DIR}/ || true
|
|
||||||
rsync -avz root@${HOST}:/etc/dkimkeys/ ${DKIM_DIR}/ || true
|
|
||||||
# backup to ns.testrun.org if contents are useful
|
|
||||||
if [ -f ${DKIM_DIR}/opendkim.private ]; then
|
|
||||||
rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" ${DKIM_DIR}/ root@ns.testrun.org:/tmp/${DKIM_DIR}/ || true
|
|
||||||
fi
|
|
||||||
if [ "$(ls -A ${ACME_DIR}/certs 2>/dev/null)" ]; then
|
|
||||||
rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" ${ACME_DIR}/ root@ns.testrun.org:/tmp/${ACME_DIR}/ || true
|
|
||||||
fi
|
|
||||||
# make sure CAA record isn't set
|
|
||||||
scp -o StrictHostKeyChecking=accept-new .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: rebuild VPS
|
|
||||||
env:
|
|
||||||
SERVER_ID: ${{ matrix.host == 'staging2.testrun.org' && secrets.STAGING_SERVER_ID || secrets.STAGING_IPV4_SERVER_ID }}
|
|
||||||
run: |
|
|
||||||
curl -X POST \
|
|
||||||
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-d '{"image":"debian-12"}' \
|
|
||||||
"https://api.hetzner.cloud/v1/servers/${SERVER_ID}/actions/rebuild"
|
|
||||||
|
|
||||||
- run: scripts/initenv.sh
|
|
||||||
- name: append venv/bin to PATH
|
|
||||||
run: echo venv/bin >>$GITHUB_PATH
|
|
||||||
|
|
||||||
- name: wait for VPS rebuild
|
|
||||||
id: wait-for-vps
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
rm ~/.ssh/known_hosts
|
|
||||||
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new root@${HOST} id -u ; do sleep 1 ; done
|
|
||||||
|
|
||||||
- name: restore ACME/DKIM
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ACME_DIR: ${{ matrix.acme_dir }}
|
|
||||||
DKIM_DIR: ${{ matrix.dkim_dir }}
|
|
||||||
run: |
|
|
||||||
# download from ns.testrun.org
|
|
||||||
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/${ACME_DIR}/ acme-restore/ || true
|
|
||||||
rsync -avz root@ns.testrun.org:/tmp/${DKIM_DIR}/ dkimkeys-restore/ || true
|
|
||||||
# restore to VPS
|
|
||||||
rsync -avz acme-restore/ root@${HOST}:/var/lib/acme/ || true
|
|
||||||
rsync -avz dkimkeys-restore/ root@${HOST}:/etc/dkimkeys/ || true
|
|
||||||
ssh root@${HOST} chown root:root -R /var/lib/acme || true
|
|
||||||
|
|
||||||
- name: bare offline tests
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
run: pytest --pyargs cmdeploy
|
|
||||||
|
|
||||||
- name: bare deploy
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
DISABLE_IPV6: ${{ matrix.disable_ipv6 }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'apt update && apt install -y git python3.11-venv python3-dev gcc'
|
|
||||||
ssh root@${HOST} 'git clone https://github.com/chatmail/relay'
|
|
||||||
ssh root@${HOST} "cd relay && git checkout ${{ github.head_ref || github.ref_name }}"
|
|
||||||
ssh root@${HOST} 'cd relay && scripts/initenv.sh'
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy init ${HOST}"
|
|
||||||
if [ "${DISABLE_IPV6}" = "true" ]; then
|
|
||||||
ssh root@${HOST} "sed -i 's#disable_ipv6 = False#disable_ipv6 = True#' relay/chatmail.ini"
|
|
||||||
fi
|
|
||||||
ssh root@${HOST} "sed -i 's/#\s*mtail_address/mtail_address/' relay/chatmail.ini"
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy run --verbose --skip-dns-check --ssh-host localhost"
|
|
||||||
|
|
||||||
- name: bare DNS
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} chown opendkim:opendkim -R /etc/dkimkeys
|
|
||||||
ssh root@${HOST} "cd relay && scripts/cmdeploy dns --zonefile staging-generated.zone --ssh-host localhost"
|
|
||||||
ssh root@${HOST} cat relay/staging-generated.zone >> .github/workflows/${ZONE}
|
|
||||||
cat .github/workflows/${ZONE}
|
|
||||||
scp .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: bare integration tests
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} "cd relay && CHATMAIL_DOMAIN2=ci-chatmail.testrun.org scripts/cmdeploy test --slow --ssh-host localhost"
|
|
||||||
|
|
||||||
- name: bare final DNS check
|
|
||||||
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} "cd relay && scripts/cmdeploy dns -v --ssh-host localhost"
|
|
||||||
|
|
||||||
# --- Docker deploy (push only, runs even if bare failed) ---
|
|
||||||
|
|
||||||
- name: stop bare services
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'systemctl stop postfix dovecot nginx opendkim unbound filtermail doveauth chatmail-metadata iroh-relay mtail fcgiwrap acmetool 2>/dev/null || true'
|
|
||||||
|
|
||||||
- name: install Docker on VPS
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'apt-get update && apt-get install -y ca-certificates curl'
|
|
||||||
ssh root@${HOST} 'install -m 0755 -d /etc/apt/keyrings'
|
|
||||||
ssh root@${HOST} 'curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc'
|
|
||||||
ssh root@${HOST} 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" > /etc/apt/sources.list.d/docker.list'
|
|
||||||
ssh root@${HOST} 'apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin'
|
|
||||||
|
|
||||||
- name: prepare Docker bind mounts
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
ssh root@${HOST} 'mkdir -p /srv/chatmail/certs /srv/chatmail/dkim'
|
|
||||||
ssh root@${HOST} 'cp -a /var/lib/acme/. /srv/chatmail/certs/ && cp -a /etc/dkimkeys/. /srv/chatmail/dkim/' || true
|
|
||||||
|
|
||||||
- name: generate and upload chatmail.ini
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
cmdeploy init ${HOST}
|
|
||||||
sed -i 's/#\s*mtail_address/mtail_address/' chatmail.ini
|
|
||||||
scp chatmail.ini root@${HOST}:/srv/chatmail/chatmail.ini
|
|
||||||
|
|
||||||
- name: deploy with Docker
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
GHCR_IMAGE="${{ needs.build-docker.outputs.image }}"
|
|
||||||
rsync -avz --exclude='.git' --exclude='venv' --exclude='__pycache__' ./ root@${HOST}:/srv/chatmail/relay/
|
|
||||||
# Login to GHCR on VPS and pull pre-built image
|
|
||||||
echo "${{ secrets.GITHUB_TOKEN }}" | ssh root@${HOST} 'docker login ghcr.io -u ${{ github.actor }} --password-stdin'
|
|
||||||
ssh root@${HOST} "docker pull ${GHCR_IMAGE}"
|
|
||||||
ssh root@${HOST} "cd /srv/chatmail/relay && CHATMAIL_IMAGE=${GHCR_IMAGE} MAIL_DOMAIN=${HOST} docker compose -f docker-compose.yaml -f docker/docker-compose.ci.yaml up -d"
|
|
||||||
|
|
||||||
- name: wait for container healthy
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
# Stream journald inside the container
|
|
||||||
ssh root@${HOST} 'docker exec chatmail journalctl -f --no-pager' &
|
|
||||||
LOG_PID=$!
|
|
||||||
trap "kill $LOG_PID 2>/dev/null || true" EXIT
|
|
||||||
for i in $(seq 1 60); do
|
|
||||||
status=$(ssh root@${HOST} 'docker inspect --format={{.State.Health.Status}} chatmail 2>/dev/null' || echo "missing")
|
|
||||||
echo " [$i/60] status=$status"
|
|
||||||
if [ "$status" = "healthy" ]; then
|
|
||||||
echo "Container is healthy."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
if [ "$status" = "unhealthy" ]; then
|
|
||||||
echo "Container is unhealthy!"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
sleep 5
|
|
||||||
done
|
|
||||||
echo "Container did not become healthy."
|
|
||||||
kill $LOG_PID 2>/dev/null || true
|
|
||||||
echo "--- failed units ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail systemctl --failed --no-pager' || true
|
|
||||||
echo "--- service logs ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail journalctl -u dovecot -u postfix -u nginx -u unbound --no-pager -n 50' || true
|
|
||||||
echo "--- listening ports ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail ss -tlnp' || true
|
|
||||||
echo "--- chatmail.ini ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cat /etc/chatmail/chatmail.ini' || true
|
|
||||||
exit 1
|
|
||||||
|
|
||||||
- name: show container state
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: |
|
|
||||||
echo "--- listening ports ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail ss -tlnp'
|
|
||||||
echo "--- chatmail.ini ---"
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cat /etc/chatmail/chatmail.ini'
|
|
||||||
|
|
||||||
- name: Docker offline tests
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: CHATMAIL_DOCKER=chatmail pytest --pyargs cmdeploy
|
|
||||||
|
|
||||||
- name: Docker DNS
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
ZONE: ${{ matrix.zone_file }}
|
|
||||||
run: |
|
|
||||||
# Reset zone file in case bare DNS already appended to it
|
|
||||||
git checkout .github/workflows/${ZONE}
|
|
||||||
ssh root@${HOST} 'docker exec chatmail chown opendkim:opendkim -R /etc/dkimkeys'
|
|
||||||
ssh root@${HOST} 'docker exec chatmail cmdeploy dns --ssh-host @local --zonefile /opt/chatmail/staging.zone --verbose'
|
|
||||||
ssh root@${HOST} 'docker cp chatmail:/opt/chatmail/staging.zone /tmp/staging.zone'
|
|
||||||
scp root@${HOST}:/tmp/staging.zone staging-generated.zone
|
|
||||||
cat staging-generated.zone >> .github/workflows/${ZONE}
|
|
||||||
cat .github/workflows/${ZONE}
|
|
||||||
scp .github/workflows/${ZONE} root@ns.testrun.org:/etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org nsd-checkzone ${HOST} /etc/nsd/${HOST}.zone
|
|
||||||
ssh root@ns.testrun.org systemctl reload nsd
|
|
||||||
|
|
||||||
- name: Docker integration tests
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: CHATMAIL_DOCKER=chatmail CHATMAIL_DOMAIN2=ci-chatmail.testrun.org cmdeploy test --slow
|
|
||||||
|
|
||||||
- name: Docker final DNS check
|
|
||||||
if: >-
|
|
||||||
!cancelled() && github.event_name == 'push'
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
env:
|
|
||||||
HOST: ${{ matrix.host }}
|
|
||||||
run: ssh root@${HOST} 'docker exec chatmail cmdeploy dns -v --ssh-host @local'
|
|
||||||
|
|
||||||
# --- Cleanup ---
|
|
||||||
|
|
||||||
- name: add SSH keys
|
|
||||||
if: >-
|
|
||||||
!cancelled() && matrix.add_ssh_keys
|
|
||||||
&& steps.wait-for-vps.outcome == 'success'
|
|
||||||
run: ssh root@${{ matrix.host }} 'curl -s https://github.com/hpk42.keys https://github.com/j4n.keys >> .ssh/authorized_keys'
|
|
||||||
296
.github/workflows/test-and-deploy-ipv4only.yaml
vendored
Normal file
296
.github/workflows/test-and-deploy-ipv4only.yaml
vendored
Normal file
@@ -0,0 +1,296 @@
|
|||||||
|
name: deploy on staging-ipv4.testrun.org, and run tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- j4n/docker-pr
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- 'scripts/**'
|
||||||
|
- '**/README.md'
|
||||||
|
- 'CHANGELOG.md'
|
||||||
|
- 'LICENSE'
|
||||||
|
|
||||||
|
env:
|
||||||
|
REGISTRY: ghcr.io
|
||||||
|
IMAGE_NAME: ${{ github.repository }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-docker:
|
||||||
|
name: Build Docker image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
outputs:
|
||||||
|
image: ${{ steps.image-ref.outputs.image }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Login to GHCR
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY }}
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels)
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
tags: |
|
||||||
|
# Tagged releases: v1.2.3 -> :1.2.3, :1.2, :latest
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
# Branch pushes: foo/docker-pr -> :foo-docker-pr
|
||||||
|
type=ref,event=branch
|
||||||
|
# Always: :sha-<hash>
|
||||||
|
type=sha
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: docker/chatmail_relay.dockerfile
|
||||||
|
push: ${{ github.event_name == 'push' }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
build-args: |
|
||||||
|
GIT_HASH=${{ github.sha }}
|
||||||
|
|
||||||
|
- name: Output image reference
|
||||||
|
id: image-ref
|
||||||
|
run: |
|
||||||
|
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||||||
|
IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
||||||
|
echo "image=${IMAGE}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
name: deploy on staging-ipv4.testrun.org, and run tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
environment:
|
||||||
|
name: staging-ipv4.testrun.org
|
||||||
|
url: https://staging-ipv4.testrun.org/
|
||||||
|
concurrency: staging-ipv4.testrun.org
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: prepare SSH
|
||||||
|
run: |
|
||||||
|
mkdir ~/.ssh
|
||||||
|
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
ssh-keyscan staging-ipv4.testrun.org > ~/.ssh/known_hosts
|
||||||
|
# save previous acme & dkim state
|
||||||
|
rsync -avz root@staging-ipv4.testrun.org:/var/lib/acme acme-ipv4 || true
|
||||||
|
rsync -avz root@staging-ipv4.testrun.org:/etc/dkimkeys dkimkeys-ipv4 || true
|
||||||
|
# store previous acme & dkim state on ns.testrun.org, if it contains useful certs
|
||||||
|
if [ -f dkimkeys-ipv4/dkimkeys/opendkim.private ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" dkimkeys-ipv4 root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
if [ "$(ls -A acme-ipv4/acme/certs)" ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" acme-ipv4 root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
# make sure CAA record isn't set
|
||||||
|
scp -o StrictHostKeyChecking=accept-new .github/workflows/staging-ipv4.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging-ipv4.testrun.org /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: rebuild staging-ipv4.testrun.org to have a clean VPS
|
||||||
|
run: |
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"image":"debian-12"}' \
|
||||||
|
"https://api.hetzner.cloud/v1/servers/${{ secrets.STAGING_IPV4_SERVER_ID }}/actions/rebuild"
|
||||||
|
|
||||||
|
- run: scripts/initenv.sh
|
||||||
|
|
||||||
|
- name: append venv/bin to PATH
|
||||||
|
run: echo venv/bin >>$GITHUB_PATH
|
||||||
|
|
||||||
|
- name: upload TLS cert after rebuilding
|
||||||
|
id: wait-for-vps
|
||||||
|
run: |
|
||||||
|
echo " --- wait until staging-ipv4.testrun.org VPS is rebuilt --- "
|
||||||
|
rm ~/.ssh/known_hosts
|
||||||
|
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org id -u ; do sleep 1 ; done
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org id -u
|
||||||
|
# download acme & dkim state from ns.testrun.org
|
||||||
|
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/acme-ipv4/acme acme-restore || true
|
||||||
|
rsync -avz root@ns.testrun.org:/tmp/dkimkeys-ipv4/dkimkeys dkimkeys-restore || true
|
||||||
|
# restore acme & dkim state to staging2.testrun.org
|
||||||
|
rsync -avz acme-restore/acme root@staging-ipv4.testrun.org:/var/lib/ || true
|
||||||
|
rsync -avz dkimkeys-restore/dkimkeys root@staging-ipv4.testrun.org:/etc/ || true
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging-ipv4.testrun.org chown root:root -R /var/lib/acme || true
|
||||||
|
|
||||||
|
- name: run deploy-chatmail offline tests
|
||||||
|
run: pytest --pyargs cmdeploy
|
||||||
|
|
||||||
|
- name: setup dependencies
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org apt update
|
||||||
|
ssh root@staging-ipv4.testrun.org apt install -y git python3.11-venv python3-dev gcc
|
||||||
|
ssh root@staging-ipv4.testrun.org git clone https://github.com/chatmail/relay
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && git checkout " ${{ github.head_ref }}
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/initenv.sh"
|
||||||
|
|
||||||
|
- name: initialize config
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy init staging-ipv4.testrun.org"
|
||||||
|
ssh root@staging-ipv4.testrun.org "sed -i 's#disable_ipv6 = False#disable_ipv6 = True#' relay/chatmail.ini"
|
||||||
|
ssh root@staging-ipv4.testrun.org "sed -i 's/#\s*mtail_address/mtail_address/' relay/chatmail.ini"
|
||||||
|
|
||||||
|
- run: ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy run --verbose --skip-dns-check --ssh-host localhost"
|
||||||
|
|
||||||
|
- name: set DNS entries
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy dns --zonefile staging-generated.zone --ssh-host localhost"
|
||||||
|
ssh root@staging-ipv4.testrun.org cat relay/staging-generated.zone >> .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging-ipv4.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging-ipv4.testrun.org /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: cmdeploy test
|
||||||
|
run: ssh root@staging-ipv4.testrun.org "cd relay && CHATMAIL_DOMAIN2=ci-chatmail.testrun.org scripts/cmdeploy test --slow --ssh-host localhost"
|
||||||
|
|
||||||
|
- name: cmdeploy dns
|
||||||
|
run: ssh root@staging-ipv4.testrun.org "cd relay && scripts/cmdeploy dns -v --ssh-host localhost"
|
||||||
|
|
||||||
|
# --- Docker deploy (push only, runs even if bare failed) ---
|
||||||
|
|
||||||
|
- name: stop bare services
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org 'systemctl stop postfix dovecot nginx opendkim unbound filtermail doveauth chatmail-metadata iroh-relay mtail fcgiwrap acmetool 2>/dev/null || true'
|
||||||
|
|
||||||
|
- name: install Docker on VPS
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org 'apt-get update && apt-get install -y ca-certificates curl'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'install -m 0755 -d /etc/apt/keyrings'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" > /etc/apt/sources.list.d/docker.list'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin'
|
||||||
|
|
||||||
|
- name: prepare Docker bind mounts
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org 'mkdir -p /srv/chatmail/certs /srv/chatmail/dkim'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'cp -a /var/lib/acme/. /srv/chatmail/certs/ && cp -a /etc/dkimkeys/. /srv/chatmail/dkim/' || true
|
||||||
|
|
||||||
|
- name: upload chatmail.ini for Docker
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Reuse chatmail.ini already created by the bare-metal deploy steps
|
||||||
|
ssh root@staging-ipv4.testrun.org "cp relay/chatmail.ini /srv/chatmail/chatmail.ini"
|
||||||
|
|
||||||
|
- name: deploy with Docker
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||||||
|
GHCR_IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
||||||
|
rsync -avz --exclude='.git' --exclude='venv' --exclude='__pycache__' ./ root@staging-ipv4.testrun.org:/srv/chatmail/relay/
|
||||||
|
# Login to GHCR on VPS and pull pre-built image
|
||||||
|
echo "${{ secrets.GITHUB_TOKEN }}" | ssh root@staging-ipv4.testrun.org 'docker login ghcr.io -u ${{ github.actor }} --password-stdin'
|
||||||
|
ssh root@staging-ipv4.testrun.org "docker pull ${GHCR_IMAGE}"
|
||||||
|
ssh root@staging-ipv4.testrun.org "cd /srv/chatmail/relay && CHATMAIL_IMAGE=${GHCR_IMAGE} MAIL_DOMAIN=staging-ipv4.testrun.org docker compose -f docker/docker-compose.yaml -f docker/docker-compose.ci.yaml up -d"
|
||||||
|
|
||||||
|
- name: wait for container healthy
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Stream journald inside the container
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail journalctl -f --no-pager' &
|
||||||
|
LOG_PID=$!
|
||||||
|
trap "kill $LOG_PID 2>/dev/null || true" EXIT
|
||||||
|
for i in $(seq 1 60); do
|
||||||
|
status=$(ssh root@staging-ipv4.testrun.org 'docker inspect --format={{.State.Health.Status}} chatmail 2>/dev/null' || echo "missing")
|
||||||
|
echo " [$i/60] status=$status"
|
||||||
|
if [ "$status" = "healthy" ]; then
|
||||||
|
echo "Container is healthy."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
if [ "$status" = "unhealthy" ]; then
|
||||||
|
echo "Container is unhealthy!"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
echo "Container did not become healthy."
|
||||||
|
kill $LOG_PID 2>/dev/null || true
|
||||||
|
echo "--- failed units ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail systemctl --failed --no-pager' || true
|
||||||
|
echo "--- service logs ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail journalctl -u dovecot -u postfix -u nginx -u unbound --no-pager -n 50' || true
|
||||||
|
echo "--- listening ports ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail ss -tlnp' || true
|
||||||
|
echo "--- chatmail.ini ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail cat /etc/chatmail/chatmail.ini' || true
|
||||||
|
exit 1
|
||||||
|
|
||||||
|
- name: show container state
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
echo "--- listening ports ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail ss -tlnp'
|
||||||
|
echo "--- chatmail.ini ---"
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail cat /etc/chatmail/chatmail.ini'
|
||||||
|
|
||||||
|
- name: Docker integration tests
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail cmdeploy test --slow --ssh-host @local'
|
||||||
|
|
||||||
|
- name: Docker DNS
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Reset zone file in case bare DNS already appended to it
|
||||||
|
git checkout .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail chown opendkim:opendkim -R /etc/dkimkeys'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker exec chatmail cmdeploy dns --ssh-host @local --zonefile /opt/chatmail/staging.zone --verbose'
|
||||||
|
ssh root@staging-ipv4.testrun.org 'docker cp chatmail:/opt/chatmail/staging.zone /tmp/staging.zone'
|
||||||
|
scp root@staging-ipv4.testrun.org:/tmp/staging.zone staging-generated.zone
|
||||||
|
cat staging-generated.zone >> .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging-ipv4.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging-ipv4.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging-ipv4.testrun.org /etc/nsd/staging-ipv4.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: Docker final DNS check
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: ssh root@staging-ipv4.testrun.org 'docker exec chatmail cmdeploy dns -v --ssh-host @local'
|
||||||
|
|
||||||
|
# --- Cleanup ---
|
||||||
|
|
||||||
|
- name: add SSH keys
|
||||||
|
if: >-
|
||||||
|
!cancelled()
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: ssh root@staging-ipv4.testrun.org 'curl -s https://github.com/hpk42.keys https://github.com/j4n.keys >> .ssh/authorized_keys'
|
||||||
289
.github/workflows/test-and-deploy.yaml
vendored
Normal file
289
.github/workflows/test-and-deploy.yaml
vendored
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
name: deploy on staging2.testrun.org, and run tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- j4n/docker-pr
|
||||||
|
pull_request:
|
||||||
|
paths-ignore:
|
||||||
|
- 'scripts/**'
|
||||||
|
- '**/README.md'
|
||||||
|
- 'CHANGELOG.md'
|
||||||
|
- 'LICENSE'
|
||||||
|
|
||||||
|
env:
|
||||||
|
REGISTRY: ghcr.io
|
||||||
|
IMAGE_NAME: ${{ github.repository }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build-docker:
|
||||||
|
name: Build Docker image
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
outputs:
|
||||||
|
image: ${{ steps.image-ref.outputs.image }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Login to GHCR
|
||||||
|
if: github.event_name == 'push'
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY }}
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels)
|
||||||
|
id: meta
|
||||||
|
uses: docker/metadata-action@v5
|
||||||
|
with:
|
||||||
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||||
|
tags: |
|
||||||
|
# Tagged releases: v1.2.3 -> :1.2.3, :1.2, :latest
|
||||||
|
type=semver,pattern={{version}}
|
||||||
|
type=semver,pattern={{major}}.{{minor}}
|
||||||
|
# Branch pushes: foo/docker-pr -> :foo-docker-pr
|
||||||
|
type=ref,event=branch
|
||||||
|
# Always: :sha-<hash>
|
||||||
|
type=sha
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: docker/chatmail_relay.dockerfile
|
||||||
|
push: ${{ github.event_name == 'push' }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=max
|
||||||
|
build-args: |
|
||||||
|
GIT_HASH=${{ github.sha }}
|
||||||
|
|
||||||
|
- name: Output image reference
|
||||||
|
id: image-ref
|
||||||
|
run: |
|
||||||
|
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||||||
|
IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
||||||
|
echo "image=${IMAGE}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
deploy:
|
||||||
|
name: deploy on staging2.testrun.org, and run tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 30
|
||||||
|
environment:
|
||||||
|
name: staging2.testrun.org
|
||||||
|
url: https://staging2.testrun.org/
|
||||||
|
concurrency: staging2.testrun.org
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: prepare SSH
|
||||||
|
run: |
|
||||||
|
mkdir ~/.ssh
|
||||||
|
echo "${{ secrets.STAGING_SSH_KEY }}" >> ~/.ssh/id_ed25519
|
||||||
|
chmod 600 ~/.ssh/id_ed25519
|
||||||
|
ssh-keyscan staging2.testrun.org > ~/.ssh/known_hosts
|
||||||
|
# save previous acme & dkim state
|
||||||
|
rsync -avz root@staging2.testrun.org:/var/lib/acme . || true
|
||||||
|
rsync -avz root@staging2.testrun.org:/etc/dkimkeys . || true
|
||||||
|
# store previous acme & dkim state on ns.testrun.org, if it contains useful certs
|
||||||
|
if [ -f dkimkeys/opendkim.private ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" dkimkeys root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
if [ "$(ls -A acme/certs)" ]; then rsync -avz -e "ssh -o StrictHostKeyChecking=accept-new" acme root@ns.testrun.org:/tmp/ || true; fi
|
||||||
|
# make sure CAA record isn't set
|
||||||
|
scp -o StrictHostKeyChecking=accept-new .github/workflows/staging.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org sed -i '/CAA/d' /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging2.testrun.org /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: rebuild staging2.testrun.org to have a clean VPS
|
||||||
|
run: |
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: Bearer ${{ secrets.HETZNER_API_TOKEN }}" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"image":"debian-12"}' \
|
||||||
|
"https://api.hetzner.cloud/v1/servers/${{ secrets.STAGING_SERVER_ID }}/actions/rebuild"
|
||||||
|
|
||||||
|
- run: scripts/initenv.sh
|
||||||
|
|
||||||
|
- name: append venv/bin to PATH
|
||||||
|
run: echo venv/bin >>$GITHUB_PATH
|
||||||
|
|
||||||
|
- name: upload TLS cert after rebuilding
|
||||||
|
id: wait-for-vps
|
||||||
|
run: |
|
||||||
|
echo " --- wait until staging2.testrun.org VPS is rebuilt --- "
|
||||||
|
rm ~/.ssh/known_hosts
|
||||||
|
while ! ssh -o ConnectTimeout=180 -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org id -u ; do sleep 1 ; done
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org id -u
|
||||||
|
# download acme & dkim state from ns.testrun.org
|
||||||
|
rsync -e "ssh -o StrictHostKeyChecking=accept-new" -avz root@ns.testrun.org:/tmp/acme acme-restore || true
|
||||||
|
rsync -avz root@ns.testrun.org:/tmp/dkimkeys dkimkeys-restore || true
|
||||||
|
# restore acme & dkim state to staging2.testrun.org
|
||||||
|
rsync -avz acme-restore/acme root@staging2.testrun.org:/var/lib/ || true
|
||||||
|
rsync -avz dkimkeys-restore/dkimkeys root@staging2.testrun.org:/etc/ || true
|
||||||
|
ssh -o StrictHostKeyChecking=accept-new -v root@staging2.testrun.org chown root:root -R /var/lib/acme || true
|
||||||
|
|
||||||
|
- name: add hpk42 key to staging server
|
||||||
|
run: ssh root@staging2.testrun.org 'curl -s https://github.com/hpk42.keys >> .ssh/authorized_keys'
|
||||||
|
|
||||||
|
- name: run deploy-chatmail offline tests
|
||||||
|
run: pytest --pyargs cmdeploy
|
||||||
|
|
||||||
|
- run: |
|
||||||
|
cmdeploy init staging2.testrun.org
|
||||||
|
sed -i 's/#\s*mtail_address/mtail_address/' chatmail.ini
|
||||||
|
|
||||||
|
- run: cmdeploy run --verbose --skip-dns-check
|
||||||
|
|
||||||
|
- name: set DNS entries
|
||||||
|
run: |
|
||||||
|
cmdeploy dns --zonefile staging-generated.zone --verbose
|
||||||
|
cat staging-generated.zone >> .github/workflows/staging.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging2.testrun.org /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: cmdeploy test
|
||||||
|
run: CHATMAIL_DOMAIN2=ci-chatmail.testrun.org cmdeploy test --slow
|
||||||
|
|
||||||
|
- name: cmdeploy dns
|
||||||
|
run: cmdeploy dns -v
|
||||||
|
|
||||||
|
# --- Docker deploy (push only, runs even if bare failed) ---
|
||||||
|
|
||||||
|
- name: stop bare services
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging2.testrun.org 'systemctl stop postfix dovecot nginx opendkim unbound filtermail doveauth chatmail-metadata iroh-relay mtail fcgiwrap acmetool 2>/dev/null || true'
|
||||||
|
|
||||||
|
- name: install Docker on VPS
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging2.testrun.org 'apt-get update && apt-get install -y ca-certificates curl'
|
||||||
|
ssh root@staging2.testrun.org 'install -m 0755 -d /etc/apt/keyrings'
|
||||||
|
ssh root@staging2.testrun.org 'curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc && chmod a+r /etc/apt/keyrings/docker.asc'
|
||||||
|
ssh root@staging2.testrun.org 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo $VERSION_CODENAME) stable" > /etc/apt/sources.list.d/docker.list'
|
||||||
|
ssh root@staging2.testrun.org 'apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin'
|
||||||
|
|
||||||
|
- name: prepare Docker bind mounts
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging2.testrun.org 'mkdir -p /srv/chatmail/certs /srv/chatmail/dkim'
|
||||||
|
ssh root@staging2.testrun.org 'cp -a /var/lib/acme/. /srv/chatmail/certs/ && cp -a /etc/dkimkeys/. /srv/chatmail/dkim/' || true
|
||||||
|
|
||||||
|
- name: upload chatmail.ini for Docker
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Reuse chatmail.ini already created by the bare-metal deploy steps
|
||||||
|
scp chatmail.ini root@staging2.testrun.org:/srv/chatmail/chatmail.ini
|
||||||
|
|
||||||
|
- name: deploy with Docker
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
||||||
|
GHCR_IMAGE="${{ env.REGISTRY }}/$(echo "${{ env.IMAGE_NAME }}" | tr '[:upper:]' '[:lower:]'):sha-${SHORT_SHA}"
|
||||||
|
rsync -avz --exclude='.git' --exclude='venv' --exclude='__pycache__' ./ root@staging2.testrun.org:/srv/chatmail/relay/
|
||||||
|
# Login to GHCR on VPS and pull pre-built image
|
||||||
|
echo "${{ secrets.GITHUB_TOKEN }}" | ssh root@staging2.testrun.org 'docker login ghcr.io -u ${{ github.actor }} --password-stdin'
|
||||||
|
ssh root@staging2.testrun.org "docker pull ${GHCR_IMAGE}"
|
||||||
|
ssh root@staging2.testrun.org "cd /srv/chatmail/relay && CHATMAIL_IMAGE=${GHCR_IMAGE} MAIL_DOMAIN=staging2.testrun.org docker compose -f docker/docker-compose.yaml -f docker/docker-compose.ci.yaml up -d"
|
||||||
|
|
||||||
|
- name: wait for container healthy
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Stream journald inside the container
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail journalctl -f --no-pager' &
|
||||||
|
LOG_PID=$!
|
||||||
|
trap "kill $LOG_PID 2>/dev/null || true" EXIT
|
||||||
|
for i in $(seq 1 60); do
|
||||||
|
status=$(ssh root@staging2.testrun.org 'docker inspect --format={{.State.Health.Status}} chatmail 2>/dev/null' || echo "missing")
|
||||||
|
echo " [$i/60] status=$status"
|
||||||
|
if [ "$status" = "healthy" ]; then
|
||||||
|
echo "Container is healthy."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
if [ "$status" = "unhealthy" ]; then
|
||||||
|
echo "Container is unhealthy!"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 5
|
||||||
|
done
|
||||||
|
echo "Container did not become healthy."
|
||||||
|
kill $LOG_PID 2>/dev/null || true
|
||||||
|
echo "--- failed units ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail systemctl --failed --no-pager' || true
|
||||||
|
echo "--- service logs ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail journalctl -u dovecot -u postfix -u nginx -u unbound --no-pager -n 50' || true
|
||||||
|
echo "--- listening ports ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail ss -tlnp' || true
|
||||||
|
echo "--- chatmail.ini ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail cat /etc/chatmail/chatmail.ini' || true
|
||||||
|
exit 1
|
||||||
|
|
||||||
|
- name: show container state
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
echo "--- listening ports ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail ss -tlnp'
|
||||||
|
echo "--- chatmail.ini ---"
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail cat /etc/chatmail/chatmail.ini'
|
||||||
|
|
||||||
|
- name: Docker integration tests
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail cmdeploy test --slow --ssh-host @local'
|
||||||
|
|
||||||
|
- name: Docker DNS
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: |
|
||||||
|
# Reset zone file in case bare DNS already appended to it
|
||||||
|
git checkout .github/workflows/staging.testrun.org-default.zone
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail chown opendkim:opendkim -R /etc/dkimkeys'
|
||||||
|
ssh root@staging2.testrun.org 'docker exec chatmail cmdeploy dns --ssh-host @local --zonefile /opt/chatmail/staging.zone --verbose'
|
||||||
|
ssh root@staging2.testrun.org 'docker cp chatmail:/opt/chatmail/staging.zone /tmp/staging.zone'
|
||||||
|
scp root@staging2.testrun.org:/tmp/staging.zone staging-generated.zone
|
||||||
|
cat staging-generated.zone >> .github/workflows/staging.testrun.org-default.zone
|
||||||
|
cat .github/workflows/staging.testrun.org-default.zone
|
||||||
|
scp .github/workflows/staging.testrun.org-default.zone root@ns.testrun.org:/etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org nsd-checkzone staging2.testrun.org /etc/nsd/staging2.testrun.org.zone
|
||||||
|
ssh root@ns.testrun.org systemctl reload nsd
|
||||||
|
|
||||||
|
- name: Docker final DNS check
|
||||||
|
if: >-
|
||||||
|
!cancelled() && github.event_name == 'push'
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: ssh root@staging2.testrun.org 'docker exec chatmail cmdeploy dns -v --ssh-host @local'
|
||||||
|
|
||||||
|
# --- Cleanup ---
|
||||||
|
|
||||||
|
- name: add SSH keys
|
||||||
|
if: >-
|
||||||
|
!cancelled()
|
||||||
|
&& steps.wait-for-vps.outcome == 'success'
|
||||||
|
run: ssh root@staging2.testrun.org 'curl -s https://github.com/hpk42.keys https://github.com/j4n.keys >> .ssh/authorized_keys'
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -168,5 +168,5 @@ chatmail.zone
|
|||||||
# docker
|
# docker
|
||||||
/data/
|
/data/
|
||||||
/custom/
|
/custom/
|
||||||
docker-compose.override.yaml
|
docker/docker-compose.override.yaml
|
||||||
.env
|
docker/.env
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ where = ['src']
|
|||||||
[project.scripts]
|
[project.scripts]
|
||||||
doveauth = "chatmaild.doveauth:main"
|
doveauth = "chatmaild.doveauth:main"
|
||||||
chatmail-metadata = "chatmaild.metadata:main"
|
chatmail-metadata = "chatmaild.metadata:main"
|
||||||
chatmail-metrics = "chatmaild.metrics:main"
|
|
||||||
chatmail-expire = "chatmaild.expire:main"
|
chatmail-expire = "chatmaild.expire:main"
|
||||||
chatmail-fsreport = "chatmaild.fsreport:main"
|
chatmail-fsreport = "chatmaild.fsreport:main"
|
||||||
lastlogin = "chatmaild.lastlogin:main"
|
lastlogin = "chatmaild.lastlogin:main"
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import filelock
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import crypt_r
|
import crypt_r
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -13,6 +16,7 @@ from .dictproxy import DictProxy
|
|||||||
from .migrate_db import migrate_from_db_to_maildir
|
from .migrate_db import migrate_from_db_to_maildir
|
||||||
|
|
||||||
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
NOCREATE_FILE = "/etc/chatmail-nocreate"
|
||||||
|
VALID_LOCALPART_RE = re.compile(r"^[a-z0-9._-]+$")
|
||||||
|
|
||||||
|
|
||||||
def encrypt_password(password: str):
|
def encrypt_password(password: str):
|
||||||
@@ -52,6 +56,10 @@ def is_allowed_to_create(config: Config, user, cleartext_password) -> bool:
|
|||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
if not VALID_LOCALPART_RE.match(localpart):
|
||||||
|
logging.warning("localpart %r contains invalid characters", localpart)
|
||||||
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
@@ -140,8 +148,13 @@ class AuthDictProxy(DictProxy):
|
|||||||
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
if not is_allowed_to_create(self.config, addr, cleartext_password):
|
||||||
return
|
return
|
||||||
|
|
||||||
user.set_password(encrypt_password(cleartext_password))
|
lock = filelock.FileLock(str(user.password_path) + ".lock", timeout=5)
|
||||||
print(f"Created address: {addr}", file=sys.stderr)
|
with lock:
|
||||||
|
userdata = user.get_userdb_dict()
|
||||||
|
if userdata:
|
||||||
|
return userdata
|
||||||
|
user.set_password(encrypt_password(cleartext_password))
|
||||||
|
print(f"Created address: {addr}", file=sys.stderr)
|
||||||
return user.get_userdb_dict()
|
return user.get_userdb_dict()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -101,7 +101,11 @@ class MetadataDictProxy(DictProxy):
|
|||||||
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
|
# Handle `GETMETADATA "" /shared/vendor/deltachat/irohrelay`
|
||||||
return f"O{self.iroh_relay}\n"
|
return f"O{self.iroh_relay}\n"
|
||||||
elif keyname == "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn":
|
elif keyname == "vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn":
|
||||||
res = turn_credentials()
|
try:
|
||||||
|
res = turn_credentials()
|
||||||
|
except Exception:
|
||||||
|
logging.exception("failed to get TURN credentials")
|
||||||
|
return "N\n"
|
||||||
port = 3478
|
port = 3478
|
||||||
return f"O{self.turn_hostname}:{port}:{res}\n"
|
return f"O{self.turn_hostname}:{port}:{res}\n"
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
|
||||||
def main(vmail_dir=None):
|
|
||||||
if vmail_dir is None:
|
|
||||||
vmail_dir = sys.argv[1]
|
|
||||||
|
|
||||||
accounts = 0
|
|
||||||
ci_accounts = 0
|
|
||||||
|
|
||||||
for path in Path(vmail_dir).iterdir():
|
|
||||||
if not path.joinpath("cur").is_dir():
|
|
||||||
continue
|
|
||||||
accounts += 1
|
|
||||||
if path.name[:3] in ("ci-", "ac_"):
|
|
||||||
ci_accounts += 1
|
|
||||||
|
|
||||||
print("# HELP total number of accounts")
|
|
||||||
print("# TYPE accounts gauge")
|
|
||||||
print(f"accounts {accounts}")
|
|
||||||
print("# HELP number of CI accounts")
|
|
||||||
print("# TYPE ci_accounts gauge")
|
|
||||||
print(f"ci_accounts {ci_accounts}")
|
|
||||||
print("# HELP number of non-CI accounts")
|
|
||||||
print("# TYPE nonci_accounts gauge")
|
|
||||||
print(f"nonci_accounts {accounts - ci_accounts}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -3,7 +3,6 @@
|
|||||||
"""CGI script for creating new accounts."""
|
"""CGI script for creating new accounts."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import random
|
|
||||||
import secrets
|
import secrets
|
||||||
import string
|
import string
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
@@ -16,7 +15,9 @@ ALPHANUMERIC_PUNCT = string.ascii_letters + string.digits + string.punctuation
|
|||||||
|
|
||||||
|
|
||||||
def create_newemail_dict(config: Config):
|
def create_newemail_dict(config: Config):
|
||||||
user = "".join(random.choices(ALPHANUMERIC, k=config.username_max_length))
|
user = "".join(
|
||||||
|
secrets.choice(ALPHANUMERIC) for _ in range(config.username_max_length)
|
||||||
|
)
|
||||||
password = "".join(
|
password = "".join(
|
||||||
secrets.choice(ALPHANUMERIC_PUNCT)
|
secrets.choice(ALPHANUMERIC_PUNCT)
|
||||||
for _ in range(config.password_min_length + 3)
|
for _ in range(config.password_min_length + 3)
|
||||||
|
|||||||
@@ -120,6 +120,60 @@ def test_handle_dovecot_protocol_iterate(gencreds, example_config):
|
|||||||
assert not lines[2]
|
assert not lines[2]
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_localpart_characters(make_config):
|
||||||
|
"""Test that is_allowed_to_create rejects localparts with invalid characters."""
|
||||||
|
config = make_config("chat.example.org", {"username_min_length": "3"})
|
||||||
|
password = "zequ0Aimuchoodaechik"
|
||||||
|
domain = config.mail_domain
|
||||||
|
|
||||||
|
# valid localparts
|
||||||
|
assert is_allowed_to_create(config, f"abc123@{domain}", password)
|
||||||
|
assert is_allowed_to_create(config, f"a.b-c_d@{domain}", password)
|
||||||
|
|
||||||
|
# uppercase rejected
|
||||||
|
assert not is_allowed_to_create(config, f"Abc123@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"ABCDEFG@{domain}", password)
|
||||||
|
|
||||||
|
# spaces and special chars rejected
|
||||||
|
assert not is_allowed_to_create(config, f"a b cde@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc+def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc!def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"ab@cdef@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc/def@{domain}", password)
|
||||||
|
assert not is_allowed_to_create(config, f"abc\\def@{domain}", password)
|
||||||
|
|
||||||
|
|
||||||
|
def test_concurrent_creation_same_account(dictproxy):
|
||||||
|
"""Test that concurrent creation of the same account doesn't corrupt password."""
|
||||||
|
addr = "racetest1@chat.example.org"
|
||||||
|
password = "zequ0Aimuchoodaechik"
|
||||||
|
num_threads = 10
|
||||||
|
results = queue.Queue()
|
||||||
|
|
||||||
|
def create():
|
||||||
|
try:
|
||||||
|
res = dictproxy.lookup_passdb(addr, password)
|
||||||
|
results.put(("ok", res))
|
||||||
|
except Exception:
|
||||||
|
results.put(("err", traceback.format_exc()))
|
||||||
|
|
||||||
|
threads = [threading.Thread(target=create, daemon=True) for _ in range(num_threads)]
|
||||||
|
for t in threads:
|
||||||
|
t.start()
|
||||||
|
for t in threads:
|
||||||
|
t.join(timeout=10)
|
||||||
|
|
||||||
|
passwords_seen = set()
|
||||||
|
for _ in range(num_threads):
|
||||||
|
status, res = results.get()
|
||||||
|
if status == "err":
|
||||||
|
pytest.fail(f"concurrent creation failed\n{res}")
|
||||||
|
passwords_seen.add(res["password"])
|
||||||
|
|
||||||
|
# all threads must see the same password hash
|
||||||
|
assert len(passwords_seen) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_50_concurrent_lookups_different_accounts(gencreds, dictproxy):
|
def test_50_concurrent_lookups_different_accounts(gencreds, dictproxy):
|
||||||
num_threads = 50
|
num_threads = 50
|
||||||
req_per_thread = 5
|
req_per_thread = 5
|
||||||
|
|||||||
@@ -112,6 +112,43 @@ def test_report(mbox1, example_config):
|
|||||||
report_main(args)
|
report_main(args)
|
||||||
|
|
||||||
|
|
||||||
|
def test_report_mdir_filters_by_path(mbox1, example_config):
|
||||||
|
"""Test that Report with mdir='cur' only counts messages in cur/ subdirectory."""
|
||||||
|
from chatmaild.fsreport import Report
|
||||||
|
|
||||||
|
now = datetime.utcnow().timestamp()
|
||||||
|
|
||||||
|
# Set password mtime to old enough so min_login_age check passes
|
||||||
|
password = Path(mbox1.basedir).joinpath("password")
|
||||||
|
old_time = now - 86400 * 10 # 10 days ago
|
||||||
|
os.utime(password, (old_time, old_time))
|
||||||
|
|
||||||
|
# Reload mailbox with updated mtime
|
||||||
|
from chatmaild.expire import MailboxStat
|
||||||
|
|
||||||
|
mbox = MailboxStat(mbox1.basedir)
|
||||||
|
|
||||||
|
# Report without mdir — should count all messages
|
||||||
|
rep_all = Report(now=now, min_login_age=1, mdir=None)
|
||||||
|
rep_all.process_mailbox_stat(mbox)
|
||||||
|
total_all = rep_all.message_buckets[0]
|
||||||
|
|
||||||
|
# Report with mdir='cur' — should only count cur/ messages
|
||||||
|
rep_cur = Report(now=now, min_login_age=1, mdir="cur")
|
||||||
|
rep_cur.process_mailbox_stat(mbox)
|
||||||
|
total_cur = rep_cur.message_buckets[0]
|
||||||
|
|
||||||
|
# Report with mdir='new' — should only count new/ messages
|
||||||
|
rep_new = Report(now=now, min_login_age=1, mdir="new")
|
||||||
|
rep_new.process_mailbox_stat(mbox)
|
||||||
|
total_new = rep_new.message_buckets[0]
|
||||||
|
|
||||||
|
# cur has 500-byte msg, new has 600-byte msg (from fill_mbox)
|
||||||
|
assert total_cur == 500
|
||||||
|
assert total_new == 600
|
||||||
|
assert total_all == 500 + 600
|
||||||
|
|
||||||
|
|
||||||
def test_expiry_cli_basic(example_config, mbox1):
|
def test_expiry_cli_basic(example_config, mbox1):
|
||||||
args = (str(example_config._inipath),)
|
args = (str(example_config._inipath),)
|
||||||
expiry_main(args)
|
expiry_main(args)
|
||||||
|
|||||||
@@ -314,6 +314,51 @@ def test_persistent_queue_items(tmp_path, testaddr, token):
|
|||||||
assert not queue_item < item2 and not item2 < queue_item
|
assert not queue_item < item2 and not item2 < queue_item
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_exception_returns_N(notifier, metadata, monkeypatch):
|
||||||
|
"""Test that turn_credentials() failure returns N\\n instead of crashing."""
|
||||||
|
import chatmaild.metadata
|
||||||
|
|
||||||
|
dictproxy = MetadataDictProxy(
|
||||||
|
notifier=notifier,
|
||||||
|
metadata=metadata,
|
||||||
|
turn_hostname="turn.example.org",
|
||||||
|
)
|
||||||
|
|
||||||
|
def mock_turn_credentials():
|
||||||
|
raise ConnectionRefusedError("socket not available")
|
||||||
|
|
||||||
|
monkeypatch.setattr(chatmaild.metadata, "turn_credentials", mock_turn_credentials)
|
||||||
|
|
||||||
|
transactions = {}
|
||||||
|
res = dictproxy.handle_dovecot_request(
|
||||||
|
"Lshared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||||
|
"\tuser@example.org",
|
||||||
|
transactions,
|
||||||
|
)
|
||||||
|
assert res == "N\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_success(notifier, metadata, monkeypatch):
|
||||||
|
"""Test that valid turn_credentials() returns TURN URI."""
|
||||||
|
import chatmaild.metadata
|
||||||
|
|
||||||
|
dictproxy = MetadataDictProxy(
|
||||||
|
notifier=notifier,
|
||||||
|
metadata=metadata,
|
||||||
|
turn_hostname="turn.example.org",
|
||||||
|
)
|
||||||
|
|
||||||
|
monkeypatch.setattr(chatmaild.metadata, "turn_credentials", lambda: "user:pass")
|
||||||
|
|
||||||
|
transactions = {}
|
||||||
|
res = dictproxy.handle_dovecot_request(
|
||||||
|
"Lshared/0123/vendor/vendor.dovecot/pvt/server/vendor/deltachat/turn"
|
||||||
|
"\tuser@example.org",
|
||||||
|
transactions,
|
||||||
|
)
|
||||||
|
assert res == "Oturn.example.org:3478:user:pass\n"
|
||||||
|
|
||||||
|
|
||||||
def test_iroh_relay(dictproxy):
|
def test_iroh_relay(dictproxy):
|
||||||
rfile = io.BytesIO(
|
rfile = io.BytesIO(
|
||||||
b"\n".join(
|
b"\n".join(
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
from chatmaild.metrics import main
|
|
||||||
|
|
||||||
|
|
||||||
def test_main(tmp_path, capsys):
|
|
||||||
paths = []
|
|
||||||
for x in ("ci-asllkj", "ac_12l3kj", "qweqwe", "ci-l1k2j31l2k3"):
|
|
||||||
p = tmp_path.joinpath(x)
|
|
||||||
p.mkdir()
|
|
||||||
p.joinpath("cur").mkdir()
|
|
||||||
paths.append(p)
|
|
||||||
|
|
||||||
tmp_path.joinpath("nomailbox").mkdir()
|
|
||||||
|
|
||||||
main(tmp_path)
|
|
||||||
out, _ = capsys.readouterr()
|
|
||||||
d = {}
|
|
||||||
for line in out.split("\n"):
|
|
||||||
if line.strip() and not line.startswith("#"):
|
|
||||||
name, num = line.split()
|
|
||||||
d[name] = int(num)
|
|
||||||
|
|
||||||
assert d["accounts"] == 4
|
|
||||||
assert d["ci_accounts"] == 3
|
|
||||||
assert d["nonci_accounts"] == 1
|
|
||||||
73
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
73
chatmaild/src/chatmaild/tests/test_turnserver.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import socket
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from chatmaild.turnserver import turn_credentials
|
||||||
|
|
||||||
|
SOCKET_PATH = "/run/chatmail-turn/turn.socket"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def turn_socket(tmp_path):
|
||||||
|
"""Create a real Unix socket server at a temp path."""
|
||||||
|
sock_path = str(tmp_path / "turn.socket")
|
||||||
|
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
|
server.bind(sock_path)
|
||||||
|
server.listen(1)
|
||||||
|
yield sock_path, server
|
||||||
|
server.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _call_turn_credentials(sock_path):
|
||||||
|
"""Call turn_credentials but connect to sock_path instead of hardcoded path."""
|
||||||
|
original_connect = socket.socket.connect
|
||||||
|
|
||||||
|
def patched_connect(self, address):
|
||||||
|
if address == SOCKET_PATH:
|
||||||
|
address = sock_path
|
||||||
|
return original_connect(self, address)
|
||||||
|
|
||||||
|
with patch.object(socket.socket, "connect", patched_connect):
|
||||||
|
return turn_credentials()
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_timeout(turn_socket):
|
||||||
|
"""Server accepts but never responds — must raise socket.timeout."""
|
||||||
|
sock_path, server = turn_socket
|
||||||
|
|
||||||
|
def accept_and_hang():
|
||||||
|
conn, _ = server.accept()
|
||||||
|
time.sleep(30)
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
t = threading.Thread(target=accept_and_hang, daemon=True)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
with pytest.raises(socket.timeout):
|
||||||
|
_call_turn_credentials(sock_path)
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_connection_refused(tmp_path):
|
||||||
|
"""Socket file doesn't exist — must raise ConnectionRefusedError or FileNotFoundError."""
|
||||||
|
missing = str(tmp_path / "nonexistent.socket")
|
||||||
|
with pytest.raises((ConnectionRefusedError, FileNotFoundError)):
|
||||||
|
_call_turn_credentials(missing)
|
||||||
|
|
||||||
|
|
||||||
|
def test_turn_credentials_success(turn_socket):
|
||||||
|
"""Server responds with credentials — must return stripped string."""
|
||||||
|
sock_path, server = turn_socket
|
||||||
|
|
||||||
|
def respond():
|
||||||
|
conn, _ = server.accept()
|
||||||
|
conn.sendall(b"testuser:testpass\n")
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
t = threading.Thread(target=respond, daemon=True)
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
result = _call_turn_credentials(sock_path)
|
||||||
|
assert result == "testuser:testpass"
|
||||||
@@ -4,6 +4,7 @@ import socket
|
|||||||
|
|
||||||
def turn_credentials() -> str:
|
def turn_credentials() -> str:
|
||||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client_socket:
|
||||||
|
client_socket.settimeout(5)
|
||||||
client_socket.connect("/run/chatmail-turn/turn.socket")
|
client_socket.connect("/run/chatmail-turn/turn.socket")
|
||||||
with client_socket.makefile("rb") as file:
|
with client_socket.makefile("rb") as file:
|
||||||
return file.readline().decode("utf-8").strip()
|
return file.readline().decode("utf-8").strip()
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class AcmetoolDeployer(Deployer):
|
|||||||
)
|
)
|
||||||
files.template(
|
files.template(
|
||||||
src=importlib.resources.files(__package__).joinpath("desired.yaml.j2"),
|
src=importlib.resources.files(__package__).joinpath("desired.yaml.j2"),
|
||||||
dest=f"/var/lib/acme/desired/{self.domains[0]}", # 0 is mailhost TLD
|
dest=f"/var/lib/acme/desired/{self.domains[0]}", # 0 is mailhost TLD
|
||||||
user="root",
|
user="root",
|
||||||
group="root",
|
group="root",
|
||||||
mode="644",
|
mode="644",
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import importlib.resources
|
import importlib.resources
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from pyinfra.operations import files, server, systemd
|
from pyinfra.operations import files, server, systemd
|
||||||
|
|
||||||
@@ -10,6 +11,28 @@ def has_systemd():
|
|||||||
return os.path.isdir("/run/systemd/system")
|
return os.path.isdir("/run/systemd/system")
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def blocked_service_startup():
|
||||||
|
"""Prevent services from auto-starting during package installation.
|
||||||
|
|
||||||
|
Installs a ``/usr/sbin/policy-rc.d`` that exits 101, blocking any
|
||||||
|
service from being started by the package manager. This avoids bind
|
||||||
|
conflicts and CPU/RAM spikes during initial setup. The file is removed
|
||||||
|
when the context exits.
|
||||||
|
"""
|
||||||
|
# For documentation about policy-rc.d, see:
|
||||||
|
# https://people.debian.org/~hmh/invokerc.d-policyrc.d-specification.txt
|
||||||
|
files.put(
|
||||||
|
src=get_resource("policy-rc.d"),
|
||||||
|
dest="/usr/sbin/policy-rc.d",
|
||||||
|
user="root",
|
||||||
|
group="root",
|
||||||
|
mode="755",
|
||||||
|
)
|
||||||
|
yield
|
||||||
|
files.file("/usr/sbin/policy-rc.d", present=False)
|
||||||
|
|
||||||
|
|
||||||
def get_resource(arg, pkg=__package__):
|
def get_resource(arg, pkg=__package__):
|
||||||
return importlib.resources.files(pkg).joinpath(arg)
|
return importlib.resources.files(pkg).joinpath(arg)
|
||||||
|
|
||||||
|
|||||||
@@ -116,24 +116,18 @@ def run_cmd(args, out):
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retcode = out.check_call(cmd, env=env)
|
out.check_call(cmd, env=env)
|
||||||
if args.website_only:
|
if args.website_only:
|
||||||
if retcode == 0:
|
out.green("Website deployment completed.")
|
||||||
out.green("Website deployment completed.")
|
|
||||||
else:
|
|
||||||
out.red("Website deployment failed.")
|
|
||||||
elif retcode == 0:
|
|
||||||
out.green("Deploy completed, call `cmdeploy dns` next.")
|
|
||||||
elif not args.dns_check_disabled and strict_tls and not remote_data["acme_account_url"]:
|
elif not args.dns_check_disabled and strict_tls and not remote_data["acme_account_url"]:
|
||||||
out.red("Deploy completed but letsencrypt not configured")
|
out.red("Deploy completed but letsencrypt not configured")
|
||||||
out.red("Run 'cmdeploy run' again")
|
out.red("Run 'cmdeploy run' again")
|
||||||
retcode = 0
|
|
||||||
else:
|
else:
|
||||||
out.red("Deploy failed")
|
out.green("Deploy completed, call `cmdeploy dns` next.")
|
||||||
|
return 0
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
out.red("Deploy failed")
|
out.red("Deploy failed")
|
||||||
retcode = 1
|
return 1
|
||||||
return retcode
|
|
||||||
|
|
||||||
|
|
||||||
def dns_cmd_options(parser):
|
def dns_cmd_options(parser):
|
||||||
|
|||||||
@@ -2,11 +2,10 @@
|
|||||||
Chat Mail pyinfra deploy.
|
Chat Mail pyinfra deploy.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from io import StringIO
|
from io import BytesIO, StringIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from chatmaild.config import read_config
|
from chatmaild.config import read_config
|
||||||
@@ -14,6 +13,7 @@ from pyinfra import facts, host, logger
|
|||||||
from pyinfra.api import FactBase
|
from pyinfra.api import FactBase
|
||||||
from pyinfra.facts import hardware
|
from pyinfra.facts import hardware
|
||||||
from pyinfra.facts.files import Sha256File
|
from pyinfra.facts.files import Sha256File
|
||||||
|
from pyinfra.facts.server import Command
|
||||||
from pyinfra.facts.systemd import SystemdEnabled
|
from pyinfra.facts.systemd import SystemdEnabled
|
||||||
from pyinfra.operations import apt, files, pip, server, systemd
|
from pyinfra.operations import apt, files, pip, server, systemd
|
||||||
|
|
||||||
@@ -123,7 +123,6 @@ def _install_remote_venv_with_chatmaild() -> None:
|
|||||||
|
|
||||||
def _configure_remote_venv_with_chatmaild(config) -> None:
|
def _configure_remote_venv_with_chatmaild(config) -> None:
|
||||||
remote_base_dir = "/usr/local/lib/chatmaild"
|
remote_base_dir = "/usr/local/lib/chatmaild"
|
||||||
remote_venv_dir = f"{remote_base_dir}/venv"
|
|
||||||
remote_chatmail_inipath = f"{remote_base_dir}/chatmail.ini"
|
remote_chatmail_inipath = f"{remote_base_dir}/chatmail.ini"
|
||||||
root_owned = dict(user="root", group="root", mode="644")
|
root_owned = dict(user="root", group="root", mode="644")
|
||||||
|
|
||||||
@@ -134,16 +133,13 @@ def _configure_remote_venv_with_chatmaild(config) -> None:
|
|||||||
**root_owned,
|
**root_owned,
|
||||||
)
|
)
|
||||||
|
|
||||||
files.template(
|
files.file(
|
||||||
src=get_resource("metrics.cron.j2"),
|
path="/etc/cron.d/chatmail-metrics",
|
||||||
dest="/etc/cron.d/chatmail-metrics",
|
present=False,
|
||||||
user="root",
|
)
|
||||||
group="root",
|
files.file(
|
||||||
mode="644",
|
path="/var/www/html/metrics",
|
||||||
config={
|
present=False,
|
||||||
"mailboxes_dir": config.mailboxes_dir,
|
|
||||||
"execpath": f"{remote_venv_dir}/bin/chatmail-metrics",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -271,6 +267,9 @@ class WebsiteDeployer(Deployer):
|
|||||||
# if www_folder is a hugo page, build it
|
# if www_folder is a hugo page, build it
|
||||||
if build_dir:
|
if build_dir:
|
||||||
www_path = build_webpages(src_dir, build_dir, self.config)
|
www_path = build_webpages(src_dir, build_dir, self.config)
|
||||||
|
if www_path is None:
|
||||||
|
logger.warning("Web page build failed, skipping website deployment")
|
||||||
|
return
|
||||||
# if it is not a hugo page, upload it as is
|
# if it is not a hugo page, upload it as is
|
||||||
files.rsync(
|
files.rsync(
|
||||||
f"{www_path}/", "/var/www/html", flags=["-avz", "--chown=www-data"]
|
f"{www_path}/", "/var/www/html", flags=["-avz", "--chown=www-data"]
|
||||||
@@ -479,6 +478,14 @@ class ChatmailDeployer(Deployer):
|
|||||||
self.mail_domain = mail_domain
|
self.mail_domain = mail_domain
|
||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
|
files.put(
|
||||||
|
name="Disable installing recommended packages globally",
|
||||||
|
src=BytesIO(b'APT::Install-Recommends "false";\n'),
|
||||||
|
dest="/etc/apt/apt.conf.d/00InstallRecommends",
|
||||||
|
user="root",
|
||||||
|
group="root",
|
||||||
|
mode="644",
|
||||||
|
)
|
||||||
apt.update(name="apt update", cache_time=24 * 3600)
|
apt.update(name="apt update", cache_time=24 * 3600)
|
||||||
apt.upgrade(name="upgrade apt packages", auto_remove=True)
|
apt.upgrade(name="upgrade apt packages", auto_remove=True)
|
||||||
|
|
||||||
@@ -586,7 +593,7 @@ def deploy_chatmail(config_path: Path, disable_mail: bool, website_only: bool) -
|
|||||||
Out().red(f"Deploy failed: mtail_address {config.mtail_address} is not available (VPN up?).\n")
|
Out().red(f"Deploy failed: mtail_address {config.mtail_address} is not available (VPN up?).\n")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
if not os.environ.get("CHATMAIL_NOPORTCHECK"):
|
if host.get_fact(Command, "systemd-detect-virt -c || true") == "none":
|
||||||
port_services = [
|
port_services = [
|
||||||
(["master", "smtpd"], 25),
|
(["master", "smtpd"], 25),
|
||||||
("unbound", 53),
|
("unbound", 53),
|
||||||
|
|||||||
@@ -1,20 +1,30 @@
|
|||||||
import os
|
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
|
||||||
from chatmaild.config import Config
|
from chatmaild.config import Config
|
||||||
from pyinfra import host
|
from pyinfra import host
|
||||||
from pyinfra.facts.server import Arch, Sysctl
|
from pyinfra.facts.deb import DebPackages
|
||||||
from pyinfra.facts.systemd import SystemdEnabled
|
from pyinfra.facts.server import Arch, Command, Sysctl
|
||||||
from pyinfra.operations import apt, files, server, systemd
|
from pyinfra.operations import apt, files, server, systemd
|
||||||
|
|
||||||
from cmdeploy.basedeploy import (
|
from cmdeploy.basedeploy import (
|
||||||
Deployer,
|
Deployer,
|
||||||
activate_remote_units,
|
activate_remote_units,
|
||||||
|
blocked_service_startup,
|
||||||
configure_remote_units,
|
configure_remote_units,
|
||||||
get_resource,
|
get_resource,
|
||||||
has_systemd,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
DOVECOT_VERSION = "2.3.21+dfsg1-3"
|
||||||
|
|
||||||
|
DOVECOT_SHA256 = {
|
||||||
|
("core", "amd64"): "dd060706f52a306fa863d874717210b9fe10536c824afe1790eec247ded5b27d",
|
||||||
|
("core", "arm64"): "e7548e8a82929722e973629ecc40fcfa886894cef3db88f23535149e7f730dc9",
|
||||||
|
("imapd", "amd64"): "8d8dc6fc00bbb6cdb25d345844f41ce2f1c53f764b79a838eb2a03103eebfa86",
|
||||||
|
("imapd", "arm64"): "178fa877ddd5df9930e8308b518f4b07df10e759050725f8217a0c1fb3fd707f",
|
||||||
|
("lmtpd", "amd64"): "2f69ba5e35363de50962d42cccbfe4ed8495265044e244007d7ccddad77513ab",
|
||||||
|
("lmtpd", "arm64"): "89f52fb36524f5877a177dff4a713ba771fd3f91f22ed0af7238d495e143b38f",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class DovecotDeployer(Deployer):
|
class DovecotDeployer(Deployer):
|
||||||
daemon_reload = False
|
daemon_reload = False
|
||||||
@@ -26,11 +36,10 @@ class DovecotDeployer(Deployer):
|
|||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
arch = host.get_fact(Arch)
|
arch = host.get_fact(Arch)
|
||||||
if has_systemd() and "dovecot.service" in host.get_fact(SystemdEnabled):
|
with blocked_service_startup():
|
||||||
return # already installed and running
|
_install_dovecot_package("core", arch)
|
||||||
_install_dovecot_package("core", arch)
|
_install_dovecot_package("imapd", arch)
|
||||||
_install_dovecot_package("imapd", arch)
|
_install_dovecot_package("lmtpd", arch)
|
||||||
_install_dovecot_package("lmtpd", arch)
|
|
||||||
|
|
||||||
def configure(self):
|
def configure(self):
|
||||||
configure_remote_units(self.config.mail_domain, self.units)
|
configure_remote_units(self.config.mail_domain, self.units)
|
||||||
@@ -42,7 +51,9 @@ class DovecotDeployer(Deployer):
|
|||||||
restart = False if self.disable_mail else self.need_restart
|
restart = False if self.disable_mail else self.need_restart
|
||||||
|
|
||||||
systemd.service(
|
systemd.service(
|
||||||
name="Disable dovecot for now" if self.disable_mail else "Start and enable Dovecot",
|
name="Disable dovecot for now"
|
||||||
|
if self.disable_mail
|
||||||
|
else "Start and enable Dovecot",
|
||||||
service="dovecot.service",
|
service="dovecot.service",
|
||||||
running=False if self.disable_mail else True,
|
running=False if self.disable_mail else True,
|
||||||
enabled=False if self.disable_mail else True,
|
enabled=False if self.disable_mail else True,
|
||||||
@@ -64,37 +75,33 @@ def _pick_url(primary, fallback):
|
|||||||
def _install_dovecot_package(package: str, arch: str):
|
def _install_dovecot_package(package: str, arch: str):
|
||||||
arch = "amd64" if arch == "x86_64" else arch
|
arch = "amd64" if arch == "x86_64" else arch
|
||||||
arch = "arm64" if arch == "aarch64" else arch
|
arch = "arm64" if arch == "aarch64" else arch
|
||||||
primary_url = f"https://download.delta.chat/dovecot/dovecot-{package}_2.3.21%2Bdfsg1-3_{arch}.deb"
|
|
||||||
fallback_url = f"https://github.com/chatmail/dovecot/releases/download/upstream%2F2.3.21%2Bdfsg1/dovecot-{package}_2.3.21%2Bdfsg1-3_{arch}.deb"
|
|
||||||
url = _pick_url(primary_url, fallback_url)
|
|
||||||
deb_filename = "/root/" + url.split("/")[-1]
|
|
||||||
|
|
||||||
match (package, arch):
|
pkg_name = f"dovecot-{package}"
|
||||||
case ("core", "amd64"):
|
sha256 = DOVECOT_SHA256.get((package, arch))
|
||||||
sha256 = "dd060706f52a306fa863d874717210b9fe10536c824afe1790eec247ded5b27d"
|
if sha256 is None:
|
||||||
case ("core", "arm64"):
|
apt.packages(packages=[pkg_name])
|
||||||
sha256 = "e7548e8a82929722e973629ecc40fcfa886894cef3db88f23535149e7f730dc9"
|
return
|
||||||
case ("imapd", "amd64"):
|
|
||||||
sha256 = "8d8dc6fc00bbb6cdb25d345844f41ce2f1c53f764b79a838eb2a03103eebfa86"
|
installed_versions = host.get_fact(DebPackages).get(pkg_name, [])
|
||||||
case ("imapd", "arm64"):
|
if DOVECOT_VERSION in installed_versions:
|
||||||
sha256 = "178fa877ddd5df9930e8308b518f4b07df10e759050725f8217a0c1fb3fd707f"
|
return
|
||||||
case ("lmtpd", "amd64"):
|
|
||||||
sha256 = "2f69ba5e35363de50962d42cccbfe4ed8495265044e244007d7ccddad77513ab"
|
url_version = DOVECOT_VERSION.replace("+", "%2B")
|
||||||
case ("lmtpd", "arm64"):
|
deb_base = f"{pkg_name}_{url_version}_{arch}.deb"
|
||||||
sha256 = "89f52fb36524f5877a177dff4a713ba771fd3f91f22ed0af7238d495e143b38f"
|
primary_url = f"https://download.delta.chat/dovecot/{deb_base}"
|
||||||
case _:
|
fallback_url = f"https://github.com/chatmail/dovecot/releases/download/upstream%2F{url_version}/{deb_base}"
|
||||||
apt.packages(packages=[f"dovecot-{package}"])
|
url = _pick_url(primary_url, fallback_url)
|
||||||
return
|
deb_filename = f"/root/{deb_base}"
|
||||||
|
|
||||||
files.download(
|
files.download(
|
||||||
name=f"Download dovecot-{package}",
|
name=f"Download {pkg_name}",
|
||||||
src=url,
|
src=url,
|
||||||
dest=deb_filename,
|
dest=deb_filename,
|
||||||
sha256sum=sha256,
|
sha256sum=sha256,
|
||||||
cache_time=60 * 60 * 24 * 365 * 10, # never redownload the package
|
cache_time=60 * 60 * 24 * 365 * 10, # never redownload the package
|
||||||
)
|
)
|
||||||
|
|
||||||
apt.deb(name=f"Install dovecot-{package}", src=deb_filename)
|
apt.deb(name=f"Install {pkg_name}", src=deb_filename)
|
||||||
|
|
||||||
|
|
||||||
def _configure_dovecot(config: Config, debug: bool = False) -> (bool, bool):
|
def _configure_dovecot(config: Config, debug: bool = False) -> (bool, bool):
|
||||||
@@ -132,19 +139,25 @@ def _configure_dovecot(config: Config, debug: bool = False) -> (bool, bool):
|
|||||||
|
|
||||||
# as per https://doc.dovecot.org/2.3/configuration_manual/os/
|
# as per https://doc.dovecot.org/2.3/configuration_manual/os/
|
||||||
# it is recommended to set the following inotify limits
|
# it is recommended to set the following inotify limits
|
||||||
if not os.environ.get("CHATMAIL_NOSYSCTL"):
|
can_modify = host.get_fact(Command, "systemd-detect-virt -c || true") == "none"
|
||||||
for name in ("max_user_instances", "max_user_watches"):
|
for name in ("max_user_instances", "max_user_watches"):
|
||||||
key = f"fs.inotify.{name}"
|
key = f"fs.inotify.{name}"
|
||||||
if host.get_fact(Sysctl)[key] > 65535:
|
value = host.get_fact(Sysctl)[key]
|
||||||
# Skip updating limits if already sufficient
|
if value > 65534:
|
||||||
# (enables running in incus containers where sysctl readonly)
|
continue
|
||||||
continue
|
if not can_modify:
|
||||||
server.sysctl(
|
print(
|
||||||
name=f"Change {key}",
|
"\n!!!! refusing to attempt sysctl setting in shared-kernel containers\n"
|
||||||
key=key,
|
f"!!!! dovecot: sysctl {key!r}={value}, should be >65534 for production setups\n"
|
||||||
value=65535,
|
"!!!!"
|
||||||
persist=True,
|
|
||||||
)
|
)
|
||||||
|
continue
|
||||||
|
server.sysctl(
|
||||||
|
name=f"Change {key}",
|
||||||
|
key=key,
|
||||||
|
value=65535,
|
||||||
|
persist=True,
|
||||||
|
)
|
||||||
|
|
||||||
timezone_env = files.line(
|
timezone_env = files.line(
|
||||||
name="Set TZ environment variable",
|
name="Set TZ environment variable",
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ class FiltermailDeployer(Deployer):
|
|||||||
|
|
||||||
def install(self):
|
def install(self):
|
||||||
arch = host.get_fact(facts.server.Arch)
|
arch = host.get_fact(facts.server.Arch)
|
||||||
url = f"https://github.com/chatmail/filtermail/releases/download/v0.5.2/filtermail-{arch}"
|
url = f"https://github.com/chatmail/filtermail/releases/download/v0.6.0/filtermail-{arch}"
|
||||||
sha256sum = {
|
sha256sum = {
|
||||||
"x86_64": "ce24ca0075aa445510291d775fb3aea8f4411818c7b885ae51a0fe18c5f789ce",
|
"x86_64": "3fd8b18282252c75a5bbfa603d8c1b65f6563e5e920bddf3e64e451b7cdb43ce",
|
||||||
"aarch64": "c5d783eefa5332db3d97a0e6a23917d72849e3eb45da3d16ce908a9b4e5a797d",
|
"aarch64": "2bd191de205f7fd60158dd8e3516ab7e3efb14627696f3d7dc186bdcd9e10a43",
|
||||||
}[arch]
|
}[arch]
|
||||||
self.need_restart |= files.download(
|
self.need_restart |= files.download(
|
||||||
name="Download filtermail",
|
name="Download filtermail",
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
*/5 * * * * root {{ config.execpath }} {{ config.mailboxes_dir }} >/var/www/html/metrics
|
|
||||||
@@ -54,7 +54,7 @@ http {
|
|||||||
include /etc/nginx/mime.types;
|
include /etc/nginx/mime.types;
|
||||||
default_type application/octet-stream;
|
default_type application/octet-stream;
|
||||||
|
|
||||||
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
|
ssl_protocols TLSv1.2 TLSv1.3;
|
||||||
ssl_prefer_server_ciphers on;
|
ssl_prefer_server_ciphers on;
|
||||||
ssl_certificate {{ config.tls_cert_path }};
|
ssl_certificate {{ config.tls_cert_path }};
|
||||||
ssl_certificate_key {{ config.tls_key_path }};
|
ssl_certificate_key {{ config.tls_key_path }};
|
||||||
@@ -79,10 +79,6 @@ http {
|
|||||||
try_files $uri $uri/ =404;
|
try_files $uri $uri/ =404;
|
||||||
}
|
}
|
||||||
|
|
||||||
location /metrics {
|
|
||||||
default_type text/plain;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /new {
|
location /new {
|
||||||
{% if config.tls_cert_mode != "self" %}
|
{% if config.tls_cert_mode != "self" %}
|
||||||
if ($request_method = GET) {
|
if ($request_method = GET) {
|
||||||
|
|||||||
@@ -97,7 +97,9 @@ class PostfixDeployer(Deployer):
|
|||||||
server.shell(
|
server.shell(
|
||||||
name="Validate postfix configuration",
|
name="Validate postfix configuration",
|
||||||
# Extract stderr and quit with error if non-zero
|
# Extract stderr and quit with error if non-zero
|
||||||
commands=["""bash -c 'w=$(postconf 2>&1 >/dev/null); [[ -z "$w" ]] || { echo "$w"; false; }'"""],
|
commands=[
|
||||||
|
"""bash -c 'w=$(postconf 2>&1 >/dev/null); [[ -z "$w" ]] || { echo "$w"; false; }'"""
|
||||||
|
],
|
||||||
)
|
)
|
||||||
self.need_restart = need_restart
|
self.need_restart = need_restart
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ def get_dkim_entry(mail_domain, pre_command, dkim_selector):
|
|||||||
print=log_progress,
|
print=log_progress,
|
||||||
)
|
)
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
return
|
return None, None
|
||||||
dkim_value_raw = f"v=DKIM1;k=rsa;p={dkim_pubkey};s=email;t=s"
|
dkim_value_raw = f"v=DKIM1;k=rsa;p={dkim_pubkey};s=email;t=s"
|
||||||
dkim_value = '" "'.join(re.findall(".{1,255}", dkim_value_raw))
|
dkim_value = '" "'.join(re.findall(".{1,255}", dkim_value_raw))
|
||||||
web_dkim_value = "".join(re.findall(".{1,255}", dkim_value_raw))
|
web_dkim_value = "".join(re.findall(".{1,255}", dkim_value_raw))
|
||||||
|
|||||||
@@ -40,5 +40,5 @@ def dovecot_recalc_quota(user):
|
|||||||
#
|
#
|
||||||
for line in output.split("\n"):
|
for line in output.split("\n"):
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if parts[2] == "STORAGE":
|
if len(parts) >= 6 and parts[2] == "STORAGE":
|
||||||
return dict(value=int(parts[3]), limit=int(parts[4]), percent=int(parts[5]))
|
return dict(value=int(parts[3]), limit=int(parts[4]), percent=int(parts[5]))
|
||||||
|
|||||||
@@ -50,9 +50,6 @@ class SSHExec:
|
|||||||
FuncError = FuncError
|
FuncError = FuncError
|
||||||
|
|
||||||
def __init__(self, host, verbose=False, python="python3", timeout=60):
|
def __init__(self, host, verbose=False, python="python3", timeout=60):
|
||||||
docker_container = os.environ.get("CHATMAIL_DOCKER")
|
|
||||||
if docker_container:
|
|
||||||
python = f"docker exec -i {docker_container} python3"
|
|
||||||
self.gateway = execnet.makegateway(f"ssh=root@{host}//python={python}")
|
self.gateway = execnet.makegateway(f"ssh=root@{host}//python={python}")
|
||||||
self._remote_cmdloop_channel = bootstrap_remote(self.gateway, remote)
|
self._remote_cmdloop_channel = bootstrap_remote(self.gateway, remote)
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import time
|
|
||||||
def test_tls_imap(benchmark, imap):
|
def test_tls_imap(benchmark, imap):
|
||||||
def imap_connect():
|
def imap_connect():
|
||||||
imap.connect()
|
imap.connect()
|
||||||
|
|||||||
@@ -89,7 +89,9 @@ def test_concurrent_logins_same_account(
|
|||||||
assert login_results.get()
|
assert login_results.get()
|
||||||
|
|
||||||
|
|
||||||
def test_no_vrfy(chatmail_config):
|
def test_no_vrfy(cmfactory, chatmail_config):
|
||||||
|
ac = cmfactory.get_online_account()
|
||||||
|
addr = ac.get_config("addr")
|
||||||
domain = chatmail_config.mail_domain
|
domain = chatmail_config.mail_domain
|
||||||
|
|
||||||
s = smtplib.SMTP(domain)
|
s = smtplib.SMTP(domain)
|
||||||
@@ -98,7 +100,7 @@ def test_no_vrfy(chatmail_config):
|
|||||||
s.putcmd("vrfy", f"wrongaddress@{chatmail_config.mail_domain}")
|
s.putcmd("vrfy", f"wrongaddress@{chatmail_config.mail_domain}")
|
||||||
result = s.getreply()
|
result = s.getreply()
|
||||||
print(result)
|
print(result)
|
||||||
s.putcmd("vrfy", f"echo@{chatmail_config.mail_domain}")
|
s.putcmd("vrfy", addr)
|
||||||
result2 = s.getreply()
|
result2 = s.getreply()
|
||||||
print(result2)
|
print(result2)
|
||||||
assert result[0] == result2[0] == 252
|
assert result[0] == result2[0] == 252
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import imap_tools
|
|||||||
import pytest
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from cmdeploy.remote import rshell
|
|
||||||
from cmdeploy.cmdeploy import get_sshexec
|
from cmdeploy.cmdeploy import get_sshexec
|
||||||
|
from cmdeploy.remote import rshell
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -402,9 +402,6 @@ class Remote:
|
|||||||
case "@local": command = []
|
case "@local": command = []
|
||||||
case "localhost": command = []
|
case "localhost": command = []
|
||||||
case _: command = ["ssh", f"root@{self.sshdomain}"]
|
case _: command = ["ssh", f"root@{self.sshdomain}"]
|
||||||
docker_container = os.environ.get("CHATMAIL_DOCKER")
|
|
||||||
if docker_container:
|
|
||||||
command += ["docker", "exec", docker_container]
|
|
||||||
[command.append(arg) for arg in getjournal.split()]
|
[command.append(arg) for arg in getjournal.split()]
|
||||||
self.popen = subprocess.Popen(
|
self.popen = subprocess.Popen(
|
||||||
command,
|
command,
|
||||||
|
|||||||
@@ -60,6 +60,29 @@ def mockdns(request, mockdns_base, mockdns_expected):
|
|||||||
return mockdns_base
|
return mockdns_base
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetDkimEntry:
|
||||||
|
def test_dkim_entry_returns_tuple_on_success(self, mockdns):
|
||||||
|
entry, web_entry = remote.rdns.get_dkim_entry(
|
||||||
|
"some.domain", "", dkim_selector="opendkim"
|
||||||
|
)
|
||||||
|
# May return None,None if openssl not available, but should never crash
|
||||||
|
if entry is not None:
|
||||||
|
assert "opendkim._domainkey.some.domain" in entry
|
||||||
|
assert "opendkim._domainkey.some.domain" in web_entry
|
||||||
|
|
||||||
|
def test_dkim_entry_returns_none_tuple_on_error(self, monkeypatch):
|
||||||
|
"""CalledProcessError must return (None, None), not bare None."""
|
||||||
|
from subprocess import CalledProcessError
|
||||||
|
|
||||||
|
def failing_shell(command, fail_ok=False, print=print):
|
||||||
|
raise CalledProcessError(1, command)
|
||||||
|
|
||||||
|
monkeypatch.setattr(remote.rdns, "shell", failing_shell)
|
||||||
|
result = remote.rdns.get_dkim_entry("some.domain", "", dkim_selector="opendkim")
|
||||||
|
assert result == (None, None)
|
||||||
|
assert result[0] is None and result[1] is None
|
||||||
|
|
||||||
|
|
||||||
class TestPerformInitialChecks:
|
class TestPerformInitialChecks:
|
||||||
def test_perform_initial_checks_ok1(self, mockdns, mockdns_expected):
|
def test_perform_initial_checks_ok1(self, mockdns, mockdns_expected):
|
||||||
remote_data = remote.rdns.perform_initial_checks("some.domain")
|
remote_data = remote.rdns.perform_initial_checks("some.domain")
|
||||||
|
|||||||
68
cmdeploy/src/cmdeploy/tests/test_rshell.py
Normal file
68
cmdeploy/src/cmdeploy/tests/test_rshell.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from cmdeploy.remote.rshell import dovecot_recalc_quota
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_normal_output():
|
||||||
|
"""Normal doveadm output returns parsed dict."""
|
||||||
|
normal_output = (
|
||||||
|
"Quota name Type Value Limit %\n"
|
||||||
|
"User quota STORAGE 5 102400 0\n"
|
||||||
|
"User quota MESSAGE 2 - 0\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", return_value=normal_output):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
# shell is called twice (recalc + get), patch returns same for both
|
||||||
|
assert result == {"value": 5, "limit": 102400, "percent": 0}
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_empty_output():
|
||||||
|
"""Empty doveadm output (trailing newline) must not IndexError."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
# quota get returns only empty lines
|
||||||
|
return "\n\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_malformed_output():
|
||||||
|
"""Malformed output with too few columns must not crash."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
# partial line, fewer than 6 parts
|
||||||
|
return "Quota name\nUser quota STORAGE\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_dovecot_recalc_quota_header_only():
|
||||||
|
"""Only header line, no data rows."""
|
||||||
|
call_count = [0]
|
||||||
|
|
||||||
|
def mock_shell(cmd):
|
||||||
|
call_count[0] += 1
|
||||||
|
if "recalc" in cmd:
|
||||||
|
return ""
|
||||||
|
return "Quota name Type Value Limit %\n"
|
||||||
|
|
||||||
|
with patch("cmdeploy.remote.rshell.shell", side_effect=mock_shell):
|
||||||
|
result = dovecot_recalc_quota("user@example.org")
|
||||||
|
|
||||||
|
assert result is None
|
||||||
@@ -17,12 +17,10 @@ Setup Preparation
|
|||||||
We use ``chat.example.org`` as the chatmail domain in the following
|
We use ``chat.example.org`` as the chatmail domain in the following
|
||||||
steps. Please substitute it with your own domain.
|
steps. Please substitute it with your own domain.
|
||||||
|
|
||||||
1. Install docker and docker compose v2 (check with `docker compose version`), install, e.g., through
|
1. Install docker and docker compose v2 (check with `docker compose version`), install, e.g., on
|
||||||
- Debian 12 through the `official install instructions <https://docs.docker.com/engine/install/debian/#install-using-the-repository>`_
|
- Debian 12 through the `official install instructions <https://docs.docker.com/engine/install/debian/#install-using-the-repository>`_
|
||||||
- Debian 13+ with `apt install docker docker-compose`
|
- Debian 13+ with `apt install docker docker-compose`
|
||||||
|
|
||||||
If you must use v1 (EOL since 2023), use `docker-compose` in the following and modify the `docker-compose.yaml` to use `privileged: true` instead of `cgroup: host`, though that gives the container full privileges.
|
|
||||||
|
|
||||||
2. Setup the initial DNS records.
|
2. Setup the initial DNS records.
|
||||||
The following is an example in the familiar BIND zone file format with
|
The following is an example in the familiar BIND zone file format with
|
||||||
a TTL of 1 hour (3600 seconds).
|
a TTL of 1 hour (3600 seconds).
|
||||||
@@ -57,16 +55,16 @@ Create service directory
|
|||||||
|
|
||||||
Either:
|
Either:
|
||||||
|
|
||||||
- Create a service directory, e.g., `/srv/chatmail-relay`::
|
- Create a service directory and download the compose files::
|
||||||
|
|
||||||
mkdir -p /srv/chatmail-relay && cd /srv/chatmail-relay
|
mkdir -p /srv/chatmail-relay && cd /srv/chatmail-relay
|
||||||
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker-compose.yaml
|
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker/docker-compose.yaml
|
||||||
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker-compose.override.yaml.example -O docker-compose.override.yaml
|
wget https://raw.githubusercontent.com/chatmail/relay/refs/heads/main/docker/docker-compose.override.yaml.example -O docker-compose.override.yaml
|
||||||
|
|
||||||
- or clone the chatmail repo ::
|
- or clone the chatmail repo and enter the docker directory::
|
||||||
|
|
||||||
git clone https://github.com/chatmail/relay
|
git clone https://github.com/chatmail/relay
|
||||||
cd relay
|
cd relay/docker
|
||||||
|
|
||||||
|
|
||||||
Customize and start
|
Customize and start
|
||||||
@@ -240,7 +238,7 @@ Clone the repository and build the Docker image::
|
|||||||
docker/build.sh
|
docker/build.sh
|
||||||
|
|
||||||
The build bakes all binaries, Python packages, and the install stage
|
The build bakes all binaries, Python packages, and the install stage
|
||||||
into the image. After building, only ``docker-compose.yaml`` and a ``.env``
|
into the image. After building, only the ``docker/`` directory and a ``.env``
|
||||||
with ``MAIL_DOMAIN`` are needed to run the container. The `build.sh` passes the
|
with ``MAIL_DOMAIN`` are needed to run the container. The `build.sh` passes the
|
||||||
git hash onto the docker build so it can be determined if there has been a
|
git hash onto the docker build so it can be determined if there has been a
|
||||||
change that warrants a redeploy.
|
change that warrants a redeploy.
|
||||||
|
|||||||
@@ -109,10 +109,6 @@ short overview of ``chatmaild`` services:
|
|||||||
is contacted by Dovecot when a user logs in and stores the date of
|
is contacted by Dovecot when a user logs in and stores the date of
|
||||||
the login.
|
the login.
|
||||||
|
|
||||||
- `metrics <https://github.com/chatmail/relay/blob/main/chatmaild/src/chatmaild/metrics.py>`_
|
|
||||||
collects some metrics and displays them at
|
|
||||||
``https://example.org/metrics``.
|
|
||||||
|
|
||||||
``www/``
|
``www/``
|
||||||
~~~~~~~~~
|
~~~~~~~~~
|
||||||
|
|
||||||
@@ -142,11 +138,9 @@ Chatmail relay dependency diagram
|
|||||||
nginx-internal --- autoconfig.xml;
|
nginx-internal --- autoconfig.xml;
|
||||||
certs-nginx[("`TLS certs
|
certs-nginx[("`TLS certs
|
||||||
/var/lib/acme`")] --> nginx-internal;
|
/var/lib/acme`")] --> nginx-internal;
|
||||||
systemd-timer --- chatmail-metrics;
|
|
||||||
systemd-timer --- acmetool;
|
systemd-timer --- acmetool;
|
||||||
systemd-timer --- chatmail-expire-daily;
|
systemd-timer --- chatmail-expire-daily;
|
||||||
systemd-timer --- chatmail-fsreport-daily;
|
systemd-timer --- chatmail-fsreport-daily;
|
||||||
chatmail-metrics --- website;
|
|
||||||
acmetool --> certs[("`TLS certs
|
acmetool --> certs[("`TLS certs
|
||||||
/var/lib/acme`")];
|
/var/lib/acme`")];
|
||||||
nginx-external --- |993|dovecot;
|
nginx-external --- |993|dovecot;
|
||||||
|
|||||||
@@ -6,4 +6,4 @@
|
|||||||
# must be passed as a build arg from the host.
|
# must be passed as a build arg from the host.
|
||||||
|
|
||||||
export GIT_HASH=$(git rev-parse HEAD)
|
export GIT_HASH=$(git rev-parse HEAD)
|
||||||
exec docker compose build "$@"
|
exec docker compose -f docker/docker-compose.yaml build "$@"
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
export CHATMAIL_INI="${CHATMAIL_INI:-/etc/chatmail/chatmail.ini}"
|
export CHATMAIL_INI="${CHATMAIL_INI:-/etc/chatmail/chatmail.ini}"
|
||||||
export CHATMAIL_NOSYSCTL=True
|
|
||||||
export CHATMAIL_NOPORTCHECK=True
|
|
||||||
|
|
||||||
CMDEPLOY=/opt/cmdeploy/bin/cmdeploy
|
CMDEPLOY=/opt/cmdeploy/bin/cmdeploy
|
||||||
|
|
||||||
|
|||||||
@@ -52,15 +52,15 @@ RUN printf '[params]\nmail_domain = build.local\n' > /tmp/chatmail.ini
|
|||||||
|
|
||||||
RUN CMDEPLOY_STAGES=install \
|
RUN CMDEPLOY_STAGES=install \
|
||||||
CHATMAIL_INI=/tmp/chatmail.ini \
|
CHATMAIL_INI=/tmp/chatmail.ini \
|
||||||
CHATMAIL_NOSYSCTL=True \
|
|
||||||
CHATMAIL_NOPORTCHECK=True \
|
|
||||||
/opt/cmdeploy/bin/pyinfra @local \
|
/opt/cmdeploy/bin/pyinfra @local \
|
||||||
/opt/chatmail/cmdeploy/src/cmdeploy/run.py -y
|
/opt/chatmail/cmdeploy/src/cmdeploy/run.py -y
|
||||||
|
|
||||||
RUN cp -a www/ /opt/chatmail-www/
|
RUN cp -a www/ /opt/chatmail-www/
|
||||||
|
|
||||||
# Remove build-only packages and their deps — not needed at runtime
|
# Remove build-only packages — not needed at runtime.
|
||||||
RUN apt-get purge -y gcc git python3-dev && \
|
# Keep git: test_deployed_state needs `git rev-parse HEAD` to verify the
|
||||||
|
# deployed version hash matches /etc/chatmail-version.
|
||||||
|
RUN apt-get purge -y gcc python3-dev && \
|
||||||
apt-get autoremove -y && \
|
apt-get autoremove -y && \
|
||||||
rm -f /tmp/chatmail.ini
|
rm -f /tmp/chatmail.ini
|
||||||
|
|
||||||
@@ -73,6 +73,13 @@ RUN apt-get purge -y gcc git python3-dev && \
|
|||||||
ARG GIT_HASH=unknown
|
ARG GIT_HASH=unknown
|
||||||
RUN echo "$GIT_HASH" > /etc/chatmail-image-version && \
|
RUN echo "$GIT_HASH" > /etc/chatmail-image-version && \
|
||||||
echo "$GIT_HASH" > /etc/chatmail-version
|
echo "$GIT_HASH" > /etc/chatmail-version
|
||||||
|
|
||||||
|
# Mock git HEAD so `git rev-parse HEAD` returns the source repo's commit hash.
|
||||||
|
# The .git/ dir was created by `git init` earlier (for setuptools); we just
|
||||||
|
# write the build hash into whatever branch HEAD points to.
|
||||||
|
RUN head_ref=$(sed 's/^ref: //' /opt/chatmail/.git/HEAD) && \
|
||||||
|
mkdir -p "/opt/chatmail/.git/$(dirname "$head_ref")" && \
|
||||||
|
echo "$GIT_HASH" > "/opt/chatmail/.git/$head_ref"
|
||||||
# --- End build-time install ---
|
# --- End build-time install ---
|
||||||
|
|
||||||
ENV TZ=:/etc/localtime
|
ENV TZ=:/etc/localtime
|
||||||
|
|||||||
70
docker/docker-compose-traefik.yaml
Normal file
70
docker/docker-compose-traefik.yaml
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# Traefik reverse-proxy example — use as a compose override:
|
||||||
|
#
|
||||||
|
# docker compose -f docker-compose.yaml -f docker-compose-traefik.yaml up -d
|
||||||
|
#
|
||||||
|
# Traefik handles HTTP→HTTPS redirect and ACME certificate issuance.
|
||||||
|
# traefik-certs-dumper extracts the certificates to the filesystem so
|
||||||
|
# chatmail's Postfix/Dovecot/nginx can use them via TLS_EXTERNAL_CERT_AND_KEY.
|
||||||
|
#
|
||||||
|
# Prerequisites:
|
||||||
|
# mkdir -p traefik/data traefik/dynamic-configs
|
||||||
|
# touch traefik/data/acme.json && chmod 600 traefik/data/acme.json
|
||||||
|
# cp traefik/config.yaml.example traefik/config.yaml # see below
|
||||||
|
#
|
||||||
|
# Required .env variables (in addition to MAIL_DOMAIN):
|
||||||
|
# ACME_EMAIL=admin@example.org
|
||||||
|
|
||||||
|
services:
|
||||||
|
chatmail:
|
||||||
|
environment:
|
||||||
|
# Point chatmail at the certs dumped by traefik-certs-dumper.
|
||||||
|
# The container's tls-cert-reload.path watches for changes.
|
||||||
|
TLS_EXTERNAL_CERT_AND_KEY: >-
|
||||||
|
/traefik-certs/${MAIL_DOMAIN}/certificate.crt
|
||||||
|
/traefik-certs/${MAIL_DOMAIN}/privatekey.key
|
||||||
|
volumes:
|
||||||
|
- traefik-certs:/traefik-certs:ro
|
||||||
|
depends_on:
|
||||||
|
- traefik-certs-dumper
|
||||||
|
labels:
|
||||||
|
- traefik.enable=true
|
||||||
|
- traefik.http.services.chatmail.loadbalancer.server.scheme=https
|
||||||
|
- traefik.http.services.chatmail.loadbalancer.server.port=443
|
||||||
|
- traefik.http.routers.chatmail.rule=Host(`${MAIL_DOMAIN}`) || Host(`mta-sts.${MAIL_DOMAIN}`) || Host(`www.${MAIL_DOMAIN}`)
|
||||||
|
- traefik.http.routers.chatmail.tls=true
|
||||||
|
- traefik.http.routers.chatmail.tls.certresolver=letsEncrypt
|
||||||
|
|
||||||
|
traefik:
|
||||||
|
image: traefik:v3.3
|
||||||
|
container_name: traefik
|
||||||
|
restart: unless-stopped
|
||||||
|
network_mode: host
|
||||||
|
command:
|
||||||
|
- "--configFile=/config.yaml"
|
||||||
|
- "--certificatesresolvers.letsEncrypt.acme.email=${ACME_EMAIL}"
|
||||||
|
volumes:
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||||
|
- ./traefik/config.yaml:/config.yaml:ro
|
||||||
|
- ./traefik/data/acme.json:/acme.json
|
||||||
|
- ./traefik/dynamic-configs:/dynamic/conf:ro
|
||||||
|
|
||||||
|
traefik-certs-dumper:
|
||||||
|
image: ldez/traefik-certs-dumper:v2.10.0
|
||||||
|
restart: unless-stopped
|
||||||
|
depends_on:
|
||||||
|
- traefik
|
||||||
|
entrypoint: sh -c '
|
||||||
|
apk add openssl
|
||||||
|
&& while ! [ -e /data/acme.json ]
|
||||||
|
|| ! [ $$(jq ".[] | .Certificates | length" /data/acme.json | jq -s "add") != 0 ]; do
|
||||||
|
sleep 1;
|
||||||
|
done
|
||||||
|
&& traefik-certs-dumper file
|
||||||
|
--version v3 --watch --domain-subdir=true
|
||||||
|
--source /data/acme.json --dest /certs'
|
||||||
|
volumes:
|
||||||
|
- ./traefik/data/acme.json:/data/acme.json:ro
|
||||||
|
- traefik-certs:/certs
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
traefik-certs:
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# Local overrides: copy to docker-compose.override.yaml in the repo root.
|
# Local overrides: copy to docker-compose.override.yaml in this directory.
|
||||||
# Compose automatically merges this with docker-compose.yaml.
|
# Compose automatically merges this with docker-compose.yaml.
|
||||||
#
|
#
|
||||||
# cp docker-compose.override.yaml.example docker-compose.override.yaml
|
# cp docker-compose.override.yaml.example docker-compose.override.yaml
|
||||||
@@ -22,9 +22,9 @@ services:
|
|||||||
## Custom website:
|
## Custom website:
|
||||||
# - ./custom/www:/opt/chatmail-www
|
# - ./custom/www:/opt/chatmail-www
|
||||||
|
|
||||||
## Debug — mount scripts from the repo for live editing:
|
## Debug — mount scripts for live editing:
|
||||||
# - ./docker/chatmail-init.sh:/chatmail-init.sh
|
# - ./chatmail-init.sh:/chatmail-init.sh
|
||||||
# - ./docker/entrypoint.sh:/entrypoint.sh
|
# - ./entrypoint.sh:/entrypoint.sh
|
||||||
|
|
||||||
# environment:
|
# environment:
|
||||||
## Mount certs (above) and set TLS_EXTERNAL_CERT_AND_KEY to in-container paths.
|
## Mount certs (above) and set TLS_EXTERNAL_CERT_AND_KEY to in-container paths.
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
# Base compose file — do not edit. Put customizations (data paths, extra
|
# Base compose file — do not edit. Put customizations (data paths, extra
|
||||||
# volumes, env overrides) in docker-compose.override.yaml instead.
|
# volumes, env overrides) in docker-compose.override.yaml instead.
|
||||||
# See docker/docker-compose.override.yaml.example for a starting point.
|
# See docker-compose.override.yaml.example in this directory for a starting point.
|
||||||
#
|
#
|
||||||
# Security notes: this container uses
|
# Security notes: this container uses
|
||||||
# - network_mode:host chatmail needs many ports (25, 53, 80, 143, 443, 465,
|
# - network_mode:host chatmail needs many ports (25, 53, 80, 143, 443, 465,
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
services:
|
services:
|
||||||
chatmail:
|
chatmail:
|
||||||
build:
|
build:
|
||||||
context: ./
|
context: ../
|
||||||
dockerfile: docker/chatmail_relay.dockerfile
|
dockerfile: docker/chatmail_relay.dockerfile
|
||||||
args:
|
args:
|
||||||
GIT_HASH: ${GIT_HASH:-unknown}
|
GIT_HASH: ${GIT_HASH:-unknown}
|
||||||
Reference in New Issue
Block a user