Tidy
This commit is contained in:
parent
5dd4a9dce6
commit
5e8ec90064
@ -5,6 +5,7 @@
|
||||
#include "servers.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "config.hpp"
|
||||
#include "templates.hpp"
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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: ["<<server.name<<"] and service: ["<<service.service_name<<"]"<<std::endl;
|
||||
std::vector<int> 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;
|
||||
|
@ -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<int> service_runner::get_ports()
|
||||
{
|
||||
std::vector<int> ports;
|
||||
|
@ -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<int> get_ports();
|
||||
|
||||
std::string healthtick();
|
||||
|
||||
std::string healthmark();
|
||||
private:
|
||||
std::string m_server_name;
|
||||
ServiceInfo m_service_info;
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -35,7 +35,8 @@ const std::map<std::string, coloredText> 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
|
||||
|
@ -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<template_info>& templates) {
|
||||
bool get_templates(std::vector<template_info>& templates) {
|
||||
templates.clear();
|
||||
|
||||
// Helper function to add templates from a directory
|
||||
@ -34,10 +27,13 @@ bool template_manager::get_templates(std::vector<template_info>& 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; });
|
||||
found = (it!=templates.end());
|
||||
found |= info.name=="example"; // don't include the example template!
|
||||
|
||||
if (it == templates.end()) {
|
||||
if (!found) {
|
||||
templates.push_back(info);
|
||||
}
|
||||
}
|
||||
@ -50,7 +46,7 @@ bool template_manager::get_templates(std::vector<template_info>& 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<template_info> 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<template_info> templates;
|
||||
|
||||
if (!tm.get_templates(templates)) {
|
||||
if (!get_templates(templates)) {
|
||||
std::cerr << "Error: Failed to get templates" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
@ -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<template_info>& templates);
|
||||
bool get_template_info(const std::string& name, template_info& info);
|
||||
};
|
||||
|
||||
bool get_templates(std::vector<template_info>& 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
|
||||
|
29
templates/example/_backup.sh
Normal file
29
templates/example/_backup.sh
Normal file
@ -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"
|
154
templates/example/_common.sh
Executable file
154
templates/example/_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/example/_install.sh
Executable file
16
templates/example/_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"
|
5
templates/example/_ports.sh
Normal file
5
templates/example/_ports.sh
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# echo $HOST_PORT
|
13
templates/example/_status.sh
Normal file
13
templates/example/_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
|
15
templates/example/example/service.env
Normal file
15
templates/example/example/service.env
Normal file
@ -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"
|
8
templates/example/logs.sh
Normal file
8
templates/example/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
|
22
templates/example/start.sh
Executable file
22
templates/example/start.sh
Executable file
@ -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"
|
7
templates/example/stop.sh
Executable file
7
templates/example/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"
|
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"
|
Loading…
x
Reference in New Issue
Block a user