diff --git a/templates/caddy/_common.sh b/templates/caddy/_common.sh deleted file mode 100644 index 8c9d4e1..0000000 --- a/templates/caddy/_common.sh +++ /dev/null @@ -1,155 +0,0 @@ -#!/bin/bash - -# COMMON FUNCTIONS -# JDE -# 2025-04-25 - -# This file is not required if you write your own template. - - -# Print error message and exit with code 1 -# Usage: die "error message" -die() { - echo -e "\033[91mError: $1\033[0m" - exit 1 -} - -grey_start() { - echo -e -n "\033[90m" -} - -grey_end() { - echo -e -n "\033[0m" -} - -create_and_start_container() { - if [ -z "$1" ] || [ -z "$2" ]; then - die "Template error: create_and_start_container " - fi - - local run_cmd="$1" - local container_name="$2" - - if _is_container_exists $container_name; then - _is_container_running $container_name && return 0 - _start_container $container_name - else - grey_start - $run_cmd - grey_end - fi - - if ! _is_container_running $container_name; then - die "Container ${container_name} failed to start" - fi - - ID=$(_get_container_id $container_name) - echo "Container ${container_name} is running with ID ${ID}" -} - -function create_folder() { - local folder="$1" - if [ -d "$folder" ]; then - return 0 - fi - if ! mkdir -p "$folder"; then - die "Failed to create folder: $folder" - fi - chmod 777 "$folder" - echo "Folder created: $folder" -} - -# Check if docker is installed -_check_docker_installed() { - if ! command -v docker &> /dev/null; then - echo "Docker is not installed" - return 1 - fi - - # check if docker daemon is running - if ! docker info &> /dev/null; then - echo "Docker daemon is not running" - return 1 - fi - - # check if user has permission to run docker - if ! docker run --rm hello-world &> /dev/null; then - echo "User does not have permission to run docker" - return 1 - fi - - return 0 -} - -# Check if a container exists -_is_container_exists() { - if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then - return 1 - fi - return 0 -} - -# Check if a container is running -_is_container_running() { - if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then - return 1 - fi - return 0 -} - -# get contianer ID -_get_container_id() { - docker ps --format "{{.ID}}" --filter "name=$1" -} - -# get container status -_get_container_status() { - docker ps --format "{{.Status}}" --filter "name=$1" -} - -# start container that exists -_start_container() { - _is_container_exists $1 || return 1 - _is_container_running $1 && return 0 - docker start $1 -} - -# stop container that exists -_stop_container() { - _is_container_running $1 || return 0; - docker stop $1 -} - -# remove container that exists -_remove_container() { - _stop_container $1 - _is_container_exists $1 || return 0; - docker rm $1 -} - -# get container logs -_get_container_logs() { - if ! _is_container_exists $1; then - echo "Container $1 does not exist" - return 1 - fi - - docker logs $1 -} - -check_required_env_vars() { - local required_vars=("$@") - for var in "${required_vars[@]}"; do - if [ -z "${!var}" ]; then - die "Required environment variable $var is not set in your service.env file" - fi - done -} - -function _root_remove_tree() { - local to_remove="$1" - parent=$(dirname "$to_remove") - abs_parent=$(realpath "$parent") - child=$(basename "$to_remove") - docker run --rm -v "$abs_parent":/data alpine rm -rf "/data/$child" -} diff --git a/templates/caddy/backup.sh b/templates/caddy/backup.sh index deed39e..2ccdb51 100644 --- a/templates/caddy/backup.sh +++ b/templates/caddy/backup.sh @@ -1,22 +1,13 @@ #!/bin/bash - -# BACKUP SCRIPT -# The backup script is OPTIONAL. -# It is used to backup the service on the server. -# It is called with one argument: the path to the destination backup file. -# If the backup file already exists, the script should exit with a message. - -source "$(dirname "$0")/_common.sh" +source "$AGENT_PATH/_common.sh" check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" -# Stop container before backup _stop_container "$CONTAINER_NAME" if ! autobackup volume=$DATA_VOLUME volume=$CONFIG_VOLUME $1 $2; then die "Failed to create backup" fi -# Start container after backup _start_container "$CONTAINER_NAME" echo "Backup created successfully: $BACKUP_FILE" diff --git a/templates/caddy/install.sh b/templates/caddy/install.sh index 35dc41a..dc625fc 100644 --- a/templates/caddy/install.sh +++ b/templates/caddy/install.sh @@ -1,28 +1,17 @@ #!/bin/bash - -# INSTALL SCRIPT -# The install script is required for all templates. -# It is used to install the service on the server. -# It is called with the path to the server specific env file as an argument. - -source "$(dirname "$0")/_common.sh" - -# Required environment variables +source "$AGENT_PATH/_common.sh" check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "DATA_VOLUME" "CONFIG_VOLUME" if ! autocreate volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then die "Failed to autocreate volumes and paths" fi -# Test Docker _check_docker_installed || die "Docker test failed, aborting installation..." -# check can pull image on remote host and exit if fails docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" [ -f "${CONFIG_PATH}/Caddyfile" ] || die "Caddyfile not found in ${CONFIG_PATH}!" -# remove and restart, as the env may have changed. bash ./stop.sh || die "Failed to stop container ${CONTAINER_NAME}" _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" bash ./start.sh || die "Failed to start container ${CONTAINER_NAME}" diff --git a/templates/caddy/logs.sh b/templates/caddy/logs.sh index 209b57b..f611c87 100644 --- a/templates/caddy/logs.sh +++ b/templates/caddy/logs.sh @@ -1,15 +1,9 @@ #!/bin/bash -# LOGS SCRIPT -# The logs script is OPTIONAL. -# It is used to return the logs of the service. -# It is called with the path to the server specific env file as an argument. - -source "$(dirname "$0")/_common.sh" - -# Required environment variables +source "$AGENT_PATH/_common.sh" check_required_env_vars "CONTAINER_NAME" +# Main script. echo "Container ${CONTAINER_NAME} logs:" grey_start docker logs "${CONTAINER_NAME}" diff --git a/templates/caddy/nuke.sh b/templates/caddy/nuke.sh index 7d5d717..7311923 100644 --- a/templates/caddy/nuke.sh +++ b/templates/caddy/nuke.sh @@ -1,11 +1,11 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # NUKE SCRIPT # This is run after the uninstall.sh script to delete all data. # dropshell handles the configuration files, so we just need to remove # any docker volumes and any custom local data folders. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" if ! autonuke volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then diff --git a/templates/caddy/ports.sh b/templates/caddy/ports.sh index 1611654..89a03f4 100644 --- a/templates/caddy/ports.sh +++ b/templates/caddy/ports.sh @@ -1,5 +1,6 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" + -source "$(dirname "$0")/_common.sh" echo 80 echo 443 diff --git a/templates/caddy/restore.sh b/templates/caddy/restore.sh index e58bfb0..daa1c58 100644 --- a/templates/caddy/restore.sh +++ b/templates/caddy/restore.sh @@ -1,11 +1,8 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # RESTORE SCRIPT -# The restore script is OPTIONAL. -# It is used to restore the service on the server from a backup file. -# It is called with one argument: the path to the backup file. -source "$(dirname "$0")/_common.sh" check_required_env_vars "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" # uninstall container before restore diff --git a/templates/caddy/start.sh b/templates/caddy/start.sh index 510abff..52f2574 100644 --- a/templates/caddy/start.sh +++ b/templates/caddy/start.sh @@ -1,11 +1,10 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # START SCRIPT # The start script is required for all templates. # It is used to start the service on the server. -# It is called with the path to the server specific env file as an argument. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" diff --git a/templates/caddy/status.sh b/templates/caddy/status.sh index afa2d57..aac9e87 100644 --- a/templates/caddy/status.sh +++ b/templates/caddy/status.sh @@ -1,13 +1,11 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # STATUS SCRIPT # The status script is OPTIONAL. # It is used to return the status of the service (0 is healthy, 1 is unhealthy). -# It is called with the path to the server specific env file as an argument. - # This is an example of a status script that checks if the service is running. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" # check if the service is running diff --git a/templates/caddy/stop.sh b/templates/caddy/stop.sh index e1cc8e9..4d53ff4 100644 --- a/templates/caddy/stop.sh +++ b/templates/caddy/stop.sh @@ -1,11 +1,10 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # STOP SCRIPT # The stop script is required for all templates. # It is used to stop the service on the server. -# It is called with the path to the server specific env file as an argument. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" diff --git a/templates/caddy/uninstall.sh b/templates/caddy/uninstall.sh index 094787c..a72764a 100644 --- a/templates/caddy/uninstall.sh +++ b/templates/caddy/uninstall.sh @@ -1,11 +1,10 @@ #!/bin/bash +source "$AGENT_PATH/_common.sh" # UNINSTALL SCRIPT # The uninstall script is required for all templates. # It is used to uninstall the service from the server. -# It is called with the path to the server specific env file as an argument. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" diff --git a/templates/simple-object-storage/backup.sh b/templates/simple-object-storage/backup.sh index dec0092..af40cfd 100644 --- a/templates/simple-object-storage/backup.sh +++ b/templates/simple-object-storage/backup.sh @@ -3,8 +3,6 @@ # BACKUP SCRIPT # The backup script is OPTIONAL. # It is used to backup the service on the server. -# It is called with one argument: the path to the destination backup file. -# If the backup file already exists, the script should exit with a message. source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" diff --git a/templates/simple-object-storage/install.sh b/templates/simple-object-storage/install.sh index 760bc04..7912414 100644 --- a/templates/simple-object-storage/install.sh +++ b/templates/simple-object-storage/install.sh @@ -3,7 +3,6 @@ # INSTALL SCRIPT # The install script is required for all templates. # It is used to install the service on the server. -# It is called with the path to the server specific env file as an argument. source "$(dirname "$0")/_common.sh"