diff --git a/templates/caddy/backup.sh b/templates/caddy/backup.sh index 2ccdb51..18ef3cb 100644 --- a/templates/caddy/backup.sh +++ b/templates/caddy/backup.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" _stop_container "$CONTAINER_NAME" diff --git a/templates/caddy/install.sh b/templates/caddy/install.sh index dc625fc..89d9d2f 100644 --- a/templates/caddy/install.sh +++ b/templates/caddy/install.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +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 diff --git a/templates/caddy/logs.sh b/templates/caddy/logs.sh index f611c87..61d4451 100644 --- a/templates/caddy/logs.sh +++ b/templates/caddy/logs.sh @@ -1,6 +1,5 @@ #!/bin/bash - -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars "CONTAINER_NAME" # Main script. diff --git a/templates/caddy/nuke.sh b/templates/caddy/nuke.sh index 7311923..b076810 100644 --- a/templates/caddy/nuke.sh +++ b/templates/caddy/nuke.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # NUKE SCRIPT # This is run after the uninstall.sh script to delete all data. diff --git a/templates/caddy/ports.sh b/templates/caddy/ports.sh index 89a03f4..aa08d2d 100644 --- a/templates/caddy/ports.sh +++ b/templates/caddy/ports.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" echo 80 diff --git a/templates/caddy/restore.sh b/templates/caddy/restore.sh index daa1c58..be65798 100644 --- a/templates/caddy/restore.sh +++ b/templates/caddy/restore.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # RESTORE SCRIPT diff --git a/templates/caddy/start.sh b/templates/caddy/start.sh index 52f2574..751dd22 100644 --- a/templates/caddy/start.sh +++ b/templates/caddy/start.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # START SCRIPT # The start script is required for all templates. diff --git a/templates/caddy/status.sh b/templates/caddy/status.sh index aac9e87..41c0473 100644 --- a/templates/caddy/status.sh +++ b/templates/caddy/status.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # STATUS SCRIPT # The status script is OPTIONAL. diff --git a/templates/caddy/stop.sh b/templates/caddy/stop.sh index 4d53ff4..86d802f 100644 --- a/templates/caddy/stop.sh +++ b/templates/caddy/stop.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # STOP SCRIPT # The stop script is required for all templates. diff --git a/templates/caddy/uninstall.sh b/templates/caddy/uninstall.sh index a72764a..dab2594 100644 --- a/templates/caddy/uninstall.sh +++ b/templates/caddy/uninstall.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$AGENT_PATH/_common.sh" +source "${AGENT_PATH}/_common.sh" # UNINSTALL SCRIPT # The uninstall script is required for all templates. diff --git a/templates/dropshell-agent/install.sh b/templates/dropshell-agent/install.sh index e9ec3c3..c6be2d3 100644 --- a/templates/dropshell-agent/install.sh +++ b/templates/dropshell-agent/install.sh @@ -6,12 +6,7 @@ # It is called with the path to the server specific env file as an argument. -install_prerequisites() { - # this script works on debian, ubuntu and Raspberry Pi OS - # it checks the following prerequisites, and installs them if missing. - # if the user is root it proceeds, otherwise it checks for sudo privileges. - # if neither exists, and a prerequisite is missing, the script exits with failure. - +check_prerequisites() { # prerequisites: # - bash # - curl @@ -29,6 +24,6 @@ install_prerequisites() { done } -install_prerequisites +check_prerequisites exit 0 diff --git a/templates/dropshell-agent/nuke.sh b/templates/dropshell-agent/nuke.sh index 4f6e6ca..a9bf588 100644 --- a/templates/dropshell-agent/nuke.sh +++ b/templates/dropshell-agent/nuke.sh @@ -1,6 +1 @@ #!/bin/bash - -# NUKE SCRIPT -# run after uninstall.sh to delete all data. - - diff --git a/templates/dropshell-agent/shared/_common.sh b/templates/dropshell-agent/shared/_common.sh index 5bd773b..4f9b994 100644 --- a/templates/dropshell-agent/shared/_common.sh +++ b/templates/dropshell-agent/shared/_common.sh @@ -7,11 +7,11 @@ # ---------------------------------------------------------------------------------------------------------- # summary of functions: -# die "message" : Prints an error message in red and exits with status code 1. -# grey_start : Switches terminal output color to grey. -# grey_end : Resets terminal output color from grey. -# create_and_start_container "" : Creates/starts a container, verifying it runs. -# create_folder : Creates a directory if it doesn't exist (chmod 777). +# _die "message" : Prints an error message in red and exits with status code 1. +# _grey_start : Switches terminal output color to grey. +# _grey_end : Resets terminal output color from grey. +# _create_and_start_container "" : Creates/starts a container, verifying it runs. +# _create_folder : Creates a directory if it doesn't exist (chmod 777). # _check_docker_installed : Checks if Docker is installed, running, and user has permission. Returns 1 on failure. # _is_container_exists : Checks if a container (any state) exists. Returns 1 if not found. # _is_container_running : Checks if a container is currently running. Returns 1 if not running. @@ -21,29 +21,31 @@ # _stop_container : Stops a running container. # _remove_container : Stops (if needed) and removes a container. # _get_container_logs : Prints the logs for a container. -# check_required_env_vars "VAR1" ... : Checks if listed environment variables are set; calls die() if any are missing. +# _check_required_env_vars "VAR1" ... : Checks if listed environment variables are set; calls _die() if any are missing. # _root_remove_tree : Removes a path using a root Docker container (for permissions). # ---------------------------------------------------------------------------------------------------------- -# Print error message and exit with code 1 -# Usage: die "error message" -die() { +# Prints an error message in red and exits with status code 1. +_die() { echo -e "\033[91mError: $1\033[0m" exit 1 } -grey_start() { +# Switches terminal output color to grey. +_grey_start() { echo -e -n "\033[90m" } -grey_end() { +# Resets terminal output color from grey. +_grey_end() { echo -e -n "\033[0m" } -create_and_start_container() { +# Creates/starts a container, verifying it runs. +_create_and_start_container() { if [ -z "$1" ] || [ -z "$2" ]; then - die "Template error: create_and_start_container " + _die "Template error: create_and_start_container " fi local run_cmd="$1" @@ -53,32 +55,33 @@ create_and_start_container() { _is_container_running $container_name && return 0 _start_container $container_name else - grey_start + _grey_start $run_cmd - grey_end + _grey_end fi if ! _is_container_running $container_name; then - die "Container ${container_name} failed to start" + _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() { +# Creates a directory if it doesn't exist (chmod 777). +_create_folder() { local folder="$1" if [ -d "$folder" ]; then return 0 fi if ! mkdir -p "$folder"; then - die "Failed to create folder: $folder" + _die "Failed to create folder: $folder" fi chmod 777 "$folder" echo "Folder created: $folder" } -# Check if docker is installed +# Checks if Docker is installed, running, and user has permission. Returns 1 on failure. _check_docker_installed() { if ! command -v docker &> /dev/null; then echo "Docker is not installed" @@ -100,7 +103,7 @@ _check_docker_installed() { return 0 } -# Check if a container exists +# Checks if a container (any state) exists. Returns 1 if not found. _is_container_exists() { if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then return 1 @@ -108,7 +111,7 @@ _is_container_exists() { return 0 } -# Check if a container is running +# Checks if a container is currently running. Returns 1 if not running. _is_container_running() { if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then return 1 @@ -116,37 +119,37 @@ _is_container_running() { return 0 } -# get contianer ID +# Prints the ID of the named container. _get_container_id() { docker ps --format "{{.ID}}" --filter "name=$1" } -# get container status +# Prints the status string of the named container. _get_container_status() { docker ps --format "{{.Status}}" --filter "name=$1" } -# start container that exists +# Starts an existing, stopped container. _start_container() { _is_container_exists $1 || return 1 _is_container_running $1 && return 0 docker start $1 } -# stop container that exists +# Stops a running container. _stop_container() { _is_container_running $1 || return 0; docker stop $1 } -# remove container that exists +# Stops (if needed) and removes a container. _remove_container() { _stop_container $1 _is_container_exists $1 || return 0; docker rm $1 } -# get container logs +# Prints the logs for a container. _get_container_logs() { if ! _is_container_exists $1; then echo "Container $1 does not exist" @@ -156,16 +159,18 @@ _get_container_logs() { docker logs $1 } -check_required_env_vars() { +# Checks if listed environment variables are set; calls _die() if any are missing. +_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" + _die "Required environment variable $var is not set in your service.env file" fi done } -function _root_remove_tree() { +# Removes a path using a root Docker container (for permissions). +_root_remove_tree() { local to_remove="$1" parent=$(dirname "$to_remove") abs_parent=$(realpath "$parent") diff --git a/templates/dropshell-agent/uninstall.sh b/templates/dropshell-agent/uninstall.sh index 822d9a2..a9bf588 100644 --- a/templates/dropshell-agent/uninstall.sh +++ b/templates/dropshell-agent/uninstall.sh @@ -1,7 +1 @@ #!/bin/bash - -# 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. - diff --git a/templates/example-nginx/backup.sh b/templates/example-nginx/backup.sh index 3833655..337366b 100644 --- a/templates/example-nginx/backup.sh +++ b/templates/example-nginx/backup.sh @@ -1,36 +1,42 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# 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. +# Nginx Example Backup Script -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" -# Get backup file path from first argument +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" + BACKUP_FILE="$1" + if [ -z "$BACKUP_FILE" ]; then - die "Backup file path not provided" + _die "Backup file path not provided" fi -# Check if backup file already exists -if [ -f "$BACKUP_FILE" ]; then - die "Backup file $BACKUP_FILE already exists" +if [ -e "$BACKUP_FILE" ]; then + _die "Backup file $BACKUP_FILE already exists" fi -# Stop container before backup -_stop_container "$CONTAINER_NAME" +echo "Backing up data for ${CONTAINER_NAME} from ${LOCAL_DATA_FOLDER} to ${BACKUP_FILE}..." -Create backup of data folder -echo "Creating backup of $LOCAL_DATA_FOLDER..." -if ! tar zcvf "$BACKUP_FILE" -C "$LOCAL_DATA_FOLDER" .; then - _start_container "$CONTAINER_NAME" - die "Failed to create backup" +# Stop container before backup? +# echo "Stopping container ${CONTAINER_NAME} for backup..." +# _stop_container "$CONTAINER_NAME" + +# Create backup of data folder +tar -czf "$BACKUP_FILE" -C "$LOCAL_DATA_FOLDER" . + +# Check if backup command was successful +if [ $? -ne 0 ]; then + # Start container again if it was stopped + # echo "Restarting container ${CONTAINER_NAME}..." + # _start_container "$CONTAINER_NAME" + _die "Failed to create backup" fi -# Start container after backup -_start_container "$CONTAINER_NAME" +# Start container again if it was stopped +# echo "Restarting container ${CONTAINER_NAME}..." +# _start_container "$CONTAINER_NAME" -echo "Backup created successfully: $BACKUP_FILE" +echo "Backup complete: ${BACKUP_FILE}" diff --git a/templates/example-nginx/install.sh b/templates/example-nginx/install.sh old mode 100755 new mode 100644 index 45b8072..de551c8 --- a/templates/example-nginx/install.sh +++ b/templates/example-nginx/install.sh @@ -1,41 +1,33 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# 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. +# Nginx Example Install Script -source "$(dirname "$0")/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER" -# Create local data folder if it doesn't exist -if [ -d "${LOCAL_DATA_FOLDER}" ]; then - echo "Local data folder ${LOCAL_DATA_FOLDER} exists, using existing data." -else - echo "Local data folder ${LOCAL_DATA_FOLDER} does not exist, creating..." +# Ensure local data folder exists +if [ ! -d "${LOCAL_DATA_FOLDER}" ]; then + echo "Creating local data folder ${LOCAL_DATA_FOLDER}..." mkdir -p "${LOCAL_DATA_FOLDER}" - cat < "${LOCAL_DATA_FOLDER}/index.html" - - -

