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