Tidy
This commit is contained in:
154
templates/watchtower/_common.sh
Executable file
154
templates/watchtower/_common.sh
Executable file
@ -0,0 +1,154 @@
|
||||
#!/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"
|
||||
}
|
||||
|
||||
create_and_start_container() {
|
||||
if _is_container_exists $CONTAINER_NAME; then
|
||||
_is_container_running $CONTAINER_NAME && return 0
|
||||
_start_container $CONTAINER_NAME
|
||||
else
|
||||
grey_start
|
||||
$1
|
||||
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
|
||||
}
|
||||
|
16
templates/watchtower/_install.sh
Executable file
16
templates/watchtower/_install.sh
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# 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"
|
||||
|
||||
# remove and restart, as the env may have changed.
|
||||
bash ./stop.sh $1 || die "Failed to stop container ${CONTAINER_NAME}"
|
||||
_remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER_NAME}"
|
||||
bash ./start.sh $1 || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Installation of ${CONTAINER_NAME} complete"
|
13
templates/watchtower/_status.sh
Normal file
13
templates/watchtower/_status.sh
Normal file
@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# 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
|
8
templates/watchtower/example/service.env
Normal file
8
templates/watchtower/example/service.env
Normal file
@ -0,0 +1,8 @@
|
||||
# Service settings
|
||||
TEMPLATE=watchtower
|
||||
|
||||
# Image settings
|
||||
IMAGE_REGISTRY="docker.io"
|
||||
IMAGE_REPO="containrrr/watchtower"
|
||||
IMAGE_TAG="latest"
|
||||
|
8
templates/watchtower/logs.sh
Normal file
8
templates/watchtower/logs.sh
Normal file
@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} logs:"
|
||||
grey_start
|
||||
docker logs --tail 100 "${CONTAINER_NAME}"
|
||||
grey_end
|
20
templates/watchtower/start.sh
Executable file
20
templates/watchtower/start.sh
Executable file
@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
DOCKER_RUN_CMD="docker run -d \
|
||||
--restart unless-stopped \
|
||||
--name ${CONTAINER_NAME} \
|
||||
-v /var/run/docker.sock:/var/run/docker.sock \
|
||||
${IMAGE_REGISTRY}/${IMAGE_REPO}:${IMAGE_TAG}"
|
||||
|
||||
if ! create_and_start_container "$DOCKER_RUN_CMD"; 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"
|
7
templates/watchtower/stop.sh
Executable file
7
templates/watchtower/stop.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
_stop_container $CONTAINER_NAME || die "Failed to stop container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} stopped"
|
Reference in New Issue
Block a user