33set -euo pipefail
44
55INSTALL_DIR=" /opt/relaysms/relaysms-publisher"
6- SERVICE_NAME=" relaysms-publisher"
76REPO_URL=" https://github.com/smswithoutborders/RelaySMS-Publisher.git"
87BRANCH=" ${BRANCH:- main} "
98CARGO_BIN=" $HOME /.cargo/bin"
109DEPS_MARKER=" /var/lib/relaysms-publisher-deps-installed"
1110
12- # Single source of truth for the unit set. Add/rename a service here and it
13- # propagates to install, enable/start, and restart — no need to touch it
14- # in more than one place.
1511TARGET_UNIT=" relaysms-publisher.target"
1612SERVICE_UNITS=(
1713 relaysms-publisher-rest.service
@@ -21,12 +17,8 @@ SERVICE_UNITS=(
2117)
2218ALL_UNITS=(" $TARGET_UNIT " " ${SERVICE_UNITS[@]} " )
2319
24- # Prefer the invoking user (SUDO_USER) as the service owner so no extra
25- # system account is needed; fall back to a dedicated 'relaysms' user when
26- # run directly as root. Note this only affects ownership of the app's
27- # runtime files/services — the build itself (Rust, venv, `make build-setup`)
28- # always runs as root/root's $HOME, since the script isn't re-exec'd with
29- # `sudo -E`.
20+ # Runtime files are owned by the invoking user if run via sudo, otherwise
21+ # a dedicated 'relaysms' user. The build itself still runs as root.
3022if [ -n " ${SUDO_USER:- } " ] && id " $SUDO_USER " & > /dev/null; then
3123 SERVICE_USER=" $SUDO_USER "
3224else
@@ -38,18 +30,14 @@ error() {
3830 echo " [$( date +' %Y-%m-%d %H:%M:%S' ) ] ERROR: $* " >&2
3931 exit 1
4032}
41- # Safety net for failures not already wrapped in `|| error "..."` (e.g. a
42- # bare apt-get/git call). Explicit exit calls from error() don't re-trigger
43- # this trap, so messages are never duplicated.
33+ # Catches failures not already wrapped in error(), with line context.
4434on_err () { echo " [$( date +' %Y-%m-%d %H:%M:%S' ) ] ERROR: aborted at line $1 (last command: $2 )" >&2 ; }
4535trap ' on_err "$LINENO" "$BASH_COMMAND"' ERR
4636
4737check_root () { [ " $EUID " -eq 0 ] || error " Run with sudo" ; }
4838
49- # Reads a single KEY=value from an env file without sourcing/evaluating it
50- # (avoids executing arbitrary shell in a file we don't fully control).
51- # Tolerates an `export ` prefix and quoted values. The `|| true` on the
52- # grep keeps a no-match result from tripping `pipefail`.
39+ # Reads KEY=value from a file without sourcing it. `|| true` on the grep
40+ # stops a no-match from tripping pipefail.
5341read_env_var () {
5442 local key=" $1 " file=" $2 " val
5543 val=$( (grep -E " ^(export[[:space:]]+)?${key} [[:space:]]*=" " $file " 2> /dev/null || true) |
@@ -61,10 +49,7 @@ read_env_var() {
6149 echo " $val "
6250}
6351
64- # Appends a directory to the global RW_DIRS array (resolving it relative to
65- # INSTALL_DIR if not absolute), de-duplicating as it goes. Defined once at
66- # top level — resolve_app_directories is called twice per run (directory
67- # creation, then systemd ReadWritePaths), and both must see the same set.
52+ # Adds a dir to the global RW_DIRS array, de-duped, relative to INSTALL_DIR.
6853_resolve_dir () {
6954 local dir=" $1 "
7055 [ -z " $dir " ] && return
@@ -120,9 +105,7 @@ setup_service_user() {
120105
121106clone_repository () {
122107 log " Cloning repository"
123- # `-c url....insteadOf` is scoped to this git invocation only, so we
124- # don't permanently rewrite the invoking user's global git config just
125- # to fetch this one repo over HTTPS instead of SSH.
108+ # -c scopes the SSH-to-HTTPS rewrite to this command, not global git config.
126109 if [ -d " $INSTALL_DIR /.git" ]; then
127110 log " Repository exists, updating"
128111 cd " $INSTALL_DIR "
@@ -184,9 +167,8 @@ setup_env() {
184167 log " Edit $INSTALL_DIR /.env before starting services"
185168}
186169
187- # Shared by create_app_directories and install_services so the directories
188- # that get created and the paths granted to systemd's ReadWritePaths can
189- # never drift apart from what .env actually configures.
170+ # Shared by create_app_directories and install_services so both stay in
171+ # sync with .env.
190172resolve_app_directories () {
191173 local envfile=" $INSTALL_DIR /.env"
192174 [ -f " $envfile " ] || error " .env not found"
@@ -256,7 +238,7 @@ install_services() {
256238 local svc
257239 for svc in " ${ALL_UNITS[@]} " ; do
258240 [ -f " $svc " ] || error " Service file not found: $svc "
259- # '#' delimiter (not '|') since a resolved path could contain a pipe.
241+ # # as delimiter, a resolved path could contain a pipe.
260242 sed \
261243 -e " s/User=relaysms/User=$SERVICE_USER /" \
262244 -e " s#__RW_PATHS__#$rw_paths #" \
0 commit comments