Repository navigation
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·142 lines (131 loc) · 6.42 KB
/
Copy pathinit.sh
File metadata and controls
executable file
·142 lines (131 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
# Host initialization for the LSDB websites' service VM. Run as root; safe to re-run.
#
# git clone <this repo> && cd middle-earth-lsdb
# sudo ./init.sh
# podman compose up -d --build
#
# Only host-level concerns live here (checking the service account from IT's
# /root/VM.set, podman packages, the catalog storage mount, .env). Everything
# else belongs in the
# container image. The service account, SSH, and SELinux are provisioned by
# IT; this script does not change them.
set -euo pipefail
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$REPO_DIR/.env"
# Written by IT when the VM is provisioned and authoritative for the service
# account. tcsh syntax: `set VMUSER=astro-myservice`.
VM_SET=/root/VM.set
# Where the HATS catalogs live on this VM: IT's NFS mount of the catalog
# directory on the shire storage server (hats/catalogs there). Its entries
# are the catalogs, served as https://data.lsdb.io/hats/<entry>/.
# compose.yaml bind-mounts it read-only into the container.
HATS_DIR=/service/hats-catalogs
WARNINGS=0
warn() { echo "WARNING: $*" >&2; WARNINGS=$((WARNINGS + 1)); }
die() { echo "FATAL: $*" >&2; exit 1; }
[ "$(id -u)" -eq 0 ] || die "must run as root"
# vmset NAME: the value of `set NAME=value` in VM.set, quotes removed. The
# file is parsed, never executed; lines of any other form are ignored.
vmset() {
sed -n -E "s/^[[:space:]]*set[[:space:]]+$1=(\"([^\"]*)\"|'([^']*)'|([^[:space:]\"']*))[[:space:]]*\$/\2\3\4/p" \
"$VM_SET" | tail -n 1
}
[ -r "$VM_SET" ] || die "$VM_SET not found. IT writes it when the VM is provisioned; it
defines the service account (VMUSER, VMUID, VMGID). Ask PACS via help-astro@uw.edu."
SVC_USER="$(vmset VMUSER)"
SVC_UID="$(vmset VMUID)"
SVC_GID="$(vmset VMGID)"
VM_NAME="$(vmset VM)"
[[ "$SVC_USER" =~ ^[a-z_][a-z0-9_.-]*$ ]] || die "VMUSER in $VM_SET is missing or not a user name: '$SVC_USER'"
[[ "$SVC_UID" =~ ^[0-9]+$ ]] || die "VMUID in $VM_SET is missing or not a number: '$SVC_UID'"
[[ "$SVC_GID" =~ ^[0-9]+$ ]] || die "VMGID in $VM_SET is missing or not a number: '$SVC_GID'"
# ── Service account ────────────────────────────────────────────────────────
echo "== Service account =="
# The container runs as this uid. On a production VM the secrets directory is
# on a shared NFS mount exported with root_squash, meaning root cannot read
# mode-0600 files -- only the service account can. IT provisions the account
# and names it in VM.set; the two must agree. See SECRETS.md for the
# production story.
account_fail() {
echo "FATAL: $*" >&2
echo "$VM_SET and the accounts on this VM disagree; ask PACS via help-astro@uw.edu." >&2
exit 1
}
have_user="$(getent passwd "$SVC_USER" || true)"
[ -n "$have_user" ] || account_fail "user $SVC_USER does not exist"
have_uid="$(echo "$have_user" | cut -d: -f3)"
have_gid="$(echo "$have_user" | cut -d: -f4)"
[ "$have_uid" = "$SVC_UID" ] \
|| account_fail "user $SVC_USER has uid $have_uid, VM.set says $SVC_UID"
[ "$have_gid" = "$SVC_GID" ] \
|| account_fail "user $SVC_USER has primary gid $have_gid, VM.set says $SVC_GID"
echo " ok user $SVC_USER ($SVC_UID:$SVC_GID) from $VM_SET exists"
# ── Packages ────────────────────────────────────────────────────────────────
echo
echo "== Packages =="
need=()
command -v podman >/dev/null || need+=(podman)
command -v podman-compose >/dev/null || need+=(podman-compose)
if [ ${#need[@]} -gt 0 ]; then
rpm -q epel-release >/dev/null 2>&1 || dnf install -y epel-release
dnf install -y "${need[@]}"
else
echo "podman and podman-compose already installed"
fi
podman --version
# A compose restart policy only survives reboot if this unit is enabled.
systemctl enable --now podman-restart.service >/dev/null 2>&1 \
&& echo "podman-restart.service enabled" \
|| warn "could not enable podman-restart.service (containers won't restart at boot)"
# ── Catalog storage ─────────────────────────────────────────────────────────
echo
echo "== Catalog storage =="
# Mounting the share is IT's job; this only checks the result. The share is
# exported with root_squash, so look at it as the service account, which is
# who Apache in the container will be.
as_svc() { setpriv --reuid="$SVC_UID" --regid="$SVC_GID" --clear-groups "$@"; }
if ! as_svc test -d "$HATS_DIR"; then
warn "$HATS_DIR does not exist or $SVC_USER cannot see it. The app container
will not start without it. Is it mounted (findmnt $HATS_DIR)?"
elif ! as_svc test -r "$HATS_DIR" || ! as_svc test -x "$HATS_DIR"; then
warn "$SVC_USER cannot read $HATS_DIR; /hats/ would answer 403"
elif [ -z "$(as_svc ls -A "$HATS_DIR" 2>/dev/null | head -n 1)" ]; then
warn "$HATS_DIR is empty; /hats/ will list no catalogs"
else
echo " ok $HATS_DIR is readable by $SVC_USER"
fi
# ── Compose environment ────────────────────────────────────────────────────
echo
echo "== Compose environment =="
FQDN="$(hostname -f 2>/dev/null || hostname)"
case "$FQDN" in
*.*) ;;
*) warn "hostname '$FQDN' is not fully qualified; check DNS and /etc/hosts" ;;
esac
# VM.set's VM is IT's short name for this VM. Nothing here depends on it, but
# a mismatch means VM.set may have been copied from another VM.
SHORT="$(hostname -s | tr '[:upper:]' '[:lower:]')"
[ -z "$VM_NAME" ] || [ "$VM_NAME" = "$SHORT" ] \
|| warn "VM.set says VM=$VM_NAME but the hostname is $SHORT; is $VM_SET from this VM?"
# .env is gitignored and derived: rewritten on every run from VM.set and the
# hostname, so podman compose can read them. Re-run init.sh rather than
# editing it.
cat > "$ENV_FILE" <<EOF
# Written by init.sh on every run; don't edit.
SVC_USER=$SVC_USER
SVC_UID=$SVC_UID
SVC_GID=$SVC_GID
SVC_FQDN=$FQDN
HATS_DIR=$HATS_DIR
EOF
echo " ok SVC_USER=$SVC_USER SVC_UID=$SVC_UID SVC_GID=$SVC_GID written to $ENV_FILE"
echo " ok SVC_FQDN=$FQDN written to $ENV_FILE"
echo " ok HATS_DIR=$HATS_DIR written to $ENV_FILE"
echo
if [ "$WARNINGS" -gt 0 ]; then
echo "init complete with $WARNINGS warning(s)."
else
echo "init complete."
fi
echo "Next: podman compose up -d --build"