From 4c0c052f20bac61f55ea3b98eabc971f6aca1c94 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 25 Apr 2025 11:25:19 +1200 Subject: [PATCH] Yay --- src/service_runner.cpp | 9 +++++--- templates/example/_backup.sh | 3 +++ templates/example/_common.sh | 27 +++++++++++++++------- templates/example/_install.sh | 3 +++ templates/example/_ports.sh | 3 +++ templates/example/_status.sh | 3 +++ templates/example/logs.sh | 3 +++ templates/example/start.sh | 5 +++- templates/example/stop.sh | 3 +++ templates/watchtower/_common.sh | 29 +++++++++++++++++------- templates/watchtower/_install.sh | 7 +++++- templates/watchtower/_status.sh | 3 +++ templates/watchtower/example/service.env | 2 ++ templates/watchtower/logs.sh | 3 +++ templates/watchtower/start.sh | 7 +++++- templates/watchtower/stop.sh | 3 +++ 16 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 97a8491..b4aba3f 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -321,10 +321,13 @@ std::vector service_runner::get_ports() std::cerr << "Error: Server service not initialized" << std::endl; return ports; } - - std::string script_path = mRemote_service_template_path + "/_ports.sh"; - + // Check if ports script exists + std::string command = "_ports"; + if (!template_command_exists(m_service_info.template_name, command)) { + return ports; + } + std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; if (!check_remote_file_exists(script_path)) { return ports; } diff --git a/templates/example/_backup.sh b/templates/example/_backup.sh index 8c6dd09..d532a1d 100644 --- a/templates/example/_backup.sh +++ b/templates/example/_backup.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER" + # Get backup file path from second argument BACKUP_FILE="$2" if [ -z "$BACKUP_FILE" ]; then diff --git a/templates/example/_common.sh b/templates/example/_common.sh index 198ca42..e4e0be3 100755 --- a/templates/example/_common.sh +++ b/templates/example/_common.sh @@ -45,21 +45,24 @@ grey_end() { } create_and_start_container() { - if _is_container_exists $CONTAINER_NAME; then - _is_container_running $CONTAINER_NAME && return 0 - _start_container $CONTAINER_NAME + 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 - $1 + $run_cmd grey_end fi - if ! _is_container_running $CONTAINER_NAME; then - die "Container ${CONTAINER_NAME} failed to start" + 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}" + ID=$(_get_container_id $container_name) + echo "Container ${container_name} is running with ID ${ID}" } function create_folder() { @@ -152,3 +155,11 @@ _get_container_logs() { 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 +} diff --git a/templates/example/_install.sh b/templates/example/_install.sh index d61c7e3..2798d32 100755 --- a/templates/example/_install.sh +++ b/templates/example/_install.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" + # Test Docker _check_docker_installed || die "Docker test failed, aborting installation..." diff --git a/templates/example/_ports.sh b/templates/example/_ports.sh index a71c654..c678af4 100644 --- a/templates/example/_ports.sh +++ b/templates/example/_ports.sh @@ -2,4 +2,7 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +# check_required_env_vars "HOST_PORT" + # echo $HOST_PORT diff --git a/templates/example/_status.sh b/templates/example/_status.sh index 16ec7c9..e939eb6 100644 --- a/templates/example/_status.sh +++ b/templates/example/_status.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + # check if the service is running _is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." diff --git a/templates/example/logs.sh b/templates/example/logs.sh index 7e174fa..915b61c 100644 --- a/templates/example/logs.sh +++ b/templates/example/logs.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + echo "Container ${CONTAINER_NAME} logs:" grey_start docker logs --tail 100 "${CONTAINER_NAME}" diff --git a/templates/example/start.sh b/templates/example/start.sh index bcf98e7..13cdc83 100755 --- a/templates/example/start.sh +++ b/templates/example/start.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" "HOST_PORT" "CONTAINER_PORT" "LOCAL_DATA_FOLDER" + DOCKER_RUN_CMD="docker run -d \ --restart unless-stopped \ --name ${CONTAINER_NAME} \ @@ -10,7 +13,7 @@ DOCKER_RUN_CMD="docker run -d \ ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}" -if ! create_and_start_container "$DOCKER_RUN_CMD"; then +if ! create_and_start_container "$DOCKER_RUN_CMD" "$CONTAINER_NAME"; then die "Failed to start container ${CONTAINER_NAME}" fi diff --git a/templates/example/stop.sh b/templates/example/stop.sh index c50b108..42ee835 100755 --- a/templates/example/stop.sh +++ b/templates/example/stop.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" echo "Container ${CONTAINER_NAME} stopped" diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh index 198ca42..4459459 100755 --- a/templates/watchtower/_common.sh +++ b/templates/watchtower/_common.sh @@ -45,21 +45,26 @@ grey_end() { } create_and_start_container() { - if _is_container_exists $CONTAINER_NAME; then - _is_container_running $CONTAINER_NAME && return 0 - _start_container $CONTAINER_NAME + local run_cmd="$1" + local container_name="$2" + + if _is_container_exists $container_name; then + if _is_container_running $container_name; then + return 0 + fi + _start_container $container_name else grey_start - $1 + $run_cmd grey_end fi - if ! _is_container_running $CONTAINER_NAME; then - die "Container ${CONTAINER_NAME} failed to start" + 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}" + ID=$(_get_container_id $container_name) + echo "Container ${container_name} is running with ID ${ID}" } function create_folder() { @@ -152,3 +157,11 @@ _get_container_logs() { 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 +} \ No newline at end of file diff --git a/templates/watchtower/_install.sh b/templates/watchtower/_install.sh index d61c7e3..97fa6e3 100755 --- a/templates/watchtower/_install.sh +++ b/templates/watchtower/_install.sh @@ -2,15 +2,20 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" + # 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" # remove and restart, as the env may have changed. -bash ./stop.sh $1 || die "Failed to stop container ${CONTAINER_NAME}" +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 "Installation of ${CONTAINER_NAME} complete" diff --git a/templates/watchtower/_status.sh b/templates/watchtower/_status.sh index 16ec7c9..e939eb6 100644 --- a/templates/watchtower/_status.sh +++ b/templates/watchtower/_status.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + # check if the service is running _is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." diff --git a/templates/watchtower/example/service.env b/templates/watchtower/example/service.env index 4d909b9..a458a44 100644 --- a/templates/watchtower/example/service.env +++ b/templates/watchtower/example/service.env @@ -1,6 +1,8 @@ # Service settings TEMPLATE=watchtower +CONTAINER_NAME=watchtower + # Image settings IMAGE_REGISTRY="docker.io" IMAGE_REPO="containrrr/watchtower" diff --git a/templates/watchtower/logs.sh b/templates/watchtower/logs.sh index 7e174fa..915b61c 100644 --- a/templates/watchtower/logs.sh +++ b/templates/watchtower/logs.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + echo "Container ${CONTAINER_NAME} logs:" grey_start docker logs --tail 100 "${CONTAINER_NAME}" diff --git a/templates/watchtower/start.sh b/templates/watchtower/start.sh index c468ef4..40dd08d 100755 --- a/templates/watchtower/start.sh +++ b/templates/watchtower/start.sh @@ -2,13 +2,18 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + DOCKER_RUN_CMD="docker run -d \ --restart unless-stopped \ --name ${CONTAINER_NAME} \ -v /var/run/docker.sock:/var/run/docker.sock \ ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}" -if ! create_and_start_container "$DOCKER_RUN_CMD"; then +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}" fi diff --git a/templates/watchtower/stop.sh b/templates/watchtower/stop.sh index c50b108..42ee835 100755 --- a/templates/watchtower/stop.sh +++ b/templates/watchtower/stop.sh @@ -2,6 +2,9 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || die "Failed to load environment variables" +# Required environment variables +check_required_env_vars "CONTAINER_NAME" + _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" echo "Container ${CONTAINER_NAME} stopped"