CaiWuXiTong/deploy/update-runtime-url.sh

38 lines
761 B
Bash

#!/bin/sh
set -eu
APP_URL="${1:?APP_URL is required}"
REMOTE_DIR="${2:-/opt/caiwuxitong}"
ENV_FILE="$REMOTE_DIR/runtime/.env"
TEMP_FILE="$ENV_FILE.tmp"
if [ "$(id -u)" -ne 0 ]; then
echo "update-runtime-url.sh must run as root" >&2
exit 1
fi
if [ ! -f "$ENV_FILE" ]; then
echo "Missing $ENV_FILE; run bootstrap-server.sh first" >&2
exit 1
fi
umask 077
awk -v app_url="$APP_URL" '
BEGIN { updated = 0 }
/^APP_URL=/ {
print "APP_URL=" app_url
updated = 1
next
}
{ print }
END {
if (!updated) {
print "APP_URL=" app_url
}
}
' "$ENV_FILE" > "$TEMP_FILE"
chown --reference="$ENV_FILE" "$TEMP_FILE"
chmod --reference="$ENV_FILE" "$TEMP_FILE"
mv "$TEMP_FILE" "$ENV_FILE"