In a mess while refactor!
This commit is contained in:
parent
c6b7e2bb41
commit
0c2b90cf8e
@ -148,9 +148,9 @@
|
||||
std::string search_string = "TEMPLATE=";
|
||||
std::string replacement_line = "TEMPLATE=" + template_name;
|
||||
// replace the line in the example/service.env file with the replacement line
|
||||
std::string service_env_path = new_template_path + "/example/service.env";
|
||||
std::string service_env_path = new_template_path + "/example/.template_info.env";
|
||||
if (!replace_line_in_file(service_env_path, search_string, replacement_line)) {
|
||||
std::cerr << "Error: Failed to replace TEMPLATE= line in service.env file" << std::endl;
|
||||
std::cerr << "Error: Failed to replace TEMPLATE= line in the .template_info.env file" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
3
templates/caddy/README.txt
Normal file
3
templates/caddy/README.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Caddy!
|
||||
|
||||
Edit the static site, and the Caddyfile.
|
155
templates/caddy/_common.sh
Normal file
155
templates/caddy/_common.sh
Normal file
@ -0,0 +1,155 @@
|
||||
#!/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
|
||||
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() {
|
||||
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
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
8
templates/caddy/_default.env
Normal file
8
templates/caddy/_default.env
Normal file
@ -0,0 +1,8 @@
|
||||
# Service settings specific to this server
|
||||
|
||||
# Image settings
|
||||
IMAGE_REGISTRY="docker.io"
|
||||
IMAGE_REPO="caddy"
|
||||
|
||||
DATA_VOLUME=caddy_data
|
||||
CONFIG_VOLUME=caddy_config
|
22
templates/caddy/backup.sh
Normal file
22
templates/caddy/backup.sh
Normal file
@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
|
||||
# BACKUP SCRIPT
|
||||
# The backup script is OPTIONAL.
|
||||
# It is used to backup the service on the server.
|
||||
# It is called with one argument: the path to the destination backup file.
|
||||
# If the backup file already exists, the script should exit with a message.
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH"
|
||||
|
||||
# Stop container before backup
|
||||
_stop_container "$CONTAINER_NAME"
|
||||
|
||||
if ! autobackup volume=$DATA_VOLUME volume=$CONFIG_VOLUME $1 $2; then
|
||||
die "Failed to create backup"
|
||||
fi
|
||||
|
||||
# Start container after backup
|
||||
_start_container "$CONTAINER_NAME"
|
||||
|
||||
echo "Backup created successfully: $BACKUP_FILE"
|
2
templates/caddy/example/.template_info.env
Normal file
2
templates/caddy/example/.template_info.env
Normal file
@ -0,0 +1,2 @@
|
||||
# Template to use - always required!
|
||||
TEMPLATE=caddy
|
2
templates/caddy/example/Caddyfile
Normal file
2
templates/caddy/example/Caddyfile
Normal file
@ -0,0 +1,2 @@
|
||||
# See https://caddyserver.com/docs/caddyfile
|
||||
|
10
templates/caddy/example/service.env
Normal file
10
templates/caddy/example/service.env
Normal file
@ -0,0 +1,10 @@
|
||||
# Service settings specific to this server
|
||||
# (can also override anything in the _default.env file in the template to make it specific to this server)
|
||||
CONTAINER_NAME=caddy
|
||||
IMAGE_TAG="latest"
|
||||
|
||||
# Scripts will have these environment variables set, plus those in _default.env, plus:
|
||||
# SERVER, SERVICE, CONFIG_PATH
|
||||
# CONFIG_PATH points to this directory!
|
||||
|
||||
|
9
templates/caddy/example/static/index.html
Normal file
9
templates/caddy/example/static/index.html
Normal file
@ -0,0 +1,9 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Static Site</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Static Site</h1>
|
||||
<p>This is a static site.</p>
|
||||
</body>
|
||||
</html>
|
30
templates/caddy/install.sh
Normal file
30
templates/caddy/install.sh
Normal file
@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
# INSTALL SCRIPT
|
||||
# The install script is required for all templates.
|
||||
# It is used to install the service on the server.
|
||||
# It is called with the path to the server specific env file as an argument.
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "DATA_VOLUME" "CONFIG_VOLUME"
|
||||
|
||||
if ! autocreate volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then
|
||||
die "Failed to autocreate volumes and paths"
|
||||
fi
|
||||
|
||||
# Test Docker
|
||||
_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"
|
||||
|
||||
[ -f "${CONFIG_PATH}/Caddyfile" ] || die "Caddyfile not found in ${CONFIG_PATH}!"
|
||||
|
||||
# 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"
|
16
templates/caddy/logs.sh
Normal file
16
templates/caddy/logs.sh
Normal file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
|
||||
# LOGS SCRIPT
|
||||
# The logs script is OPTIONAL.
|
||||
# It is used to return the logs of the service.
|
||||
# It is called with the path to the server specific env file as an argument.
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} logs:"
|
||||
grey_start
|
||||
docker logs "${CONTAINER_NAME}"
|
||||
grey_end
|
15
templates/caddy/nuke.sh
Normal file
15
templates/caddy/nuke.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
# NUKE SCRIPT
|
||||
# This is run after the uninstall.sh script to delete all data.
|
||||
# dropshell handles the configuration files, so we just need to remove
|
||||
# any docker volumes and any custom local data folders.
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
check_required_env_vars "CONTAINER_NAME" "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH"
|
||||
|
||||
if ! autonuke volume=$DATA_VOLUME volume=$CONFIG_VOLUME; then
|
||||
die "Failed to nuke"
|
||||
fi
|
||||
|
||||
echo "Nuking of ${CONTAINER_NAME} complete."
|
5
templates/caddy/ports.sh
Normal file
5
templates/caddy/ports.sh
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
echo 80
|
||||
echo 443
|
22
templates/caddy/restore.sh
Normal file
22
templates/caddy/restore.sh
Normal file
@ -0,0 +1,22 @@
|
||||
#!/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 "DATA_VOLUME" "CONFIG_VOLUME" "CONFIG_PATH"
|
||||
|
||||
# uninstall container 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"
|
||||
fi
|
||||
|
||||
# reinstall service
|
||||
bash ./install.sh || die "Failed to reinstall service after restore"
|
||||
|
||||
echo "Restore complete! Service is running again."
|
34
templates/caddy/start.sh
Normal file
34
templates/caddy/start.sh
Normal file
@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
|
||||
# START SCRIPT
|
||||
# The start script is required for all templates.
|
||||
# It is used to start the service on 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" "DATA_VOLUME" "CONFIG_VOLUME"
|
||||
|
||||
|
||||
DOCKER_RUN_CMD="docker run -d \
|
||||
--restart unless-stopped \
|
||||
--name ${CONTAINER_NAME} \
|
||||
-p 80:80 \
|
||||
-p 443:443 \
|
||||
-p 443:443/udp \
|
||||
-v ${CONFIG_PATH}/Caddyfile:/etc/caddy/Caddyfile \
|
||||
-v ${DATA_VOLUME}:/data \
|
||||
-v ${CONFIG_VOLUME}:/config \
|
||||
-v ${CONFIG_PATH}/static:/srv \
|
||||
${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"
|
17
templates/caddy/status.sh
Normal file
17
templates/caddy/status.sh
Normal file
@ -0,0 +1,17 @@
|
||||
#!/bin/bash
|
||||
|
||||
# STATUS SCRIPT
|
||||
# The status script is OPTIONAL.
|
||||
# It is used to return the status of the service (0 is healthy, 1 is unhealthy).
|
||||
# It is called with the path to the server specific env file as an argument.
|
||||
|
||||
|
||||
# This is an example of a status script that checks if the service is running.
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
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."
|
||||
|
||||
echo "Service is healthy"
|
||||
exit 0
|
13
templates/caddy/stop.sh
Normal file
13
templates/caddy/stop.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
# STOP SCRIPT
|
||||
# The stop script is required for all templates.
|
||||
# It is used to stop the service on 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"
|
||||
|
||||
_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} stopped"
|
19
templates/caddy/uninstall.sh
Normal file
19
templates/caddy/uninstall.sh
Normal 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 still in place."
|
174
templates/dropshell-agent/_common.sh
Normal file
174
templates/dropshell-agent/_common.sh
Normal file
@ -0,0 +1,174 @@
|
||||
# COMMON FUNCTIONS
|
||||
# JDE
|
||||
# 2025-05-03
|
||||
|
||||
# This file is available TO ***ALL*** templates, as ${AGENT_PATH}/_common.sh
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
# summary of functions:
|
||||
# die "message" : Prints an error message in red and exits with status code 1.
|
||||
# grey_start : Switches terminal output color to grey.
|
||||
# grey_end : Resets terminal output color from grey.
|
||||
# create_and_start_container "<run_cmd>" <container_name> : Creates/starts a container, verifying it runs.
|
||||
# create_folder <folder_path> : Creates a directory if it doesn't exist (chmod 777).
|
||||
# _check_docker_installed : Checks if Docker is installed, running, and user has permission. Returns 1 on failure.
|
||||
# _is_container_exists <container_name> : Checks if a container (any state) exists. Returns 1 if not found.
|
||||
# _is_container_running <container_name>: Checks if a container is currently running. Returns 1 if not running.
|
||||
# _get_container_id <container_name> : Prints the ID of the named container.
|
||||
# _get_container_status <container_name>: Prints the status string of the named container.
|
||||
# _start_container <container_name> : Starts an existing, stopped container.
|
||||
# _stop_container <container_name> : Stops a running container.
|
||||
# _remove_container <container_name> : Stops (if needed) and removes a container.
|
||||
# _get_container_logs <container_name> : Prints the logs for a container.
|
||||
# check_required_env_vars "VAR1" ... : Checks if listed environment variables are set; calls die() if any are missing.
|
||||
# _root_remove_tree <path> : Removes a path using a root Docker container (for permissions).
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 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
|
||||
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() {
|
||||
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
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
@ -1,40 +1 @@
|
||||
DropShell Template Example - Nginx simple webserver
|
||||
|
||||
A simple service example, creating a single docker container running nginx, with contant on the host
|
||||
in the configurable LOCAL_DATA_FOLDER directory.
|
||||
|
||||
Shell scripts defined in this folder are run as DropShell commands on the remote server (not locally!).
|
||||
|
||||
When they are run, the following environment variables will be set:
|
||||
- SERVER (server name), SERVICE (service name) and CONFIG_PATH (path to the user's service configuration folder)
|
||||
- everything in _default.env
|
||||
- everything in the server's particular service.env file (defaults in example/service.env)
|
||||
|
||||
The optional backup and restore scripts get two additional arguments, which are the backup file to create/restore
|
||||
(must be a single tgz file), and an empty temporary directory on the server which automatically gets cleaned up.
|
||||
|
||||
Mandatory scripts are:
|
||||
- install.sh (installs, or if already installed updates - non-destructive)
|
||||
- uninstall.sh (preserves data)
|
||||
- nuke.sh (deletes all data)
|
||||
|
||||
Optional standard scripts are:
|
||||
- start.sh
|
||||
- stop.sh
|
||||
- backup.sh
|
||||
- restore.sh
|
||||
- status.sh
|
||||
- ports.sh
|
||||
- logs.sh
|
||||
|
||||
|
||||
The example/ folder gets copied to the service's configuration, edited for the particular server settings,
|
||||
then copied onto the server. The location is server-specific, but can be accessed via CONFIG_PATH.
|
||||
|
||||
You can use it to store things like an nginx config file (this simple example does not).
|
||||
|
||||
The example/.template_info.env must contain the TEMPLATE= definition, which must match the template folder name
|
||||
(in this case 'example-nginx').
|
||||
|
||||
You can start a new template with the command:
|
||||
dropshell create-template <TEMPLATE_NAME>
|
||||
Caddy
|
||||
|
150
templates/squashkiwi/_common.sh
Executable file
150
templates/squashkiwi/_common.sh
Executable 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"
|
||||
}
|
10
templates/squashkiwi/_default.env
Normal file
10
templates/squashkiwi/_default.env
Normal 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"
|
29
templates/squashkiwi/backup.sh
Normal file
29
templates/squashkiwi/backup.sh
Normal 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"
|
2
templates/squashkiwi/example/.template_info.env
Normal file
2
templates/squashkiwi/example/.template_info.env
Normal file
@ -0,0 +1,2 @@
|
||||
# Template to use - always required!
|
||||
TEMPLATE=squashkiwi
|
6
templates/squashkiwi/example/service.env
Normal file
6
templates/squashkiwi/example/service.env
Normal 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
36
templates/squashkiwi/install.sh
Executable 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"
|
11
templates/squashkiwi/logs.sh
Normal file
11
templates/squashkiwi/logs.sh
Normal 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
|
6
templates/squashkiwi/ports.sh
Normal file
6
templates/squashkiwi/ports.sh
Normal file
@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
check_required_env_vars "HOST_PORT"
|
||||
|
||||
echo $HOST_PORT
|
41
templates/squashkiwi/restore.sh
Normal file
41
templates/squashkiwi/restore.sh
Normal 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."
|
13
templates/squashkiwi/ssh.sh
Normal file
13
templates/squashkiwi/ssh.sh
Normal 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
31
templates/squashkiwi/start.sh
Executable 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}"
|
16
templates/squashkiwi/status.sh
Normal file
16
templates/squashkiwi/status.sh
Normal 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
9
templates/squashkiwi/stop.sh
Executable 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"
|
19
templates/squashkiwi/uninstall.sh
Normal file
19
templates/squashkiwi/uninstall.sh
Normal 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."
|
Loading…
x
Reference in New Issue
Block a user