11#! /usr/bin/env bash
22set -e
33
4- # --- Global Config - --
4+ # -- Constants --
55
6+ 7+ REPO_URL=" https://github.com/wmde/wikibase-release-pipeline.git"
68WBS_DIR=" /opt/wbs"
79LOG_PATH=" $WBS_DIR /setup.log"
10+ DEPLOY_DIR=" $WBS_DIR /wikibase-release-pipeline/deploy"
11+ SETUP_DIR=" $DEPLOY_DIR /setup"
12+ ENV_FILE_PATH=" $DEPLOY_DIR /.env"
13+
14+ # --- CLI related ---
815
9- # CLI options (can be overridden by env or args)
16+ CLOUD_INIT=" ${CLOUD_INIT:- false} "
17+ DEV=" ${DEV:- false} "
18+ LAUNCH_MODE=false
19+ SKIP_DEPENDENCY_INSTALLS=" ${SKIP_DEPENDENCY_INSTALLS:- false} "
1020VERBOSE=" ${VERBOSE:- false} "
11- SKIP_DOCKER_INSTALL=" ${SKIP_DOCKER_INSTALL:- false} "
12- CERT_EMAIL=" ${CERT_EMAIL:- wbs-setup@ wikimedia.de} "
1321
14- # CLI overrides
1522for arg in " $@ " ; do
1623 case " $arg " in
17- --verbose )
18- VERBOSE =true
24+ --cloud-init )
25+ CLOUD_INIT =true
1926 ;;
20- --skip-docker-install)
21- SKIP_DOCKER_INSTALL=true
27+ --dev)
28+ DEV=true
29+ SKIP_DEPENDENCY_INSTALLS=true
30+ WBS_DIR=../../..
31+ LOG_PATH=" $WBS_DIR /setup.log"
32+ DEPLOY_DIR=" $WBS_DIR /wikibase-release-pipeline/deploy"
33+ SETUP_DIR=" $DEPLOY_DIR /setup"
34+ SETUP_HOST=localhost
2235 ;;
23- --cert-email=* )
24- CERT_EMAIL=" ${arg#* =} "
36+ --launch)
37+ LAUNCH_MODE=true
38+ ;;
39+ --skip-depedency-installs)
40+ SKIP_DEPENDENCY_INSTALLS=true
41+ ;;
42+ --verbose)
43+ VERBOSE=true
2544 ;;
2645 esac
2746done
2847
29- # Logging functions
48+ # -- Setup host for SSL cert generation --
49+
50+ PUBLIC_IP=$( curl --silent --show-error --fail https://api.ipify.org)
51+ SETUP_PORT=8888
52+ # Random suffix keeps Let's Encrypt from rate limiting
53+ # but not in CLOUD_INIT so that the setup webserver address
54+ # remains known (https://<SERVER-IP>.nip.io:8888) in that case.
55+ if [[ " $CLOUD_INIT " = " true" ]]; then
56+ SETUP_HOST=$PUBLIC_IP .nip.io
57+ else
58+ SETUP_SUBDOMAIN=wbs-setup-$( LC_ALL=C tr -dc ' a-z0-9' < /dev/urandom | head -c 6)
59+ SETUP_HOST=$SETUP_SUBDOMAIN .$PUBLIC_IP .nip.io
60+ fi
61+
62+ # -- Setup logging --
63+
64+ mkdir -p " $WBS_DIR "
65+ touch " $LOG_PATH "
66+
67+
3068log () {
3169 if $VERBOSE ; then
3270 echo " $@ "
@@ -39,149 +77,145 @@ log_cmd() {
3977 else
4078 bash -c " $@ " & > /dev/null
4179 fi
80+
4281 return $?
4382}
4483
45- # --- Run detached so closing terminal will not terminate setup process ---
84+ # --- Non-launch Mode: Run setup ---
4685
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
86+ if ! $LAUNCH_MODE ; then
87+ exec > >( tee -a " $LOG_PATH " ) 2>&1
88+ # clear || printf "\033c"
89+ echo
90+ echo " Wikibase Suite Deploy Setup"
91+ echo
5492
55- # --- Begin main execution (detached) ---
93+ install_git () {
94+ if command -v git & > /dev/null; then
95+ log " Git already installed"
96+ return
97+ fi
5698
57- exec > >( tee -a " $LOG_PATH " ) 2>&1
99+ echo " Installing Git..."
100+
101+ if command -v apt-get & > /dev/null; then
102+ log_cmd " apt-get update && apt-get install -y git"
103+ elif command -v dnf & > /dev/null; then
104+ log_cmd " dnf install -y git"
105+ elif command -v yum & > /dev/null; then
106+ log_cmd " yum install -y git"
107+ elif command -v apk & > /dev/null; then
108+ log_cmd " apk add git"
109+ elif command -v pacman & > /dev/null; then
110+ log_cmd " pacman -Sy --noconfirm git"
111+ else
112+ echo " ⚠️ Unsupported package manager. Please install Git manually."
113+ exit 1
114+ fi
115+ }
116+
117+ install_docker () {
118+ echo " Installing Docker..."
119+ log_cmd " curl -fsSL https://get.docker.com | sh"
120+ log_cmd " systemctl enable --now docker"
121+ echo " Installing Docker Compose plugin..."
122+ mkdir -p ~ /.docker/cli-plugins
123+ log_cmd " curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-$( uname -s) -$( uname -m) -o ~/.docker/cli-plugins/docker-compose"
124+ chmod +x ~ /.docker/cli-plugins/docker-compose
125+ }
126+
127+ clone_repo () {
128+ echo " Cloning Wikibase Release Pipeline repository..."
129+ cd " $WBS_DIR "
130+ log_cmd " git clone $REPO_URL "
131+ cd wikibase-release-pipeline
132+ log_cmd " git checkout deploy-setup-script"
133+ }
134+
135+ generate_cert_for_setup_webserver () {
136+ log " Generating Let's Encrypt TLS certificate for setup page..."
137+
138+ log_cmd " mkdir -p $SETUP_DIR /letsencrypt $SETUP_DIR /certs"
139+ log " Using domain: $SETUP_HOST "
140+
141+ log_cmd " docker pull certbot/certbot"
142+
143+ if log_cmd " docker run --rm \
144+ -v $SETUP_DIR /letsencrypt:/etc/letsencrypt \
145+ -v $SETUP_DIR /certs:/certs \
146+ -p 80:80 \
147+ certbot/certbot certonly \
148+ --standalone \
149+ --non-interactive \
150+ --preferred-challenges http \
151+ --agree-tos \
152+ --email $CERT_EMAIL \
153+ -d $SETUP_HOST " ; then
154+
155+ CERT_PATH=" $SETUP_DIR /letsencrypt/live/$SETUP_HOST "
156+ cp " $CERT_PATH /fullchain.pem" " $SETUP_DIR /certs/cert.pem"
157+ cp " $CERT_PATH /privkey.pem" " $SETUP_DIR /certs/key.pem"
58158
59- log " Verbose mode enabled"
159+ else
160+ echo " Let's Encrypt challenge failed, falling back to self-signed certificate."
60161
61- # --- Constants ---
162+ openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
163+ -out " $SETUP_DIR /certs/cert.pem" \
164+ -keyout " $SETUP_DIR /certs/key.pem" \
165+ -subj " /CN=$SETUP_HOST "
166+ fi
167+ }
62168
63- REPO_URL=" https://github.com/wmde/wikibase-release-pipeline.git"
64- DEPLOY_DIR=" $WBS_DIR /wikibase-release-pipeline/deploy"
65- SETUP_DIR=" $DEPLOY_DIR /setup"
66- ENV_FILE_PATH=" $DEPLOY_DIR /.env"
169+ start_setup_webserver () {
170+ log " Starting setup page webserver container..."
171+ cd " $SETUP_DIR "
67172
68- PUBLIC_IP=$( curl --silent --show-error --fail https://api.ipify.org)
69- SETUP_SUBDOMAIN=wbs-setup-$( LC_ALL=C tr -dc ' a-z0-9' < /dev/urandom | head -c 6)
70- SETUP_HOST=$SETUP_SUBDOMAIN .$PUBLIC_IP .nip.io
71- SETUP_PORT=8888
173+ generate_cert_for_setup_webserver
72174
73- # --- Functions ---
175+ # Makes compatible with environments that have Buildx installed
176+ BUILDX_DRIVER=$( docker buildx inspect | grep ' Driver:' | awk ' {print $2}' )
177+ if [ " $BUILDX_DRIVER " = " docker-container" ]; then
178+ LOAD_FLAG=" --load"
179+ else
180+ LOAD_FLAG=" "
181+ fi
74182
75- start_message () {
76- echo
77- echo " Wikibase Suite Deploy Setup"
78- echo
79- }
183+ log_cmd " docker build $LOAD_FLAG -t wikibase/deploy-setup-webserver ."
184+ log_cmd " docker run -d \
185+ -p $SETUP_PORT :443 \
186+ -v $DEPLOY_DIR :/app/deploy \
187+ -v $SETUP_DIR /certs:/app/certs \
188+ -v $LOG_PATH :/app/setup.log \
189+ wikibase/deploy-setup-webserver"
80190
81- install_git () {
82- if command -v git & > /dev/null; then
83- log " Git already installed"
84- return
85- fi
191+ echo
192+ echo " To continue setup navigate to:"
193+ echo
194+ echo " https://$SETUP_HOST :$SETUP_PORT "
195+ echo
196+ }
86197
87- echo " Installing Git..."
88-
89- if command -v apt-get & > /dev/null; then
90- log_cmd " apt-get update && apt-get install -y git"
91- elif command -v dnf & > /dev/null; then
92- log_cmd " dnf install -y git"
93- elif command -v yum & > /dev/null; then
94- log_cmd " yum install -y git"
95- elif command -v apk & > /dev/null; then
96- log_cmd " apk add git"
97- elif command -v pacman & > /dev/null; then
98- log_cmd " pacman -Sy --noconfirm git"
198+ if ! $SKIP_DEPENDENCY_INSTALLS ; then
199+ install_git
200+ install_docker
99201 else
100- echo " ⚠️ Unsupported package manager. Please install Git manually."
101- exit 1
202+ echo " Skipping dependency checks and installations..."
102203 fi
103- }
104-
105- install_docker () {
106- echo " Installing Docker..."
107- log_cmd " curl -fsSL https://get.docker.com | sh"
108- log_cmd " systemctl enable --now docker"
109- echo " Installing Docker Compose plugin..."
110- mkdir -p ~ /.docker/cli-plugins
111- log_cmd " curl -SL https://github.com/docker/compose/releases/latest/download/docker-compose-$( uname -s) -$( uname -m) -o ~/.docker/cli-plugins/docker-compose"
112- chmod +x ~ /.docker/cli-plugins/docker-compose
113- }
114-
115- clone_repo () {
116- echo " Cloning Wikibase Release Pipeline repository..."
117- cd " $WBS_DIR "
118- log_cmd " git clone $REPO_URL "
119- cd wikibase-release-pipeline
120- log_cmd " git checkout deploy-setup-script"
121- }
122-
123- generate_ssl_cert_for_setup_webserver () {
124- log " Generating Let's Encrypt TLS certificate for setup page..."
125-
126- log_cmd " mkdir -p $SETUP_DIR /letsencrypt $SETUP_DIR /certs"
127- log " Using domain: $SETUP_HOST "
128-
129- # Pre-pull certbot image to suppress output
130- log_cmd " docker pull certbot/certbot"
131-
132- if log_cmd " docker run --rm \
133- -v $SETUP_DIR /letsencrypt:/etc/letsencrypt \
134- -v $SETUP_DIR /certs:/certs \
135- -p 80:80 \
136- certbot/certbot certonly \
137- --standalone \
138- --non-interactive \
139- --preferred-challenges http \
140- --agree-tos \
141- --email $CERT_EMAIL \
142- -d $SETUP_HOST " ; then
143-
144- CERT_PATH=" $SETUP_DIR /letsencrypt/live/$SETUP_HOST "
145- cp " $CERT_PATH /fullchain.pem" " $SETUP_DIR /certs/cert.pem"
146- cp " $CERT_PATH /privkey.pem" " $SETUP_DIR /certs/key.pem"
147- else
148- echo " Let's Encrypt challenge failed, falling back to self-signed certificate."
149-
150- openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
151- -out " $SETUP_DIR /certs/cert.pem" \
152- -keyout " $SETUP_DIR /certs/key.pem" \
153- -subj " /CN=$SETUP_HOST "
204+ if ! $DEV ; then
205+ clone_repo
154206 fi
155- }
156-
157- start_config_webserver () {
158- log " Starting setup page webserver container..."
159- cd " $SETUP_DIR "
160-
161- generate_ssl_cert_for_setup_webserver
162-
163- # Makes compatible with environments that have Buildx installed which
164- # Add --load only if using docker-container driver
165- BUILDX_DRIVER=$( docker buildx inspect | grep ' Driver:' | awk ' {print $2}' )
166- if [ " $BUILDX_DRIVER " = " docker-container" ]; then
167- LOAD_FLAG=" --load"
168- else
169- LOAD_FLAG=" "
207+ start_setup_webserver
208+
209+ if ! $DEV ; then
210+ # Launch second phase in background
211+ CMD=(bash " $DEPLOY_DIR /setup/setup.sh" --launch)
212+ if $VERBOSE ; then CMD+=(--verbose); fi
213+ setsid " ${CMD[@]} " >> " $LOG_PATH " 2>&1 < /dev/null &
214+ exit 0
170215 fi
216+ fi
171217
172- log_cmd " docker build $LOAD_FLAG -t wikibase/deploy-setup-webserver ."
173- log_cmd " docker run -d \
174- -p $SETUP_PORT :443 \
175- -v $DEPLOY_DIR :/app/deploy \
176- -v $SETUP_DIR /certs:/app/certs \
177- -v $LOG_PATH :/app/setup.log \
178- wikibase/deploy-setup-webserver"
179- echo
180- echo " To complete setup, navigate to:"
181- echo
182- echo " https://$SETUP_HOST :$SETUP_PORT "
183- echo
184- }
218+ # --- Launch Mode: Wait for .env and start services ---
185219
186220wait_for_env_file () {
187221 until [ -f " $ENV_FILE_PATH " ]; do
@@ -192,13 +226,13 @@ wait_for_env_file() {
192226
193227launch_wikibase () {
194228 echo " Launching Wikibase Suite Docker containers..."
195- log_cmd " cd $DEPLOY_DIR "
229+ cd " $DEPLOY_DIR "
196230 log_cmd " docker compose up -d"
197231}
198232
199233final_message () {
200234 echo
201- echo " ✅ Setup is Complete!"
235+ echo " Setup is Complete!"
202236 echo
203237
204238 if [[ -f " $ENV_FILE_PATH " ]]; then
@@ -227,17 +261,7 @@ final_message() {
227261 fi
228262}
229263
230- # --- Execution ---
231-
232- start_message
233- install_git
234- if [[ " $SKIP_DOCKER_INSTALL " != " true" ]]; then
235- install_docker
236- else
237- echo " Skipping Docker installation"
238- fi
239- clone_repo
240- start_config_webserver
264+ exec >> " $LOG_PATH " 2>&1
241265wait_for_env_file
242266launch_wikibase
243267final_message
0 commit comments