From 5dd4a9dce61f7fda05a11579150d6dfa27bb2c10 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 25 Apr 2025 10:21:41 +1200 Subject: [PATCH] Tidying --- src/servers.cpp | 40 ++++-- src/service_runner.cpp | 47 ++++--- src/service_runner.hpp | 12 +- src/tableprint.cpp | 3 +- templates/squashkiwi/_backup.sh | 29 ----- templates/squashkiwi/_common.sh | 154 ----------------------- templates/squashkiwi/_install.sh | 20 --- templates/squashkiwi/_ports.sh | 5 - templates/squashkiwi/_status.sh | 13 -- templates/squashkiwi/example/service.env | 15 --- templates/squashkiwi/logs.sh | 8 -- templates/squashkiwi/start.sh | 22 ---- templates/squashkiwi/stop.sh | 7 -- 13 files changed, 72 insertions(+), 303 deletions(-) delete mode 100644 templates/squashkiwi/_backup.sh delete mode 100755 templates/squashkiwi/_common.sh delete mode 100755 templates/squashkiwi/_install.sh delete mode 100644 templates/squashkiwi/_ports.sh delete mode 100644 templates/squashkiwi/_status.sh delete mode 100644 templates/squashkiwi/example/service.env delete mode 100644 templates/squashkiwi/logs.sh delete mode 100755 templates/squashkiwi/start.sh delete mode 100755 templates/squashkiwi/stop.sh diff --git a/src/servers.cpp b/src/servers.cpp index 8acbae9..b55870b 100644 --- a/src/servers.cpp +++ b/src/servers.cpp @@ -65,10 +65,21 @@ void list_servers() { service_runner ss; if (ss.init(server.name, service.service_name)) { - if (ss.is_healthy()) - serviceticks += ":tick: "; - else - serviceticks += ":cross: "; + 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; + } } else std::cout<<"Error: Failed to initialise service runner for server: ["< ports = ss.get_ports(); @@ -140,13 +151,26 @@ void show_server_details(const std::string& server_name) { std::vector services = get_server_services_info(server_name); for (const auto& service : services) { - bool healthy = false; + std::string healthy = "?"; std::vector ports; service_runner ss; if (ss.init(server_name, service.service_name)) { - if (ss.is_healthy()) - healthy=true; + 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; + } ports = ss.get_ports(); } bool first = true; @@ -158,7 +182,7 @@ void show_server_details(const std::string& server_name) { ports_str += std::to_string(port); first = false; } - tp.add_row({healthy ? ":tick:" : ":cross:", service.service_name, ports_str}); + tp.add_row({healthy, service.service_name, ports_str}); } // end of for (const auto& service : services) tp.print(); } // end of list services diff --git a/src/service_runner.cpp b/src/service_runner.cpp index ce10f40..bc6985b 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -56,8 +56,8 @@ std::string service_runner::construct_ssh_cmd() const { return ssh_cmd.str(); } -bool service_runner::check_remote_dir_exists(const std::string& ssh_cmd, const std::string& dir_path) const { - std::string check_dir_cmd = ssh_cmd + "'test -d " + dir_path + "'"; +bool service_runner::check_remote_dir_exists(const std::string& dir_path) const { + std::string check_dir_cmd = construct_ssh_cmd() + "'test -d " + dir_path + "'"; if (system(check_dir_cmd.c_str()) != 0) { std::cerr << "Error: Directory not found on remote server:" << dir_path << std::endl; return false; @@ -65,8 +65,8 @@ bool service_runner::check_remote_dir_exists(const std::string& ssh_cmd, const s return true; } -bool service_runner::check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const { - std::string check_cmd = ssh_cmd + "'test -f " + file_path + "'"; +bool service_runner::check_remote_file_exists(const std::string& file_path) const { + std::string check_cmd = construct_ssh_cmd() + "'test -f " + file_path + "'"; if (system(check_cmd.c_str()) != 0) { std::cerr << "Error: File not found on remote server: " << file_path << std::endl; return false; @@ -164,21 +164,20 @@ bool service_runner::run_command(const std::string& command) { return false; } - std::string ssh_cmd = construct_ssh_cmd(); std::string script_path = mRemote_service_template_path + "/" + command + ".sh"; // Check if service directory exists - if (!check_remote_dir_exists(ssh_cmd, mRemote_service_path)) { + if (!check_remote_dir_exists(mRemote_service_path)) { return false; } // Check if command script exists - if (!check_remote_file_exists(ssh_cmd, script_path)) { + if (!check_remote_file_exists(script_path)) { return false; } // Check if env file exists - if (!check_remote_file_exists(ssh_cmd, mRemote_service_env_file)) { + if (!check_remote_file_exists(mRemote_service_env_file)) { return false; } @@ -196,11 +195,10 @@ bool service_runner::backup() { return false; } - std::string ssh_cmd = construct_ssh_cmd(); std::string script_path = mRemote_service_template_path + "/_backup.sh"; // Check if basic installed stuff is in place. - if (!check_remote_dir_exists(ssh_cmd, mRemote_service_path) || !check_remote_file_exists(ssh_cmd, script_path) || !check_remote_file_exists(ssh_cmd, mRemote_service_env_file)) + if (!check_remote_dir_exists(mRemote_service_path) || !check_remote_file_exists(script_path) || !check_remote_file_exists(mRemote_service_env_file)) return false; // Create backups directory on server if it doesn't exist @@ -246,26 +244,40 @@ bool service_runner::backup() { return true; } -bool service_runner::is_healthy() +service_runner::HealthStatus service_runner::is_healthy() { if (!m_server_env) { std::cerr << "Error: Server service not initialized" << std::endl; - return false; + return HealthStatus::ERROR; + } + + // Check if status script exists + std::string script_path = mRemote_service_template_path + "/_status.sh"; + if (!check_remote_file_exists(script_path)) { + return HealthStatus::NOTINSTALLED; } // Run status script, does not display output. - return execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash _status.sh " + mRemote_service_env_file + " > /dev/null 2>&1'",""); + bool ok = execute_ssh_command("'cd " + mRemote_service_template_path + " && /bin/bash _status.sh " + mRemote_service_env_file + " > /dev/null 2>&1'", ""); + if (!ok) + return HealthStatus::UNHEALTHY; + + return HealthStatus::HEALTHY; } 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"; - if (is_healthy()) + HealthStatus status = is_healthy(); + if (status == HealthStatus::HEALTHY) return green_tick; - else + else if (status == HealthStatus::UNHEALTHY) return red_cross; + else + return yellow_exclamation; } std::vector service_runner::get_ports() @@ -276,11 +288,10 @@ std::vector service_runner::get_ports() return ports; } - std::string ssh_cmd = construct_ssh_cmd(); std::string script_path = mRemote_service_template_path + "/_ports.sh"; // Check if ports script exists - if (!check_remote_file_exists(ssh_cmd, script_path)) { + if (!check_remote_file_exists(script_path)) { return ports; } @@ -290,7 +301,7 @@ std::vector service_runner::get_ports() // Create a temporary file to store the output std::string temp_file = "/tmp/dropshell_ports_" + std::to_string(getpid()); - std::string full_cmd = ssh_cmd + run_cmd + " > " + temp_file; + std::string full_cmd = construct_ssh_cmd() + run_cmd + " > " + temp_file; if (!execute_local_command(full_cmd, "Failed to run ports script")) { return ports; diff --git a/src/service_runner.hpp b/src/service_runner.hpp index 632d15f..cd46d7c 100644 --- a/src/service_runner.hpp +++ b/src/service_runner.hpp @@ -47,7 +47,13 @@ class service_runner { // check health of service. Silent. // 1. run status.sh on the server // 2. return the output of the status.sh script - bool is_healthy(); + enum class HealthStatus { + HEALTHY, + UNHEALTHY, + NOTINSTALLED, + ERROR + }; + HealthStatus is_healthy(); // get the ports of the service // 1. run _ports.sh on the server @@ -68,8 +74,8 @@ class service_runner { // Helper methods std::string construct_ssh_cmd() const; - bool check_remote_dir_exists(const std::string& ssh_cmd, const std::string& dir_path) const; - bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const; + bool check_remote_dir_exists(const std::string& dir_path) const; + bool check_remote_file_exists(const std::string& file_path) const; bool execute_ssh_command(const std::string& command, const std::string& error_msg) const; bool execute_local_command(const std::string& command, const std::string& error_msg) const; void maketitle(const std::string& title) const; diff --git a/src/tableprint.cpp b/src/tableprint.cpp index 821cfce..5f0dc8d 100644 --- a/src/tableprint.cpp +++ b/src/tableprint.cpp @@ -34,7 +34,8 @@ const std::map kReplacements = { {":warning:", {"!", kTextColor_Yellow}}, {":info:", {"i", kTextColor_Blue}}, {":check:", {"+", kTextColor_Green}}, - {":x:", {"x", kTextColor_Red}} + {":x:", {"x", kTextColor_Red}}, + {":error:", {"!", kTextColor_Red}} }; // Helper function to get ANSI color code diff --git a/templates/squashkiwi/_backup.sh b/templates/squashkiwi/_backup.sh deleted file mode 100644 index 03116df..0000000 --- a/templates/squashkiwi/_backup.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/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/squashkiwi/_common.sh b/templates/squashkiwi/_common.sh deleted file mode 100755 index 198ca42..0000000 --- a/templates/squashkiwi/_common.sh +++ /dev/null @@ -1,154 +0,0 @@ -#!/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/squashkiwi/_install.sh b/templates/squashkiwi/_install.sh deleted file mode 100755 index 96b86c4..0000000 --- a/templates/squashkiwi/_install.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/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..." - -# 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 $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/squashkiwi/_ports.sh b/templates/squashkiwi/_ports.sh deleted file mode 100644 index 7290b43..0000000 --- a/templates/squashkiwi/_ports.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -source "$(dirname "$0")/_common.sh" -load_env "$1" || die "Failed to load environment variables" - -echo $HOST_PORT diff --git a/templates/squashkiwi/_status.sh b/templates/squashkiwi/_status.sh deleted file mode 100644 index 0a82bd2..0000000 --- a/templates/squashkiwi/_status.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/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/squashkiwi/example/service.env b/templates/squashkiwi/example/service.env deleted file mode 100644 index 2437e58..0000000 --- a/templates/squashkiwi/example/service.env +++ /dev/null @@ -1,15 +0,0 @@ -# Service settings -TEMPLATE=squashkiwi - -# Application settings -CONTAINER_PORT=8181 -HOST_PORT=80 - -# Deployment settings -LOCAL_DATA_FOLDER="${HOME}/.sk" -CONTAINER_NAME="squashkiwi" - -# Image settings -IMAGE_REGISTRY="gitea.jde.nz" -IMAGE_REPO="squashkiwi/squashkiwi" -IMAGE_TAG="latest" diff --git a/templates/squashkiwi/logs.sh b/templates/squashkiwi/logs.sh deleted file mode 100644 index 7e174fa..0000000 --- a/templates/squashkiwi/logs.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/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/squashkiwi/start.sh b/templates/squashkiwi/start.sh deleted file mode 100755 index bcf98e7..0000000 --- a/templates/squashkiwi/start.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/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/squashkiwi/stop.sh b/templates/squashkiwi/stop.sh deleted file mode 100755 index c50b108..0000000 --- a/templates/squashkiwi/stop.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/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"