#!/bin/bash set -euo pipefail # 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// # ├── config/ # │ └── service.env # └── template/ # ├── template_info.env # ├── install.sh, uninstall.sh, etc. # └── config/ # └── service.env (template defaults) # -- Determine paths -- SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")" export AGENT_PATH="$(dirname "${SCRIPT_PATH}")" export DROPSHELL_DIR="$(dirname "${AGENT_PATH}")" # -- 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 -- if [[ $# -lt 3 ]]; then echo "Usage: ds_run.sh SERVER SERVICE COMMAND [args...]" >&2 exit 1 fi export SERVER="$1" export SERVICE="$2" export DSCOMMAND="$3" shift 3 # Suppress Docker CLI hints export DOCKER_CLI_HINTS=false # -- Set up paths -- export CONFIG_PATH="${DROPSHELL_DIR}/services/${SERVICE}/config" export TEMPLATE_PATH="${DROPSHELL_DIR}/services/${SERVICE}/template" # -- Validate service exists -- [[ -d "${CONFIG_PATH}" ]] || _die "Service '${SERVICE}' does not exist on server (missing ${CONFIG_PATH})" # -- 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}" # -- Load service config (overrides template defaults) -- export SERVICE_ENV="${CONFIG_PATH}/service.env" [[ -f "${SERVICE_ENV}" ]] || _die "Missing service.env at ${SERVICE_ENV}" # -- Source env files with auto-export (set -a exports all assigned variables) -- set -a source "${TEMPLATE_INFO_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])" # -- Execute the command with any remaining arguments -- cd "${TEMPLATE_PATH}" exec bash "${COMMAND_TO_RUN}" "$@"