This commit is contained in:
Your Name
2025-04-21 09:58:37 +12:00
parent 9e5a54ff36
commit 093dee3fd3
14 changed files with 577 additions and 36 deletions

127
templates/squashkiwi/_common.sh Executable file
View File

@ -0,0 +1,127 @@
#!/bin/bash
# Print error message and exit with code 1
# Usage: die "error message"
die() {
echo -e "\033[91mError: $1\033[0m"
exit 1
}
# Load environment variables from .env file
# Usage: load_env [path_to_env_file]
# If no path is provided, looks for .env in the same directory as the script
load_env() {
local script_dir="$(dirname "${BASH_SOURCE[0]}")"
local env_file
if [ -z "$1" ]; then
echo "Usage: $0 [path_to_env_file]"
return 1
else
# If path is relative, make it absolute using script directory as base
if [[ "$1" != /* ]]; then
env_file="$script_dir/$1"
else
env_file="$1"
fi
fi
if [ -f "$env_file" ]; then
set -a
source "$env_file"
set +a
else
echo "Warning: .env file not found at $env_file"
return 1
fi
}
grey_start() {
echo -e -n "\033[90m"
}
grey_end() {
echo -e -n "\033[0m"
}
# Test if Docker is installed and working on the local machine
# Returns 0 if Docker is working, 1 if there are any issues
test_docker() {
echo "Testing Docker on local machine..."
# Test Docker installation
if ! command -v docker >/dev/null 2>&1; then
die "Docker is not installed on this machine"
fi
# Test Docker daemon is running
if ! docker info >/dev/null 2>&1; then
die "Docker daemon is not running"
fi
# Test Docker can run containers
if ! docker run --rm hello-world >/dev/null 2>&1; then
die "Docker cannot run containers"
fi
echo "Docker is working correctly on local machine"
return 0
}
start_container() {
# Check if container exists and is stopped
echo "Checking container status..."
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
die "Container ${CONTAINER_NAME} is already running"
else
echo "Starting existing container ${CONTAINER_NAME}..."
grey_start
if ! docker start "${CONTAINER_NAME}"; then
die "Failed to start container ${CONTAINER_NAME}"
fi
grey_end
fi
else
echo "Creating and starting new container ${CONTAINER_NAME}..."
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}
grey_end
fi
# check if the container is running
if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
die "Container ${CONTAINER_NAME} is not running"
fi
# print the container id
ID=$(docker ps --format '{{.ID}}' --filter "name=^${CONTAINER_NAME}$")
echo "Container ${CONTAINER_NAME} is running with ID ${ID}"
return 0
}
stop_container() {
# Check if container is running
echo "Checking if container is running..."
if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Stopping container ${CONTAINER_NAME}..."
grey_start
if ! docker stop "${CONTAINER_NAME}"; then
die "Failed to stop container ${CONTAINER_NAME}"
fi
grey_end
echo "Container ${CONTAINER_NAME} stopped successfully"
return 0
else
echo "Container ${CONTAINER_NAME} is not running"
return 1
fi
}

24
templates/squashkiwi/backup.sh Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || die "Failed to load environment variables"
# set the backup file name to be the current date and time
BACKUP_FILE="$BACKUP_FOLDER/backup-squashkiwi-$(date +%Y-%m-%d_%H-%M-%S).tar.gz"
if [ -f "$BACKUP_FILE" ]; then
echo "Backup file $BACKUP_FILE already exists"
exit 1
fi
stop_container
# create the backup file, using relative paths.
tar zcvf "$BACKUP_FILE" -C "$DATA_FOLDER" . || die "Failed to create backup"
start_container
echo "Backup created in $BACKUP_FILE"

View File

@ -0,0 +1,20 @@
# This file contains environment variables specific to the server where the application will be deployed.
# Copy this file to .env and modify the values according to your server's configuration.
# Do not commit the actual .env file to version control as it may contain sensitive information.
# Application settings
CONTAINER_PORT=8181
HOST_PORT=80
# Deployment settings
DEPLOY_FOLDER="${HOME}/sk/deploy"
DATA_FOLDER="${HOME}/sk/data"
BACKUP_FOLDER="${HOME}/sk/backups"
CONTAINER_NAME="squashkiwi"
# Image settings
IMAGE_REGISTRY="gitea.jde.nz"
IMAGE_REPO="squashkiwi/squashkiwi"
IMAGE_TAG="latest"

36
templates/squashkiwi/install.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || exit 1
# Test Docker
if ! test_docker; then
echo "Docker test failed, aborting installation..."
exit 1
fi
# Rest of your install script here
function create_folder() {
local folder="$1"
if ! mkdir -p "$folder"; then
die "Failed to create folder: $folder"
fi
}
# Create deploy and data folders
echo "Ensuring deployment folders exist..."
create_folder "$DEPLOY_FOLDER"
create_folder "$DATA_FOLDER"
create_folder "$BACKUP_FOLDER"
# 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"

13
templates/squashkiwi/start.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || die "Failed to load environment variables"
start_container || die "Failed to start container ${CONTAINER_NAME}"
grey_start
docker logs --tail 20 "${CONTAINER_NAME}"
grey_end

13
templates/squashkiwi/stop.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || die "Failed to load environment variables"
stop_container || die "Failed to stop container ${CONTAINER_NAME}"
grey_start
docker logs --tail 20 "${CONTAINER_NAME}"
grey_end

30
templates/squashkiwi/update.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
# Source common functions
source "$(dirname "$0")/_common.sh"
# Load environment variables
load_env "$1" || exit 1
# 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"
# stop the old container
stop_container
# remove the old container
grey_start
if ! docker rm "$CONTAINER_NAME"; then
echo "Failed to remove container $CONTAINER_NAME"
exit 1
fi
grey_end
echo "Successfully removed the old container"
# start the new container
start_container