Files
hla_framework_bd/.gitea/scripts/deploy-service.sh
2026-08-27 14:54:03 +02:00

77 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# Deploys a service to /sls/bd/hla and (re)starts its systemd unit.
#
# Usage: ./.gitea/scripts/deploy-service.sh <service-name> <dev|prod> [restart]
source "$(dirname "$0")/common.sh"
init_service_env "${1:-}" "${2:-}"
DO_RESTART_SERVICE=$3
NOW=$(date +%Y-%m-%dT%H:%M:%S%Z)
echo "=========================================="
echo "Deploying service: $SERVICE_NAME_LOWER (${AGEBD_ENV})"
echo "=========================================="
SYSTEMD_UNIT_NAME=AGEBD-SERVICE-${SERVICE_NAME_UPPER}.service
SYSTEMD_UNIT_FILE_PATH=${SERVICE_DIR}/systemd/${SYSTEMD_UNIT_NAME}
IOC_PORT=$(uv run --directory scripts ci/get_ioc_port.py ${SERVICE_NAME_LOWER})
# TODO: see https://gitea.psi.ch/sls/hla_framework_bd/issues/50
# rm -rf "${SERVICE_DIR}"
# cp -r ./services/${SERVICE_NAME_LOWER}/current/* ${SERVICE_DIR}/
mkdir -p "${SERVICE_DIR}"
rsync -avz --delete \
--exclude='.nfs*' \
--exclude='.git/' \
--exclude='.venv/' \
--exclude='__pycache__/' \
--exclude='*.pyc' \
--exclude='.pytest_cache/' \
./services/${SERVICE_NAME_LOWER}/current/* ${SERVICE_DIR}/
### SUBSTITUTIONS
### IOC
sed -i "s/{{ *agebd_env_suffix_upper* }}/${AGEBD_ENV_SUFFIX_UPPER}/g" "${IOC_DIR}/"*
sed -i "s/{{ *agebd_env_suffix_lower* }}/${AGEBD_ENV_SUFFIX_LOWER}/g" "${IOC_DIR}/"*
sed -i "s/{{ *ioc_port* }}/${IOC_PORT}/g" "${IOC_DIR}/"*
### RENAMING
### The ioc files carry the environment suffix in their name, e.g. in dev
### AGEBD-CPCL-NTURNS_main.subs -> AGEBD-CPCL-NTURNS-DEV_main.subs
### (rsync --delete restores the original names on every deploy, so this runs every time)
if [ -n "${AGEBD_ENV_SUFFIX_UPPER}" ]; then
for FILE_SUFFIX in "_main.subs" "_parameters.yaml"; do
ORIG_FILE=${IOC_DIR}/AGEBD-CPCL-${SERVICE_NAME_UPPER}${FILE_SUFFIX}
if [ -f "${ORIG_FILE}" ]; then
mv "${ORIG_FILE}" "${IOC_DIR}/AGEBD-CPCL-${SERVICE_NAME_UPPER}${AGEBD_ENV_SUFFIX_UPPER}${FILE_SUFFIX}"
fi
done
fi
VERSION_DEPLOYED_PV="AGEBD-ALH${AGEBD_ENV_SUFFIX_UPPER}:${SERVICE_NAME_UPPER}-VERSION-DEPLOYED"
echo "Putting ${NOW} in ${VERSION_DEPLOYED_PV}"
caput "${VERSION_DEPLOYED_PV}" "${NOW}"
### SYSTEMD
# Not every service has a systemd unit (e.g. IOC-only services like params) -
# skip enabling/restarting entirely when there's nothing to enable/restart.
if [ -f "${SYSTEMD_UNIT_FILE_PATH}" ]; then
sed -i "s/{{ *agebd_env* }}/${AGEBD_ENV}/g" "${SYSTEMD_UNIT_FILE_PATH}"
mkdir -p ${HOME}/.config/systemd/user
ln -sf "${SYSTEMD_UNIT_FILE_PATH}" "${HOME}/.config/systemd/user/${SYSTEMD_UNIT_NAME}"
if [ "$DO_RESTART_SERVICE" ]; then
echo "Enabling and restarting systemd service ${SYSTEMD_UNIT_NAME}"
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
systemctl --user daemon-reload
systemctl --user enable "${SYSTEMD_UNIT_NAME}"
systemctl --user restart "${SYSTEMD_UNIT_NAME}"
fi
else
echo "No systemd unit for ${SERVICE_NAME_LOWER}, skipping enable/restart"
fi