In a mess while refactor!

This commit is contained in:
Your Name
2025-05-03 21:46:32 +12:00
parent c6b7e2bb41
commit 0c2b90cf8e
34 changed files with 938 additions and 42 deletions

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

@ -0,0 +1,150 @@
#!/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 <run_cmd> <container_name>"
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
echo "Creating and running $container_name"
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}"
}
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
chmod 777 "$folder"
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() {
docker ps -a --format "{{.Names}}" | grep -q "^$1$"
}
# Check if a container is running
_is_container_running() {
docker ps --format "{{.Names}}" | grep -q "^$1$"
}
# 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
}
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"
}

View File

@ -0,0 +1,10 @@
# Application settings
CONTAINER_PORT=8181
# Deployment settings
CONTAINER_NAME="squashkiwi"
# Image settings
IMAGE_REGISTRY="gitea.jde.nz"
IMAGE_REPO="squashkiwi/squashkiwi"
IMAGE_TAG="latest"

View File

@ -0,0 +1,29 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
# Get backup file path from argument
BACKUP_FILE="$1"
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
# We include the parent folder in the backup paths (.), and strip on restore.
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"
fi
# Start container after backup
_start_container "$CONTAINER_NAME"
echo "Backup created successfully: $BACKUP_FILE"

View File

@ -0,0 +1,2 @@
# Template to use - always required!
TEMPLATE=squashkiwi

View File

@ -0,0 +1,6 @@
# Service settings specific to this server
# (can also override anything in the _basic.env file in the template to make it specific to this server)
HOST_PORT=80
LOCAL_DATA_FOLDER="${HOME}/.sk"
IMAGE_TAG="latest"

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

@ -0,0 +1,36 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
check_required_env_vars \
"IMAGE_REGISTRY" \
"IMAGE_REPO" \
"IMAGE_TAG" \
"CONTAINER_NAME" \
"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..."
mkdir -p "${LOCAL_DATA_FOLDER}"
fi
# Test Docker
_check_docker_installed || die "Docker test failed, aborting installation..."
# Create deploy and data folders
[ -z "$LOCAL_DATA_FOLDER" ] && die "LOCAL_DATA_FOLDER is not set"
create_folder "$LOCAL_DATA_FOLDER"
# 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"

View File

@ -0,0 +1,11 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
# check required env vars
check_required_env_vars \
"CONTAINER_NAME"
echo "Container ${CONTAINER_NAME} logs:"
grey_start
docker logs "${CONTAINER_NAME}"
grey_end

View File

@ -0,0 +1,6 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
check_required_env_vars "HOST_PORT"
echo $HOST_PORT

View File

@ -0,0 +1,41 @@
#!/bin/bash
# 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
BACKUP_FILE="$1"
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 does not exist"
fi
# # Stop container before backup
bash ./uninstall.sh || die "Failed to uninstall service before restore"
# Remove existing data folder
echo "Deleting ALL data in $LOCAL_DATA_FOLDER."
_root_remove_tree "$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"
# 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"
fi
# reinstall 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."

View File

@ -0,0 +1,13 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
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 "Disconnected from ${CONTAINER_NAME}"

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

@ -0,0 +1,31 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
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 \
--restart unless-stopped \
--name ${CONTAINER_NAME} \
-p ${HOST_PORT}:${CONTAINER_PORT} \
-v ${LOCAL_DATA_FOLDER}:/skdata \
${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}"
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"
fi
echo "Container ${CONTAINER_NAME} started, on port ${HOST_PORT}"

View File

@ -0,0 +1,16 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
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."
# 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

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

@ -0,0 +1,9 @@
#!/bin/bash
source "$(dirname "$0")/_common.sh"
check_required_env_vars \
"CONTAINER_NAME"
_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}"
echo "Container ${CONTAINER_NAME} stopped"

View File

@ -0,0 +1,19 @@
#!/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.
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}"
_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"
echo "Uninstallation of ${CONTAINER_NAME} complete."
echo "Local data folder ${LOCAL_DATA_FOLDER} still in place."