Elffuss BraidHub

kikocis/clonagent public

917760fb3891 · 5.0 KiB · executableRawPermalink
1#!/usr/bin/env bash
2#
3# One-shot setup for ClonAgent's optional mail server.
4#
5# Prepares:
6# 1. DNS records (A, MX) on the subdomain
7# 2. Issues a Let's Encrypt cert for the mail hostname (so STARTTLS works)
8# 3. Brings up docker-mailserver
9# 4. Generates DKIM keys
10# 5. Prints the DKIM/SPF/DMARC TXT records you must add to the DNS zone
11#
12# Run on the host (as a user with sudo + docker access):
13# MAIL_DOMAIN=bot.utopiaia.com ./scripts/setup-mailserver.sh
14#
15set -euo pipefail
16
17MAIL_DOMAIN="${MAIL_DOMAIN:-bot.utopiaia.com}"
18MAIL_HOSTNAME="${MAIL_HOSTNAME:-$MAIL_DOMAIN}"
19PARENT_ZONE="${PARENT_ZONE:-utopiaia.com}"
20# CoreDNS reads from project-manager (legacy path); fall back to agent-manager.
21ZONE_FILE="${ZONE_FILE:-/home/ubuntu/project-manager/dns/zones/${PARENT_ZONE}.zone}"
22[[ -f "$ZONE_FILE" ]] || ZONE_FILE="/home/ubuntu/agent-manager/dns/zones/${PARENT_ZONE}.zone"
23SERVER_IP="${SERVER_IP:-145.239.65.26}"
24LE_EMAIL="${LE_EMAIL:-kikocisneros@gmail.com}"
25COMPOSE="${COMPOSE:-docker compose -f docker-compose.mail.yml}"
26
27PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
28cd "$PROJECT_ROOT"
29
30# Subdomain part — bot.utopiaia.com → "bot"
31SUB="${MAIL_DOMAIN%.${PARENT_ZONE}}"
32
33say() { printf "\n\033[1;36m→ %s\033[0m\n" "$*"; }
34warn() { printf "\033[1;33m! %s\033[0m\n" "$*" >&2; }
35
36# ── 1. DNS records ────────────────────────────────────────────────────────
37say "1. DNS records in $ZONE_FILE"
38if [[ ! -f "$ZONE_FILE" ]]; then
39 warn "Zone file not found at $ZONE_FILE — skipping (edit DNS by hand)."
40else
41 if grep -qE "^${SUB}\\s+IN\\s+A\\s+${SERVER_IP}" "$ZONE_FILE"; then
42 say " A record already present — skipping."
43 else
44 sudo cp "$ZONE_FILE" "${ZONE_FILE}.bak.$(date +%Y%m%d%H%M%S)"
45 sudo bash -c "cat >> '$ZONE_FILE' <<EOF
46
47; ─── ClonAgent mail server (added by setup-mailserver.sh) ───
48${SUB} IN A ${SERVER_IP}
49${SUB} IN MX 10 ${MAIL_HOSTNAME}.
50EOF"
51 # bump zone serial (YYYYMMDDNN format → +1)
52 sudo sed -i -E "s/([0-9]{10}) ; serial.*/\$(date +%Y%m%d)01 ; serial (bumped by setup-mailserver.sh)/" "$ZONE_FILE" || true
53 say " Added A + MX records."
54 if docker ps --format '{{.Names}}' | grep -q '^coredns$'; then
55 docker restart coredns >/dev/null && say " Restarted CoreDNS."
56 fi
57 fi
58fi
59
60# ── 2. Let's Encrypt cert for mail hostname ───────────────────────────────
61say "2. TLS cert for $MAIL_HOSTNAME"
62if [[ -d "/etc/letsencrypt/live/$MAIL_HOSTNAME" ]]; then
63 say " Cert already exists."
64else
65 if command -v certbot >/dev/null; then
66 sudo certbot certonly --nginx -d "$MAIL_HOSTNAME" --non-interactive --agree-tos --email "$LE_EMAIL" || \
67 warn "certbot failed — run manually: sudo certbot certonly --nginx -d $MAIL_HOSTNAME"
68 else
69 warn "certbot not installed — install with: sudo apt-get install -y certbot python3-certbot-nginx"
70 fi
71fi
72
73# ── 3. Bring up docker-mailserver ─────────────────────────────────────────
74say "3. Bringing up docker-mailserver"
75mkdir -p mail/mail-data mail/mail-state mail/mail-logs mail/config
76MAIL_DOMAIN="$MAIL_DOMAIN" MAIL_HOSTNAME="$MAIL_HOSTNAME" $COMPOSE up -d
77sleep 5
78
79# ── 4. Generate DKIM keys ──────────────────────────────────────────────────
80say "4. Generating DKIM keys"
81docker exec mailserver setup config dkim domain "$MAIL_DOMAIN" || warn "DKIM setup failed — try again after the container has stabilized."
82
83# ── 5. Print the TXT records you must add to the DNS zone ──────────────────
84say "5. TXT records for SPF, DKIM, DMARC — append these to $ZONE_FILE"
85echo
86echo "; ── SPF (sender policy) ──"
87echo "${SUB} IN TXT \"v=spf1 mx ~all\""
88echo
89echo "; ── DKIM ──"
90DKIM_FILE="mail/config/opendkim/keys/${MAIL_DOMAIN}/mail.txt"
91if [[ -f "$DKIM_FILE" ]]; then
92 cat "$DKIM_FILE"
93else
94 warn "DKIM key file not found yet ($DKIM_FILE). Re-run after the mailserver is fully up:"
95 echo " docker exec mailserver setup config dkim domain $MAIL_DOMAIN"
96fi
97echo
98echo "; ── DMARC ──"
99echo "_dmarc.${SUB} IN TXT \"v=DMARC1; p=quarantine; rua=mailto:postmaster@${MAIL_DOMAIN}\""
100echo
101say "Add the three TXT records above to $ZONE_FILE, bump the serial, and restart coredns."
102
103# ── 6. Test mailbox ───────────────────────────────────────────────────────
104say "6. Creating a test mailbox: postmaster@${MAIL_DOMAIN}"
105docker exec mailserver setup email add "postmaster@${MAIL_DOMAIN}" "$(openssl rand -base64 16)" || warn "Mailbox creation failed — run manually."
106
107echo
108say "Done. ClonAgent's UI (Mail page) can now provision per-agent mailboxes."