Hello, World!

- - -EOF + chmod 777 "${LOCAL_DATA_FOLDER}" + # Optionally, copy default content if needed + # cp -r ./example/* "${LOCAL_DATA_FOLDER}/" fi -# Test Docker -_check_docker_installed || die "Docker test failed, aborting installation..." +echo "Checking Docker installation..." +_check_docker_installed || _die "Docker test failed, aborting installation..." +echo "Pulling image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}..." +docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || _die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" -# 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" +echo "Stopping and removing any existing container..." +bash ./stop.sh || _die "Failed to stop container ${CONTAINER_NAME}" +_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}" -# 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}" +echo "Starting container..." +bash ./start.sh || _die "Failed to start container ${CONTAINER_NAME}" -echo "Installation of ${CONTAINER_NAME} complete" -echo "You can access the service at http://${SERVER}:${HOST_PORT}" +echo "Installation complete for service ${CONTAINER_NAME}." diff --git a/templates/example-nginx/logs.sh b/templates/example-nginx/logs.sh index 209b57b..514693f 100644 --- a/templates/example-nginx/logs.sh +++ b/templates/example-nginx/logs.sh @@ -1,16 +1,14 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# 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. +# Nginx Example Logs Script -source "$(dirname "$0")/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" -echo "Container ${CONTAINER_NAME} logs:" -grey_start -docker logs "${CONTAINER_NAME}" -grey_end +echo "Showing logs for ${CONTAINER_NAME}... (Press Ctrl+C to stop)" +_grey_start +_get_container_logs $CONTAINER_NAME +_grey_end diff --git a/templates/example-nginx/nuke.sh b/templates/example-nginx/nuke.sh index cc5512d..83d0dab 100644 --- a/templates/example-nginx/nuke.sh +++ b/templates/example-nginx/nuke.sh @@ -1,14 +1,18 @@ #!/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. +# Nginx Example Nuke Script +# Removes container and local data folder. -source "$(dirname "$0")/_common.sh" -check_required_env_vars "LOCAL_DATA_FOLDER" "CONTAINER_NAME" -# remove the local data folder -rm -rf $LOCAL_DATA_FOLDER || die "Failed to remove local data folder ${LOCAL_DATA_FOLDER}" +# Load service environment variables +source ./service.env +_check_required_env_vars "LOCAL_DATA_FOLDER" "CONTAINER_NAME" -echo "Nuking of ${CONTAINER_NAME} complete." +# Call uninstall script first +./uninstall.sh + +echo "Removing local data folder ${LOCAL_DATA_FOLDER}..." +rm -rf $LOCAL_DATA_FOLDER || _die "Failed to remove local data folder ${LOCAL_DATA_FOLDER}" + +echo "Nuke complete for service ${CONTAINER_NAME}." diff --git a/templates/example-nginx/ports.sh b/templates/example-nginx/ports.sh index e88f668..c7e0fd6 100644 --- a/templates/example-nginx/ports.sh +++ b/templates/example-nginx/ports.sh @@ -1,13 +1,18 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# PORT SCRIPT -# The port script is OPTIONAL. -# It is used to return the ports used by the service. -# It is called with the path to the server specific env file as an argument. +# Nginx Example Ports Script -source "$(dirname "$0")/_common.sh" -# Required environment variables -# check_required_env_vars "HOST_PORT" +# Load service environment variables +source ./service.env -echo $HOST_PORT +# This template uses HOST_PORT directly if set +# check_required_env_vars "HOST_PORT" + +if [ -n "$HOST_PORT" ]; then + echo $HOST_PORT +else + # Default or logic to determine port if not in env + echo 80 # Default Nginx port +fi diff --git a/templates/example-nginx/restore.sh b/templates/example-nginx/restore.sh index 335938a..6acd49c 100644 --- a/templates/example-nginx/restore.sh +++ b/templates/example-nginx/restore.sh @@ -1,41 +1,42 @@ #!/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. +# Nginx Example Restore Script -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" -# Get backup file path from first argument +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" + BACKUP_FILE="$1" + if [ -z "$BACKUP_FILE" ]; then - die "Backup file path not provided" + _die "Backup file path not provided" fi -# Check if backup file already exists if [ ! -f "$BACKUP_FILE" ]; then - die "Backup file $BACKUP_FILE does not exist" + _die "Backup file $BACKUP_FILE does not exist" fi -# # Stop container before backup -bash ./uninstall.sh || die "Failed to uninstall service before restore" +echo "Uninstalling service before restore..." +bash ./uninstall.sh || _die "Failed to uninstall service before restore" -# Remove existing data folder -echo "Deleting ALL data in $LOCAL_DATA_FOLDER." +echo "Removing existing data folder ${LOCAL_DATA_FOLDER}..." +# Use root remove in case of permission issues _root_remove_tree "$LOCAL_DATA_FOLDER" -[ ! -d "$LOCAL_DATA_FOLDER" ] || die "Failed to delete $LOCAL_DATA_FOLDER" +[ ! -d "$LOCAL_DATA_FOLDER" ] || _die "Failed to delete $LOCAL_DATA_FOLDER" mkdir -p "$LOCAL_DATA_FOLDER" -[ -d "$LOCAL_DATA_FOLDER" ] || die "Failed to create $LOCAL_DATA_FOLDER" +[ -d "$LOCAL_DATA_FOLDER" ] || _die "Failed to create $LOCAL_DATA_FOLDER" +chmod 777 "$LOCAL_DATA_FOLDER" # Ensure permissions -# Restore data folder from backup -# --strip-components=1 removes the parent folder in the tgz from the restore paths. -if ! tar xzvf "$BACKUP_FILE" -C "$LOCAL_DATA_FOLDER" --strip-components=1; then - die "Failed to restore data folder from backup" +echo "Restoring data from ${BACKUP_FILE} to ${LOCAL_DATA_FOLDER}..." +# Assuming backup is a simple tarball of the folder contents +tar -xzf "$BACKUP_FILE" -C "$LOCAL_DATA_FOLDER" --strip-components=1 +if [ $? -ne 0 ]; then + _die "Failed to restore data folder from backup" fi -# reinstall service -bash ./install.sh || die "Failed to reinstall service after restore" +echo "Restore complete. Reinstalling service..." +bash ./install.sh || _die "Failed to reinstall service after restore" -echo "Restore complete! Service is running again on port $HOST_PORT with restored website." +echo "Service ${CONTAINER_NAME} restored and reinstalled." diff --git a/templates/example-nginx/start.sh b/templates/example-nginx/start.sh old mode 100755 new mode 100644 index f160e53..3935e3a --- a/templates/example-nginx/start.sh +++ b/templates/example-nginx/start.sh @@ -1,11 +1,11 @@ #!/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" "HOST_PORT" "LOCAL_DATA_FOLDER" [ -d "${LOCAL_DATA_FOLDER}" ] || die "Local data folder ${LOCAL_DATA_FOLDER} does not exist." diff --git a/templates/example-nginx/status.sh b/templates/example-nginx/status.sh index 990d9a7..5424d79 100644 --- a/templates/example-nginx/status.sh +++ b/templates/example-nginx/status.sh @@ -1,4 +1,5 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" # STATUS SCRIPT # The status script is OPTIONAL. @@ -7,7 +8,6 @@ # 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/example-nginx/stop.sh b/templates/example-nginx/stop.sh old mode 100755 new mode 100644 index e1cc8e9..43c8f2e --- a/templates/example-nginx/stop.sh +++ b/templates/example-nginx/stop.sh @@ -1,11 +1,11 @@ #!/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/example-nginx/uninstall.sh b/templates/example-nginx/uninstall.sh index 3f16c3a..8cc813d 100644 --- a/templates/example-nginx/uninstall.sh +++ b/templates/example-nginx/uninstall.sh @@ -1,19 +1,21 @@ #!/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. +# Nginx Example Uninstall Script -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER" -_remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" -_is_container_running && die "Couldn't stop existing container" -_is_container_exists && die "Couldn't remove existing container" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER" -# remove the image -docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Failed to remove image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" +echo "Uninstalling service ${CONTAINER_NAME}..." +_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}" +if _is_container_running $CONTAINER_NAME; then _die "Couldn't stop existing container"; fi +if _is_container_exists $CONTAINER_NAME; then _die "Couldn't remove existing container"; fi -echo "Uninstallation of ${CONTAINER_NAME} complete." -echo "Local data folder ${LOCAL_DATA_FOLDER} still in place." +# Optional: Remove image? +# echo "Removing image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}..." +# docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Warning: Failed to remove image (might be in use)" + +echo "Service ${CONTAINER_NAME} uninstalled." +# Note: This does NOT remove the local data folder. Use nuke.sh for that. diff --git a/templates/simple-object-storage/backup.sh b/templates/simple-object-storage/backup.sh index af40cfd..2c59fdc 100644 --- a/templates/simple-object-storage/backup.sh +++ b/templates/simple-object-storage/backup.sh @@ -1,40 +1,13 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# BACKUP SCRIPT -# The backup script is OPTIONAL. -# It is used to backup the service on the server. +# Simple Object Storage Backup Script +# Creates a backup tarball of the volume contents. -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" +_check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" # HOT backup is fine for simple-object-storage +autobackup "volume=${VOLUME_NAME}" $1 $2 || _die "Failed to create backup" - -# Get backup file path from first argument -BACKUP_FILE="$1" -if [ -z "$BACKUP_FILE" ]; then - die "Backup file path not provided" -fi - -TEMP_DIR=$2 -if [ -z "$TEMP_DIR" ]; then - die "Temporary directory not provided" -fi - -# Check if backup file already exists -if [ -f "$BACKUP_FILE" ]; then - die "Backup file $BACKUP_FILE already exists" -fi - -# Create backup of data folder -echo "Creating backup of $VOLUME_NAME..." - -docker run --rm -v ${VOLUME_NAME}:/data -v ${TEMP_DIR}:/tempdir alpine sh -c "\ -tar zcvf /tempdir/backup.tar.gz -C /data ." - -cp ${TEMP_DIR}/backup.tar.gz $BACKUP_FILE -# dropshell cleans up temp dir after script finishes - - -echo "Backup created successfully: $BACKUP_FILE" +echo "Backup complete: ${BACKUP_FILE}" diff --git a/templates/simple-object-storage/install.sh b/templates/simple-object-storage/install.sh index 7912414..801e5cb 100644 --- a/templates/simple-object-storage/install.sh +++ b/templates/simple-object-storage/install.sh @@ -1,42 +1,27 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# INSTALL SCRIPT -# The install script is required for all templates. -# It is used to install the service on the server. +# Simple Object Storage Install Script +# Pulls image, creates volume/config, starts container. -source "$(dirname "$0")/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "VOLUME_NAME" "HOST_NAME" - -# Create volume if it doesn't exist -if ! docker volume inspect "${VOLUME_NAME}" &>/dev/null; then - echo "Volume ${VOLUME_NAME} does not exist, creating..." - docker volume create "${VOLUME_NAME}" -fi -if ! docker volume inspect "${VOLUME_NAME}" &>/dev/null; then - die "Failed to create volume ${VOLUME_NAME}" -fi - -# Test Docker -_check_docker_installed || die "Docker test failed, aborting installation..." +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "VOLUME_NAME" "HOST_NAME" +autocreate "volume=${VOLUME_NAME}" # 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" - -# 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}" - -echo "Installation of ${CONTAINER_NAME} complete" +echo "Pulling image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}..." +docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || _die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" +echo "Stopping and removing any existing container..." +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}" +echo "Installation complete for service ${CONTAINER_NAME}." # determine port. command -v jq &> /dev/null || die "jq could not be found, please install it" [ -f "${CONFIG_PATH}/sos_config.json" ] || die "sos_config.json does not exist" PORT=$(jq -r '.port' "${CONFIG_PATH}/sos_config.json") - echo "You can access the service at http://${HOST_NAME}:${PORT}" diff --git a/templates/simple-object-storage/logs.sh b/templates/simple-object-storage/logs.sh index 209b57b..ebea282 100644 --- a/templates/simple-object-storage/logs.sh +++ b/templates/simple-object-storage/logs.sh @@ -1,16 +1,13 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# 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. +# Simple Object Storage Logs Script +# Shows the logs for the running container. -source "$(dirname "$0")/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" -echo "Container ${CONTAINER_NAME} logs:" -grey_start -docker logs "${CONTAINER_NAME}" -grey_end +echo "Showing logs for ${CONTAINER_NAME}... (Press Ctrl+C to stop)" +_grey_start +_get_container_logs $CONTAINER_NAME +_grey_end diff --git a/templates/simple-object-storage/nuke.sh b/templates/simple-object-storage/nuke.sh index 25fd57c..c00853b 100644 --- a/templates/simple-object-storage/nuke.sh +++ b/templates/simple-object-storage/nuke.sh @@ -1,12 +1,9 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" -# NUKE SCRIPT -# Gets run after uninstall.sh +# Simple Object Storage Nuke Script +# Removes container AND volume. -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" - -docker volume rm $VOLUME_NAME || die "Failed to remove volume ${VOLUME_NAME}" - -echo "Nuking of ${CONTAINER_NAME} complete." +_check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" +autonuke "volume=${VOLUME_NAME}" diff --git a/templates/simple-object-storage/ports.sh b/templates/simple-object-storage/ports.sh index 6eaf762..285d36b 100644 --- a/templates/simple-object-storage/ports.sh +++ b/templates/simple-object-storage/ports.sh @@ -1,11 +1,11 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" # PORT SCRIPT # The port script is OPTIONAL. # It is used to return the ports used by 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 check_required_env_vars "CONFIG_PATH" diff --git a/templates/simple-object-storage/restore.sh b/templates/simple-object-storage/restore.sh index 4d82373..7586ed4 100644 --- a/templates/simple-object-storage/restore.sh +++ b/templates/simple-object-storage/restore.sh @@ -1,46 +1,23 @@ #!/bin/bash +source "${AGENT_PATH}/_common.sh" || _die "Failed to source _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. +# Simple Object Storage Restore Script +# Restores data from a backup file. -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" -# Get backup file path from first argument -BACKUP_FILE="$1" -if [ -z "$BACKUP_FILE" ]; then - die "Backup file path not provided" +_check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" + +echo "Uninstalling service before restore..." +bash ./uninstall.sh || _die "Failed to uninstall service before restore" + +echo "Restoring data for ${CONTAINER_NAME} from ${BACKUP_FILE}..." + +if ! autorestore "volume=${VOLUME_NAME}" "$1" "$2"; then + _die "Failed to restore data from backup file" fi -TEMP_DIR=$2 -if [ -z "$TEMP_DIR" ]; then - die "Temporary directory not provided" -fi +echo "Restore complete. Reinstalling service..." -# Check if backup file already exists -if [ ! -f "$BACKUP_FILE" ]; then - die "Backup file $BACKUP_FILE does not exist" -fi +bash ./install.sh || _die "Failed to reinstall service after restore" -# # Stop container before backup -bash ./uninstall.sh || die "Failed to uninstall service before restore" - -# Remove existing data folder -echo "Deleting ALL data in $VOLUME_NAME." - -docker run --rm -v ${VOLUME_NAME}:/data alpine sh -c "\ -rm -rf /data/*" - -echo "Restoring data from $BACKUP_FILE to $VOLUME_NAME." -docker run --rm \ - -v ${VOLUME_NAME}:/data \ - -v ${BACKUP_FILE}:/backup.tar.gz \ - alpine sh -c "\ - tar xzvf /backup.tar.gz -C /data --strip-components=1" - -# reinstall service - ensure everything is latest. -bash ./install.sh || die "Failed to reinstall service after restore" - -echo "Restore complete! Service is running again." +echo "Service ${CONTAINER_NAME} restored and reinstalled." diff --git a/templates/simple-object-storage/ssh.sh b/templates/simple-object-storage/ssh.sh index 33189c8..12795a3 100644 --- a/templates/simple-object-storage/ssh.sh +++ b/templates/simple-object-storage/ssh.sh @@ -1,14 +1,18 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +# Simple Object Storage SSH Script +# Opens a shell in the running container. -if ! _is_container_running "$CONTAINER_NAME"; then - die "Container ${CONTAINER_NAME} is not running. Can't connect to it." + + +_check_required_env_vars "CONTAINER_NAME" + +if ! _is_container_running $CONTAINER_NAME; then + _die "Container ${CONTAINER_NAME} is not running. Can't connect to it." fi -echo "Connecting to ${CONTAINER_NAME}..." - -docker exec -it ${CONTAINER_NAME} bash +echo "Connecting to container ${CONTAINER_NAME}..." +docker exec -it "${CONTAINER_NAME}" /bin/bash echo "Disconnected from ${CONTAINER_NAME}" diff --git a/templates/simple-object-storage/start.sh b/templates/simple-object-storage/start.sh index 5722d66..34228fb 100644 --- a/templates/simple-object-storage/start.sh +++ b/templates/simple-object-storage/start.sh @@ -1,21 +1,19 @@ #!/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. +# Simple Object Storage Start Script +# Creates and starts the container using environment variables. -source "$(dirname "$0")/_common.sh" check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" "CONFIG_PATH" # check volume exists. if ! docker volume inspect "${VOLUME_NAME}" &>/dev/null; then - die "Docker volume ${VOLUME_NAME} does not exist" + _die "Docker volume ${VOLUME_NAME} does not exist" fi # determine port. -command -v jq &> /dev/null || die "jq could not be found, please install it" -[ -f "${CONFIG_PATH}/sos_config.json" ] || die "sos_config.json does not exist" +command -v jq &> /dev/null || _die "jq could not be found, please install it" +[ -f "${CONFIG_PATH}/sos_config.json" ] || _die "sos_config.json does not exist" PORT=$(jq -r '.port' "${CONFIG_PATH}/sos_config.json") @@ -29,12 +27,12 @@ DOCKER_RUN_CMD="docker run -d \ if ! create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then - die "Failed to start container ${CONTAINER_NAME}" + _die "Failed to start container ${CONTAINER_NAME}" fi # Check if the container is running if ! _is_container_running "$CONTAINER_NAME"; then - die "Container ${CONTAINER_NAME} is not running" + _die "Container ${CONTAINER_NAME} is not running" fi echo "Container ${CONTAINER_NAME} started" diff --git a/templates/simple-object-storage/status.sh b/templates/simple-object-storage/status.sh index fb13ad8..8721118 100644 --- a/templates/simple-object-storage/status.sh +++ b/templates/simple-object-storage/status.sh @@ -1,13 +1,9 @@ #!/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/simple-object-storage/stop.sh b/templates/simple-object-storage/stop.sh index e1cc8e9..751e03a 100644 --- a/templates/simple-object-storage/stop.sh +++ b/templates/simple-object-storage/stop.sh @@ -1,13 +1,12 @@ #!/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. +# Simple Object Storage Stop Script +# Stops the running container. -source "$(dirname "$0")/_common.sh" -check_required_env_vars "CONTAINER_NAME" -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" +_check_required_env_vars "CONTAINER_NAME" -echo "Container ${CONTAINER_NAME} stopped" +echo "Stopping service ${CONTAINER_NAME}..." +_stop_container $CONTAINER_NAME || _die "Failed to stop container ${CONTAINER_NAME}" +echo "Service ${CONTAINER_NAME} stopped." diff --git a/templates/simple-object-storage/uninstall.sh b/templates/simple-object-storage/uninstall.sh index 5a59244..82e0310 100644 --- a/templates/simple-object-storage/uninstall.sh +++ b/templates/simple-object-storage/uninstall.sh @@ -1,11 +1,11 @@ #!/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" "VOLUME_NAME" _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" diff --git a/templates/squashkiwi/backup.sh b/templates/squashkiwi/backup.sh index c060d97..c546ccb 100644 --- a/templates/squashkiwi/backup.sh +++ b/templates/squashkiwi/backup.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" # Get backup file path from argument BACKUP_FILE="$1" diff --git a/templates/squashkiwi/install.sh b/templates/squashkiwi/install.sh old mode 100755 new mode 100644 index 5646333..ff9da79 --- a/templates/squashkiwi/install.sh +++ b/templates/squashkiwi/install.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars \ "IMAGE_REGISTRY" \ diff --git a/templates/squashkiwi/logs.sh b/templates/squashkiwi/logs.sh index 684d64d..48c67e8 100644 --- a/templates/squashkiwi/logs.sh +++ b/templates/squashkiwi/logs.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" # check required env vars check_required_env_vars \ diff --git a/templates/squashkiwi/ports.sh b/templates/squashkiwi/ports.sh index d9792fb..23f71f0 100644 --- a/templates/squashkiwi/ports.sh +++ b/templates/squashkiwi/ports.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars "HOST_PORT" diff --git a/templates/squashkiwi/restore.sh b/templates/squashkiwi/restore.sh index 335938a..918f373 100644 --- a/templates/squashkiwi/restore.sh +++ b/templates/squashkiwi/restore.sh @@ -1,11 +1,11 @@ #!/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 "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # Get backup file path from first argument diff --git a/templates/squashkiwi/ssh.sh b/templates/squashkiwi/ssh.sh index 01d620d..15fc899 100644 --- a/templates/squashkiwi/ssh.sh +++ b/templates/squashkiwi/ssh.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" if ! _is_container_running "$CONTAINER_NAME"; then diff --git a/templates/squashkiwi/start.sh b/templates/squashkiwi/start.sh old mode 100755 new mode 100644 index d672d7a..d12be5f --- a/templates/squashkiwi/start.sh +++ b/templates/squashkiwi/start.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars \ "CONTAINER_NAME" \ diff --git a/templates/squashkiwi/status.sh b/templates/squashkiwi/status.sh index c04e447..6104384 100644 --- a/templates/squashkiwi/status.sh +++ b/templates/squashkiwi/status.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars \ "CONTAINER_NAME" \ diff --git a/templates/squashkiwi/stop.sh b/templates/squashkiwi/stop.sh old mode 100755 new mode 100644 index 442cc13..a41ec1a --- a/templates/squashkiwi/stop.sh +++ b/templates/squashkiwi/stop.sh @@ -1,5 +1,5 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" check_required_env_vars \ "CONTAINER_NAME" diff --git a/templates/squashkiwi/uninstall.sh b/templates/squashkiwi/uninstall.sh index 185a91b..3ccd8b3 100644 --- a/templates/squashkiwi/uninstall.sh +++ b/templates/squashkiwi/uninstall.sh @@ -1,11 +1,11 @@ #!/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/watchtower/install.sh b/templates/watchtower/install.sh old mode 100755 new mode 100644 index f2171f0..c946fb2 --- a/templates/watchtower/install.sh +++ b/templates/watchtower/install.sh @@ -1,20 +1,25 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +# Watchtower Install Script -# Test Docker -_check_docker_installed || die "Docker test failed, aborting installation..." -# check can pull image on remote host and exit if fails -echo "Pulling image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}" -docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" -# remove and restart, as the env may have changed. -echo "Removing old container ${CONTAINER_NAME}" -_remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" -echo "Starting container ${CONTAINER_NAME}" -bash ./start.sh $1 || die "Failed to start container ${CONTAINER_NAME}" +echo "Checking Docker installation..." +_check_docker_installed || _die "Docker test failed, aborting installation..." -echo "Installation of ${CONTAINER_NAME} complete" +echo "Pulling image ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}..." +docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || _die "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" + +echo "Stopping and removing any existing container..." +# No need to check failure, might not exist +_stop_container $CONTAINER_NAME +_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}" + +echo "Starting container..." +bash ./start.sh $1 || _die "Failed to start container ${CONTAINER_NAME}" + +echo "Installation complete for service ${CONTAINER_NAME}." diff --git a/templates/watchtower/logs.sh b/templates/watchtower/logs.sh index 05e3930..6a2d176 100644 --- a/templates/watchtower/logs.sh +++ b/templates/watchtower/logs.sh @@ -1,10 +1,14 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" +# Watchtower Logs Script -echo "Container ${CONTAINER_NAME} logs:" -grey_start -docker logs "${CONTAINER_NAME}" -grey_end + +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" + +echo "Showing logs for ${CONTAINER_NAME}... (Press Ctrl+C to stop)" +_grey_start +_get_container_logs $CONTAINER_NAME +_grey_end diff --git a/templates/watchtower/start.sh b/templates/watchtower/start.sh old mode 100755 new mode 100644 index 0eacaa4..963ba48 --- a/templates/watchtower/start.sh +++ b/templates/watchtower/start.sh @@ -1,28 +1,38 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -SERVICE_CONFIG_DIR="$1" +# Watchtower Start Script -# Required environment variables -check_required_env_vars "CONTAINER_NAME" "INTERVAL" + +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "INTERVAL" + +# Optional arguments (e.g., --cleanup, --monitor-only) +EXTRA_ARGS=$1 DOCKER_RUN_CMD="docker run -d \ - --restart unless-stopped \ - --name ${CONTAINER_NAME} \ - -v /var/run/docker.sock:/var/run/docker.sock \ - -v ${SERVICE_CONFIG_DIR}/config.json:/config.json - ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG} \ - --interval ${INTERVAL}" + --name ${CONTAINER_NAME} \ + --restart=always \ + -v /var/run/docker.sock:/var/run/docker.sock \ + ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG} \ + --interval ${INTERVAL} ${EXTRA_ARGS}" -if ! create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then - echo "RUN_CMD failed:" - echo "$DOCKER_RUN_CMD" - die "Failed to start container ${CONTAINER_NAME}" +echo "Starting container ${CONTAINER_NAME}..." + +if ! _create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then + # If creation/start failed, attempt to display logs if container exists + if _is_container_exists $CONTAINER_NAME; then + echo "Attempting to get logs from failed container..." + _get_container_logs $CONTAINER_NAME + fi + _die "Failed to start container ${CONTAINER_NAME}" fi -# Check if the container is running +# Check if the container is running after successful start command if ! _is_container_running "$CONTAINER_NAME"; then - die "Container ${CONTAINER_NAME} is not running" + _get_container_logs $CONTAINER_NAME # Show logs if it immediately exited + _die "Container ${CONTAINER_NAME} is not running after start attempt" fi -echo "Container ${CONTAINER_NAME} started" +echo "Service ${CONTAINER_NAME} started successfully." diff --git a/templates/watchtower/status.sh b/templates/watchtower/status.sh index c4909b9..a6f3c73 100644 --- a/templates/watchtower/status.sh +++ b/templates/watchtower/status.sh @@ -1,14 +1,17 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" +# Watchtower Status Script -# check if the service is running -_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." -# check if the service is healthy -# curl -s -X GET http://localhost:${HOST_PORT}/health | grep -q "OK" \ -# || die "Service is not healthy - did not get OK response from /health endpoint." +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" -echo "Service is healthy" +_is_container_running $CONTAINER_NAME || _die "Service is not running - did not find container $CONTAINER_NAME." + +# Optional: Add specific health checks for Watchtower if needed. +# For example, check logs for recent activity or errors. + +echo "Service ${CONTAINER_NAME} is running." +exit 0 diff --git a/templates/watchtower/stop.sh b/templates/watchtower/stop.sh old mode 100755 new mode 100644 index b9dd8ff..f0ad18f --- a/templates/watchtower/stop.sh +++ b/templates/watchtower/stop.sh @@ -1,9 +1,13 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" +# Watchtower Stop Script -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" -echo "Container ${CONTAINER_NAME} stopped" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" + +echo "Stopping service ${CONTAINER_NAME}..." +_stop_container $CONTAINER_NAME || _die "Failed to stop container ${CONTAINER_NAME}" +echo "Service ${CONTAINER_NAME} stopped." diff --git a/templates/watchtower/uninstall.sh b/templates/watchtower/uninstall.sh index cdab565..72cb6db 100644 --- a/templates/watchtower/uninstall.sh +++ b/templates/watchtower/uninstall.sh @@ -1,13 +1,17 @@ #!/bin/bash -source "$(dirname "$0")/_common.sh" +source "${AGENT_PATH}/_common.sh" -# Required environment variables -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +# Watchtower Uninstall Script -_remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" -# remove the image -docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Failed to remove image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" +# Load service environment variables +source ./service.env +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" -echo "Uninstallation of ${CONTAINER_NAME} complete." +echo "Uninstalling service ${CONTAINER_NAME}..." +_remove_container $CONTAINER_NAME || _die "Failed to remove container ${CONTAINER_NAME}" + +echo "Service ${CONTAINER_NAME} uninstalled." + +# Note: Watchtower doesn't typically have volumes to nuke.