From e049e2971bdff7537d3f78b0c17ecfe4fa80b9ac Mon Sep 17 00:00:00 2001 From: Athanasius Date: Tue, 6 Jul 2021 10:44:44 +0000 Subject: [PATCH] systemd/start-eddn-service: Can now --from-source * Also now stores PID in per-service file in configured logs directory. * Swapped the environment arg and service so it's, e.g. "live gateway" now. * New optional third arg `--from-source` to... run from source, not installed files. The script *will* exit and redirect output, but record the PID and tell you it. New SRC_DIR in the config files to know where to run from. --- systemd/eddn_beta_config | 1 + systemd/eddn_dev_config | 1 + systemd/eddn_live_config | 1 + systemd/start-eddn-service | 65 ++++++++++++++++++++++++++++++-------- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/systemd/eddn_beta_config b/systemd/eddn_beta_config index 4677abc..3ac98e6 100644 --- a/systemd/eddn_beta_config +++ b/systemd/eddn_beta_config @@ -1,3 +1,4 @@ CONFIG_OVERRIDE="${HOME}/.local/share/eddn/beta/config.json" LOG_DIR="${HOME}/beta/logs" PYTHON_VENV="${HOME}/beta/python-venv" +SRC_DIR="${HOME}/beta/EDDN.git" diff --git a/systemd/eddn_dev_config b/systemd/eddn_dev_config index cdfdee2..6891bcd 100644 --- a/systemd/eddn_dev_config +++ b/systemd/eddn_dev_config @@ -1,3 +1,4 @@ CONFIG_OVERRIDE="${HOME}/.local/share/eddn/dev/config.json" LOG_DIR="${HOME}/dev/logs" PYTHON_VENV="${HOME}/dev/python-venv" +SRC_DIR="${HOME}/dev/EDDN.git" diff --git a/systemd/eddn_live_config b/systemd/eddn_live_config index 1ce0827..38d264d 100644 --- a/systemd/eddn_live_config +++ b/systemd/eddn_live_config @@ -1,3 +1,4 @@ CONFIG_OVERRIDE="${HOME}/.local/share/eddn/live/config.json" LOG_DIR="${HOME}/live/logs" PYTHON_VENV="${HOME}/live/python-venv" +SRC_DIR="${HOME}/live/EDDN.git" diff --git a/systemd/start-eddn-service b/systemd/start-eddn-service index 4f27af1..318e1ce 100755 --- a/systemd/start-eddn-service +++ b/systemd/start-eddn-service @@ -1,27 +1,42 @@ -#!/bin/sh -# vim: tabstop=2 shiftwidth=2 textwidth=0 wrapmargin=0 expandtab +#!/bin/bash +# Add ' -x' above to debug # # Start an EDDN Service, including redirecting output to a log file. +EXIT_CONFIG_MISSING=1 +EXIT_SERVICE_BIN_MISSING=2 +EXIT_CL_ARGS=3 + usage() { - echo "Usage: $(basename $0) [ gateway | monitor | relay ] [ live | beta | dev ]" + echo "Usage: $(basename $0) ( live | beta | dev ) ( gateway | monitor | relay ) [ --from-source ]" } if [ -z "${1}" ]; then usage - echo "No EDDN service specified." - exit 3 + echo "No EDDN environment specified." + exit ${EXIT_CL_ARGS} fi -SERVICE="${1}" +EDDN_ENV="${1}" if [ -z "${2}" ]; then usage - echo "No EDDN environment specified." - exit 3 + echo "No EDDN service specified." + exit ${EXIT_CL_ARGS} +fi +SERVICE="${2}" + +if [ ! -z "${3}" ]; +then + if [ "${3}" != "--from-source" ]; + then + usage + echo "Un-recognised argument: ${3}" + exit ${EXIT_CL_ARGS} + fi + FROM_SOURCE="1" fi -EDDN_ENV="${2}" EXEC_PATH=$(dirname $0) #echo "EXEC_PATH: ${EXEC_PATH}" @@ -34,17 +49,39 @@ cd ${EXEC_PATH} if [ ! -f "eddn_${EDDN_ENV}_config" ]; then echo "eddn_${EDDN_ENV}_config is missing from $(pwd)" - exit 1 + exit ${EXIT_CONFIG_MISSING} fi . "./eddn_${EDDN_ENV}_config" # Use the python venv . "${PYTHON_VENV}/bin/activate" -if [ ! -f "${PYTHON_VENV}/bin/eddn-${SERVICE}" ]; +# Running from source or install ? +if [ -z "${FROM_SOURCE}" ]; then - echo "${SERVICE} is missing from ${PYTHON_VENV}/bin" - exit 2 + # From install + if [ ! -f "${PYTHON_VENV}/bin/eddn-${SERVICE}" ]; + then + echo "${SERVICE} is missing from ${PYTHON_VENV}/bin" + exit ${EXIT_SERVICE_BIN_MISSING} + fi + + ${PYTHON_VENV}/bin/eddn-${SERVICE} --config "${CONFIG_OVERRIDE}" >> "${LOG_DIR}/${SERVICE}.log" 2>&1 & + echo $! > "${LOG_DIR}/${SERVICE}.pid" + wait $(cat "${LOG_DIR}/${SERVICE}.pid") +else + # From source + cd "${SRC_DIR}/src/eddn" + #pwd + # For now the source file has Camel case. + #echo ${SERVICE} + SERVICE_CAMEL=$(echo ${SERVICE} | sed -s 's/^\(.\)/\u\1/;') + #echo ${SERVICE_CAMEL} + ${PYTHON_VENV}/bin/python ${SERVICE_CAMEL}.py --config "${CONFIG_OVERRIDE}" >> "${LOG_DIR}/${SERVICE}.log" 2>&1 & + PID=$! + echo ${PID} > "${LOG_DIR}/${SERVICE}.pid" + echo "${SERVICE_CAMEL}.py started with PID: ${PID}" fi -exec ${PYTHON_VENV}/bin/eddn-${SERVICE} --config "${CONFIG_OVERRIDE}" >> "${LOG_DIR}/${SERVICE}.log" 2>&1 + +# vim: tabstop=2 shiftwidth=2 textwidth=0 wrapmargin=0 expandtab