diff --git a/replace_die.sh b/replace_die.sh old mode 100644 new mode 100755 index abbf62f..e42c71a --- a/replace_die.sh +++ b/replace_die.sh @@ -1,2 +1,9 @@ #!/bin/bash echo "Replacing die with _die in all template scripts..." +find templates -type f -name "*.sh" | grep -v "_common.sh\|test_template.sh" | while read -r file; do + if grep -q "\bdie\b" "$file"; then + sed -i "s/\\bdie\\b/_die/g" "$file" + echo "Updated: $file" + fi +done +echo "Replacement complete!" diff --git a/templates/caddy/backup.sh b/templates/caddy/backup.sh index e6d6d76..9d38bcf 100644 --- a/templates/caddy/backup.sh +++ b/templates/caddy/backup.sh @@ -1,11 +1,11 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars _stop_container "$CONTAINER_NAME" if ! autobackup volume=$DATA_VOLUME volume=$CONFIG_VOLUME $1 $2; then - die "Failed to create backup" + _die "Failed to create backup" fi _start_container "$CONTAINER_NAME" diff --git a/templates/caddy/install.sh b/templates/caddy/install.sh index dbc90cb..706067d 100644 --- a/templates/caddy/install.sh +++ b/templates/caddy/install.sh @@ -1,19 +1,19 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH" if ! autocreate volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then - die "Failed to autocreate volumes and paths" + _die "Failed to autocreate volumes and paths" fi -_check_docker_installed || die "Docker test failed, aborting installation..." +_check_docker_installed || _die "Docker test failed, aborting installation..." -docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || die "Failed to pull 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" -[ -f "${CONFIG_PATH}/Caddyfile" ] || die "Caddyfile not found in ${CONFIG_PATH}!" +[ -f "${CONFIG_PATH}/Caddyfile" ] || _die "Caddyfile not found in ${CONFIG_PATH}!" -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}" +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" diff --git a/templates/caddy/logs.sh b/templates/caddy/logs.sh index 25ee2d3..227f617 100644 --- a/templates/caddy/logs.sh +++ b/templates/caddy/logs.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Main script. echo "Container ${CONTAINER_NAME} logs:" diff --git a/templates/caddy/nuke.sh b/templates/caddy/nuke.sh index 04652ca..02f0814 100644 --- a/templates/caddy/nuke.sh +++ b/templates/caddy/nuke.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # NUKE SCRIPT # This is run after the uninstall.sh script to delete all data. @@ -9,7 +9,7 @@ check_required_env_vars if ! autonuke volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then - die "Failed to nuke" + _die "Failed to nuke" fi echo "Nuking of ${CONTAINER_NAME} complete." diff --git a/templates/caddy/restore.sh b/templates/caddy/restore.sh index 3264d14..cfc10ba 100644 --- a/templates/caddy/restore.sh +++ b/templates/caddy/restore.sh @@ -1,19 +1,19 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # RESTORE SCRIPT # uninstall container before restore -bash ./uninstall.sh || die "Failed to uninstall service before restore" +bash ./uninstall.sh || _die "Failed to uninstall service before restore" # restore data from backup file if ! autorestore volume=$DATA_VOLUME volume=$CONFIG_VOLUME "$1" "$2"; then - die "Failed to restore data from backup file" + _die "Failed to restore data from backup file" fi # reinstall service -bash ./install.sh || die "Failed to reinstall service after restore" +bash ./install.sh || _die "Failed to reinstall service after restore" echo "Restore complete! Service is running again." diff --git a/templates/caddy/start.sh b/templates/caddy/start.sh index a58ff2e..23a99ee 100644 --- a/templates/caddy/start.sh +++ b/templates/caddy/start.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # START SCRIPT # The start script is required for all templates. @@ -22,12 +22,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/caddy/status.sh b/templates/caddy/status.sh index a482e35..85d3e27 100644 --- a/templates/caddy/status.sh +++ b/templates/caddy/status.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # STATUS SCRIPT # The status script is OPTIONAL. @@ -9,7 +9,7 @@ check_required_env_vars # This is an example of a status script that checks if the service is running. # check if the service is running -_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." +_is_container_running $CONTAINER_NAME || _die "Service is not running - did not find container $CONTAINER_NAME." echo "Service is healthy" exit 0 diff --git a/templates/caddy/stop.sh b/templates/caddy/stop.sh index 11e836c..9b83cc5 100644 --- a/templates/caddy/stop.sh +++ b/templates/caddy/stop.sh @@ -1,12 +1,12 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # STOP SCRIPT # The stop script is required for all templates. # It is used to stop the service on the server. -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" +_stop_container $CONTAINER_NAME || _die "Failed to stop container ${CONTAINER_NAME}" echo "Container ${CONTAINER_NAME} stopped" diff --git a/templates/caddy/uninstall.sh b/templates/caddy/uninstall.sh index 8c5a8e4..6163001 100644 --- a/templates/caddy/uninstall.sh +++ b/templates/caddy/uninstall.sh @@ -1,15 +1,15 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # UNINSTALL SCRIPT # The uninstall script is required for all templates. # It is used to uninstall the service from the server. -_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" +_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" # remove the image docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Failed to remove image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" diff --git a/templates/dropshell-agent/shared/_autocommands.sh b/templates/dropshell-agent/shared/_autocommands.sh new file mode 100644 index 0000000..68aaffa --- /dev/null +++ b/templates/dropshell-agent/shared/_autocommands.sh @@ -0,0 +1,84 @@ +#!/bin/bash + + +_autocommandrun() { + command="$1" + key="$2" + value="$3" + backup_file="$4" + temp_path="$5" + + case "$key" in + volume) + echo "Volume: $value" + ;; + path) + echo "Path: $value" + ;; + file) + echo "File: $value" + ;; + *) + _die "Unknown key $key passed to auto${command}. We only support volume, path and file." + ;; + esac +} + +_autocommandparse() { + # first argument is the command + # last two arguments are the backup file and the temporary path + # all other arguments are of form: + # key=value + # where key can be one of volume, path or file. + # value is the path or volume name. + + # we iterate over the key=value arguments, and for each we call: + # autorun + + local command="$1" + shift + + # Extract the backup file and temp path (last two arguments) + local args=("$@") + local arg_count=${#args[@]} + local backup_file="${args[$arg_count-2]}" + local temp_path="${args[$arg_count-1]}" + + # Process all key=value pairs + for ((i=0; i<$arg_count-2; i++)); do + local pair="${args[$i]}" + + # Skip if not in key=value format + if [[ "$pair" != *"="* ]]; then + continue + fi + + local key="${pair%%=*}" + local value="${pair#*=}" + + # Key must be one of volume, path or file + _autocommandrun "$command" "$key" "$value" "$backup_file" "$temp_path" + done +} + + +autocreate() { + _autocommandparse create "$@" "-" "-" +} + + +autonuke() { + _autocommandparse nuke "$@" "-" "-" +} + + +autobackup() { + _autocommandparse backup "$@" +} + + +autorestore() { + _autocommandparse restore "$@" +} + + diff --git a/templates/dropshell-agent/shared/_common.sh b/templates/dropshell-agent/shared/_common.sh index d5bcae6..6731ec0 100644 --- a/templates/dropshell-agent/shared/_common.sh +++ b/templates/dropshell-agent/shared/_common.sh @@ -177,3 +177,7 @@ _root_remove_tree() { child=$(basename "$to_remove") docker run --rm -v "$abs_parent":/data alpine rm -rf "/data/$child" } + + +# Load autocommands +source "${AGENT_PATH}/_autocommands.sh" \ No newline at end of file diff --git a/templates/example-nginx/backup.sh b/templates/example-nginx/backup.sh index 58c19fd..104ce1f 100644 --- a/templates/example-nginx/backup.sh +++ b/templates/example-nginx/backup.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "LOCAL_DATA_FOLDER" +_check_required_env_vars "LOCAL_DATA_FOLDER" # Nginx Example Backup Script # hot backup is fine for nginx website content. diff --git a/templates/example-nginx/install.sh b/templates/example-nginx/install.sh index 8969408..56bacec 100644 --- a/templates/example-nginx/install.sh +++ b/templates/example-nginx/install.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "LOCAL_DATA_FOLDER" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "CONTAINER_NAME" +_check_required_env_vars "LOCAL_DATA_FOLDER" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "CONTAINER_NAME" # Nginx Example Install Script diff --git a/templates/example-nginx/logs.sh b/templates/example-nginx/logs.sh index b5dcd99..085f83a 100644 --- a/templates/example-nginx/logs.sh +++ b/templates/example-nginx/logs.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" # Nginx Example Logs Script diff --git a/templates/example-nginx/nuke.sh b/templates/example-nginx/nuke.sh index 5a45b35..dddb809 100644 --- a/templates/example-nginx/nuke.sh +++ b/templates/example-nginx/nuke.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "LOCAL_DATA_FOLDER" "CONTAINER_NAME" +_check_required_env_vars "LOCAL_DATA_FOLDER" "CONTAINER_NAME" # Nginx Example Nuke Script # Removes container and local data folder. diff --git a/templates/example-nginx/ports.sh b/templates/example-nginx/ports.sh index 1ccdc1b..edb1534 100644 --- a/templates/example-nginx/ports.sh +++ b/templates/example-nginx/ports.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "HOST_PORT" +_check_required_env_vars "HOST_PORT" # Nginx Example Ports Script diff --git a/templates/example-nginx/restore.sh b/templates/example-nginx/restore.sh index 0174aca..c6f6902 100644 --- a/templates/example-nginx/restore.sh +++ b/templates/example-nginx/restore.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "LOCAL_DATA_FOLDER" +_check_required_env_vars "LOCAL_DATA_FOLDER" # Nginx Example Restore Script diff --git a/templates/example-nginx/start.sh b/templates/example-nginx/start.sh index e5bde11..ca2104a 100644 --- a/templates/example-nginx/start.sh +++ b/templates/example-nginx/start.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" "HOST_PORT" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" "HOST_PORT" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" # START SCRIPT # The start script is required for all templates. @@ -8,7 +8,7 @@ check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" "HOST_PORT" "IMAGE_ # It is called with the path to the server specific env file as an argument. -[ -d "${LOCAL_DATA_FOLDER}" ] || die "Local data folder ${LOCAL_DATA_FOLDER} does not exist." +[ -d "${LOCAL_DATA_FOLDER}" ] || _die "Local data folder ${LOCAL_DATA_FOLDER} does not exist." DOCKER_RUN_CMD="docker run -d \ --restart unless-stopped \ @@ -19,12 +19,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/example-nginx/status.sh b/templates/example-nginx/status.sh index 4e9fe90..86ba20f 100644 --- a/templates/example-nginx/status.sh +++ b/templates/example-nginx/status.sh @@ -1,13 +1,13 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" # STATUS SCRIPT # The status script is OPTIONAL. # It is used to return the status of the service (0 is healthy, 1 is unhealthy). # check if the service is running -_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." +_is_container_running $CONTAINER_NAME || _die "Service is not running - did not find container $CONTAINER_NAME." echo "Service is healthy" exit 0 diff --git a/templates/example-nginx/stop.sh b/templates/example-nginx/stop.sh index 2983646..f908886 100644 --- a/templates/example-nginx/stop.sh +++ b/templates/example-nginx/stop.sh @@ -1,7 +1,7 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" +_stop_container $CONTAINER_NAME || _die "Failed to stop container ${CONTAINER_NAME}" echo "Container ${CONTAINER_NAME} stopped" diff --git a/templates/example-nginx/uninstall.sh b/templates/example-nginx/uninstall.sh index 62e9b15..4507a0d 100644 --- a/templates/example-nginx/uninstall.sh +++ b/templates/example-nginx/uninstall.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" # Nginx Example Uninstall Script diff --git a/templates/simple-object-storage/backup.sh b/templates/simple-object-storage/backup.sh index 35662f8..5d7f145 100644 --- a/templates/simple-object-storage/backup.sh +++ b/templates/simple-object-storage/backup.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Backup Script # Creates a backup tarball of the volume contents. diff --git a/templates/simple-object-storage/install.sh b/templates/simple-object-storage/install.sh index 89c41a3..cb79fff 100644 --- a/templates/simple-object-storage/install.sh +++ b/templates/simple-object-storage/install.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Install Script # Pulls image, creates volume/config, starts container. @@ -20,8 +20,8 @@ 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" +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 506db2e..71945d6 100644 --- a/templates/simple-object-storage/logs.sh +++ b/templates/simple-object-storage/logs.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Logs Script # Shows the logs for the running container. diff --git a/templates/simple-object-storage/nuke.sh b/templates/simple-object-storage/nuke.sh index 01ba91c..00053df 100644 --- a/templates/simple-object-storage/nuke.sh +++ b/templates/simple-object-storage/nuke.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Nuke Script # Removes container AND volume. diff --git a/templates/simple-object-storage/ports.sh b/templates/simple-object-storage/ports.sh index 5437f21..deb0295 100644 --- a/templates/simple-object-storage/ports.sh +++ b/templates/simple-object-storage/ports.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # PORT SCRIPT # The port script is OPTIONAL. @@ -11,8 +11,8 @@ check_required_env_vars # Required environment variables # 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") echo $PORT diff --git a/templates/simple-object-storage/restore.sh b/templates/simple-object-storage/restore.sh index f7dcb3c..01d0989 100644 --- a/templates/simple-object-storage/restore.sh +++ b/templates/simple-object-storage/restore.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" || _die "Failed to source _common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Restore Script # Restores data from a backup file. diff --git a/templates/simple-object-storage/ssh.sh b/templates/simple-object-storage/ssh.sh index 15c7c86..b99e9cb 100644 --- a/templates/simple-object-storage/ssh.sh +++ b/templates/simple-object-storage/ssh.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage SSH Script # Opens a shell in the running container. diff --git a/templates/simple-object-storage/start.sh b/templates/simple-object-storage/start.sh index 2099d72..42f8d95 100644 --- a/templates/simple-object-storage/start.sh +++ b/templates/simple-object-storage/start.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" "CONFIG_PATH" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +_check_required_env_vars "CONTAINER_NAME" "VOLUME_NAME" "CONFIG_PATH" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" # Simple Object Storage Start Script # Creates and starts the container using environment variables. diff --git a/templates/simple-object-storage/status.sh b/templates/simple-object-storage/status.sh index c634613..26e96c5 100644 --- a/templates/simple-object-storage/status.sh +++ b/templates/simple-object-storage/status.sh @@ -1,22 +1,22 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # STATUS SCRIPT # This is an example of a status script that checks if the service is running. # check if the service is running -_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." +_is_container_running $CONTAINER_NAME || _die "Service is not running - did not find container $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" +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") # check if the service is healthy curl -s -X GET http://localhost:${PORT}/status | jq -e '.result == "success"' \ - || die "Service is not healthy - did not get OK response from /status endpoint." + || _die "Service is not healthy - did not get OK response from /status endpoint." echo "Service is healthy" exit 0 diff --git a/templates/simple-object-storage/stop.sh b/templates/simple-object-storage/stop.sh index 462b3c9..98b8169 100644 --- a/templates/simple-object-storage/stop.sh +++ b/templates/simple-object-storage/stop.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Simple Object Storage Stop Script # Stops the running container. diff --git a/templates/simple-object-storage/uninstall.sh b/templates/simple-object-storage/uninstall.sh index 3aded75..31fad56 100644 --- a/templates/simple-object-storage/uninstall.sh +++ b/templates/simple-object-storage/uninstall.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # UNINSTALL SCRIPT # The uninstall script is required for all templates. @@ -8,9 +8,9 @@ check_required_env_vars # It is called with the path to the server specific env file as an argument. -_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" +_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" # remove the image docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Failed to remove image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" diff --git a/templates/squashkiwi/backup.sh b/templates/squashkiwi/backup.sh index aabf928..e3b4a8d 100644 --- a/templates/squashkiwi/backup.sh +++ b/templates/squashkiwi/backup.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # Stop container before backup _stop_container "$CONTAINER_NAME" diff --git a/templates/squashkiwi/install.sh b/templates/squashkiwi/install.sh index ede4c63..561148b 100644 --- a/templates/squashkiwi/install.sh +++ b/templates/squashkiwi/install.sh @@ -8,11 +8,11 @@ autocreate path=$LOCAL_DATA_FOLDER || _die "Failed to create local data folder" _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" +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}" +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" diff --git a/templates/squashkiwi/logs.sh b/templates/squashkiwi/logs.sh index 0bb4c69..2efa469 100644 --- a/templates/squashkiwi/logs.sh +++ b/templates/squashkiwi/logs.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" echo "Container ${CONTAINER_NAME} logs:" _grey_start diff --git a/templates/squashkiwi/ports.sh b/templates/squashkiwi/ports.sh index 1c94ac7..8178ba0 100644 --- a/templates/squashkiwi/ports.sh +++ b/templates/squashkiwi/ports.sh @@ -1,5 +1,5 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "HOST_PORT" +_check_required_env_vars "HOST_PORT" echo $HOST_PORT diff --git a/templates/squashkiwi/restore.sh b/templates/squashkiwi/restore.sh index c6c5484..78744ca 100644 --- a/templates/squashkiwi/restore.sh +++ b/templates/squashkiwi/restore.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # RESTORE SCRIPT # The restore script is OPTIONAL. @@ -8,11 +8,11 @@ check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # It is called with one argument: the path to the backup file. # # Stop container before backup -bash ./uninstall.sh || die "Failed to uninstall service before restore" +bash ./uninstall.sh || _die "Failed to uninstall service before restore" -autorestore "path=${LOCAL_DATA_FOLDER}" $1 $2 || die "Failed to restore data folder from backup" +autorestore "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to restore data folder from backup" # reinstall service -bash ./install.sh || die "Failed to reinstall service after restore" +bash ./install.sh || _die "Failed to reinstall service after restore" echo "Restore complete! Service is running again on port $HOST_PORT with restored website." diff --git a/templates/squashkiwi/ssh.sh b/templates/squashkiwi/ssh.sh index 001af88..3357154 100644 --- a/templates/squashkiwi/ssh.sh +++ b/templates/squashkiwi/ssh.sh @@ -1,9 +1,9 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_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." + _die "Container ${CONTAINER_NAME} is not running. Can't connect to it." fi echo "Connecting to ${CONTAINER_NAME}..." diff --git a/templates/squashkiwi/start.sh b/templates/squashkiwi/start.sh index ec2bc77..5f39d7e 100644 --- a/templates/squashkiwi/start.sh +++ b/templates/squashkiwi/start.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "HOST_PORT" "CONTAINER_PORT" "LOCAL_DATA_FOLDER" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +_check_required_env_vars "CONTAINER_NAME" "HOST_PORT" "CONTAINER_PORT" "LOCAL_DATA_FOLDER" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" DOCKER_RUN_CMD="docker run -d \ @@ -11,13 +11,13 @@ DOCKER_RUN_CMD="docker run -d \ ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}" -if ! create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then - die "Failed to start container ${CONTAINER_NAME}" +if ! _create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then + _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, on port ${HOST_PORT}" diff --git a/templates/squashkiwi/status.sh b/templates/squashkiwi/status.sh index a7ddff4..d02d18f 100644 --- a/templates/squashkiwi/status.sh +++ b/templates/squashkiwi/status.sh @@ -1,13 +1,13 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "HOST_PORT" +_check_required_env_vars "CONTAINER_NAME" "HOST_PORT" # check if the service is running -_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." +_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." + || _die "Service is not healthy - did not get OK response from /health endpoint." echo "Service is healthy" exit 0 diff --git a/templates/squashkiwi/stop.sh b/templates/squashkiwi/stop.sh index 2983646..f908886 100644 --- a/templates/squashkiwi/stop.sh +++ b/templates/squashkiwi/stop.sh @@ -1,7 +1,7 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" +_stop_container $CONTAINER_NAME || _die "Failed to stop container ${CONTAINER_NAME}" echo "Container ${CONTAINER_NAME} stopped" diff --git a/templates/squashkiwi/uninstall.sh b/templates/squashkiwi/uninstall.sh index e1ad3d1..41f87f8 100644 --- a/templates/squashkiwi/uninstall.sh +++ b/templates/squashkiwi/uninstall.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" +_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # UNINSTALL SCRIPT # The uninstall script is required for all templates. @@ -8,9 +8,9 @@ check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" # It is called with the path to the server specific env file as an argument. -_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" +_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" # remove the image docker rmi "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" || echo "Failed to remove image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh deleted file mode 100755 index d89d737..0000000 --- a/templates/watchtower/_common.sh +++ /dev/null @@ -1,128 +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}" -} - -# 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" -} - -# 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 -} - -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/watchtower/install.sh b/templates/watchtower/install.sh index 4ce5598..7ac2c86 100644 --- a/templates/watchtower/install.sh +++ b/templates/watchtower/install.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" +_check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" # Watchtower Install Script diff --git a/templates/watchtower/logs.sh b/templates/watchtower/logs.sh index 272c822..916614f 100644 --- a/templates/watchtower/logs.sh +++ b/templates/watchtower/logs.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" # Watchtower Logs Script diff --git a/templates/watchtower/start.sh b/templates/watchtower/start.sh index eea314c..9c17be0 100644 --- a/templates/watchtower/start.sh +++ b/templates/watchtower/start.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Watchtower Start Script diff --git a/templates/watchtower/status.sh b/templates/watchtower/status.sh index a7ac4da..47a18f6 100644 --- a/templates/watchtower/status.sh +++ b/templates/watchtower/status.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars "CONTAINER_NAME" +_check_required_env_vars "CONTAINER_NAME" # Watchtower Status Script diff --git a/templates/watchtower/stop.sh b/templates/watchtower/stop.sh index ae89820..1237bd5 100644 --- a/templates/watchtower/stop.sh +++ b/templates/watchtower/stop.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Watchtower Stop Script diff --git a/templates/watchtower/uninstall.sh b/templates/watchtower/uninstall.sh index 37a2779..4b78d06 100644 --- a/templates/watchtower/uninstall.sh +++ b/templates/watchtower/uninstall.sh @@ -1,6 +1,6 @@ #!/bin/bash source "${AGENT_PATH}/_common.sh" -check_required_env_vars +_check_required_env_vars # Watchtower Uninstall Script