Files
hub/deploy/run.sh
T

137 lines
3.4 KiB
Bash

#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
if [ ! -f .env ]; then
echo ".env file not found. You can create one by copying the provided example:"
echo "cp $(dirname "$0")/example.env $(dirname "$0")/.env"
exit 1
fi
source .env
# Required: crash if not set in .env
: "${domain:?domain is not defined in .env}"
: "${install_path:?install_path is not defined in .env}"
# Optional: fall back to defaults
: "${data_path:=$install_path/local-data}"
: "${launch_path:=$data_path/hub-launch.sh}"
: "${service_name:=horde-hub}"
: "${port:=17922}"
: "${remote:=$domain}"
read_template() {
sed \
-e "s/\$DOMAIN/$domain/g" \
-e "s|\$INSTALL_PATH|$install_path|g" \
-e "s|\$LAUNCH_PATH|$launch_path|g" \
-e "s|\$DATA_PATH|$data_path|g" \
-e "s/\$SERVICE_NAME/$service_name/g" \
-e "s/\$PORT/$port/g" \
template/$1
}
upload_template() {
read_template $1 | ssh $remote "cat > \"$2\""
}
run_nginx() {
echo "Uploading nginx config file"
upload_template nginx.conf "/etc/nginx/sites-enabled/$domain"
ssh $remote -t "
# full process is currently broken
#certbot --keep -d $domain
# uncomment if needed
#certbot certonly -d $domain
nginx -s reload
"
}
run_service() {
echo "Uploading Launcher and Service"
upload_template launch-hub.sh $launch_path
upload_template systemd.service "/etc/systemd/system/$service_name.service"
ssh $remote -t "
systemctl daemon-reload
systemctl enable $service_name.service
systemctl start $service_name.service
"
}
run_update() {
echo "Updating repository"
ssh $remote -t "
source ~/.nvm/nvm.sh #?
cd $install_path
git pull
make install
make web
"
}
run_dist() {
echo "Uploading frontend/dist to $remote:$install_path/frontend/dist"
ssh $remote "rm -rf $install_path/frontend/dist && mkdir $install_path/frontend/dist"
scp -r ../frontend/dist $remote:$install_path/frontend/
}
restart_service() {
echo "Restarting $service_name.service"
ssh $remote -t "systemctl restart $service_name.service"
}
# Determine which stages to run
stages=()
do_restart=0
if [[ $# -eq 0 ]]; then
# Interactive prompts when no arguments are given
read -p "Run nginx deployment? [Y/n] " choice
if [[ -z "$choice" || "$choice" =~ ^[Yy]$ ]]; then
stages+=(nginx)
fi
read -p "Run service deployment? [Y/n] " choice
if [[ -z "$choice" || "$choice" =~ ^[Yy]$ ]]; then
stages+=(service)
fi
read -p "Run update (git pull + make install)? [Y/n] " choice
if [[ -z "$choice" || "$choice" =~ ^[Yy]$ ]]; then
stages+=(update)
fi
else
# Parse arguments
for arg in "$@"; do
case "$arg" in
nginx|service|update|dist)
stages+=("$arg")
do_restart=1
;;
--dist)
stages+=("dist")
;;
*)
echo "Unknown argument: $arg" >&2
exit 1
;;
esac
done
fi
# Execute selected stages
for stage in "${stages[@]}"; do
case "$stage" in
nginx) run_nginx ;;
service) run_service ;;
update) run_update ;;
dist) run_dist ;;
esac
done
# Final stage: always restart the systemd service at the end
if [[ $do_restart -eq 1 ]]; then restart_service; fi