From 6887264a5bd8c7ab6b99aabae9b75c557478af0d Mon Sep 17 00:00:00 2001 From: Gerhard Salvini Date: Tue, 16 Jul 2024 16:08:08 +0200 Subject: [PATCH] Added infrastructure for running ECDC-Bridgehead under systemctl This comprises of the service file to be copied into the systemctl directory, plus Bridgehead start and stop scripts for both systemctl and for the administrator on the command line. --- ecdc.service | 14 ++++++++ restart_service.sh | 30 +++++++++++++++++ run.sh | 79 +++++++++++++++++++++++++++++++++++++++++++++ shutdown_service.sh | 13 ++++++++ stop.sh | 39 ++++++++++++++++++++++ 5 files changed, 175 insertions(+) create mode 100644 ecdc.service create mode 100755 restart_service.sh create mode 100755 run.sh create mode 100755 shutdown_service.sh create mode 100755 stop.sh diff --git a/ecdc.service b/ecdc.service new file mode 100644 index 0000000..e899ca5 --- /dev/null +++ b/ecdc.service @@ -0,0 +1,14 @@ +[Unit] +Description=Start ECDC Bridgehead + +[Service] +Type=simple +ExecStart=/srv/docker/bridgehead/restart_service.sh +ExecStop=/srv/docker/bridgehead/shutdown_service.sh +Restart=on-failure +RestartSec=36000 +KillMode=mixed + +[Install] +WantedBy=multi.user.target + diff --git a/restart_service.sh b/restart_service.sh new file mode 100755 index 0000000..4eecfc2 --- /dev/null +++ b/restart_service.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Start a running Bridgehead. If there is already a Bridgehead running, +# stop it first. +# This is intended to be used by systemctl. + +cd /srv/docker/bridgehead + +echo "git status before stop" +git status + +echo "Stopping running Bridgehead, if present" +./bridgehead stop bbmri + +# If "flush_blaze" is present, delete the Blaze volume before starting +# the Bridgehead again. This allows a user to upload all data, if +# requested. +if [ -f "/srv/docker/ecdc/data/flush_blaze" ]; then + docker volume rm bbmri_blaze-data + rm -f /srv/docker/ecdc/data/flush_blaze +fi + +echo "git status before start" +git status | systemd-cat -p info + +echo "Start the Bridgehead anew" +./bridgehead start bbmri + +echo "Bridgehead has unexpectedly terminated" + diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..1e6b36c --- /dev/null +++ b/run.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +# Start a Bridgehead from the command line. Upload data if requested. +# Behind the scenes we use systemctl to do the work. + +# Function to print usage +print_usage() { + echo "Start a Bridghead, optionally upload data" + echo "Usage: $0 [--upload | --upload-all | --help | -h]" + echo "Options:" + echo " --upload Run Bridgehead and upload just the new CSV data files." + echo " --upload-all Run Bridgehead and upload all CSV data files." + echo " --help, -h Display this help message." + echo " No options Run Bridgehead only." +} + +# Initialize variables +UPLOAD=false +UPLOAD_ALL=false + +# Parse arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --upload) + UPLOAD=true + ;; + --upload-all) + UPLOAD_ALL=true + ;; + --help|-h) + print_usage + exit 0 + ;; + *) + echo "Error: Unknown argument '$1'" + print_usage + exit 1 + ;; + esac + shift +done + +# Check for conflicting options +if [ "$UPLOAD" = true ] && [ "$UPLOAD_ALL" = true ]; then + echo "Error: you must specify either --upload or --upload-all, specifying both is not permitted." + print_usage + exit 1 +fi + +# Disable/stop standard Bridgehead systemctl services, if present +sudo systemctl disable bridgehead@bbmri.service +sudo systemctl disable system-bridgehead.slice +sudo systemctl disable bridgehead-update@bbmri.timer +sudo systemctl stop bridgehead@bbmri.service +sudo systemctl stop system-bridgehead.slice +sudo systemctl stop bridgehead-update@bbmri.timer + +# Set up systemctl for EHDS2/ECDC if necessary +cp /srv/docker/bridgehead/ecdc.service /etc/systemd/system +systemctl daemon-reload +systemctl enable ecdc.service + +# Use systemctl to stop the Bridgehead if it is running +sudo systemctl stop ecdc.service + +# Use files to tell the Bridgehead what to do with any data present +if [ "$UPLOAD" = true ] || [ "$UPLOAD_ALL" = true ]; then + if [ -f /srv/docker/ecdc/data/lock ]; then + rm /srv/docker/ecdc/data/lock + fi +fi +if [ "$UPLOAD_ALL" = true ]; then + echo "All CSV files in /srv/docker/ecdc/data will be uploaded" + touch /srv/docker/ecdc/data/flush_blaze +fi + +# Start up the Bridgehead +sudo systemctl start ecdc.service + diff --git a/shutdown_service.sh b/shutdown_service.sh new file mode 100755 index 0000000..3ea931a --- /dev/null +++ b/shutdown_service.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Shut down a running Bridgehead. +# This is intended to be used by systemctl. + +cd /srv/docker/bridgehead + +echo "git status before stop" +git status + +echo "Stopping running Bridgehead, if present" +./bridgehead stop bbmri + diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..cc71d96 --- /dev/null +++ b/stop.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +# Shut down a running Bridgehead and disable auto restart. +# Behind the scenes we use systemctl to do the work. + +# Function to print usage +print_usage() { + echo "Stop the running Bridgehead, disable auto-restart" + echo "Usage: $0 [--help | -h]" + echo "Options:" + echo " --help, -h Display this help message." + echo " No options Stop Bridgehead only." +} + +# Parse arguments +while [[ "$#" -gt 0 ]]; do + case $1 in + --help|-h) + print_usage + exit 0 + ;; + *) + echo "Error: Unknown argument '$1'" + print_usage + exit 1 + ;; + esac + shift +done + +# Set up systemctl for EHDS2/ECDC if necessary +cp /srv/docker/bridgehead/ecdc.service /etc/systemd/system +systemctl daemon-reload +systemctl enable ecdc.service + +# Use systemctl to stop the Bridgehead if it is running +sudo systemctl disable ecdc.service +sudo systemctl stop ecdc.service +