Skip to content

Commit 9e02775

Browse files
committed
feat(deploy): Setup script now runs detached so it doesn't terminate when initiated terminal is closed
1 parent bce1433 commit 9e02775

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

deploy/setup/setup.sh

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
# --- Config CLI ---
4+
# --- Global Config ---
55

6-
# Default values (can be overridden by CLI)
6+
WBS_DIR="/opt/wbs"
7+
LOG_PATH="$WBS_DIR/setup.log"
8+
9+
# CLI options (can be overridden by env or args)
710
VERBOSE="${VERBOSE:-false}"
811
SKIP_DOCKER_INSTALL="${SKIP_DOCKER_INSTALL:-false}"
912
CERT_EMAIL="${CERT_EMAIL:-wbs-setup@wikimedia.de}"
@@ -23,6 +26,7 @@ for arg in "$@"; do
2326
esac
2427
done
2528

29+
# Logging functions
2630
log() {
2731
if $VERBOSE; then
2832
echo "$@"
@@ -35,31 +39,37 @@ log_cmd() {
3539
else
3640
bash -c "$@" &> /dev/null
3741
fi
38-
39-
return $? # Pass the exit code back to caller
42+
return $?
4043
}
4144

42-
# Constants
43-
WBS_DIR="/opt/wbs"
44-
LOG_PATH="$WBS_DIR/setup.log"
45+
# --- Run detached so closing terminal will not terminate setup process---
46+
47+
if [[ -z "$WBS_SETUP_DETACHED" ]]; then
48+
export WBS_SETUP_DETACHED=1
49+
mkdir -p "$WBS_DIR"
50+
touch "$LOG_PATH"
51+
nohup bash "$0" "$@" >> "$LOG_PATH" 2>&1 &
52+
exit 0
53+
fi
54+
55+
# --- Begin main execution (detached) ---
56+
57+
exec > >(tee -a "$LOG_PATH") 2>&1
58+
59+
log "Verbose mode enabled"
60+
61+
# --- Constants ---
62+
4563
REPO_URL="https://github.com/wmde/wikibase-release-pipeline.git"
4664
DEPLOY_DIR="$WBS_DIR/wikibase-release-pipeline/deploy"
4765
SETUP_DIR="$DEPLOY_DIR/setup"
4866
ENV_FILE_PATH="$DEPLOY_DIR/.env"
4967

5068
PUBLIC_IP=$(curl --silent --show-error --fail https://api.ipify.org)
51-
# Random suffix keeps Let's Encrypt from rate limiting
5269
SETUP_SUBDOMAIN=wbs-setup-$(LC_ALL=C tr -dc 'a-z0-9' </dev/urandom | head -c 6)
5370
SETUP_HOST=$SETUP_SUBDOMAIN.$PUBLIC_IP.nip.io
5471
SETUP_PORT=8888
5572

56-
# Setup logging
57-
mkdir -p "$WBS_DIR"
58-
touch $LOG_PATH
59-
exec > >(tee -a "$LOG_PATH") 2>&1
60-
61-
log "Verbose mode enabled"
62-
6373
# --- Functions ---
6474

6575
start_message() {
@@ -134,7 +144,6 @@ generate_ssl_cert_for_setup_webserver() {
134144
CERT_PATH="$SETUP_DIR/letsencrypt/live/$SETUP_HOST"
135145
cp "$CERT_PATH/fullchain.pem" "$SETUP_DIR/certs/cert.pem"
136146
cp "$CERT_PATH/privkey.pem" "$SETUP_DIR/certs/key.pem"
137-
138147
else
139148
echo "Let's Encrypt challenge failed, falling back to self-signed certificate."
140149

@@ -168,7 +177,7 @@ start_config_webserver() {
168177
-v $LOG_PATH:/app/setup.log \
169178
wikibase/deploy-setup-webserver"
170179
echo
171-
echo "To complete setup navigate to:"
180+
echo "To complete setup, navigate to:"
172181
echo
173182
echo "https://$SETUP_HOST:$SETUP_PORT"
174183
echo
@@ -189,13 +198,11 @@ launch_wikibase() {
189198

190199
final_message() {
191200
echo
192-
echo "Setup is Complete!"
201+
echo "Setup is Complete!"
193202
echo
194203

195204
if [[ -f "$ENV_FILE_PATH" ]]; then
196-
# Load key=value pairs from .env into current shell (safe since we control the format)
197205
while IFS= read -r line; do
198-
# Ignore comments and blank lines
199206
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
200207
eval "export $line"
201208
done < "$ENV_FILE_PATH"

0 commit comments

Comments
 (0)