Files
akyldash/infra/bootstrap/bootstrap-ubuntu-24.04.sh
agent 16f97d5f73
CI / tests-and-images (pull_request) Has been cancelled
Guard recovery bootstrap from production host
2026-10-11 19:16:54 +03:00

79 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ $# -gt 1 || ( $# -eq 1 && $1 != --recovery-target ) ]]; then
echo "Usage: $0 [--recovery-target]" >&2
exit 2
fi
recovery_target=false
if [[ ${1:-} == --recovery-target ]]; then
recovery_target=true
. "$(dirname "${BASH_SOURCE[0]}")/host-guard.sh"
recovery_hostname=$(hostname -f 2>/dev/null || true)
recovery_ipv4=$(ip -o -4 addr show 2>/dev/null | awk '{split($4, address, "/"); print address[1]}')
if [[ -z $recovery_hostname || -z $recovery_ipv4 ]]; then
echo "Cannot verify recovery host identity; refusing recovery bootstrap." >&2
exit 1
fi
if akyldash_is_production_host "$recovery_hostname" "$recovery_ipv4"; then
echo "Refusing recovery bootstrap on the current production host." >&2
exit 1
fi
fi
if [[ ${EUID} -ne 0 ]]; then
echo "Run this script as root." >&2
exit 1
fi
. /etc/os-release
if [[ ${ID:-} != ubuntu || ${VERSION_ID:-} != 24.04 ]]; then
echo "Expected Ubuntu 24.04; found ${PRETTY_NAME:-unknown}." >&2
exit 1
fi
if ! id codex >/dev/null 2>&1; then
echo "Create the SSH-authorized codex account before running bootstrap." >&2
exit 1
fi
if [[ -e /etc/docker/daemon.json ]] && ! python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); assert d.get("log-driver") == "local" and d.get("log-opts",{}).get("max-size") == "10m" and d.get("log-opts",{}).get("max-file") == "3"' /etc/docker/daemon.json; then
echo "/etc/docker/daemon.json exists with different log settings; review it manually." >&2
exit 1
fi
install -d -m 0755 /etc/docker
if [[ ! -e /etc/docker/daemon.json ]]; then
cat > /etc/docker/daemon.json <<'EOF'
{
"log-driver": "local",
"log-opts": {"max-size": "10m", "max-file": "3"}
}
EOF
fi
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y ca-certificates curl git
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
printf 'deb [arch=%s signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu %s stable\n' \
"$(dpkg --print-architecture)" "$VERSION_CODENAME" > /etc/apt/sources.list.d/docker.list
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
install -d -o root -g root -m 0755 /opt/akyldash
install -d -o root -g root -m 0700 /etc/akyldash
install -d -o root -g root -m 0750 /var/lib/akyldash/data
install -d -o root -g root -m 0700 /var/backups/akyldash
if [[ $recovery_target == true ]]; then
install -o root -g root -m 0600 /dev/null /etc/akyldash/recovery-target
fi
systemctl enable --now docker
docker compose version
echo "Bootstrap complete. codex was not added to sudo or the docker group; SSH and firewall settings were not changed."
if [[ $recovery_target == true ]]; then
echo 'Recovery target marker created at /etc/akyldash/recovery-target.'
fi