This commit is contained in:
j
2025-12-29 23:41:53 +13:00
parent 7a406168e7
commit a9ccdedf89

View File

@@ -1,64 +1,81 @@
#!/bin/bash
set -euo pipefail
# Dropshell Run
# Dropshell Run - Execute a service command on the remote server
#
# Usage: ds_run.sh SERVER SERVICE COMMAND [args...]
#
# Remote directory structure:
# DROPSHELL_DIR/
# ├── agent/
# │ ├── ds_run.sh (this script)
# │ └── common.sh
# └── services/<service>/
# ├── config/
# │ └── service.env
# └── template/
# ├── template_info.env
# ├── install.sh, uninstall.sh, etc.
# └── config/
# └── service.env (template defaults)
# Usage:
# -- Determine paths --
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
export AGENT_PATH="$(dirname "${SCRIPT_PATH}")"
export DROPSHELL_DIR="$(dirname "${AGENT_PATH}")"
# ds_run.sh SERVER SERVICE COMMAND [param1] [param2] ...
# -- Source common functions first (needed for _die) --
if [[ ! -f "${AGENT_PATH}/common.sh" ]]; then
echo "Error: common.sh not found at ${AGENT_PATH}/common.sh" >&2
exit 1
fi
source "${AGENT_PATH}/common.sh"
# //------------------------------------------------------------------------------------------------
# // remote paths
# // DROPSHELL_DIR
# // |-- server.json
# // |-- backups
# // |-- temp_files
# // |-- agent
# // | |-- bb64
# // | |-- (other agent files)
# // |-- services
# // |-- service name
# // |-- config
# // |-- service.env (actual service config)
# // |-- template
# // |-- (script files)
# // |-- template_info.env
# // |-- config
# // |-- service.env (default service config)
# // |-- (other template/example config files)
# -- Validate arguments --
if [[ $# -lt 3 ]]; then
echo "Usage: ds_run.sh SERVER SERVICE COMMAND [args...]" >&2
exit 1
fi
# read SERVER, SERVICE from the command line args
export SERVER=TODO
export SERVICE=TODO
export DSCOMMAND=TODO
export SERVER="$1"
export SERVICE="$2"
export DSCOMMAND="$3"
shift 3
# Suppress Docker CLI hints
export DOCKER_CLI_HINTS=false
# we are in the agent directory.
export AGENT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
export DROPSHELL_DIR=TODO
# -- Set up paths --
export CONFIG_PATH="${DROPSHELL_DIR}/services/${SERVICE}/config"
export TEMPLATE_PATH="${DROPSHELL_DIR}/services/${SERVICE}/template"
source "${AGENT_PATH}/commmon.sh"
# -- Validate service exists --
[[ -d "${CONFIG_PATH}" ]] || _die "Service '${SERVICE}' does not exist on server (missing ${CONFIG_PATH})"
[ -d ${CONFIG_PATH} ] || _die "Service ${SERVICE} does not exist on the server."
# -- Load template info (template defaults, loaded first) --
export TEMPLATE_INFO_ENV="${TEMPLATE_PATH}/template_info.env"
if [[ ! -f "${TEMPLATE_INFO_ENV}" ]]; then
TEMPLATE_INFO_ENV="${TEMPLATE_PATH}/config/.template_info.env"
fi
[[ -f "${TEMPLATE_INFO_ENV}" ]] || _die "Missing template_info.env at ${TEMPLATE_INFO_ENV}"
export TEMPLATE_INFO_ENV="${DROPSHELL_DIR}/services/${SERVICE}/template/template_info.env"
[ -f ${TEMPLATE_INFO_ENV} ] || _die "Couldn't find template_info.env at ${TEMPLATE_INFO_ENV}"
# -- Load service config (overrides template defaults) --
export SERVICE_ENV="${CONFIG_PATH}/service.env"
[[ -f "${SERVICE_ENV}" ]] || _die "Missing service.env at ${SERVICE_ENV}"
#read in the TEMPLATE_INFO_ENV, exporting all variables
# -- Source env files with auto-export (set -a exports all assigned variables) --
set -a
source "${TEMPLATE_INFO_ENV}"
export SERVICE_ENV="${DROPSHELL_DIR}/services/${SERVICE}/config/service.env"
[ -f ${SERVICE_ENV} ] || _die "Couldn't find service.env at ${SERVICE_ENV}"
# read in the SERVICE_ENV, exporting all varialbes
source "${SERVICE_ENV}"
set +a
# -- Locate and validate command script --
COMMAND_TO_RUN="${TEMPLATE_PATH}/${DSCOMMAND}"
if [[ ! -f "${COMMAND_TO_RUN}" ]]; then
COMMAND_TO_RUN="${COMMAND_TO_RUN}.sh"
fi
[[ -f "${COMMAND_TO_RUN}" ]] || _die "Command not found: ${DSCOMMAND} (looked for ${TEMPLATE_PATH}/${DSCOMMAND}[.sh])"
## Run the command
export COMMAND_TO_RUN="${DROPSHELL_DIR}/services/${SERVICE}/template/${DSCOMMAND}"
[ -f "${COMMAND_TO_RUN}" ] || COMMAND_TO_RUN="${COMMAND_TO_RUN}.sh"
[ -f "${COMMAND_TO_RUN}" ] || _die "Couldn't find the command to run: ${COMMAND_TO_RUN}"
# ensure all variables are in the environment of the command being run!!
${COMMAND_TO_RUN}
# -- Execute the command with any remaining arguments --
cd "${TEMPLATE_PATH}"
exec bash "${COMMAND_TO_RUN}" "$@"