biiig wrench

This commit is contained in:
Your Name
2025-05-03 22:58:39 +12:00
parent 107034cf7b
commit 340170b248
51 changed files with 347 additions and 395 deletions

View File

@ -6,12 +6,7 @@
# It is called with the path to the server specific env file as an argument.
install_prerequisites() {
# this script works on debian, ubuntu and Raspberry Pi OS
# it checks the following prerequisites, and installs them if missing.
# if the user is root it proceeds, otherwise it checks for sudo privileges.
# if neither exists, and a prerequisite is missing, the script exits with failure.
check_prerequisites() {
# prerequisites:
# - bash
# - curl
@ -29,6 +24,6 @@ install_prerequisites() {
done
}
install_prerequisites
check_prerequisites
exit 0

View File

@ -1,6 +1 @@
#!/bin/bash
# NUKE SCRIPT
# run after uninstall.sh to delete all data.

View File

@ -7,11 +7,11 @@
# ----------------------------------------------------------------------------------------------------------
# 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).
# _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.
@ -21,29 +21,31 @@
# _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.
# _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() {
# Prints an error message in red and exits with status code 1.
_die() {
echo -e "\033[91mError: $1\033[0m"
exit 1
}
grey_start() {
# Switches terminal output color to grey.
_grey_start() {
echo -e -n "\033[90m"
}
grey_end() {
# Resets terminal output color from grey.
_grey_end() {
echo -e -n "\033[0m"
}
create_and_start_container() {
# Creates/starts a container, verifying it runs.
_create_and_start_container() {
if [ -z "$1" ] || [ -z "$2" ]; then
die "Template error: create_and_start_container <run_cmd> <container_name>"
_die "Template error: create_and_start_container <run_cmd> <container_name>"
fi
local run_cmd="$1"
@ -53,32 +55,33 @@ create_and_start_container() {
_is_container_running $container_name && return 0
_start_container $container_name
else
grey_start
_grey_start
$run_cmd
grey_end
_grey_end
fi
if ! _is_container_running $container_name; then
die "Container ${container_name} failed to start"
_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() {
# Creates a directory if it doesn't exist (chmod 777).
_create_folder() {
local folder="$1"
if [ -d "$folder" ]; then
return 0
fi
if ! mkdir -p "$folder"; then
die "Failed to create folder: $folder"
_die "Failed to create folder: $folder"
fi
chmod 777 "$folder"
echo "Folder created: $folder"
}
# Check if docker is installed
# Checks if Docker is installed, running, and user has permission. Returns 1 on failure.
_check_docker_installed() {
if ! command -v docker &> /dev/null; then
echo "Docker is not installed"
@ -100,7 +103,7 @@ _check_docker_installed() {
return 0
}
# Check if a container exists
# Checks if a container (any state) exists. Returns 1 if not found.
_is_container_exists() {
if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then
return 1
@ -108,7 +111,7 @@ _is_container_exists() {
return 0
}
# Check if a container is running
# Checks if a container is currently running. Returns 1 if not running.
_is_container_running() {
if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then
return 1
@ -116,37 +119,37 @@ _is_container_running() {
return 0
}
# get contianer ID
# Prints the ID of the named container.
_get_container_id() {
docker ps --format "{{.ID}}" --filter "name=$1"
}
# get container status
# Prints the status string of the named container.
_get_container_status() {
docker ps --format "{{.Status}}" --filter "name=$1"
}
# start container that exists
# Starts an existing, stopped container.
_start_container() {
_is_container_exists $1 || return 1
_is_container_running $1 && return 0
docker start $1
}
# stop container that exists
# Stops a running container.
_stop_container() {
_is_container_running $1 || return 0;
docker stop $1
}
# remove container that exists
# Stops (if needed) and removes a container.
_remove_container() {
_stop_container $1
_is_container_exists $1 || return 0;
docker rm $1
}
# get container logs
# Prints the logs for a container.
_get_container_logs() {
if ! _is_container_exists $1; then
echo "Container $1 does not exist"
@ -156,16 +159,18 @@ _get_container_logs() {
docker logs $1
}
check_required_env_vars() {
# Checks if listed environment variables are set; calls _die() if any are missing.
_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"
_die "Required environment variable $var is not set in your service.env file"
fi
done
}
function _root_remove_tree() {
# Removes a path using a root Docker container (for permissions).
_root_remove_tree() {
local to_remove="$1"
parent=$(dirname "$to_remove")
abs_parent=$(realpath "$parent")

View File

@ -1,7 +1 @@
#!/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.