Big Refactor
This commit is contained in:
parent
d5c7dc7de6
commit
8614d29b06
@ -4,6 +4,7 @@
|
||||
#include "utils/utils.hpp"
|
||||
#include "services.hpp"
|
||||
#include "contrib/base64.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@ -76,10 +77,7 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se
|
||||
std::string script_path = remote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(get_local_service_env_path(mServer_name,service_name));
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
env_vars["CONFIG_PATH"] = remote_service_config_path;
|
||||
get_all_service_env_vars(service_name, env_vars);
|
||||
|
||||
std::string argstr = quote(remote_service_config_path);
|
||||
for (const auto& arg : args) {
|
||||
@ -91,6 +89,40 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se
|
||||
return run_cmd;
|
||||
}
|
||||
|
||||
void server_env::get_all_service_env_vars(const std::string &service_name, std::map<std::string, std::string> & all_env_vars) const
|
||||
{
|
||||
all_env_vars.clear();
|
||||
|
||||
// add in some handy variables.
|
||||
all_env_vars["CONFIG_PATH"] = get_remote_service_config_path(mServer_name,service_name);
|
||||
all_env_vars["SERVER"] = mServer_name;
|
||||
all_env_vars["SERVICE"] = service_name;
|
||||
|
||||
|
||||
{ // load service.env from the service on this machine.
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(get_local_service_env_path(mServer_name,service_name));
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
|
||||
{ // load _default.env from the template on this machine.
|
||||
std::map<std::string, std::string> env_vars;
|
||||
ServiceInfo service_info = get_service_info(mServer_name, service_name);
|
||||
std::string defaultenvpath = service_info.local_template_default_env_path;
|
||||
if (std::filesystem::exists(defaultenvpath)) {
|
||||
envmanager env_manager(defaultenvpath);
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
else
|
||||
std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool server_env::check_remote_dir_exists(const std::string &dir_path) const
|
||||
{
|
||||
sCommand scommand("test -d " + quote(dir_path));
|
||||
@ -125,9 +157,6 @@ bool server_env::check_remote_items_exist(const std::vector<std::string> &file_p
|
||||
bool server_env::execute_ssh_command(const sCommand& command) const {
|
||||
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
|
||||
bool okay = execute_local_command(full_cmd);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
@ -135,9 +164,6 @@ bool server_env::execute_ssh_command_and_capture_output(const sCommand& command,
|
||||
{
|
||||
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
|
||||
bool okay = execute_local_command_and_capture_output(full_cmd, output);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
@ -145,17 +171,11 @@ bool server_env::run_remote_template_command(const std::string &service_name, co
|
||||
{
|
||||
std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent);
|
||||
bool okay = execute_ssh_command(full_cmd);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
bool server_env::execute_local_command(const sCommand& command) {
|
||||
bool okay = (system(command.construct_safecmd().c_str()) == 0);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on local machine: " << command.construct_safecmd() << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
|
@ -73,6 +73,9 @@ class server_env {
|
||||
std::string construct_ssh_cmd() const;
|
||||
std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent) const;
|
||||
|
||||
private:
|
||||
void get_all_service_env_vars(const std::string& service_name, std::map<std::string, std::string> & all_env_vars) const;
|
||||
|
||||
private:
|
||||
std::string mServer_name;
|
||||
std::map<std::string, std::string> variables;
|
||||
|
@ -95,6 +95,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string &
|
||||
|
||||
// find the template path
|
||||
service.local_template_path = tinfo.local_template_path;
|
||||
service.local_template_default_env_path = tinfo.local_template_default_env_path;
|
||||
|
||||
return service;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace dropshell {
|
||||
std::string template_name;
|
||||
std::string local_service_path;
|
||||
std::string local_template_path;
|
||||
std::string local_template_default_env_path;
|
||||
};
|
||||
|
||||
std::vector<ServiceInfo> get_server_services_info(const std::string& server_name);
|
||||
|
@ -27,6 +27,7 @@ bool get_templates(std::vector<template_info>& templates) {
|
||||
template_info info;
|
||||
info.template_name = entry.path().filename().string();
|
||||
info.local_template_path = entry.path().string();
|
||||
info.local_template_default_env_path = entry.path() / "_default.env";
|
||||
|
||||
// Check if template with same name already exists
|
||||
bool duplicate = false;
|
||||
|
@ -8,6 +8,7 @@ class template_info {
|
||||
public:
|
||||
std::string template_name;
|
||||
std::string local_template_path;
|
||||
std::string local_template_default_env_path;
|
||||
};
|
||||
|
||||
// templates are stored in two locations:
|
||||
|
@ -14,41 +14,6 @@ die() {
|
||||
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
|
||||
|
||||
# first load basic.env for the template defaults
|
||||
if [ -f "$script_dir/_basic.env" ]; then
|
||||
set -a
|
||||
source "$script_dir/_basic.env"
|
||||
set +a
|
||||
else
|
||||
echo "Warning: _basic.env file not found at $script_dir/_basic.env. Broken template?"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# now load the server specific env file
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 [path_to_env_file]"
|
||||
return 1
|
||||
fi
|
||||
|
||||
env_file="$1/service.env"
|
||||
|
||||
if [ ! -f "$env_file" ]; then
|
||||
echo "Warning: service.env file not found at $1/service.env"
|
||||
return 1
|
||||
fi
|
||||
|
||||
set -a
|
||||
source "$env_file"
|
||||
set +a
|
||||
}
|
||||
|
||||
grey_start() {
|
||||
echo -e -n "\033[90m"
|
||||
}
|
||||
|
@ -4,5 +4,5 @@
|
||||
CONTAINER_PORT=8181
|
||||
|
||||
# Image settings
|
||||
IMAGE_REGISTRY="gitea.jde.nz"
|
||||
IMAGE_REPO="example/example"
|
||||
IMAGE_REGISTRY="docker.io"
|
||||
IMAGE_REPO="nginx"
|
@ -2,8 +2,14 @@
|
||||
TEMPLATE=example
|
||||
|
||||
# Service settings specific to this server
|
||||
# (can also override anything in the _basic.env file in the template to make it specific to this server)
|
||||
HOST_PORT=80
|
||||
# (can also override anything in the _default.env file in the template to make it specific to this server)
|
||||
HOST_PORT=60123
|
||||
LOCAL_DATA_FOLDER="${HOME}/.example"
|
||||
CONTAINER_NAME=example-nginx
|
||||
IMAGE_TAG="latest"
|
||||
|
||||
# Scripts will have these environment variables set, plus those in _default.env, plus:
|
||||
# SERVER, SERVICE, CONFIG_PATH
|
||||
# CONFIG_PATH points to this directory!
|
||||
|
||||
|
||||
|
@ -6,18 +6,23 @@
|
||||
# It is called with the path to the server specific env file as an argument.
|
||||
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG" "LOCAL_DATA_FOLDER"
|
||||
|
||||
|
||||
# Create local data folder if it doesn't exist
|
||||
if [ -d "${LOCAL_DATA_FOLDER}" ]; then
|
||||
echo "Local data folder ${LOCAL_DATA_FOLDER} exists, using existing data."
|
||||
else
|
||||
echo "Local data folder ${LOCAL_DATA_FOLDER} does not exist, creating..."
|
||||
mkdir -p "${LOCAL_DATA_FOLDER}"
|
||||
cat <<EOF > "${LOCAL_DATA_FOLDER}/index.html"
|
||||
<html>
|
||||
<body>
|
||||
<h1>Hello, World!</h1>
|
||||
</body>
|
||||
</html>
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Test Docker
|
||||
@ -33,3 +38,4 @@ _remove_container $CONTAINER_NAME || die "Failed to remove container ${CONTAINER
|
||||
bash ./start.sh $1 || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Installation of ${CONTAINER_NAME} complete"
|
||||
echo "You can access the service at http://${SERVER}:{HOST_PORT}"
|
||||
|
@ -13,5 +13,5 @@ check_required_env_vars "CONTAINER_NAME"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} logs:"
|
||||
grey_start
|
||||
docker logs --tail 100 "${CONTAINER_NAME}"
|
||||
docker logs "${CONTAINER_NAME}"
|
||||
grey_end
|
||||
|
@ -11,4 +11,4 @@ load_env "$1" || die "Failed to load environment variables"
|
||||
# Required environment variables
|
||||
# check_required_env_vars "HOST_PORT"
|
||||
|
||||
# echo $HOST_PORT
|
||||
echo $HOST_PORT
|
||||
|
@ -14,41 +14,6 @@ die() {
|
||||
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
|
||||
|
||||
# first load basic.env for the template defaults
|
||||
if [ -f "$script_dir/_basic.env" ]; then
|
||||
set -a
|
||||
source "$script_dir/_basic.env"
|
||||
set +a
|
||||
else
|
||||
echo "Warning: _basic.env file not found at $script_dir/_basic.env. Broken template?"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# now load the server specific env file
|
||||
if [ -z "$1" ]; then
|
||||
echo "Usage: $0 [path_to_env_file]"
|
||||
return 1
|
||||
fi
|
||||
|
||||
env_file="$1/service.env"
|
||||
|
||||
if [ ! -f "$env_file" ]; then
|
||||
echo "Warning: service.env file not found at $1"
|
||||
return 1
|
||||
fi
|
||||
|
||||
set -a
|
||||
source "$env_file"
|
||||
set +a
|
||||
}
|
||||
|
||||
grey_start() {
|
||||
echo -e -n "\033[90m"
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG"
|
||||
|
@ -1,11 +1,10 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME"
|
||||
|
||||
echo "Container ${CONTAINER_NAME} logs:"
|
||||
grey_start
|
||||
docker logs --tail 100 "${CONTAINER_NAME}"
|
||||
docker logs "${CONTAINER_NAME}"
|
||||
grey_end
|
||||
|
@ -2,7 +2,6 @@
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
|
||||
SERVICE_CONFIG_DIR="$1"
|
||||
load_env "$SERVICE_CONFIG_DIR" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME" "INTERVAL"
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME"
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME"
|
||||
|
@ -1,6 +1,5 @@
|
||||
#!/bin/bash
|
||||
source "$(dirname "$0")/_common.sh"
|
||||
load_env "$1" || die "Failed to load environment variables"
|
||||
|
||||
# Required environment variables
|
||||
check_required_env_vars "CONTAINER_NAME" "IMAGE_REGISTRY" "IMAGE_REPO" "IMAGE_TAG"
|
||||
|
Loading…
x
Reference in New Issue
Block a user