Files
at-container-registry/deploy/upcloud/configs/cloudinit.sh.tmpl
2026-02-10 21:20:13 -06:00

72 lines
2.0 KiB
Bash

#!/bin/bash
set -euo pipefail
exec > >(tee {{.LogFile}}) 2>&1
echo "=== {{.DisplayName}} Setup: {{.BinaryName}} ==="
echo "Started at $(date -u)"
# Wait for network/DNS
for i in $(seq 1 30); do
if getent hosts go.dev >/dev/null 2>&1; then
echo "Network ready after ${i}s"
break
fi
sleep 1
done
# System packages
export DEBIAN_FRONTEND=noninteractive
apt-get update && apt-get upgrade -y
apt-get install -y git gcc make curl libsqlite3-dev nodejs npm htop
# Swap (for builds on small instances)
if [ ! -f /swapfile ]; then
dd if=/dev/zero of=/swapfile bs=1M count=2048
chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
fi
# Go {{.GoVersion}}
curl -fsSL https://go.dev/dl/go{{.GoVersion}}.linux-amd64.tar.gz | tar -C /usr/local -xz
echo 'export PATH=$PATH:/usr/local/go/bin' > /etc/profile.d/go.sh
export PATH=$PATH:/usr/local/go/bin
export GOTMPDIR=/var/tmp
# Clone & build
if [ -d {{.InstallDir}} ]; then
cd {{.InstallDir}} && git pull origin {{.RepoBranch}}
else
git clone -b {{.RepoBranch}} {{.RepoURL}} {{.InstallDir}}
cd {{.InstallDir}}
fi
npm ci
go generate ./...
CGO_ENABLED=1 go build \
-ldflags="-s -w" \
-trimpath \
-o bin/{{.BinaryName}} ./cmd/{{.BuildCmd}}
# Service user & data dirs
useradd --system --no-create-home --shell /usr/sbin/nologin {{.SystemUser}} || true
mkdir -p {{.DataDir}} && chown {{.SystemUser}}:{{.SystemUser}} {{.DataDir}}
# Config file
mkdir -p {{.ConfigDir}}
if [ ! -f {{.ConfigPath}} ]; then
cat > {{.ConfigPath}} << 'CFGEOF'
{{.ConfigYAML}}
CFGEOF
else
echo "Config {{.ConfigPath}} already exists, skipping overwrite (missing keys merged separately)"
fi
# Systemd service
cat > /etc/systemd/system/{{.ServiceName}}.service << 'SVCEOF'
{{.ServiceUnit}}
SVCEOF
systemctl daemon-reload
systemctl enable {{.ServiceName}}
echo "=== Setup complete at $(date -u) ==="
echo "Edit {{.ConfigPath}} then: systemctl start {{.ServiceName}}"