.
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 18s

This commit is contained in:
Your Name
2025-05-10 12:44:37 +12:00
parent b5bc7b611d
commit 9e9d80570c
3 changed files with 46 additions and 22 deletions

View File

@ -0,0 +1,113 @@
#!/bin/bash
# This script checks ALL services on the server and returns a status for each.
# Return format is simple ENV with the following format:
# SERVICE_NAME_HEALTH=healthy|unhealthy|unknown
# SERVICE_NAME_PORTS=port1,port2,port3
# Get all services on the server
SCRIPT_DIR="$(dirname "$0")"
# // DROPSHELL_DIR
# // |-- backups
# // |-- services
# // |-- service name
# // |-- config <-- this is passed as argument to all scripts
# // |-- service.env
# // |-- template
# // |-- (script files)
# // |-- config
# // |-- service.env
# // |-- (other config files for specific server&service)
CURRENT_OUTPUT=""
CURRENT_EXIT_CODE=0
load_dotenv(){
local file_path=$1
if [ -f "${file_path}" ]; then
source "${file_path}"
fi
}
function run_command() {
local service_path=$1
local command=$2
local capture_output=${3:-false} # default to false if not specified
# check if the command is a file
if [ ! -f "${service_path}/template/${command}.sh" ]; then
return;
fi
# run the command in a subshell to prevent environment changes
CURRENT_OUTPUT=$(
set -a
load_dotenv "${service_path}/template/_default.env"
load_dotenv "${service_path}/config/service.env"
set +a
# update the main variables.
export CONFIG_PATH="${service_path}/config"
# SERVER is correct
export SERVICE="${SERVICE_NAME}"
if [ "$capture_output" = "true" ]; then
# Capture and return output
bash "${service_path}/template/${command}.sh" 2>&1
else
# Run silently and return exit code
bash "${service_path}/template/${command}.sh" > /dev/null 2>&1
fi
)
CURRENT_EXIT_CODE=$?
}
function command_exists() {
local service_path=$1
local command=$2
if [ ! -f "${service_path}/template/${command}.sh" ]; then
return 1
fi
return 0
}
# Get all services on the server
SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../../")
# Get all service names
SERVICE_NAMES=$(ls "${SERVICES_PATH}")
# Iterate over all service names
for SERVICE_NAME in ${SERVICE_NAMES}; do
SERVICE_PATH=$(realpath "${SERVICES_PATH}/${SERVICE_NAME}")
#--------------------------------
# Get the service health
if ! command_exists "${SERVICE_PATH}" "status"; then
SERVICE_HEALTH="unknown"
else
run_command "${SERVICE_PATH}" "status" "false"
if [ "${CURRENT_EXIT_CODE}" -eq 0 ]; then
SERVICE_HEALTH="healthy"
else
SERVICE_HEALTH="unhealthy"
fi
fi
#--------------------------------
# Get the service ports
if ! command_exists "${SERVICE_PATH}" "ports"; then
SERVICE_PORTS=""
else
run_command "${SERVICE_PATH}" "ports" "true"
SERVICE_PORTS="${CURRENT_OUTPUT}"
fi
#--------------------------------
# return the health and ports
echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}"
echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}"
done

View File

@ -1,13 +1,9 @@
#!/bin/bash
# This script contains the common code for the autocommands.
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
MYID=$(id -u)
MYGRP=$(id -g)
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
_autocommandrun_volume() {
local command="$1"
local volume_name="$2"
@ -112,7 +108,8 @@ _autocommandparse() {
local command="$1"
shift
echo "autocommandparse: command=$command"
local backup_temp_path="$1"
shift
# Extract the backup file and temp path (last two arguments)
local args=("$@")
@ -132,7 +129,7 @@ _autocommandparse() {
# create backup folder unique to key/value.
local bfolder=$(echo "${key}_${value}" | tr -cd '[:alnum:]_-')
local targetpath="${BACKUP_TEMP_PATH}/${bfolder}"
local targetpath="${backup_temp_path}/${bfolder}"
mkdir -p ${targetpath}
# Key must be one of volume, path or file
@ -155,31 +152,57 @@ _autocommandparse() {
autocreate() {
_autocommandparse create "$@"
_check_required_env_vars "TEMP_DIR"
local create_temp_path="$TEMP_DIR/create"
mkdir -p "$create_temp_path"
_autocommandparse create "$create_temp_path" "$@"
rm -rf "$create_temp_path"
}
autonuke() {
_autocommandparse nuke "$@"
_check_required_env_vars "TEMP_DIR"
local nuke_temp_path="$TEMP_DIR/nuke"
mkdir -p "$nuke_temp_path"
_autocommandparse nuke "$nuke_temp_path" "$@"
rm -rf "$nuke_temp_path"
}
autobackup() {
mkdir -p "$BACKUP_TEMP_PATH"
echo "_autocommandparse [backup] [$@]"
_autocommandparse backup "$@"
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
local backup_temp_path="$TEMP_DIR/backup"
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
mkdir -p "$backup_temp_path"
echo "_autocommandparse [backup] [$backup_temp_path] [$@]"
# backup the files into the temp path.
_autocommandparse backup "$backup_temp_path" "$@"
# create the backup file.
tar zcvf "$BACKUP_FILE" -C "$backup_temp_path" .
rm -rf "$backup_temp_path"
}
autorestore() {
echo "_autocommandparse [restore] [$@]"
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
local backup_temp_path="$TEMP_DIR/restore"
mkdir -p "$BACKUP_TEMP_PATH"
tar zxvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" --strip-components=1
echo "_autocommandparse [restore] [$backup_temp_path] [$@]"
_autocommandparse restore "$@"
mkdir -p "$backup_temp_path"
# extract the backup file into the temp path.
tar zxvf "$BACKUP_FILE" -C "$backup_temp_path" --strip-components=1
# restore the files from the temp path.
_autocommandparse restore "$backup_temp_path" "$@"
rm -rf "$backup_temp_path"
}