From 3b3f1cf392004c65b085885c05c99018719a2aea Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 21 Apr 2025 18:09:19 +1200 Subject: [PATCH] Modfify template. --- templates/squashkiwi/_backup.sh | 34 ----------- templates/squashkiwi/_common.sh | 100 ++++++++++++++++++++++++++++--- templates/squashkiwi/_install.sh | 36 +++-------- templates/squashkiwi/_status.sh | 13 ++-- templates/squashkiwi/example.env | 5 +- templates/squashkiwi/logs.sh | 8 +++ templates/squashkiwi/start.sh | 17 +++--- templates/squashkiwi/stop.sh | 9 --- 8 files changed, 120 insertions(+), 102 deletions(-) delete mode 100755 templates/squashkiwi/_backup.sh create mode 100644 templates/squashkiwi/logs.sh diff --git a/templates/squashkiwi/_backup.sh b/templates/squashkiwi/_backup.sh deleted file mode 100755 index 748abe7..0000000 --- a/templates/squashkiwi/_backup.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -# Source common functions -source "$(dirname "$0")/_dockerhelper.sh" -source "$(dirname "$0")/_common.sh" - -# Load environment variables -load_env "$1" || die "Failed to load environment variables" - -# Get backup file path from second argument -BACKUP_FILE="$2" -if [ -z "$BACKUP_FILE" ]; then - 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" -fi - -# Stop container before backup -_stop_container "$CONTAINER_NAME" - -# Create backup of data folder -echo "Creating backup of $DATA_FOLDER..." -if ! tar zcvf "$BACKUP_FILE" -C "$DATA_FOLDER" .; then - _start_container "$CONTAINER_NAME" - die "Failed to create backup" -fi - -# Start container after backup -_start_container "$CONTAINER_NAME" - -echo "Backup created successfully: $BACKUP_FILE" diff --git a/templates/squashkiwi/_common.sh b/templates/squashkiwi/_common.sh index 8e68f77..9e06122 100755 --- a/templates/squashkiwi/_common.sh +++ b/templates/squashkiwi/_common.sh @@ -1,7 +1,5 @@ #!/bin/bash -source _dockerhelper.sh - # Print error message and exit with code 1 # Usage: die "error message" die() { @@ -52,12 +50,7 @@ create_and_start_container() { _start_container $CONTAINER_NAME else grey_start - docker run -d \ - --restart unless-stopped \ - --name ${CONTAINER_NAME} \ - -p ${HOST_PORT}:${CONTAINER_PORT} \ - -v ${DATA_FOLDER}:/skdata \ - ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG} + $1 grey_end fi @@ -69,3 +62,94 @@ create_and_start_container() { echo "Container ${CONTAINER_NAME} is running with ID ${ID}" } +function create_folder() { + local folder="$1" + if [ -d "$folder" ]; then + return 0 + fi + if ! mkdir -p "$folder"; then + die "Failed to create folder: $folder" + fi + echo "Folder created: $folder" +} + + + +# Check if docker is installed +_check_docker_installed() { + if ! command -v docker &> /dev/null; then + echo "Docker is not installed" + return 1 + fi + + # check if docker daemon is running + if ! docker info &> /dev/null; then + echo "Docker daemon is not running" + return 1 + fi + + # check if user has permission to run docker + if ! docker run --rm hello-world &> /dev/null; then + echo "User does not have permission to run docker" + return 1 + fi + + return 0 +} + +# Check if a container exists +_is_container_exists() { + if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then + return 1 + fi + return 0 +} + +# Check if a container is running +_is_container_running() { + if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then + return 1 + fi + return 0 +} + +# get contianer ID +_get_container_id() { + docker ps --format "{{.ID}}" --filter "name=$1" +} + +# get container status +_get_container_status() { + docker ps --format "{{.Status}}" --filter "name=$1" +} + +# start container that exists +_start_container() { + _is_container_exists $1 || return 1 + _is_container_running $1 && return 0 + docker start $1 +} + +# stop container that exists +_stop_container() { + _is_container_running $1 || return 0; + docker stop $1 +} + +# remove container that exists +_remove_container() { + _stop_container $1 + _is_container_exists $1 || return 0; + docker rm $1 +} + +# get container logs +_get_container_logs() { + if ! _is_container_exists $1; then + echo "Container $1 does not exist" + return 1 + fi + + docker logs $1 +} + diff --git a/templates/squashkiwi/_install.sh b/templates/squashkiwi/_install.sh index 23b4015..e188dc7 100755 --- a/templates/squashkiwi/_install.sh +++ b/templates/squashkiwi/_install.sh @@ -1,40 +1,20 @@ #!/bin/bash - -# Source common functions -source "$(dirname "$0")/_dockerhelper.sh" source "$(dirname "$0")/_common.sh" - -# Load environment variables -load_env "$1" || exit 1 +load_env "$1" || die "Failed to load environment variables" # Test Docker -if ! _check_docker_installed; then - echo "Docker test failed, aborting installation..." - exit 1 -fi - -function create_folder() { - local folder="$1" - if ! mkdir -p "$folder"; then - die "Failed to create folder: $folder" - fi -} +_check_docker_installed || die "Docker test failed, aborting installation..." # Create deploy and data folders -echo "Ensuring deployment folders exist..." -create_folder "$DEPLOY_FOLDER" -create_folder "$DATA_FOLDER" +create_folder "$LOCAL_DATA_FOLDER/data" +create_folder "$LOCAL_DATA_FOLDER/backups" # check can pull image on remote host and exit if fails -if ! docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"; then - echo "Failed to pull image $IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG" - exit 1 -fi -echo "Successfully pulled the docker image from the registry" +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. -_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" +./stop.sh || die "Failed to stop container ${CONTAINER_NAME}" _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}" -create_and_start_container || die "Failed to start container ${CONTAINER_NAME}" +./start.sh || die "Failed to start container ${CONTAINER_NAME}" -echo "Installation complete" +echo "Installation of ${CONTAINER_NAME} complete" diff --git a/templates/squashkiwi/_status.sh b/templates/squashkiwi/_status.sh index 8c52ded..e4a2c70 100644 --- a/templates/squashkiwi/_status.sh +++ b/templates/squashkiwi/_status.sh @@ -8,16 +8,11 @@ source "$(dirname "$0")/_common.sh" load_env "$1" || exit 1 # check if the service is running -if ! _is_container_running $CONTAINER_NAME; then - echo "Service is not running - did not find container $CONTAINER_NAME." - exit 1 -fi +_is_container_running $CONTAINER_NAME || die "Service is not running - did not find container $CONTAINER_NAME." -# curl -s -X GET http://localhost:8080/health | grep -q "OK" -if ! curl -s -X GET http://localhost:${HOST_PORT}/health | grep -q "OK"; then - echo "Service is not healthy - did not get OK response from /health endpoint." - exit 1 -fi +# 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." echo "Service is healthy" exit 0 diff --git a/templates/squashkiwi/example.env b/templates/squashkiwi/example.env index b4cc999..a16f0d5 100644 --- a/templates/squashkiwi/example.env +++ b/templates/squashkiwi/example.env @@ -7,13 +7,10 @@ CONTAINER_PORT=8181 HOST_PORT=80 # Deployment settings -DEPLOY_FOLDER="${HOME}/sk/deploy" -DATA_FOLDER="${HOME}/sk/data" +LOCAL_DATA_FOLDER="${HOME}/.sk" CONTAINER_NAME="squashkiwi" # Image settings IMAGE_REGISTRY="gitea.jde.nz" IMAGE_REPO="squashkiwi/squashkiwi" IMAGE_TAG="latest" - - diff --git a/templates/squashkiwi/logs.sh b/templates/squashkiwi/logs.sh new file mode 100644 index 0000000..7e174fa --- /dev/null +++ b/templates/squashkiwi/logs.sh @@ -0,0 +1,8 @@ +#!/bin/bash +source "$(dirname "$0")/_common.sh" +load_env "$1" || die "Failed to load environment variables" + +echo "Container ${CONTAINER_NAME} logs:" +grey_start +docker logs --tail 100 "${CONTAINER_NAME}" +grey_end diff --git a/templates/squashkiwi/start.sh b/templates/squashkiwi/start.sh index a29144e..2b59ddf 100755 --- a/templates/squashkiwi/start.sh +++ b/templates/squashkiwi/start.sh @@ -1,14 +1,11 @@ #!/bin/bash - -# Source common functions -source "$(dirname "$0")/_dockerhelper.sh" source "$(dirname "$0")/_common.sh" - -# Load environment variables load_env "$1" || die "Failed to load environment variables" -create_and_start_container || die "Failed to start container ${CONTAINER_NAME}" - -grey_start -docker logs --tail 20 "${CONTAINER_NAME}" -grey_end \ No newline at end of file +create_and_start_container "docker run -d \ + --restart unless-stopped \ + --name ${CONTAINER_NAME} \ + -p ${HOST_PORT}:${CONTAINER_PORT} \ + -v ${LOCAL_DATA_FOLDER}/data:/skdata \ + ${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}" + || die "Failed to start container ${CONTAINER_NAME}" diff --git a/templates/squashkiwi/stop.sh b/templates/squashkiwi/stop.sh index 4bca4fe..7a9ab85 100755 --- a/templates/squashkiwi/stop.sh +++ b/templates/squashkiwi/stop.sh @@ -1,14 +1,5 @@ #!/bin/bash - -# Source common functions -source "$(dirname "$0")/_dockerhelper.sh" source "$(dirname "$0")/_common.sh" - -# Load environment variables load_env "$1" || die "Failed to load environment variables" _stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}" - -grey_start -docker logs --tail 20 "${CONTAINER_NAME}" -grey_end \ No newline at end of file