| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | set -eu |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | RELEASE_BASE_URL="${STEID_RELEASE_BASE_URL:-https://releases.steid.dev}" |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | VERSION="${STEID_VERSION:-0.6.0}" |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | FLAVOUR="gnu" |
| 81 | |
| 82 | DOMAIN="" |
| 83 | WWW=0 |
| 84 | FORCE_CADDYFILE=0 |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | TARBALL="" |
| 90 | PORT="3000" |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | BACKUPS="" |
| 98 | |
| 99 | |
| 100 | |
| 101 | OWNER_EMAIL="" |
| 102 | OWNER_HANDLE="" |
| 103 | INSTALL_DIR="/opt/steid" |
| 104 | STATE_DIR="/var/lib/steid" |
| 105 | CONF_DIR="/etc/steid" |
| 106 | STEID_USER="steid" |
| 107 | INSTALL_CADDY=1 |
| 108 | |
| 109 | die() { echo "install.sh: $*" >&2; exit 1; } |
| 110 | say() { echo "==> $*"; } |
| 111 | |
| 112 | usage() { |
| 113 | cat >&2 <<'USAGE' |
| 114 | Usage: install.sh --domain <hostname> [options] |
| 115 | |
| 116 | --tarball <path> install from a local tarball instead of downloading. |
| 117 | Needed for the first install, which has nowhere to |
| 118 | download from yet, and for offline installs. |
| 119 | --www also serve www.<hostname>, redirecting it to the bare |
| 120 | hostname. Requires a www DNS record pointing here — |
| 121 | without one Caddy retries a certificate it cannot get. |
| 122 | --force-caddyfile overwrite an existing /etc/caddy/Caddyfile. Off by |
| 123 | default so an upgrade cannot silently discard your edits. |
| 124 | --domain <hostname> the public hostname, e.g. git.example.com. Its DNS must |
| 125 | already point at this machine or the certificate cannot |
| 126 | be issued. Required. |
| 127 | --version <v> release to install (default: the pinned one above) |
| 128 | --flavour <musl|gnu> which build to fetch (default: gnu — see below; musl |
| 129 | is not built, and asking for it gets a 404) |
| 130 | --backups daily install a systemd timer that runs `steid backup` into |
| 131 | /var/lib/steid/backups every night, keeps the most |
| 132 | recent 14, and tells the settings page when it last |
| 133 | ran. Off unless you ask. `--backups off` removes it. |
| 134 | These stay on the same machine — see the note at the |
| 135 | end for copying them somewhere else. |
| 136 | --owner-email <addr> create this owner at first boot, so the instance is |
| 137 | --owner-handle <name> never unclaimed on the internet. Both or neither. The |
| 138 | owner gets no usable password: get in with |
| 139 | `steid reset-password <handle>` on this machine. |
| 140 | --port <n> loopback port Steid listens on (default: 3000) |
| 141 | --no-caddy install and run Steid but do not touch Caddy. Only for |
| 142 | putting your own TLS-terminating proxy in front. Steid |
| 143 | has no TLS of its own; without a proxy, access tokens |
| 144 | cross the network in cleartext. |
| 145 | -h, --help this |
| 146 | USAGE |
| 147 | exit "${1:-0}" |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | while [ $# -gt 0 ]; do |
| 153 | case "$1" in |
| 154 | --domain) DOMAIN="${2:-}"; [ -n "$DOMAIN" ] || die "--domain needs a hostname"; shift 2 ;; |
| 155 | --tarball) TARBALL="${2:-}"; [ -n "$TARBALL" ] || die "--tarball needs a path"; shift 2 ;; |
| 156 | --www) WWW=1; shift ;; |
| 157 | --force-caddyfile) FORCE_CADDYFILE=1; shift ;; |
| 158 | --version) VERSION="${2:-}"; [ -n "$VERSION" ] || die "--version needs a value"; shift 2 ;; |
| 159 | --flavour) FLAVOUR="${2:-}"; [ -n "$FLAVOUR" ] || die "--flavour needs a value"; shift 2 ;; |
| 160 | --port) PORT="${2:-}"; [ -n "$PORT" ] || die "--port needs a number"; shift 2 ;; |
| 161 | --owner-email) OWNER_EMAIL="${2:-}"; [ -n "$OWNER_EMAIL" ] || die "--owner-email needs an address"; shift 2 ;; |
| 162 | --owner-handle) OWNER_HANDLE="${2:-}"; [ -n "$OWNER_HANDLE" ] || die "--owner-handle needs a handle"; shift 2 ;; |
| 163 | --backups) BACKUPS="${2:-}"; [ -n "$BACKUPS" ] || die "--backups needs daily or off"; shift 2 ;; |
| 164 | --no-caddy) INSTALL_CADDY=0; shift ;; |
| 165 | -h|--help) usage 0 ;; |
| 166 | *) echo "install.sh: unknown argument: $1" >&2; usage 1 ;; |
| 167 | esac |
| 168 | done |
| 169 | |
| 170 | |
| 171 | |
| 172 | [ "$(id -u)" = "0" ] || die "must run as root (try: sudo sh install.sh --domain …)" |
| 173 | |
| 174 | if [ "$INSTALL_CADDY" = 1 ]; then |
| 175 | [ -n "$DOMAIN" ] || die "--domain is required. Caddy needs a real hostname to |
| 176 | obtain a certificate for, and Steid has no TLS of its own. If you are putting |
| 177 | your own proxy in front, pass --no-caddy." |
| 178 | fi |
| 179 | |
| 180 | if [ -n "$DOMAIN" ]; then |
| 181 | case "$DOMAIN" in |
| 182 | *[!A-Za-z0-9.-]*|-*|.*|*.) die "'$DOMAIN' does not look like a hostname" ;; |
| 183 | esac |
| 184 | fi |
| 185 | |
| 186 | case "$PORT" in |
| 187 | ''|*[!0-9]*) die "--port must be a number" ;; |
| 188 | esac |
| 189 | |
| 190 | case "$FLAVOUR" in |
| 191 | musl|gnu) ;; |
| 192 | *) die "--flavour must be 'musl' or 'gnu'" ;; |
| 193 | esac |
| 194 | |
| 195 | case "$BACKUPS" in |
| 196 | ''|daily|off) ;; |
| 197 | *) die "--backups must be 'daily' or 'off'" ;; |
| 198 | esac |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | if [ -n "$OWNER_EMAIL" ] && [ -z "$OWNER_HANDLE" ]; then |
| 205 | die "--owner-email needs --owner-handle too. Pass both or neither." |
| 206 | fi |
| 207 | if [ -n "$OWNER_HANDLE" ] && [ -z "$OWNER_EMAIL" ]; then |
| 208 | die "--owner-handle needs --owner-email too. Pass both or neither." |
| 209 | fi |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | BACKUP_DIR="${STATE_DIR}/backups" |
| 215 | BACKUP_KEEP=14 |
| 216 | BACKUP_MARKER="${BACKUP_DIR}/.last-backup" |
| 217 | |
| 218 | case "${TARBALL:+local}${RELEASE_BASE_URL}" in |
| 219 | local*) : ;; |
| 220 | *REPLACE-ME*) die "RELEASE_BASE_URL is still the placeholder. Edit the top of |
| 221 | this script (or set STEID_RELEASE_BASE_URL) to point at real release artefacts, |
| 222 | or pass --tarball to install from a local file." ;; |
| 223 | esac |
| 224 | |
| 225 | command -v systemctl >/dev/null 2>&1 || die "no systemd here; follow the manual path in README.md" |
| 226 | command -v apt-get >/dev/null 2>&1 || die "this installer only knows apt (Debian/Ubuntu). |
| 227 | The manual path in README.md works on anything with systemd." |
| 228 | |
| 229 | case "$(uname -m)" in |
| 230 | x86_64|amd64) ARCH="x86_64" ;; |
| 231 | aarch64|arm64) ARCH="aarch64" ;; |
| 232 | *) die "unsupported architecture: $(uname -m). Only x86_64 and aarch64 are built." ;; |
| 233 | esac |
| 234 | |
| 235 | TARGET="${ARCH}-unknown-linux-${FLAVOUR}" |
| 236 | NAME="steid-${VERSION}-${TARGET}" |
| 237 | URL="${RELEASE_BASE_URL}/v${VERSION}/${NAME}.tar.gz" |
| 238 | |
| 239 | say "installing Steid ${VERSION} (${TARGET}) for ${DOMAIN:-<no domain>}" |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | say "installing prerequisites (git, curl, ca-certificates)" |
| 247 | export DEBIAN_FRONTEND=noninteractive |
| 248 | apt-get update -qq |
| 249 | apt-get install -y -qq --no-install-recommends git curl ca-certificates |
| 250 | |
| 251 | |
| 252 | |
| 253 | TMP="$(mktemp -d)" |
| 254 | |
| 255 | trap "rm -rf '$TMP'" EXIT INT TERM |
| 256 | |
| 257 | if [ -n "$TARBALL" ]; then |
| 258 | [ -f "$TARBALL" ] || die "--tarball: no such file: ${TARBALL}" |
| 259 | say "installing from ${TARBALL}" |
| 260 | cp "$TARBALL" "${TMP}/${NAME}.tar.gz" |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | if [ -f "${TARBALL}.sha256" ]; then |
| 266 | say "verifying checksum" |
| 267 | |
| 268 | |
| 269 | |
| 270 | EXPECTED="$(cut -d" " -f1 < "${TARBALL}.sha256")" |
| 271 | ACTUAL="$(sha256sum < "${TMP}/${NAME}.tar.gz" | cut -d" " -f1)" |
| 272 | [ "$EXPECTED" = "$ACTUAL" ] \ |
| 273 | || die "checksum mismatch — ${TARBALL} does not match its .sha256" |
| 274 | else |
| 275 | say "no ${TARBALL}.sha256 beside it; installing unverified" |
| 276 | fi |
| 277 | else |
| 278 | say "downloading ${URL}" |
| 279 | curl -fsSL "$URL" -o "${TMP}/${NAME}.tar.gz" \ |
| 280 | || die "download failed. Is version ${VERSION} published for ${TARGET}?" |
| 281 | curl -fsSL "${URL}.sha256" -o "${TMP}/${NAME}.tar.gz.sha256" \ |
| 282 | || die "checksum file missing next to the tarball; refusing to install unverified" |
| 283 | |
| 284 | say "verifying checksum" |
| 285 | ( cd "$TMP" && sha256sum -c "${NAME}.tar.gz.sha256" >/dev/null ) \ |
| 286 | || die "checksum mismatch — the download is corrupt or tampered with" |
| 287 | fi |
| 288 | |
| 289 | tar -xzf "${TMP}/${NAME}.tar.gz" -C "$TMP" |
| 290 | [ -x "${TMP}/${NAME}/steid" ] || die "tarball has no steid binary at ${NAME}/steid" |
| 291 | |
| 292 | |
| 293 | |
| 294 | [ -f "${TMP}/${NAME}/assets/manifest.toml" ] || die "tarball has no assets/manifest.toml" |
| 295 | |
| 296 | |
| 297 | |
| 298 | if ! id "$STEID_USER" >/dev/null 2>&1; then |
| 299 | say "creating system user ${STEID_USER}" |
| 300 | |
| 301 | |
| 302 | useradd --system --home-dir "$STATE_DIR" --shell /usr/sbin/nologin "$STEID_USER" |
| 303 | fi |
| 304 | |
| 305 | mkdir -p "$INSTALL_DIR" "$STATE_DIR" "$CONF_DIR" |
| 306 | |
| 307 | |
| 308 | mkdir -p "${STATE_DIR}/repos" |
| 309 | chown -R "${STEID_USER}:${STEID_USER}" "$STATE_DIR" |
| 310 | chmod 750 "$STATE_DIR" |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | if systemctl is-active --quiet steid 2>/dev/null; then |
| 318 | say "stopping steid for the upgrade" |
| 319 | systemctl stop steid |
| 320 | fi |
| 321 | |
| 322 | say "installing to ${INSTALL_DIR}" |
| 323 | install -m 0755 "${TMP}/${NAME}/steid" "${INSTALL_DIR}/steid" |
| 324 | rm -rf "${INSTALL_DIR}/assets" |
| 325 | cp -R "${TMP}/${NAME}/assets" "${INSTALL_DIR}/assets" |
| 326 | if [ -f "${TMP}/${NAME}/README.md" ]; then |
| 327 | cp "${TMP}/${NAME}/README.md" "${INSTALL_DIR}/README.md" |
| 328 | fi |
| 329 | chown -R root:root "$INSTALL_DIR" |
| 330 | |
| 331 | chmod -R a+rX "$INSTALL_DIR" |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | ENV_WRITTEN=0 |
| 341 | if [ ! -f "${CONF_DIR}/steid.env" ]; then |
| 342 | ENV_WRITTEN=1 |
| 343 | say "writing ${CONF_DIR}/steid.env" |
| 344 | cat > "${CONF_DIR}/steid.env" <<EOF |
| 345 | # Steid configuration. Restart after editing: systemctl restart steid |
| 346 | |
| 347 | # All state lives under ${STATE_DIR}. SQLite writes -wal and -shm siblings, so |
| 348 | # the directory must be writable, not just the file. |
| 349 | STEID_DATABASE_URL=sqlite:${STATE_DIR}/steid.db?mode=rwc |
| 350 | STEID_DATA_DIR=${STATE_DIR}/repos |
| 351 | |
| 352 | # HOST and PORT are read by the web framework itself and are deliberately not |
| 353 | # STEID_-prefixed. Loopback only: Caddy is the way in, and binding 0.0.0.0 would |
| 354 | # expose plain HTTP — and therefore access tokens in cleartext — to the internet. |
| 355 | HOST=127.0.0.1 |
| 356 | PORT=${PORT} |
| 357 | |
| 358 | # Caddy sits in front and appends the address it accepted the connection from to |
| 359 | # X-Forwarded-For, so the rate limiters may trust that header to tell one caller |
| 360 | # from another. Turn this OFF if you ever put Steid on a public port directly: |
| 361 | # with nothing appending the header a caller can invent one per request and buy |
| 362 | # itself a fresh rate-limit budget each time. |
| 363 | STEID_TRUSTED_PROXY=true |
| 364 | |
| 365 | # The address this instance answers on, used by \`steid reset-password\` to print a |
| 366 | # whole link. Every link built while serving is derived from the request instead, |
| 367 | # so this is read by the command line and nothing else. |
| 368 | STEID_PUBLIC_URL=https://${DOMAIN} |
| 369 | |
| 370 | # Outgoing mail, for password reset. Both are needed or neither does anything: |
| 371 | # without them the forgot-password page says so and points at |
| 372 | # \`steid reset-password <handle>\`, which prints a one-time link on this machine. |
| 373 | # The URL carries the submission password, which is why this file is mode 640. Quote |
| 374 | # any value containing spaces or angle brackets. |
| 375 | # STEID_SMTP_URL=smtps://user:password@smtp.example.com:465 |
| 376 | # STEID_MAIL_FROM="Steid <steid@${DOMAIN}>" |
| 377 | |
| 378 | # Deliberately absent: STEID_INSECURE_COOKIES. It strips Secure from the session |
| 379 | # cookie and exists only for plain-HTTP local development. Setting it here would |
| 380 | # hand out a session cookie that any network hop can read. |
| 381 | EOF |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | if [ -n "$OWNER_EMAIL" ]; then |
| 387 | cat >> "${CONF_DIR}/steid.env" <<EOF |
| 388 | |
| 389 | # The owner, created on the first boot that finds this instance unclaimed. Read |
| 390 | # together — Steid refuses to start with one of them set and not the other — and |
| 391 | # inert from the moment an owner exists, so they can stay here for the life of |
| 392 | # the instance. This is what makes an unattended install come up already claimed |
| 393 | # rather than serving a setup page to whoever finds it first. |
| 394 | # |
| 395 | # The owner created this way has NO usable password. The way in is |
| 396 | # ${INSTALL_DIR}/steid reset-password ${OWNER_HANDLE} |
| 397 | # run on this machine, which prints a one-time link. |
| 398 | STEID_OWNER_EMAIL=${OWNER_EMAIL} |
| 399 | STEID_OWNER_HANDLE=${OWNER_HANDLE} |
| 400 | EOF |
| 401 | fi |
| 402 | chmod 640 "${CONF_DIR}/steid.env" |
| 403 | chown "root:${STEID_USER}" "${CONF_DIR}/steid.env" |
| 404 | else |
| 405 | say "keeping existing ${CONF_DIR}/steid.env" |
| 406 | if [ -n "$OWNER_EMAIL" ]; then |
| 407 | say "--owner-email/--owner-handle ignored: ${CONF_DIR}/steid.env already exists" |
| 408 | fi |
| 409 | fi |
| 410 | |
| 411 | say "writing /etc/systemd/system/steid.service" |
| 412 | cat > /etc/systemd/system/steid.service <<EOF |
| 413 | # Managed by install.sh. Re-running the installer rewrites this file. |
| 414 | [Unit] |
| 415 | Description=Steid |
| 416 | After=network-online.target |
| 417 | Wants=network-online.target |
| 418 | |
| 419 | [Service] |
| 420 | Type=simple |
| 421 | User=${STEID_USER} |
| 422 | Group=${STEID_USER} |
| 423 | WorkingDirectory=${INSTALL_DIR} |
| 424 | ExecStart=${INSTALL_DIR}/steid |
| 425 | EnvironmentFile=${CONF_DIR}/steid.env |
| 426 | Environment=HOME=${STATE_DIR} |
| 427 | Restart=on-failure |
| 428 | RestartSec=2s |
| 429 | |
| 430 | NoNewPrivileges=true |
| 431 | PrivateTmp=true |
| 432 | PrivateDevices=true |
| 433 | ProtectSystem=strict |
| 434 | ProtectHome=true |
| 435 | ProtectKernelTunables=true |
| 436 | ProtectKernelModules=true |
| 437 | ProtectControlGroups=true |
| 438 | RestrictSUIDSGID=true |
| 439 | RestrictNamespaces=true |
| 440 | LockPersonality=true |
| 441 | RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX |
| 442 | ReadWritePaths=${STATE_DIR} |
| 443 | |
| 444 | [Install] |
| 445 | WantedBy=multi-user.target |
| 446 | EOF |
| 447 | |
| 448 | systemctl daemon-reload |
| 449 | systemctl enable --quiet steid |
| 450 | say "starting steid" |
| 451 | systemctl restart steid |
| 452 | |
| 453 | |
| 454 | |
| 455 | if [ "$BACKUPS" = "daily" ]; then |
| 456 | say "installing the nightly backup timer" |
| 457 | |
| 458 | mkdir -p "$BACKUP_DIR" |
| 459 | chown "${STEID_USER}:${STEID_USER}" "$BACKUP_DIR" |
| 460 | |
| 461 | chmod 700 "$BACKUP_DIR" |
| 462 | |
| 463 | |
| 464 | |
| 465 | if ! grep -q '^STEID_BACKUP_MARKER=' "${CONF_DIR}/steid.env"; then |
| 466 | cat >> "${CONF_DIR}/steid.env" <<EOF |
| 467 | |
| 468 | # Added by install.sh --backups daily. Steid reads this file's modification |
| 469 | # time to show the last automatic backup on the settings page, and nothing else |
| 470 | # about it. Removing the line makes the page say "none configured". |
| 471 | STEID_BACKUP_MARKER=${BACKUP_MARKER} |
| 472 | EOF |
| 473 | fi |
| 474 | |
| 475 | cat > /etc/systemd/system/steid-backup.service <<EOF |
| 476 | # Managed by install.sh --backups daily. |
| 477 | [Unit] |
| 478 | Description=Steid backup |
| 479 | # The backup is taken through SQLite's own VACUUM INTO, so it is consistent with |
| 480 | # the service running — this deliberately does not stop Steid. |
| 481 | After=steid.service |
| 482 | |
| 483 | [Service] |
| 484 | Type=oneshot |
| 485 | User=${STEID_USER} |
| 486 | Group=${STEID_USER} |
| 487 | WorkingDirectory=${INSTALL_DIR} |
| 488 | EnvironmentFile=${CONF_DIR}/steid.env |
| 489 | Environment=HOME=${STATE_DIR} |
| 490 | ExecStart=${INSTALL_DIR}/steid backup ${BACKUP_DIR} |
| 491 | # Pruning and the marker run only if the backup succeeded, which is the point of |
| 492 | # ExecStartPost: a failed run must not look like a successful one on the page. |
| 493 | # The \`;\` before touch is deliberate — an empty glob must not fail the unit. |
| 494 | ExecStartPost=/bin/sh -c 'cd ${BACKUP_DIR} && ls -1t steid-*.tar.gz 2>/dev/null | tail -n +$((BACKUP_KEEP + 1)) | xargs -r rm -f; touch ${BACKUP_MARKER}' |
| 495 | |
| 496 | NoNewPrivileges=true |
| 497 | PrivateTmp=true |
| 498 | PrivateDevices=true |
| 499 | ProtectSystem=strict |
| 500 | ProtectHome=true |
| 501 | ProtectKernelTunables=true |
| 502 | ProtectKernelModules=true |
| 503 | ProtectControlGroups=true |
| 504 | RestrictSUIDSGID=true |
| 505 | RestrictNamespaces=true |
| 506 | LockPersonality=true |
| 507 | RestrictAddressFamilies=AF_UNIX |
| 508 | ReadWritePaths=${STATE_DIR} |
| 509 | EOF |
| 510 | |
| 511 | cat > /etc/systemd/system/steid-backup.timer <<'EOF' |
| 512 | # Managed by install.sh --backups daily. |
| 513 | [Unit] |
| 514 | Description=Nightly Steid backup |
| 515 | |
| 516 | [Timer] |
| 517 | OnCalendar=daily |
| 518 | # Spread across the hour so a fleet of instances does not all gzip themselves at |
| 519 | # once, and catch up after a reboot rather than skipping the night. |
| 520 | RandomizedDelaySec=1h |
| 521 | Persistent=true |
| 522 | |
| 523 | [Install] |
| 524 | WantedBy=timers.target |
| 525 | EOF |
| 526 | |
| 527 | systemctl daemon-reload |
| 528 | systemctl enable --quiet --now steid-backup.timer |
| 529 | elif [ "$BACKUPS" = "off" ]; then |
| 530 | if [ -f /etc/systemd/system/steid-backup.timer ]; then |
| 531 | say "removing the backup timer (archives in ${BACKUP_DIR} are left alone)" |
| 532 | systemctl disable --quiet --now steid-backup.timer 2>/dev/null || true |
| 533 | rm -f /etc/systemd/system/steid-backup.timer /etc/systemd/system/steid-backup.service |
| 534 | systemctl daemon-reload |
| 535 | fi |
| 536 | fi |
| 537 | |
| 538 | |
| 539 | |
| 540 | if [ "$INSTALL_CADDY" = 1 ]; then |
| 541 | |
| 542 | |
| 543 | |
| 544 | CADDY_WAS_INSTALLED=1 |
| 545 | if ! command -v caddy >/dev/null 2>&1; then |
| 546 | CADDY_WAS_INSTALLED=0 |
| 547 | say "installing Caddy from its official apt repository" |
| 548 | apt-get install -y -qq --no-install-recommends debian-keyring debian-archive-keyring apt-transport-https gnupg |
| 549 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \ |
| 550 | | gpg --dearmor --yes -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg |
| 551 | curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \ |
| 552 | > /etc/apt/sources.list.d/caddy-stable.list |
| 553 | apt-get update -qq |
| 554 | apt-get install -y -qq caddy |
| 555 | else |
| 556 | say "Caddy already installed" |
| 557 | fi |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | if [ -f /etc/caddy/Caddyfile ] && [ "$FORCE_CADDYFILE" = 0 ] && [ "$CADDY_WAS_INSTALLED" = 1 ]; then |
| 572 | say "keeping existing /etc/caddy/Caddyfile (--force-caddyfile to replace)" |
| 573 | |
| 574 | |
| 575 | if ! grep -q "127.0.0.1:${PORT}" /etc/caddy/Caddyfile; then |
| 576 | echo "install.sh: WARNING — /etc/caddy/Caddyfile does not proxy 127.0.0.1:${PORT}," >&2 |
| 577 | echo " so ${DOMAIN} will not reach Steid. Add a reverse_proxy to it (see" >&2 |
| 578 | echo " deploy/Caddyfile in the source tree), or re-run with --force-caddyfile" >&2 |
| 579 | echo " to have this installer write a fresh one." >&2 |
| 580 | fi |
| 581 | else |
| 582 | say "writing /etc/caddy/Caddyfile for ${DOMAIN}" |
| 583 | mkdir -p /etc/caddy |
| 584 | |
| 585 | WWW_BLOCK="" |
| 586 | if [ "$WWW" = 1 ]; then |
| 587 | WWW_BLOCK=" |
| 588 | # www redirects to the bare hostname so there is one canonical URL. |
| 589 | www.${DOMAIN} { |
| 590 | redir https://${DOMAIN}{uri} permanent |
| 591 | }" |
| 592 | fi |
| 593 | |
| 594 | cat > /etc/caddy/Caddyfile <<EOF |
| 595 | # Written by install.sh on first install only. Later runs leave it alone, so edits |
| 596 | # here survive an upgrade. Use --force-caddyfile to have the installer replace it. |
| 597 | # |
| 598 | # Steid has no TLS of its own (Topcoat 0.5 ships none), and it authenticates git |
| 599 | # over HTTP Basic — so without this proxy every push would send a personal access |
| 600 | # token in cleartext. Caddy obtains and renews the certificate automatically, |
| 601 | # provided ${DOMAIN} resolves here and ports 80 and 443 are open. |
| 602 | ${DOMAIN} { |
| 603 | reverse_proxy 127.0.0.1:${PORT} { |
| 604 | # Git's smart HTTP is a streaming protocol in both directions; buffering |
| 605 | # it turns a clone into a long silence and can stall negotiation. |
| 606 | flush_interval -1 |
| 607 | } |
| 608 | } |
| 609 | ${WWW_BLOCK} |
| 610 | EOF |
| 611 | fi |
| 612 | |
| 613 | systemctl enable --quiet caddy |
| 614 | systemctl reload caddy 2>/dev/null || systemctl restart caddy |
| 615 | fi |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | sleep 2 |
| 622 | if ! systemctl is-active --quiet steid; then |
| 623 | echo >&2 |
| 624 | echo "install.sh: steid is installed but not running. Look at:" >&2 |
| 625 | echo " journalctl -u steid -n 50 --no-pager" >&2 |
| 626 | exit 1 |
| 627 | fi |
| 628 | |
| 629 | if [ "$INSTALL_CADDY" = 1 ]; then |
| 630 | BASE_URL="https://${DOMAIN}" |
| 631 | else |
| 632 | BASE_URL="http://127.0.0.1:${PORT} (put your own TLS proxy in front of this)" |
| 633 | fi |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | if [ -f /etc/systemd/system/steid-backup.timer ]; then |
| 639 | BACKUP_REPORT=" Backups nightly into ${BACKUP_DIR}, keeping ${BACKUP_KEEP} |
| 640 | systemctl list-timers steid-backup.timer |
| 641 | Take one now: ${INSTALL_DIR}/steid backup ${BACKUP_DIR} |
| 642 | |
| 643 | !! These sit on this machine's disk. Copy them somewhere else — any |
| 644 | !! rclone remote will do, as a second timer or from your laptop: |
| 645 | !! rclone sync ${BACKUP_DIR} remote:steid-backups |
| 646 | " |
| 647 | else |
| 648 | BACKUP_REPORT=" Backups none scheduled. Re-run with --backups daily, or take one by |
| 649 | hand: ${INSTALL_DIR}/steid backup /var/backups |
| 650 | " |
| 651 | fi |
| 652 | |
| 653 | |
| 654 | |
| 655 | |
| 656 | if [ -n "$OWNER_EMAIL" ] && [ "$ENV_WRITTEN" = 1 ]; then |
| 657 | CLAIM_REPORT="This instance is already claimed: ${OWNER_HANDLE} <${OWNER_EMAIL}> was created |
| 658 | at first boot, so there was never a moment when a stranger could claim it. |
| 659 | |
| 660 | That owner has no password. Mint a one-time sign-in link on this machine: |
| 661 | |
| 662 | ${INSTALL_DIR}/steid reset-password ${OWNER_HANDLE} |
| 663 | |
| 664 | It works once and expires in 30 minutes, and it is as good as the password — |
| 665 | hand it over the way you would one. |
| 666 | " |
| 667 | else |
| 668 | CLAIM_REPORT="Next: claim the instance. |
| 669 | |
| 670 | The setup token is printed to the log at startup, and ONLY while the instance |
| 671 | is unclaimed. It is held in memory, so every restart mints a new one: |
| 672 | |
| 673 | journalctl -u steid --no-pager | tail -n 30 |
| 674 | |
| 675 | Then open ${BASE_URL}/auth/setup and paste it in. |
| 676 | " |
| 677 | fi |
| 678 | |
| 679 | cat <<EOF |
| 680 | |
| 681 | Steid ${VERSION} is running. |
| 682 | |
| 683 | Service systemctl status steid |
| 684 | Logs journalctl -u steid -f |
| 685 | Config ${CONF_DIR}/steid.env |
| 686 | State ${STATE_DIR} (the database and the repos — back up this directory) |
| 687 | ${BACKUP_REPORT} |
| 688 | ${CLAIM_REPORT} |
| 689 | To upgrade later, re-run this script with a newer --version. It replaces the |
| 690 | binary and assets and leaves ${STATE_DIR} untouched. |
| 691 | EOF |