From 5e8ec900648f3501608545b92f9eeac1939ba27b Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 25 Apr 2025 10:48:38 +1200 Subject: [PATCH] Tidy --- src/main.cpp | 1 + src/main.hpp | 1 - src/servers.cpp | 34 +---- src/service_runner.cpp | 44 ++++++- src/service_runner.hpp | 5 +- src/services.cpp | 3 +- src/tableprint.cpp | 3 +- src/templates.cpp | 32 ++--- src/templates.hpp | 12 +- templates/example/_backup.sh | 29 +++++ templates/example/_common.sh | 154 +++++++++++++++++++++++ templates/example/_install.sh | 16 +++ templates/example/_ports.sh | 5 + templates/example/_status.sh | 13 ++ templates/example/example/service.env | 15 +++ templates/example/logs.sh | 8 ++ templates/example/start.sh | 22 ++++ templates/example/stop.sh | 7 ++ templates/watchtower/_common.sh | 154 +++++++++++++++++++++++ templates/watchtower/_install.sh | 16 +++ templates/watchtower/_status.sh | 13 ++ templates/watchtower/example/service.env | 8 ++ templates/watchtower/logs.sh | 8 ++ templates/watchtower/start.sh | 20 +++ templates/watchtower/stop.sh | 7 ++ 25 files changed, 566 insertions(+), 64 deletions(-) create mode 100644 templates/example/_backup.sh create mode 100755 templates/example/_common.sh create mode 100755 templates/example/_install.sh create mode 100644 templates/example/_ports.sh create mode 100644 templates/example/_status.sh create mode 100644 templates/example/example/service.env create mode 100644 templates/example/logs.sh create mode 100755 templates/example/start.sh create mode 100755 templates/example/stop.sh create mode 100755 templates/watchtower/_common.sh create mode 100755 templates/watchtower/_install.sh create mode 100644 templates/watchtower/_status.sh create mode 100644 templates/watchtower/example/service.env create mode 100644 templates/watchtower/logs.sh create mode 100755 templates/watchtower/start.sh create mode 100755 templates/watchtower/stop.sh diff --git a/src/main.cpp b/src/main.cpp index ba34fba..68bb70a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "servers.hpp" #include "utils/directories.hpp" #include "config.hpp" +#include "templates.hpp" #include #include diff --git a/src/main.hpp b/src/main.hpp index e18463e..e52283a 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -19,7 +19,6 @@ void print_help(const boost::program_options::options_description& desc); void print_version(); void check_status(); void list_servers(); -void list_templates(); void show_server_details(const std::string& server_name); void interactive_mode(); diff --git a/src/servers.cpp b/src/servers.cpp index b55870b..b0e6713 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -64,23 +64,7 @@ void list_servers() { for (const auto& service : services) { service_runner ss; if (ss.init(server.name, service.service_name)) - { - switch (ss.is_healthy()) - { - case service_runner::HealthStatus::HEALTHY: - serviceticks += ":tick: "; - break; - case service_runner::HealthStatus::UNHEALTHY: - serviceticks += ":cross: "; - break; - case service_runner::HealthStatus::NOTINSTALLED: - serviceticks += ":warning: "; - break; - case service_runner::HealthStatus::ERROR: - serviceticks += ":error: "; - break; - } - } + serviceticks += ss.healthmark() + " "; else std::cout<<"Error: Failed to initialise service runner for server: ["< ports = ss.get_ports(); ports_used.insert(ports_used.end(), ports.begin(), ports.end()); @@ -156,21 +140,7 @@ void show_server_details(const std::string& server_name) { service_runner ss; if (ss.init(server_name, service.service_name)) { - switch (ss.is_healthy()) - { - case service_runner::HealthStatus::HEALTHY: - healthy = ":check:"; - break; - case service_runner::HealthStatus::UNHEALTHY: - healthy = ":cross:"; - break; - case service_runner::HealthStatus::NOTINSTALLED: - healthy = ":warning:"; - break; - default: - healthy = ":error:"; - break; - } + healthy = ss.healthmark(); ports = ss.get_ports(); } bool first = true; diff --git a/src/service_runner.cpp b/src/service_runner.cpp index bc6985b..97a8491 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -100,9 +100,8 @@ bool service_runner::install() { } // Check if template exists - template_manager tm; template_info tinfo; - if (!tm.get_template_info(m_service_info.template_name, tinfo)) { + if (!get_template_info(m_service_info.template_name, tinfo)) { std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl; return false; } @@ -164,6 +163,11 @@ bool service_runner::run_command(const std::string& command) { return false; } + if (!template_command_exists(m_service_info.template_name, command)) { + std::cout << "No command script for " << m_service_info.template_name << " : " << command << std::endl; + return true; // nothing to run. + } + std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; // Check if service directory exists @@ -195,7 +199,13 @@ bool service_runner::backup() { return false; } - std::string script_path = mRemote_service_template_path + "/_backup.sh"; + std::string command = "_backup"; + + std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; + if (!template_command_exists(m_service_info.template_name, command)) { + std::cout << "No backup script for " << m_service_info.template_name << std::endl; + return true; // nothing to back up. + } // Check if basic installed stuff is in place. if (!check_remote_dir_exists(mRemote_service_path) || !check_remote_file_exists(script_path) || !check_remote_file_exists(mRemote_service_env_file)) @@ -252,7 +262,13 @@ service_runner::HealthStatus service_runner::is_healthy() } // Check if status script exists - std::string script_path = mRemote_service_template_path + "/_status.sh"; + std::string command = "_status"; + + if (!template_command_exists(m_service_info.template_name, command)) { + return HealthStatus::UNKNOWN; + } + + std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; if (!check_remote_file_exists(script_path)) { return HealthStatus::NOTINSTALLED; } @@ -270,16 +286,34 @@ std::string service_runner::healthtick() std::string green_tick = "\033[32m✓\033[0m"; std::string red_cross = "\033[31m✗\033[0m"; std::string yellow_exclamation = "\033[33m!\033[0m"; - + std::string unknown = "\033[33m?\033[0m"; + HealthStatus status = is_healthy(); if (status == HealthStatus::HEALTHY) return green_tick; else if (status == HealthStatus::UNHEALTHY) return red_cross; + else if (status == HealthStatus::UNKNOWN) + return unknown; else return yellow_exclamation; } +std::string service_runner::healthmark() +{ + HealthStatus status = is_healthy(); + if (status == HealthStatus::HEALTHY) + return ":tick:"; + else if (status == HealthStatus::UNHEALTHY) + return ":cross:"; + else if (status == HealthStatus::UNKNOWN) + return ":question:"; + else if (status == HealthStatus::NOTINSTALLED) + return ":warning:"; + else + return ":error:"; +} + std::vector service_runner::get_ports() { std::vector ports; diff --git a/src/service_runner.hpp b/src/service_runner.hpp index cd46d7c..0ba2218 100644 --- a/src/service_runner.hpp +++ b/src/service_runner.hpp @@ -51,7 +51,8 @@ class service_runner { HEALTHY, UNHEALTHY, NOTINSTALLED, - ERROR + ERROR, + UNKNOWN }; HealthStatus is_healthy(); @@ -61,7 +62,7 @@ class service_runner { std::vector get_ports(); std::string healthtick(); - + std::string healthmark(); private: std::string m_server_name; ServiceInfo m_service_info; diff --git a/src/services.cpp b/src/services.cpp index 6f4ff72..99e5146 100644 --- a/src/services.cpp +++ b/src/services.cpp @@ -67,8 +67,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string & } template_info tinfo; - template_manager tm; - if (!tm.get_template_info(service.template_name, tinfo)) { + if (!get_template_info(service.template_name, tinfo)) { std::cerr << "Error: Template '" << service.template_name << "' not found" << std::endl; return ServiceInfo(); } diff --git a/src/tableprint.cpp b/src/tableprint.cpp index 5f0dc8d..fadfbf0 100644 --- a/src/tableprint.cpp +++ b/src/tableprint.cpp @@ -35,7 +35,8 @@ const std::map kReplacements = { {":info:", {"i", kTextColor_Blue}}, {":check:", {"+", kTextColor_Green}}, {":x:", {"x", kTextColor_Red}}, - {":error:", {"!", kTextColor_Red}} + {":error:", {"!", kTextColor_Red}}, + {":question:", {"?", kTextColor_DarkGrey}} }; // Helper function to get ANSI color code diff --git a/src/templates.cpp b/src/templates.cpp index 669cf76..fa87fcd 100644 --- a/src/templates.cpp +++ b/src/templates.cpp @@ -10,15 +10,8 @@ namespace dropshell { -template_manager::template_manager() { - // Constructor implementation -} -template_manager::~template_manager() { - // Destructor implementation -} - -bool template_manager::get_templates(std::vector& templates) { +bool get_templates(std::vector& templates) { templates.clear(); // Helper function to add templates from a directory @@ -34,10 +27,13 @@ bool template_manager::get_templates(std::vector& templates) { info.path = entry.path().string(); // Check if template with same name already exists + bool found = false; auto it = std::find_if(templates.begin(), templates.end(), [&info](const template_info& t) { return t.name == info.name; }); - - if (it == templates.end()) { + found = (it!=templates.end()); + found |= info.name=="example"; // don't include the example template! + + if (!found) { templates.push_back(info); } } @@ -50,7 +46,7 @@ bool template_manager::get_templates(std::vector& templates) { return true; } -bool template_manager::get_template_info(const std::string& name, template_info& info) { +bool get_template_info(const std::string& name, template_info& info) { std::vector templates; if (!get_templates(templates)) { return false; @@ -67,13 +63,21 @@ bool template_manager::get_template_info(const std::string& name, template_info& return false; } - +bool template_command_exists(const std::string &template_name, const std::string &command) +{ + template_info info; + if (!get_template_info(template_name, info)) { + return false; + } + + std::string path = info.path + "/" + command + ".sh"; + return (std::filesystem::exists(path)); +} void list_templates() { - template_manager tm; std::vector templates; - if (!tm.get_templates(templates)) { + if (!get_templates(templates)) { std::cerr << "Error: Failed to get templates" << std::endl; return; } diff --git a/src/templates.hpp b/src/templates.hpp index c75bfca..ada160a 100644 --- a/src/templates.hpp +++ b/src/templates.hpp @@ -17,13 +17,11 @@ class template_info { // if a template exists in both locations, the one in the user directory takes precedence. // the template name is just the subfolder name in the templates directory. // the template path is the path of that subfolder. -class template_manager { - public: - template_manager(); - ~template_manager(); - bool get_templates(std::vector& templates); - bool get_template_info(const std::string& name, template_info& info); -}; + +bool get_templates(std::vector& templates); +bool get_template_info(const std::string& name, template_info& info); +bool template_command_exists(const std::string& template_name,const std::string& command); +void list_templates(); } // namespace dropshell diff --git a/templates/example/_backup.sh b/templates/example/_backup.sh new file mode 100644 index 0000000..8c6dd09 --- /dev/null +++ b/templates/example/_backup.sh @@ -0,0 +1,29 @@ +#!/bin/bash +source "$(dirname "$0")/_common.sh" +load_env "$1" || die "Failed to load environment variables" + +# Get backup file path from second argument +BACKUP_FILE="$2" +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 +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" diff --git a/templates/example/_common.sh b/templates/example/_common.sh new file mode 100755 index 0000000..198ca42 --- /dev/null +++ b/templates/example/_common.sh @@ -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 +} + diff --git a/templates/example/_install.sh b/templates/example/_install.sh new file mode 100755 index 0000000..d61c7e3 --- /dev/null +++ b/templates/example/_install.sh @@ -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" diff --git a/templates/example/_ports.sh b/templates/example/_ports.sh new file mode 100644 index 0000000..a71c654 --- /dev/null +++ b/templates/example/_ports.sh @@ -0,0 +1,5 @@ +#!/bin/bash +source "$(dirname "$0")/_common.sh" +load_env "$1" || die "Failed to load environment variables" + +# echo $HOST_PORT diff --git a/templates/example/_status.sh b/templates/example/_status.sh new file mode 100644 index 0000000..16ec7c9 --- /dev/null +++ b/templates/example/_status.sh @@ -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 diff --git a/templates/example/example/service.env b/templates/example/example/service.env new file mode 100644 index 0000000..c358eb2 --- /dev/null +++ b/templates/example/example/service.env @@ -0,0 +1,15 @@ +# Service settings +TEMPLATE=example + +# Application settings +CONTAINER_PORT=8181 +HOST_PORT=80 + +# Deployment settings +LOCAL_DATA_FOLDER="${HOME}/.example" +CONTAINER_NAME="example" + +# Image settings +IMAGE_REGISTRY="gitea.jde.nz" +IMAGE_REPO="example/example" +IMAGE_TAG="latest" diff --git a/templates/example/logs.sh b/templates/example/logs.sh new file mode 100644 index 0000000..7e174fa --- /dev/null +++ b/templates/example/logs.sh @@ -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 diff --git a/templates/example/start.sh b/templates/example/start.sh new file mode 100755 index 0000000..bcf98e7 --- /dev/null +++ b/templates/example/start.sh @@ -0,0 +1,22 @@ +#!/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} \ + -p ${HOST_PORT}:${CONTAINER_PORT} \ + -v ${LOCAL_DATA_FOLDER}:/skdata \ + ${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" diff --git a/templates/example/stop.sh b/templates/example/stop.sh new file mode 100755 index 0000000..c50b108 --- /dev/null +++ b/templates/example/stop.sh @@ -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" diff --git a/templates/watchtower/_common.sh b/templates/watchtower/_common.sh new file mode 100755 index 0000000..198ca42 --- /dev/null +++ b/templates/watchtower/_common.sh @@ -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 +} + diff --git a/templates/watchtower/_install.sh b/templates/watchtower/_install.sh new file mode 100755 index 0000000..d61c7e3 --- /dev/null +++ b/templates/watchtower/_install.sh @@ -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" diff --git a/templates/watchtower/_status.sh b/templates/watchtower/_status.sh new file mode 100644 index 0000000..16ec7c9 --- /dev/null +++ b/templates/watchtower/_status.sh @@ -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 diff --git a/templates/watchtower/example/service.env b/templates/watchtower/example/service.env new file mode 100644 index 0000000..4d909b9 --- /dev/null +++ b/templates/watchtower/example/service.env @@ -0,0 +1,8 @@ +# Service settings +TEMPLATE=watchtower + +# Image settings +IMAGE_REGISTRY="docker.io" +IMAGE_REPO="containrrr/watchtower" +IMAGE_TAG="latest" + diff --git a/templates/watchtower/logs.sh b/templates/watchtower/logs.sh new file mode 100644 index 0000000..7e174fa --- /dev/null +++ b/templates/watchtower/logs.sh @@ -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 diff --git a/templates/watchtower/start.sh b/templates/watchtower/start.sh new file mode 100755 index 0000000..c468ef4 --- /dev/null +++ b/templates/watchtower/start.sh @@ -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" diff --git a/templates/watchtower/stop.sh b/templates/watchtower/stop.sh new file mode 100755 index 0000000..c50b108 --- /dev/null +++ b/templates/watchtower/stop.sh @@ -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"