This commit is contained in:
Your Name 2025-05-03 23:03:16 +12:00
parent 340170b248
commit 2fe7d4c3d9
5 changed files with 2 additions and 466 deletions

View File

@ -1,128 +0,0 @@
#!/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}"
}
# 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"
}
# 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
}
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

@ -3,40 +3,10 @@ source "${AGENT_PATH}/_common.sh"
# Nginx Example Backup Script
# Load service environment variables
source ./service.env
_check_required_env_vars "CONTAINER_NAME" "LOCAL_DATA_FOLDER"
BACKUP_FILE="$1"
# hot backup is fine for nginx website content.
if [ -z "$BACKUP_FILE" ]; then
_die "Backup file path not provided"
fi
if [ -e "$BACKUP_FILE" ]; then
_die "Backup file $BACKUP_FILE already exists"
fi
echo "Backing up data for ${CONTAINER_NAME} from ${LOCAL_DATA_FOLDER} to ${BACKUP_FILE}..."
# Stop container before backup?
# echo "Stopping container ${CONTAINER_NAME} for backup..."
# _stop_container "$CONTAINER_NAME"
# Create backup of data folder
tar -czf "$BACKUP_FILE" -C "$LOCAL_DATA_FOLDER" .
# Check if backup command was successful
if [ $? -ne 0 ]; then
# Start container again if it was stopped
# echo "Restarting container ${CONTAINER_NAME}..."
# _start_container "$CONTAINER_NAME"
_die "Failed to create backup"
fi
# Start container again if it was stopped
# echo "Restarting container ${CONTAINER_NAME}..."
# _start_container "$CONTAINER_NAME"
autobackup "path=${LOCAL_DATA_FOLDER}" $1 $2 || _die "Failed to create backup"
echo "Backup complete: ${BACKUP_FILE}"

View File

@ -1,155 +0,0 @@
#!/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"
}

View File

@ -5,7 +5,6 @@ source "${AGENT_PATH}/_common.sh"
# Opens a shell in the running container.
_check_required_env_vars "CONTAINER_NAME"
if ! _is_container_running $CONTAINER_NAME; then

View File

@ -1,150 +0,0 @@
#!/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"
}