Tidy
This commit is contained in:
@@ -1,64 +1,81 @@
|
|||||||
#!/bin/bash
|
#!/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"
|
||||||
|
|
||||||
# //------------------------------------------------------------------------------------------------
|
# -- Validate arguments --
|
||||||
# // remote paths
|
if [[ $# -lt 3 ]]; then
|
||||||
# // DROPSHELL_DIR
|
echo "Usage: ds_run.sh SERVER SERVICE COMMAND [args...]" >&2
|
||||||
# // |-- server.json
|
exit 1
|
||||||
# // |-- backups
|
fi
|
||||||
# // |-- 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)
|
|
||||||
|
|
||||||
# read SERVER, SERVICE from the command line args
|
export SERVER="$1"
|
||||||
export SERVER=TODO
|
export SERVICE="$2"
|
||||||
export SERVICE=TODO
|
export DSCOMMAND="$3"
|
||||||
export DSCOMMAND=TODO
|
shift 3
|
||||||
|
|
||||||
|
# Suppress Docker CLI hints
|
||||||
export DOCKER_CLI_HINTS=false
|
export DOCKER_CLI_HINTS=false
|
||||||
|
|
||||||
# we are in the agent directory.
|
# -- Set up paths --
|
||||||
export AGENT_PATH="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)"
|
|
||||||
export DROPSHELL_DIR=TODO
|
|
||||||
export CONFIG_PATH="${DROPSHELL_DIR}/services/${SERVICE}/config"
|
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"
|
# -- Load service config (overrides template defaults) --
|
||||||
[ -f ${TEMPLATE_INFO_ENV} ] || _die "Couldn't find template_info.env at ${TEMPLATE_INFO_ENV}"
|
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}"
|
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}"
|
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
|
# -- Execute the command with any remaining arguments --
|
||||||
|
cd "${TEMPLATE_PATH}"
|
||||||
export COMMAND_TO_RUN="${DROPSHELL_DIR}/services/${SERVICE}/template/${DSCOMMAND}"
|
exec bash "${COMMAND_TO_RUN}" "$@"
|
||||||
[ -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}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user