This commit is contained in:
145
source/CMakeLists.txt
Normal file
145
source/CMakeLists.txt
Normal file
@ -0,0 +1,145 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(dropshell VERSION 1.0.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
# Set default build type to Release if not specified
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
|
||||
endif()
|
||||
|
||||
# Configure build-specific compiler flags
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")
|
||||
|
||||
# Configure version information
|
||||
string(TIMESTAMP CURRENT_YEAR "%Y")
|
||||
string(TIMESTAMP CURRENT_MONTH "%m")
|
||||
string(TIMESTAMP CURRENT_DAY "%d")
|
||||
string(TIMESTAMP CURRENT_HOUR "%H")
|
||||
string(TIMESTAMP CURRENT_MINUTE "%M")
|
||||
set(PROJECT_VERSION "${CURRENT_YEAR}.${CURRENT_MONTH}${CURRENT_DAY}.${CURRENT_HOUR}${CURRENT_MINUTE}")
|
||||
string(TIMESTAMP RELEASE_DATE "%Y-%m-%d")
|
||||
|
||||
# Configure version.hpp file
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.hpp.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/src/autogen/version.hpp"
|
||||
@ONLY
|
||||
)
|
||||
|
||||
# Set CMAKE_MODULE_PATH to include our custom find modules
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
||||
|
||||
# Auto-detect source files
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
file(GLOB_RECURSE HEADERS "src/*.hpp")
|
||||
|
||||
# Add custom target to run make_createagent.sh at the start of the build process
|
||||
add_custom_target(run_createagent ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E echo "Running make_createagent.sh..."
|
||||
COMMAND ${CMAKE_COMMAND} -E env bash ${CMAKE_CURRENT_SOURCE_DIR}/make_createagent.sh
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||
COMMENT "Running make_createagent.sh before build"
|
||||
)
|
||||
|
||||
# Add executable
|
||||
add_executable(dropshell ${SOURCES})
|
||||
add_dependencies(dropshell run_createagent)
|
||||
|
||||
# Set include directories
|
||||
# build dir goes first so that we can use the generated version.hpp
|
||||
target_include_directories(dropshell PRIVATE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/utils
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/contrib
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/commands
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
add_custom_command(
|
||||
TARGET dropshell POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||
$<TARGET_FILE_DIR:dropshell>
|
||||
)
|
||||
endif()
|
||||
|
||||
# Link libraries
|
||||
target_link_libraries(dropshell PRIVATE
|
||||
)
|
||||
|
||||
# Install targets
|
||||
install(TARGETS dropshell
|
||||
RUNTIME DESTINATION bin
|
||||
)
|
||||
|
||||
# Create symbolic link 'ds' pointing to 'dropshell'
|
||||
install(CODE "
|
||||
message(STATUS \"Checking if 'ds' command already exists...\")
|
||||
execute_process(
|
||||
COMMAND which ds
|
||||
RESULT_VARIABLE DS_NOT_EXISTS
|
||||
OUTPUT_QUIET
|
||||
ERROR_QUIET
|
||||
)
|
||||
if(DS_NOT_EXISTS)
|
||||
message(STATUS \"Command 'ds' does not exist. Creating symlink.\")
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
||||
\${CMAKE_INSTALL_PREFIX}/bin/dropshell
|
||||
\${CMAKE_INSTALL_PREFIX}/bin/ds
|
||||
)
|
||||
else()
|
||||
message(STATUS \"Command 'ds' already exists. Skipping symlink creation.\")
|
||||
endif()
|
||||
")
|
||||
|
||||
# Install completion script
|
||||
install(FILES src/dropshell-completion.bash
|
||||
DESTINATION /etc/bash_completion.d
|
||||
RENAME dropshell
|
||||
)
|
||||
|
||||
# Create a symlink for the completion script to work with 'ds' command
|
||||
install(CODE "
|
||||
# First check if 'ds' command exists after our installation
|
||||
execute_process(
|
||||
COMMAND which ds
|
||||
RESULT_VARIABLE DS_NOT_EXISTS
|
||||
OUTPUT_VARIABLE DS_PATH
|
||||
ERROR_QUIET
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
|
||||
# Only proceed if 'ds' exists
|
||||
if(NOT DS_NOT_EXISTS)
|
||||
# Check if 'ds' is a symlink pointing to dropshell
|
||||
execute_process(
|
||||
COMMAND readlink -f \${DS_PATH}
|
||||
RESULT_VARIABLE READLINK_FAILED
|
||||
OUTPUT_VARIABLE REAL_PATH
|
||||
ERROR_QUIET
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
|
||||
# Get the path to our dropshell binary
|
||||
set(DROPSHELL_PATH \${CMAKE_INSTALL_PREFIX}/bin/dropshell)
|
||||
|
||||
# Check if the real path is our dropshell binary
|
||||
if(NOT READLINK_FAILED AND \"\${REAL_PATH}\" STREQUAL \"\${DROPSHELL_PATH}\")
|
||||
message(STATUS \"Command 'ds' exists and points to dropshell. Creating completion script symlink.\")
|
||||
execute_process(
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
||||
/etc/bash_completion.d/dropshell
|
||||
/etc/bash_completion.d/ds
|
||||
)
|
||||
else()
|
||||
message(STATUS \"Command 'ds' exists but doesn't point to dropshell. Skipping completion symlink.\")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS \"Command 'ds' not found. Skipping completion symlink.\")
|
||||
endif()
|
||||
")
|
115
source/agent/_allservicesstatus.sh
Executable file
115
source/agent/_allservicesstatus.sh
Executable file
@ -0,0 +1,115 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script checks ALL services on the server and returns a status for each.
|
||||
|
||||
# Return format is simple ENV with the following format:
|
||||
# SERVICE_NAME_HEALTH=healthy|unhealthy|unknown
|
||||
# SERVICE_NAME_PORTS=port1,port2,port3
|
||||
|
||||
# Get all services on the server
|
||||
SCRIPT_DIR="$(dirname "$0")"
|
||||
|
||||
# // DROPSHELL_DIR
|
||||
# // |-- backups
|
||||
# // |-- services
|
||||
# // |-- service name
|
||||
# // |-- config <-- this is passed as argument to all scripts
|
||||
# // |-- service.env
|
||||
# // |-- template
|
||||
# // |-- (script files)
|
||||
# // |-- shared
|
||||
# // |-- _allservicesstatus.sh
|
||||
# // |-- config
|
||||
# // |-- service.env
|
||||
# // |-- (other config files for specific server&service)
|
||||
|
||||
CURRENT_OUTPUT=""
|
||||
CURRENT_EXIT_CODE=0
|
||||
|
||||
load_dotenv(){
|
||||
local file_path=$1
|
||||
if [ -f "${file_path}" ]; then
|
||||
source "${file_path}"
|
||||
fi
|
||||
}
|
||||
|
||||
function run_command() {
|
||||
local service_path=$1
|
||||
local command=$2
|
||||
local capture_output=${3:-false} # default to false if not specified
|
||||
|
||||
# check if the command is a file
|
||||
if [ ! -f "${service_path}/template/${command}.sh" ]; then
|
||||
return;
|
||||
fi
|
||||
|
||||
# run the command in a subshell to prevent environment changes
|
||||
CURRENT_OUTPUT=$(
|
||||
set -a
|
||||
load_dotenv "${service_path}/template/_default.env"
|
||||
load_dotenv "${service_path}/config/service.env"
|
||||
set +a
|
||||
|
||||
# update the main variables.
|
||||
export CONFIG_PATH="${service_path}/config"
|
||||
# SERVER is correct
|
||||
export SERVICE="${SERVICE_NAME}"
|
||||
|
||||
if [ "$capture_output" = "true" ]; then
|
||||
# Capture and return output
|
||||
bash "${service_path}/template/${command}.sh" 2>&1
|
||||
else
|
||||
# Run silently and return exit code
|
||||
bash "${service_path}/template/${command}.sh" > /dev/null 2>&1
|
||||
fi
|
||||
)
|
||||
CURRENT_EXIT_CODE=$?
|
||||
}
|
||||
|
||||
function command_exists() {
|
||||
local service_path=$1
|
||||
local command=$2
|
||||
if [ ! -f "${service_path}/template/${command}.sh" ]; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Get all services on the server
|
||||
SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../../../")
|
||||
|
||||
# Get all service names
|
||||
SERVICE_NAMES=$(ls "${SERVICES_PATH}")
|
||||
|
||||
# Iterate over all service names
|
||||
for SERVICE_NAME in ${SERVICE_NAMES}; do
|
||||
|
||||
SERVICE_PATH=$(realpath "${SERVICES_PATH}/${SERVICE_NAME}")
|
||||
|
||||
#--------------------------------
|
||||
# Get the service health
|
||||
if ! command_exists "${SERVICE_PATH}" "status"; then
|
||||
SERVICE_HEALTH="unknown"
|
||||
else
|
||||
run_command "${SERVICE_PATH}" "status" "false"
|
||||
if [ "${CURRENT_EXIT_CODE}" -eq 0 ]; then
|
||||
SERVICE_HEALTH="healthy"
|
||||
else
|
||||
SERVICE_HEALTH="unhealthy"
|
||||
fi
|
||||
fi
|
||||
|
||||
#--------------------------------
|
||||
# Get the service ports
|
||||
if ! command_exists "${SERVICE_PATH}" "ports"; then
|
||||
SERVICE_PORTS=""
|
||||
else
|
||||
run_command "${SERVICE_PATH}" "ports" "true"
|
||||
SERVICE_PORTS="${CURRENT_OUTPUT}"
|
||||
fi
|
||||
|
||||
#--------------------------------
|
||||
# return the health and ports
|
||||
echo "${SERVICE_NAME}_HEALTH=${SERVICE_HEALTH}"
|
||||
echo "${SERVICE_NAME}_PORTS=${SERVICE_PORTS}"
|
||||
done
|
189
source/agent/_autocommands.sh
Executable file
189
source/agent/_autocommands.sh
Executable file
@ -0,0 +1,189 @@
|
||||
#!/bin/bash
|
||||
|
||||
# This script contains the common code for the autocommands.
|
||||
|
||||
MYID=$(id -u)
|
||||
MYGRP=$(id -g)
|
||||
|
||||
_autocommandrun_volume() {
|
||||
local command="$1"
|
||||
local volume_name="$2"
|
||||
local backup_folder="$3"
|
||||
|
||||
case "$command" in
|
||||
create)
|
||||
echo "Creating volume ${volume_name}"
|
||||
docker volume create ${volume_name}
|
||||
;;
|
||||
nuke)
|
||||
echo "Nuking volume ${volume_name}"
|
||||
docker volume rm ${volume_name}
|
||||
;;
|
||||
backup)
|
||||
echo "Backing up volume ${volume_name}"
|
||||
docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -czvf /backup/backup.tgz -C /volume . && chown -R $MYID:$MYGRP /backup"
|
||||
;;
|
||||
restore)
|
||||
echo "Restoring volume ${volume_name}"
|
||||
docker volume rm ${volume_name}
|
||||
docker volume create ${volume_name}
|
||||
docker run --rm -v ${volume_name}:/volume -v ${backup_folder}:/backup debian bash -c "tar -xzvf /backup/backup.tgz -C /volume --strip-components=1"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_autocommandrun_path() {
|
||||
local command="$1"
|
||||
local path="$2"
|
||||
local backup_folder="$3"
|
||||
|
||||
case "$command" in
|
||||
create)
|
||||
echo "Creating path ${path}"
|
||||
mkdir -p ${path}
|
||||
;;
|
||||
nuke)
|
||||
echo "Nuking path ${path}"
|
||||
local path_parent=$(dirname ${path})
|
||||
local path_child=$(basename ${path})
|
||||
if [ -d "${path_parent}/${path_child}" ]; then
|
||||
docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rf /volume/${path_child}" || echo "Failed to nuke path ${path}"
|
||||
else
|
||||
echo "Path ${path} does not exist - nothing to nuke"
|
||||
fi
|
||||
;;
|
||||
backup)
|
||||
echo "Backing up path ${path}"
|
||||
if [ -d "${path}" ]; then
|
||||
docker run --rm -v ${path}:/path -v ${backup_folder}:/backup debian bash -c "tar -czvf /backup/backup.tgz -C /path . && chown -R $MYID:$MYGRP /backup"
|
||||
else
|
||||
echo "Path ${path} does not exist - nothing to backup"
|
||||
fi
|
||||
;;
|
||||
restore)
|
||||
echo "Restoring path ${path}"
|
||||
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_autocommandrun_file() {
|
||||
local command="$1"
|
||||
local filepath="$2"
|
||||
local backup_folder="$3"
|
||||
|
||||
case "$command" in
|
||||
create)
|
||||
;;
|
||||
nuke)
|
||||
rm -f ${filepath}
|
||||
;;
|
||||
backup)
|
||||
echo "Backing up file ${filepath}"
|
||||
local file_parent=$(dirname ${filepath})
|
||||
local file_name=$(basename ${filepath})
|
||||
if [ -f "${file_parent}/${file_name}" ]; then
|
||||
docker run --rm -v ${file_parent}:/volume -v ${backup_folder}:/backup debian bash -c "cp /volume/${file_name} /backup/${file_name} && chown -R $MYID:$MYGRP /backup"
|
||||
else
|
||||
echo "File ${filepath} does not exist - nothing to backup"
|
||||
fi
|
||||
;;
|
||||
restore)
|
||||
echo "Restoring file ${filepath}"
|
||||
local file_name=$(basename ${filepath})
|
||||
cp ${backup_folder}/${file_name} ${filepath}
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_autocommandparse() {
|
||||
# first argument is the command
|
||||
# if the command is backup or restore, then the last two arguments are the backup file and the temporary path
|
||||
# all other arguments are of form:
|
||||
# key=value
|
||||
# where key can be one of volume, path or file.
|
||||
# value is the path or volume name.
|
||||
|
||||
# we iterate over the key=value arguments, and for each we call:
|
||||
# autorun <command> <backupfile> <key> <value>
|
||||
|
||||
local command="$1"
|
||||
shift
|
||||
|
||||
local backup_temp_path="$1"
|
||||
shift
|
||||
|
||||
echo "autocommandparse: command=$command backup_temp_path=$backup_temp_path"
|
||||
|
||||
# Extract the backup file and temp path (last two arguments)
|
||||
local args=("$@")
|
||||
local arg_count=${#args[@]}
|
||||
|
||||
# Process all key=value pairs
|
||||
for ((i=0; i<$arg_count; i++)); do
|
||||
local pair="${args[$i]}"
|
||||
|
||||
# Skip if not in key=value format
|
||||
if [[ "$pair" != *"="* ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
local key="${pair%%=*}"
|
||||
local value="${pair#*=}"
|
||||
|
||||
# create backup folder unique to key/value.
|
||||
local bfolder=$(echo "${key}_${value}" | tr -cd '[:alnum:]_-')
|
||||
local targetpath="${backup_temp_path}/${bfolder}"
|
||||
mkdir -p ${targetpath}
|
||||
|
||||
# Key must be one of volume, path or file
|
||||
case "$key" in
|
||||
volume)
|
||||
_autocommandrun_volume "$command" "$value" "$targetpath"
|
||||
;;
|
||||
path)
|
||||
_autocommandrun_path "$command" "$value" "$targetpath"
|
||||
;;
|
||||
file)
|
||||
_autocommandrun_file "$command" "$value" "$targetpath"
|
||||
;;
|
||||
*)
|
||||
_die "Unknown key $key passed to auto${command}. We only support volume, path and file."
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
autocreate() {
|
||||
_autocommandparse create none "$@"
|
||||
}
|
||||
|
||||
|
||||
autonuke() {
|
||||
_autocommandparse nuke none "$@"
|
||||
}
|
||||
|
||||
autobackup() {
|
||||
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
||||
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
|
||||
|
||||
|
||||
mkdir -p "$BACKUP_TEMP_PATH"
|
||||
echo "_autocommandparse [backup] [$BACKUP_TEMP_PATH] [$@]"
|
||||
_autocommandparse backup "$BACKUP_TEMP_PATH" "$@"
|
||||
|
||||
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
|
||||
}
|
||||
|
||||
autorestore() {
|
||||
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
||||
BACKUP_TEMP_PATH="$TEMP_DIR/restore"
|
||||
|
||||
echo "_autocommandparse [restore] [$BACKUP_TEMP_PATH] [$@]"
|
||||
|
||||
mkdir -p "$BACKUP_TEMP_PATH"
|
||||
tar zxvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" --strip-components=1
|
||||
|
||||
_autocommandparse restore "$BACKUP_TEMP_PATH" "$@"
|
||||
}
|
183
source/agent/_common.sh
Executable file
183
source/agent/_common.sh
Executable file
@ -0,0 +1,183 @@
|
||||
# COMMON FUNCTIONS
|
||||
# JDE
|
||||
# 2025-05-03
|
||||
|
||||
# This file is available TO ***ALL*** templates, as ${AGENT_PATH}/_common.sh
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
# summary of functions:
|
||||
# _die "message" : Prints an error message in red and exits with status code 1.
|
||||
# _grey_start : Switches terminal output color to grey.
|
||||
# _grey_end : Resets terminal output color from grey.
|
||||
# _create_and_start_container "<run_cmd>" <container_name> : Creates/starts a container, verifying it runs.
|
||||
# _create_folder <folder_path> : Creates a directory if it doesn't exist (chmod 777).
|
||||
# _check_docker_installed : Checks if Docker is installed, running, and user has permission. Returns 1 on failure.
|
||||
# _is_container_exists <container_name> : Checks if a container (any state) exists. Returns 1 if not found.
|
||||
# _is_container_running <container_name>: Checks if a container is currently running. Returns 1 if not running.
|
||||
# _get_container_id <container_name> : Prints the ID of the named container.
|
||||
# _get_container_status <container_name>: Prints the status string of the named container.
|
||||
# _start_container <container_name> : Starts an existing, stopped container.
|
||||
# _stop_container <container_name> : Stops a running container.
|
||||
# _remove_container <container_name> : Stops (if needed) and removes a container.
|
||||
# _get_container_logs <container_name> : Prints the logs for a container.
|
||||
# _check_required_env_vars "VAR1" ... : Checks if listed environment variables are set; calls _die() if any are missing.
|
||||
# _root_remove_tree <path> : Removes a path using a root Docker container (for permissions).
|
||||
|
||||
# ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
# Prints an error message in red and exits with status code 1.
|
||||
_die() {
|
||||
echo -e "\033[91mError: $1\033[0m"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Switches terminal output color to grey.
|
||||
_grey_start() {
|
||||
echo -e -n "\033[90m"
|
||||
}
|
||||
|
||||
# Resets terminal output color from grey.
|
||||
_grey_end() {
|
||||
echo -e -n "\033[0m"
|
||||
}
|
||||
|
||||
# Creates/starts a container, verifying it runs.
|
||||
_create_and_start_container() {
|
||||
if [ -z "$1" ] || [ -z "$2" ]; then
|
||||
_die "Template error: create_and_start_container <run_cmd> <container_name>"
|
||||
fi
|
||||
|
||||
local run_cmd="$1"
|
||||
local container_name="$2"
|
||||
|
||||
if _is_container_exists $container_name; then
|
||||
_is_container_running $container_name && return 0
|
||||
_start_container $container_name
|
||||
else
|
||||
_grey_start
|
||||
$run_cmd
|
||||
_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}"
|
||||
}
|
||||
|
||||
# Creates a directory if it doesn't exist (chmod 777).
|
||||
_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"
|
||||
}
|
||||
|
||||
# Checks if Docker is installed, running, and user has permission. Returns 1 on failure.
|
||||
_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
|
||||
}
|
||||
|
||||
# Checks if a container (any state) exists. Returns 1 if not found.
|
||||
_is_container_exists() {
|
||||
if ! docker ps -a --format "{{.Names}}" | grep -q "^$1$"; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Checks if a container is currently running. Returns 1 if not running.
|
||||
_is_container_running() {
|
||||
if ! docker ps --format "{{.Names}}" | grep -q "^$1$"; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Prints the ID of the named container.
|
||||
_get_container_id() {
|
||||
docker ps --format "{{.ID}}" --filter "name=$1"
|
||||
}
|
||||
|
||||
# Prints the status string of the named container.
|
||||
_get_container_status() {
|
||||
docker ps --format "{{.Status}}" --filter "name=$1"
|
||||
}
|
||||
|
||||
# Starts an existing, stopped container.
|
||||
_start_container() {
|
||||
_is_container_exists $1 || return 1
|
||||
_is_container_running $1 && return 0
|
||||
docker start $1
|
||||
}
|
||||
|
||||
# Stops a running container.
|
||||
_stop_container() {
|
||||
_is_container_running $1 || return 0;
|
||||
docker stop $1
|
||||
}
|
||||
|
||||
# Stops (if needed) and removes a container.
|
||||
_remove_container() {
|
||||
_stop_container $1
|
||||
_is_container_exists $1 || return 0;
|
||||
docker rm $1
|
||||
}
|
||||
|
||||
# Prints the logs for a container.
|
||||
_get_container_logs() {
|
||||
if ! _is_container_exists $1; then
|
||||
echo "Container $1 does not exist"
|
||||
return 1
|
||||
fi
|
||||
|
||||
docker logs $1
|
||||
}
|
||||
|
||||
# Checks if listed environment variables are set; calls _die() if any are missing.
|
||||
_check_required_env_vars() {
|
||||
local required_vars=("$@")
|
||||
for var in "${required_vars[@]}"; do
|
||||
if [ -z "${!var}" ]; then
|
||||
_die "Required environment variable $var is not set"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Removes a path using a root Docker container (for permissions).
|
||||
_root_remove_tree() {
|
||||
local to_remove="$1"
|
||||
parent=$(dirname "$to_remove")
|
||||
abs_parent=$(realpath "$parent")
|
||||
child=$(basename "$to_remove")
|
||||
docker run --rm -v "$abs_parent":/data alpine rm -rf "/data/$child"
|
||||
}
|
||||
|
||||
|
||||
# Load autocommands
|
||||
source "${AGENT_PATH}/_autocommands.sh"
|
25
source/agent/_nuke_other.sh
Executable file
25
source/agent/_nuke_other.sh
Executable file
@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
source "$SCRIPT_DIR/shared/_common.sh"
|
||||
|
||||
|
||||
A_SERVICE="$1"
|
||||
A_SERVICE_PATH="$2"
|
||||
|
||||
|
||||
# 1. Check if service directory exists on server
|
||||
[ -d "$A_SERVICE_PATH" ] || _die "Service is not installed: $A_SERVICE"
|
||||
|
||||
# uninstall the service
|
||||
if [ -f "$A_SERVICE_PATH/uninstall.sh" ]; then
|
||||
$A_SERVICE_PATH/uninstall.sh
|
||||
fi
|
||||
|
||||
# nuke the service
|
||||
if [ -f "$A_SERVICE_PATH/nuke.sh" ]; then
|
||||
$A_SERVICE_PATH/nuke.sh
|
||||
fi
|
||||
|
||||
# remove the service directory
|
||||
rm -rf "$A_SERVICE_PATH"
|
102
source/build.sh
Executable file
102
source/build.sh
Executable file
@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
JOBS=4
|
||||
# Determine number of CPU cores for parallel build
|
||||
if command -v nproc >/dev/null 2>&1; then
|
||||
JOBS=$(nproc)
|
||||
fi
|
||||
|
||||
|
||||
# Function to print status messages
|
||||
print_status() {
|
||||
echo -e "${GREEN}[*] $1${NC}"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[!] $1${NC}"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[!] $1${NC}"
|
||||
}
|
||||
|
||||
# Check if build directory exists, if not create it
|
||||
if [ ! -d "build" ]; then
|
||||
print_status "Creating build directory..."
|
||||
mkdir build
|
||||
fi
|
||||
|
||||
# Enter build directory
|
||||
cd build
|
||||
|
||||
# Check if CMake is installed
|
||||
if ! command -v cmake &> /dev/null; then
|
||||
print_error "CMake is not installed. Please install CMake first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if make is installed
|
||||
if ! command -v make &> /dev/null; then
|
||||
print_error "Make is not installed. Please install Make first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if pkg-config is installed
|
||||
if ! command -v pkg-config &> /dev/null; then
|
||||
print_error "pkg-config is not installed. Please install pkg-config first."
|
||||
print_warning "On Ubuntu/Debian: sudo apt-get install pkg-config"
|
||||
print_warning "On Fedora: sudo dnf install pkg-config"
|
||||
print_warning "On Arch: sudo pacman -S pkg-config"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if ncurses is installed
|
||||
if ! pkg-config --exists ncurses; then
|
||||
print_error "ncurses is not installed. Please install ncurses first."
|
||||
print_warning "On Ubuntu/Debian: sudo apt-get install libncurses-dev"
|
||||
print_warning "On Fedora: sudo dnf install ncurses-devel"
|
||||
print_warning "On Arch: sudo pacman -S ncurses"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Configure with CMake
|
||||
print_status "Configuring with CMake..."
|
||||
cmake .. -DCMAKE_BUILD_TYPE=Debug
|
||||
#cmake .. -DCMAKE_BUILD_TYPE=Release
|
||||
|
||||
# Build the project
|
||||
print_status "Building project..."
|
||||
make -j"$JOBS"
|
||||
|
||||
# Check if build was successful
|
||||
if [ $? -eq 0 ]; then
|
||||
print_status "Build successful!"
|
||||
print_status "Binary location: $(pwd)/dropshell"
|
||||
else
|
||||
print_error "Build failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_status "Auto-installing dropshell..."
|
||||
sudo make install
|
||||
if [ $? -eq 0 ]; then
|
||||
print_status "Installation successful!"
|
||||
else
|
||||
print_error "Installation failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
# Return to original directory
|
||||
cd ..
|
||||
|
||||
print_status "Build process completed!"
|
150
source/install_build_prerequisites.sh
Executable file
150
source/install_build_prerequisites.sh
Executable file
@ -0,0 +1,150 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to print status messages
|
||||
print_status() {
|
||||
echo -e "${GREEN}[*] $1${NC}"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[!] $1${NC}"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[!] $1${NC}"
|
||||
}
|
||||
|
||||
# Check if running as root
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
print_error "Please run this script as root (use sudo)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Detect distribution
|
||||
if [ -f /etc/os-release ]; then
|
||||
. /etc/os-release
|
||||
OS=$NAME
|
||||
VER=$VERSION_ID
|
||||
else
|
||||
print_error "Could not detect distribution"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_status "Detected OS: $OS $VER"
|
||||
|
||||
# Define packages based on distribution
|
||||
case $OS in
|
||||
"Ubuntu"|"Debian GNU/Linux")
|
||||
# Common packages for both Ubuntu and Debian
|
||||
PACKAGES="cmake make g++ devscripts debhelper"
|
||||
;;
|
||||
*)
|
||||
print_error "Unsupported distribution: $OS"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Function to check if a package is installed
|
||||
is_package_installed() {
|
||||
dpkg -l "$1" 2>/dev/null | grep -q "^ii"
|
||||
}
|
||||
|
||||
# Update package lists
|
||||
print_status "Updating package lists..."
|
||||
apt-get update
|
||||
|
||||
# Install missing packages
|
||||
print_status "Checking and installing required packages..."
|
||||
for pkg in $PACKAGES; do
|
||||
if ! is_package_installed "$pkg"; then
|
||||
print_status "Installing $pkg..."
|
||||
apt-get install -y "$pkg"
|
||||
if [ $? -ne 0 ]; then
|
||||
print_error "Failed to install $pkg"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
print_status "$pkg is already installed"
|
||||
fi
|
||||
done
|
||||
|
||||
# Verify all required tools are installed
|
||||
print_status "Verifying installation..."
|
||||
for tool in cmake make g++; do
|
||||
if ! command -v "$tool" &> /dev/null; then
|
||||
print_error "$tool is not installed properly"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
|
||||
# Install other required packages
|
||||
apt install -y musl-tools wget tar
|
||||
|
||||
# Set install directory
|
||||
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
|
||||
USER_HOME=$(eval echo ~$SUDO_USER)
|
||||
else
|
||||
USER_HOME="$HOME"
|
||||
fi
|
||||
INSTALL_DIR="$USER_HOME/.musl-cross"
|
||||
mkdir -p "$INSTALL_DIR"
|
||||
|
||||
MUSL_CC_URL="https://musl.cc"
|
||||
TMPDIR=$(mktemp -d)
|
||||
trap 'rm -rf "$TMPDIR"' EXIT
|
||||
|
||||
# x86_64
|
||||
if [ ! -d "$INSTALL_DIR/x86_64-linux-musl-cross" ]; then
|
||||
echo "Downloading x86_64 musl cross toolchain..."
|
||||
wget -nc -O "$TMPDIR/x86_64-linux-musl-cross.tgz" $MUSL_CC_URL/x86_64-linux-musl-cross.tgz
|
||||
tar -C "$INSTALL_DIR" -xvf "$TMPDIR/x86_64-linux-musl-cross.tgz"
|
||||
fi
|
||||
|
||||
# aarch64
|
||||
if [ ! -d "$INSTALL_DIR/aarch64-linux-musl-cross" ]; then
|
||||
echo "Downloading aarch64 musl cross toolchain..."
|
||||
wget -nc -O "$TMPDIR/aarch64-linux-musl-cross.tgz" $MUSL_CC_URL/aarch64-linux-musl-cross.tgz
|
||||
tar -C "$INSTALL_DIR" -xvf "$TMPDIR/aarch64-linux-musl-cross.tgz"
|
||||
fi
|
||||
|
||||
# Print instructions for adding to PATH
|
||||
# cat <<EOF
|
||||
|
||||
# To use the musl cross compilers, add the following to your shell:
|
||||
# export PATH="$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:$PATH"
|
||||
|
||||
# Or run:
|
||||
# export PATH="$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:\$PATH"
|
||||
|
||||
# EOF
|
||||
|
||||
# Clean up
|
||||
rm -rf "$TMPDIR"
|
||||
|
||||
# If run with sudo, add to invoking user's ~/.bashrc
|
||||
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
|
||||
BASHRC="$USER_HOME/.bashrc"
|
||||
EXPORT_LINE="export PATH=\"$INSTALL_DIR/x86_64-linux-musl-cross/bin:$INSTALL_DIR/aarch64-linux-musl-cross/bin:\$PATH\""
|
||||
if ! grep -Fxq "$EXPORT_LINE" "$BASHRC"; then
|
||||
echo "" >> "$BASHRC"
|
||||
echo "# Add musl cross compilers to PATH for bb64" >> "$BASHRC"
|
||||
echo "$EXPORT_LINE" >> "$BASHRC"
|
||||
echo "Added musl cross compilers to $BASHRC"
|
||||
else
|
||||
echo "musl cross compiler PATH already present in $BASHRC"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
print_status "All dependencies installed successfully!"
|
||||
print_status "You can now run ./build.sh to build the project"
|
19
source/make_createagent.sh
Executable file
19
source/make_createagent.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# This script creates two files:
|
||||
# src/utils/createagent.hpp
|
||||
# src/utils/createagent.cpp
|
||||
#
|
||||
|
||||
SCRIPT_DIR=$(dirname "$0")
|
||||
|
||||
# check if dehydrate is installed
|
||||
if ! command -v dehydrate &> /dev/null; then
|
||||
echo "dehydrate could not be found - installing"
|
||||
curl -fsSL https://gitea.jde.nz/public/dehydrate/releases/download/latest/install.sh | bash
|
||||
fi
|
||||
|
||||
SCRIPT_DIR=$(dirname "$0")
|
||||
|
||||
dehydrate "${SCRIPT_DIR}/agent" "${SCRIPT_DIR}/src/autogen"
|
52
source/multibuild.sh
Executable file
52
source/multibuild.sh
Executable file
@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
|
||||
# build amd64 and arm64 versions of dropshell, to:
|
||||
# build/dropshell.amd64
|
||||
# build/dropshell.arm64
|
||||
|
||||
set -e
|
||||
|
||||
rm -f build_amd64/dropshell build_arm64/dropshell build/dropshell.amd64 build/dropshell.arm64
|
||||
|
||||
# Determine number of CPU cores for parallel build
|
||||
if command -v nproc >/dev/null 2>&1; then
|
||||
JOBS=$(nproc)
|
||||
else
|
||||
JOBS=4 # fallback default
|
||||
fi
|
||||
|
||||
# Build for amd64 (musl)
|
||||
echo "Building for amd64 (musl)..."
|
||||
cmake -B build_amd64 -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_C_COMPILER=x86_64-linux-musl-gcc \
|
||||
-DCMAKE_CXX_COMPILER=x86_64-linux-musl-g++ \
|
||||
-DCMAKE_EXE_LINKER_FLAGS="-static" \
|
||||
-DCMAKE_CXX_FLAGS="-march=x86-64" .
|
||||
cmake --build build_amd64 --target dropshell --config Release -j"$JOBS"
|
||||
mkdir -p build
|
||||
cp build_amd64/dropshell build/dropshell.amd64
|
||||
|
||||
# Build for arm64 (musl)
|
||||
echo "Building for arm64 (musl)..."
|
||||
cmake -B build_arm64 -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_C_COMPILER=aarch64-linux-musl-gcc \
|
||||
-DCMAKE_CXX_COMPILER=aarch64-linux-musl-g++ \
|
||||
-DCMAKE_EXE_LINKER_FLAGS="-static" \
|
||||
-DCMAKE_CXX_FLAGS="-march=armv8-a" \
|
||||
-DCMAKE_SYSTEM_PROCESSOR=aarch64 .
|
||||
cmake --build build_arm64 --target dropshell --config Release -j"$JOBS"
|
||||
mkdir -p build
|
||||
cp build_arm64/dropshell build/dropshell.arm64
|
||||
|
||||
if [ ! -f build/dropshell.amd64 ]; then
|
||||
echo "build/dropshell.amd64 not found!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f build/dropshell.arm64 ]; then
|
||||
echo "build/dropshell.arm64 not found!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Builds complete:"
|
||||
ls -lh build/dropshell.*
|
95
source/publish.sh
Executable file
95
source/publish.sh
Executable file
@ -0,0 +1,95 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
||||
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
|
||||
TOKEN="$GITEA_TOKEN_DEPLOY"
|
||||
elif [ -n "$GITEA_TOKEN" ]; then
|
||||
TOKEN="$GITEA_TOKEN"
|
||||
else
|
||||
echo "GITEA_TOKEN_DEPLOY or GITEA_TOKEN environment variable not set!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
./multibuild.sh
|
||||
|
||||
if [ ! -f "build/dropshell.amd64" ]; then
|
||||
echo "build/dropshell.amd64 not found!" >&2
|
||||
echo "Please run multibuild.sh first." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "build/dropshell.arm64" ]; then
|
||||
echo "build/dropshell.arm64 not found!" >&2
|
||||
echo "Please run multibuild.sh first." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TAG=$(./build/dropshell.amd64 --version)
|
||||
|
||||
echo "Publishing dropshell version $TAG"
|
||||
|
||||
# make sure we've commited.
|
||||
git add . && git commit -m "dropshell release $TAG" && git push
|
||||
|
||||
|
||||
# Find repo info from .git/config
|
||||
REPO_URL=$(git config --get remote.origin.url)
|
||||
if [[ ! $REPO_URL =~ gitea ]]; then
|
||||
echo "Remote origin is not a Gitea repository: $REPO_URL" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Extract base URL, owner, and repo
|
||||
# Example: https://gitea.example.com/username/reponame.git
|
||||
BASE_URL=$(echo "$REPO_URL" | sed -E 's#(https?://[^/]+)/.*#\1#')
|
||||
OWNER=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)/[^/]+(\.git)?$#\1#')
|
||||
REPO=$(echo "$REPO_URL" | sed -E 's#.*/([^/]+)(\.git)?$#\1#')
|
||||
|
||||
API_URL="$BASE_URL/api/v1/repos/$OWNER/$REPO"
|
||||
|
||||
# Create release
|
||||
RELEASE_DATA=$(cat <<EOF
|
||||
{
|
||||
"tag_name": "$TAG",
|
||||
"name": "$TAG",
|
||||
"body": "dropshell release $TAG",
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
RELEASE_ID=$(curl -s -X POST "$API_URL/releases" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
-d "$RELEASE_DATA" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||
|
||||
if [ -z "$RELEASE_ID" ]; then
|
||||
echo "Failed to create release on Gitea." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Upload binaries and install.sh
|
||||
for FILE in dropshell.amd64 dropshell.arm64 install.sh server_autosetup.sh; do
|
||||
if [ -f "build/$FILE" ]; then
|
||||
filetoupload="build/$FILE"
|
||||
elif [ -f "$FILE" ]; then
|
||||
filetoupload="$FILE"
|
||||
else
|
||||
echo "File $FILE not found!" >&2
|
||||
continue
|
||||
fi
|
||||
|
||||
# Auto-detect content type
|
||||
ctype=$(file --mime-type -b "$filetoupload")
|
||||
|
||||
curl -s -X POST "$API_URL/releases/$RELEASE_ID/assets?name=$FILE" \
|
||||
-H "Content-Type: $ctype" \
|
||||
-H "Authorization: token $TOKEN" \
|
||||
--data-binary @"$filetoupload"
|
||||
echo "Uploaded $FILE to release $TAG as $ctype."
|
||||
done
|
||||
|
||||
echo "Published dropshell version $TAG to $REPO_URL (tag $TAG) with binaries."
|
120
source/src/autocomplete.cpp
Normal file
120
source/src/autocomplete.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
#include "autocomplete.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "config.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
|
||||
#include <assert.hpp>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
namespace autocomplete {
|
||||
|
||||
const std::set<std::string> system_commands_noargs = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"};
|
||||
const std::set<std::string> system_commands_always_available = {"help","edit"};
|
||||
const std::set<std::string> system_commands_require_config = {"server","templates","create-service","create-template","create-server","ssh","list"};
|
||||
const std::set<std::string> system_commands_hidden = {"nuke","_allservicesstatus"};
|
||||
|
||||
const std::set<std::string> service_commands_require_config = {"ssh","edit","nuke","_allservicesstatus"};
|
||||
|
||||
void merge_commands(std::set<std::string> &commands, const std::set<std::string> &new_commands)
|
||||
{
|
||||
commands.insert(new_commands.begin(), new_commands.end());
|
||||
}
|
||||
|
||||
bool is_no_arg_cmd(std::string cmd)
|
||||
{
|
||||
return system_commands_noargs.find(cmd) != system_commands_noargs.end();
|
||||
}
|
||||
|
||||
bool autocomplete(const std::vector<std::string> &args)
|
||||
{
|
||||
if (args.size() < 3) // dropshell autocomplete ???
|
||||
{
|
||||
autocomplete_list_commands();
|
||||
return true;
|
||||
}
|
||||
|
||||
ASSERT(args.size() >= 3, "Invalid number of arguments");
|
||||
std::string cmd = args[2];
|
||||
|
||||
// std::cout<<" cmd = ["<<cmd<<"]"<<std::endl;
|
||||
|
||||
if (cmd=="hash")
|
||||
{ // output files and folders in the current directory, one per line.
|
||||
std::filesystem::directory_iterator dir_iter(std::filesystem::current_path());
|
||||
for (const auto& entry : dir_iter)
|
||||
std::cout << entry.path().filename().string() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (autocomplete::is_no_arg_cmd(cmd))
|
||||
return true; // no arguments needed.
|
||||
|
||||
if (!dropshell::gConfig().is_config_set())
|
||||
return false; // can't help without working config.
|
||||
|
||||
if (args.size()==3) // we have the command but nothing else. dropshell autocomplete command <server>
|
||||
{
|
||||
auto servers = dropshell::get_configured_servers();
|
||||
for (const auto& server : servers)
|
||||
std::cout << server.name << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.size()==4) // we have the command and the server. dropshell autocomplete command server <service>
|
||||
{
|
||||
std::string server = args[3];
|
||||
|
||||
if (cmd=="create-service")
|
||||
{ // create-service <server> <template> <service>
|
||||
auto templates = dropshell::gTemplateManager().get_template_list();
|
||||
for (const auto& t : templates)
|
||||
std::cout << t << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
auto services = dropshell::get_server_services_info(server);
|
||||
for (const auto& service : services)
|
||||
std::cout << service.service_name << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (args.size()==5) // we have the command and the server and the service. dropshell autocomplete command server service_name <command?>
|
||||
{
|
||||
std::string server_name = args[3];
|
||||
std::string service_name = args[4];
|
||||
if (cmd=="restore")
|
||||
{
|
||||
std::set<std::string> backups = dropshell::list_backups(server_name, service_name);
|
||||
for (auto backup : backups)
|
||||
std::cout << backup << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // no more autocompletion possible - don't know what the argument is for.
|
||||
}
|
||||
|
||||
// args>5 - no more autocompletion possible - don't know what the argument is for.
|
||||
return false; // catch-all.
|
||||
}
|
||||
|
||||
bool autocomplete_list_commands()
|
||||
{
|
||||
std::set<std::string> commands;
|
||||
dropshell::get_all_used_commands(commands);
|
||||
|
||||
// add in commmands hard-coded and handled in main
|
||||
autocomplete::merge_commands(commands, autocomplete::system_commands_always_available);
|
||||
|
||||
if (dropshell::gConfig().is_config_set())
|
||||
autocomplete::merge_commands(commands, autocomplete::system_commands_require_config);
|
||||
|
||||
for (const auto& command : commands) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace autocomplete
|
28
source/src/autocomplete.hpp
Normal file
28
source/src/autocomplete.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef AUTOCOMPLETE_HPP
|
||||
#define AUTOCOMPLETE_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
namespace autocomplete {
|
||||
|
||||
extern const std::set<std::string> system_commands_noargs;
|
||||
extern const std::set<std::string> system_commands_always_available;
|
||||
extern const std::set<std::string> system_commands_require_config;
|
||||
extern const std::set<std::string> system_commands_hidden;
|
||||
|
||||
extern const std::set<std::string> service_commands_require_config;
|
||||
|
||||
void merge_commands(std::set<std::string> &commands, const std::set<std::string> &new_commands);
|
||||
|
||||
bool is_no_arg_cmd(std::string cmd);
|
||||
|
||||
bool autocomplete(const std::vector<std::string> &args);
|
||||
|
||||
bool autocomplete_list_commands();
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
1111
source/src/autogen/_agent.cpp
Normal file
1111
source/src/autogen/_agent.cpp
Normal file
File diff suppressed because it is too large
Load Diff
15
source/src/autogen/_agent.hpp
Normal file
15
source/src/autogen/_agent.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
|
||||
THIS FILE IS AUTO-GENERATED BY DEHYDRATE.
|
||||
DO NOT EDIT THIS FILE.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <string>
|
||||
namespace recreate_agent {
|
||||
bool recreate_tree(std::string destination_folder);
|
||||
}
|
15
source/src/autogen/version.hpp
Normal file
15
source/src/autogen/version.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
// DUMMY VERSION - replaced by build process.
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// Version information
|
||||
const std::string VERSION = "DEV";
|
||||
const std::string RELEASE_DATE = "NEVER";
|
||||
const std::string AUTHOR = "j842";
|
||||
const std::string LICENSE = "MIT";
|
||||
|
||||
} // namespace dropshell
|
75
source/src/commands/command_registry.cpp
Normal file
75
source/src/commands/command_registry.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
CommandRegistry& CommandRegistry::instance() {
|
||||
static CommandRegistry reg;
|
||||
return reg;
|
||||
}
|
||||
|
||||
void CommandRegistry::register_command(const CommandInfo& info) {
|
||||
auto ptr = std::make_shared<CommandInfo>(info);
|
||||
for (const auto& name : info.names) {
|
||||
command_map_[name] = ptr;
|
||||
}
|
||||
all_commands_.push_back(ptr);
|
||||
}
|
||||
|
||||
const CommandInfo* CommandRegistry::find_command(const std::string& name) const {
|
||||
auto it = command_map_.find(name);
|
||||
if (it != command_map_.end()) return it->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandRegistry::list_commands(bool include_hidden) const {
|
||||
std::set<std::string> out;
|
||||
for (const auto& cmd : all_commands_) {
|
||||
if (!cmd->hidden || include_hidden) {
|
||||
for (const auto& name : cmd->names) out.insert(name);
|
||||
}
|
||||
}
|
||||
return std::vector<std::string>(out.begin(), out.end());
|
||||
}
|
||||
|
||||
std::vector<std::string> CommandRegistry::list_primary_commands(bool include_hidden) const {
|
||||
std::set<std::string> out;
|
||||
for (const auto& cmd : all_commands_) {
|
||||
if (!cmd->hidden || include_hidden) {
|
||||
if (cmd->names.size() > 0)
|
||||
{
|
||||
if (cmd->requires_config && !gConfig().is_config_set())
|
||||
continue;
|
||||
if (cmd->requires_install && !gConfig().is_agent_installed())
|
||||
continue;
|
||||
out.insert(cmd->names[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return std::vector<std::string>(out.begin(), out.end());
|
||||
}
|
||||
|
||||
|
||||
void CommandRegistry::autocomplete(const CommandContext& ctx) const {
|
||||
// dropshell autocomplete <command> <arg> <arg> ...
|
||||
if (ctx.args.size() < 1) {
|
||||
for (const auto& name : list_primary_commands(false)) {
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ctx command is autocomplete, so recreate ctx with the first arg removed
|
||||
CommandContext childcontext = {
|
||||
ctx.exename,
|
||||
ctx.args[0],
|
||||
std::vector<std::string>(ctx.args.begin() + 1, ctx.args.end())
|
||||
};
|
||||
auto* info = find_command(childcontext.command);
|
||||
if (info && info->autocomplete) {
|
||||
info->autocomplete(childcontext);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
60
source/src/commands/command_registry.hpp
Normal file
60
source/src/commands/command_registry.hpp
Normal file
@ -0,0 +1,60 @@
|
||||
#ifndef COMMAND_REGISTRY_HPP
|
||||
#define COMMAND_REGISTRY_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
struct CommandContext {
|
||||
std::string exename;
|
||||
std::string command;
|
||||
std::vector<std::string> args;
|
||||
|
||||
// Add more fields as needed (e.g., config pointer, output stream, etc.)
|
||||
};
|
||||
|
||||
struct CommandInfo {
|
||||
std::vector<std::string> names;
|
||||
std::function<int(const CommandContext&)> handler;
|
||||
std::function<void(const CommandContext&)> autocomplete; // optional
|
||||
bool hidden = false;
|
||||
bool requires_config = true;
|
||||
bool requires_install = true;
|
||||
int min_args = 0;
|
||||
int max_args = -1; // -1 = unlimited
|
||||
std::string help_usage; // install SERVER [SERVICE]
|
||||
std::string help_description; // Install/reinstall/update service(s). Safe/non-destructive.
|
||||
std::string full_help; // detailed help for the command
|
||||
};
|
||||
|
||||
class CommandRegistry {
|
||||
public:
|
||||
static CommandRegistry& instance();
|
||||
|
||||
void register_command(const CommandInfo& info);
|
||||
|
||||
// Returns nullptr if not found
|
||||
const CommandInfo* find_command(const std::string& name) const;
|
||||
|
||||
// List all commands (optionally including hidden)
|
||||
std::vector<std::string> list_commands(bool include_hidden = false) const;
|
||||
std::vector<std::string> list_primary_commands(bool include_hidden = false) const;
|
||||
|
||||
// For autocomplete
|
||||
void autocomplete(const CommandContext& ctx) const;
|
||||
|
||||
private:
|
||||
CommandRegistry() = default;
|
||||
std::map<std::string, std::shared_ptr<CommandInfo>> command_map_;
|
||||
std::vector<std::shared_ptr<CommandInfo>> all_commands_;
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
#endif // COMMAND_REGISTRY_HPP
|
180
source/src/commands/edit.cpp
Normal file
180
source/src/commands/edit.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int edit_handler(const CommandContext& ctx);
|
||||
|
||||
static std::vector<std::string> edit_name_list={"edit"};
|
||||
|
||||
// Static registration
|
||||
struct EditCommandRegister {
|
||||
EditCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
edit_name_list,
|
||||
edit_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"edit [SERVER] [SERVICE]",
|
||||
"Edit dropshell, server or service configuration",
|
||||
// heredoc
|
||||
R"(
|
||||
Edit dropshell, server or service configuration.
|
||||
edit edit the dropshell config.
|
||||
edit <server> edit the server config.
|
||||
edit <server> <service> edit the service config.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} edit_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// utility function to edit a file
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool edit_file(const std::string &file_path, bool has_bb64)
|
||||
{
|
||||
// make sure parent directory exists.
|
||||
std::string parent_dir = get_parent(file_path);
|
||||
std::filesystem::create_directories(parent_dir);
|
||||
|
||||
std::string editor_cmd;
|
||||
const char* editor_env = std::getenv("EDITOR");
|
||||
|
||||
if (editor_env && std::strlen(editor_env) > 0) {
|
||||
editor_cmd = std::string(editor_env) + " " + quote(file_path);
|
||||
} else if (isatty(STDIN_FILENO)) {
|
||||
// Check if stdin is connected to a terminal if EDITOR is not set
|
||||
editor_cmd = "nano -w " + quote(file_path);
|
||||
} else {
|
||||
std::cerr << "Error: Standard input is not a terminal and EDITOR environment variable is not set." << std::endl;
|
||||
std::cerr << "Try setting the EDITOR environment variable (e.g., export EDITOR=nano) or run in an interactive terminal." << std::endl;
|
||||
std::cerr << "You can manually edit the file at: " << file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "Editing file: " << file_path << std::endl;
|
||||
|
||||
if (has_bb64) {
|
||||
return execute_local_command(editor_cmd, nullptr, cMode::Interactive);
|
||||
}
|
||||
else {
|
||||
// might not have bb64 at this early stage. Direct edit.
|
||||
int ret = system(editor_cmd.c_str());
|
||||
return EXITSTATUSCHECK(ret);
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit config
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_config()
|
||||
{
|
||||
if (!gConfig().is_config_set())
|
||||
gConfig().save_config(false); // save defaults.
|
||||
|
||||
std::string config_file = localfile::dropshell_json();
|
||||
if (!edit_file(config_file, false) || !std::filesystem::exists(config_file))
|
||||
return die("Error: Failed to edit config file.");
|
||||
|
||||
gConfig().load_config();
|
||||
if (!gConfig().is_config_set())
|
||||
return die("Error: Failed to load and parse edited config file!");
|
||||
|
||||
gConfig().save_config(true);
|
||||
|
||||
std::cout << "Successfully edited config file at " << config_file << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit server
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_server(const std::string &server_name)
|
||||
{
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
if (serverpath.empty()) {
|
||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::ostringstream aftertext;
|
||||
aftertext << "If you have changed DROPSHELL_DIR, you should manually move the files to the new location NOW.\n"
|
||||
<< "You can ssh in to the remote server with: dropshell ssh "<<server_name<<"\n"
|
||||
<< "Once moved, reinstall all services with: dropshell install " << server_name;
|
||||
|
||||
std::string config_file = serverpath + "/server.env";
|
||||
if (!edit_file(config_file, true)) {
|
||||
std::cerr << "Error: Failed to edit server.env" << std::endl;
|
||||
std::cerr << "You can manually edit this file at: " << config_file << std::endl;
|
||||
std::cerr << "After editing, " << aftertext.str() << std::endl;
|
||||
}
|
||||
else
|
||||
std::cout << aftertext.str() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit service config
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_service_config(const std::string &server, const std::string &service)
|
||||
{
|
||||
std::string config_file = localfile::service_env(server, service);
|
||||
if (!std::filesystem::exists(config_file))
|
||||
{
|
||||
std::cerr << "Error: Service config file not found: " << config_file << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (edit_file(config_file, true) && std::filesystem::exists(config_file))
|
||||
std::cout << "To apply your changes, run:\n dropshell install " + server + " " + service << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// edit command handler
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int edit_handler(const CommandContext& ctx) {
|
||||
// edit dropshell config
|
||||
if (ctx.args.size() < 1)
|
||||
return edit_config();
|
||||
|
||||
// edit server config
|
||||
if (ctx.args.size() < 2) {
|
||||
edit_server(safearg(ctx.args,0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
// edit service config
|
||||
if (ctx.args.size() < 3) {
|
||||
edit_service_config(safearg(ctx.args,0), safearg(ctx.args,1));
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "Edit handler called with " << ctx.args.size() << " args\n";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
138
source/src/commands/health.cpp
Normal file
138
source/src/commands/health.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "transwarp.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int health_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> health_name_list = {"health", "check", "healthcheck", "status"};
|
||||
|
||||
// Static registration
|
||||
struct HealthCommandRegister
|
||||
{
|
||||
HealthCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({health_name_list,
|
||||
health_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
1, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"health SERVER",
|
||||
"Check the health of a server.",
|
||||
R"(
|
||||
health <server>
|
||||
)"});
|
||||
}
|
||||
} health_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// health command implementation
|
||||
|
||||
HealthStatus is_healthy(const std::string &server, const std::string &service)
|
||||
{
|
||||
server_env_manager env(server);
|
||||
if (!env.is_valid())
|
||||
{
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return HealthStatus::ERROR;
|
||||
}
|
||||
|
||||
if (!env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
{
|
||||
return HealthStatus::NOTINSTALLED;
|
||||
}
|
||||
|
||||
std::string script_path = remotepath::service_template(server, service) + "/status.sh";
|
||||
if (!env.check_remote_file_exists(script_path))
|
||||
{
|
||||
return HealthStatus::UNKNOWN;
|
||||
}
|
||||
|
||||
// Run status script, does not display output.
|
||||
if (!env.run_remote_template_command(service, "status", {}, true, {}))
|
||||
return HealthStatus::UNHEALTHY;
|
||||
return HealthStatus::HEALTHY;
|
||||
}
|
||||
|
||||
std::string healthtick(const std::string &server, const std::string &service)
|
||||
{
|
||||
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[37m✓\033[0m";
|
||||
|
||||
HealthStatus status = is_healthy(server, service);
|
||||
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 HealthStatus2String(HealthStatus status)
|
||||
{
|
||||
if (status == HealthStatus::HEALTHY)
|
||||
return ":tick:";
|
||||
else if (status == HealthStatus::UNHEALTHY)
|
||||
return ":cross:";
|
||||
else if (status == HealthStatus::UNKNOWN)
|
||||
return ":greytick:";
|
||||
else if (status == HealthStatus::NOTINSTALLED)
|
||||
return ":warning:";
|
||||
else
|
||||
return ":error:";
|
||||
}
|
||||
|
||||
std::string healthmark(const std::string &server, const std::string &service)
|
||||
{
|
||||
HealthStatus status = is_healthy(server, service);
|
||||
return HealthStatus2String(status);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// health command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int health_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{
|
||||
std::cerr << "Error: Server name is required" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1) {
|
||||
// get all services on server
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
transwarp::parallel exec{services.size()};
|
||||
auto task = transwarp::for_each(exec, services.begin(), services.end(), [&](const LocalServiceInfo& service) {
|
||||
std::string status = healthtick(server, service.service_name);
|
||||
std::cout << status << " " << service.service_name << " (" << service.template_name << ")" << std::endl << std::flush;
|
||||
});
|
||||
task->wait();
|
||||
return 0;
|
||||
} else {
|
||||
// get service status
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
std::cout << healthtick(server, service) << " " << service << " (" << service_info.template_name << ")" << std::endl << std::flush;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
178
source/src/commands/help.cpp
Normal file
178
source/src/commands/help.cpp
Normal file
@ -0,0 +1,178 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
void help_autocomplete(const CommandContext& ctx);
|
||||
int help_handler(const CommandContext& ctx);
|
||||
|
||||
static std::vector<std::string> help_name_list={"help","h","--help","-h"};
|
||||
|
||||
// Static registration
|
||||
struct HelpCommandRegister {
|
||||
HelpCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
help_name_list,
|
||||
help_handler,
|
||||
help_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
1, // max_args (after command)
|
||||
"help [COMMAND]",
|
||||
"Show help for dropshell, or detailed help for a specific command.",
|
||||
// heredoc
|
||||
R"(
|
||||
Show help for dropshell, or detailed help for a specific command.
|
||||
If you want to see documentation, please visit the DropShell website:
|
||||
https://dropshell.app
|
||||
)"
|
||||
});
|
||||
}
|
||||
} help_command_register;
|
||||
|
||||
|
||||
void help_autocomplete(const CommandContext& ctx) {
|
||||
if (ctx.args.size() == 1) {
|
||||
// list all commands
|
||||
for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) {
|
||||
std::cout << cmd << std::endl;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void show_command(const std::string& cmd) {
|
||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
if (!cmd_info)
|
||||
std::cout << "Unknown command: " << cmd << std::endl;
|
||||
|
||||
std::cout << " ";
|
||||
print_left_aligned(cmd_info->help_usage, 30);
|
||||
std::cout << cmd_info->help_description << std::endl;
|
||||
}
|
||||
|
||||
extern const std::string VERSION;
|
||||
extern const std::string RELEASE_DATE;
|
||||
extern const std::string AUTHOR;
|
||||
extern const std::string LICENSE;
|
||||
|
||||
|
||||
int show_command_help(const std::string& cmd) {
|
||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
if (!cmd_info)
|
||||
{
|
||||
std::cout << "Unknown command: " << cmd << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "Usage: " << std::endl;
|
||||
std::cout << " ";
|
||||
print_left_aligned(cmd_info->help_usage, 30);
|
||||
std::cout << cmd_info->help_description << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << " Equivalent names: ";
|
||||
bool first = true;
|
||||
for (const auto& name : cmd_info->names) {
|
||||
if (!first) std::cout << ", ";
|
||||
std::cout << name;
|
||||
first = false;
|
||||
}
|
||||
std::cout << std::endl << std::endl;
|
||||
|
||||
std::cout << cmd_info->full_help << std::endl << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int help_handler(const CommandContext& ctx) {
|
||||
|
||||
if (ctx.args.size() > 0)
|
||||
return show_command_help(ctx.args[0]);
|
||||
|
||||
std::cout << std::endl;
|
||||
maketitle("DropShell version " + VERSION);
|
||||
std::cout << std::endl;
|
||||
std::cout << "A tool for managing remote servers, by " << AUTHOR << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "dropshell ..." << std::endl;
|
||||
|
||||
show_command("help");
|
||||
show_command("edit");
|
||||
|
||||
if (gConfig().is_config_set())
|
||||
{
|
||||
// show more!
|
||||
show_command("list");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// void show_command(const std::string& cmd) {
|
||||
// const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||
// if (cmd_info) {
|
||||
// std::cout << " " << cmd_info->help_usage
|
||||
// << std::string(' ', std::min(1,(int)(30-cmd_info->help_usage.length())))
|
||||
// << cmd_info->help_description << std::endl;
|
||||
// }
|
||||
// }
|
||||
|
||||
// bool print_help() {
|
||||
// std::cout << std::endl;
|
||||
// maketitle("DropShell version " + VERSION);
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "A tool for managing server configurations" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "dropshell ..." << std::endl;
|
||||
// show_command("help");
|
||||
// show_command("edit");
|
||||
|
||||
// if (gConfig().is_config_set()) {
|
||||
// std::cout << " templates List all available templates" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl;
|
||||
// std::cout << " list [SERVER] [SERVICE] List status/details of all servers/server/service." << std::endl;
|
||||
// std::cout << " edit [SERVER] [SERVICE] Edit the configuration of dropshell/server/service." << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Safe/non-destructive." << std::endl;
|
||||
// std::cout << " uninstall SERVER [SERVICE] Uninstalls the service on the remote server. Leaves data intact." << std::endl;
|
||||
// std::cout << " nuke SERVER SERVICE Nuke the service, deleting ALL local and remote data." << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s), e.g." << std::endl;
|
||||
// std::cout << " backup, restore, start, stop, logs" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << " ssh SERVER SERVICE Launch an interactive shell on a server or service" << std::endl;
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Creation commands: (apply to the first local config directory)"<<std::endl;
|
||||
// std::cout << " create-template TEMPLATE" << std::endl;
|
||||
// std::cout << " create-server SERVER" << std::endl;
|
||||
// std::cout << " create-service SERVER TEMPLATE SERVICE" << std::endl;
|
||||
// }
|
||||
// else {
|
||||
// show_command("help");
|
||||
// show_command("edit");
|
||||
// std::cout << std::endl;
|
||||
// std::cout << "Other commands available once initialised." << std::endl;
|
||||
// }
|
||||
// return true;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
381
source/src/commands/install.cpp
Normal file
381
source/src/commands/install.cpp
Normal file
@ -0,0 +1,381 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
#include "autogen/_agent.hpp"
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int install_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> install_name_list = {"install", "reinstall", "update"};
|
||||
|
||||
// Static registration
|
||||
struct InstallCommandRegister
|
||||
{
|
||||
InstallCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({install_name_list,
|
||||
install_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"install SERVER [SERVICE]",
|
||||
"Install/reinstall service(s). Safe/non-destructive way to update.",
|
||||
// heredoc
|
||||
R"(
|
||||
Install components on a server. This is safe to re-run (non-destructive) and used to update
|
||||
servers and their services.
|
||||
|
||||
install (re)install dropshell components on this computer.
|
||||
install SERVER (re)install dropshell agent on the given server.
|
||||
install SERVER [SERVICE|*] (re)install the given service (or all services) on the given server.
|
||||
|
||||
Note you need to create the service first with:
|
||||
dropshell create-service <server> <template> <service>
|
||||
)"});
|
||||
}
|
||||
} install_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// rsync_tree_to_remote : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool rsync_tree_to_remote(
|
||||
const std::string &local_path,
|
||||
const std::string &remote_path,
|
||||
server_env_manager &server_env,
|
||||
bool silent)
|
||||
{
|
||||
ASSERT(!local_path.empty() && !remote_path.empty(), "Local or remote path not specified. Can't rsync.");
|
||||
|
||||
std::string rsync_cmd = "rsync --delete --mkpath -zrpc -e 'ssh -p " + server_env.get_SSH_PORT() + "' " +
|
||||
quote(local_path + "/") + " " +
|
||||
quote(server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" +
|
||||
remote_path + "/");
|
||||
return execute_local_command(rsync_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// install service over ssh : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool install_service(const std::string &server, const std::string &service, bool silent)
|
||||
{
|
||||
LocalServiceInfo service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
return false;
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
return false;
|
||||
|
||||
maketitle("Installing " + service + " (" + service_info.template_name + ") on " + server);
|
||||
|
||||
if (!server_env.is_valid())
|
||||
return false; // should never hit this.
|
||||
|
||||
// Check if template exists
|
||||
template_info tinfo = gTemplateManager().get_template_info(service_info.template_name);
|
||||
if (!tinfo.is_set())
|
||||
return false;
|
||||
|
||||
// Create service directory
|
||||
std::string remote_service_path = remotepath::service(server, service);
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Silent))
|
||||
{
|
||||
std::cerr << "Failed to create service directory " << remote_service_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if rsync is installed on remote host
|
||||
std::string check_rsync_cmd = "which rsync";
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", check_rsync_cmd, {}), cMode::Silent))
|
||||
{
|
||||
std::cerr << "rsync is not installed on the remote host" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy template files
|
||||
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_template(server, service) << "/" << std::endl;
|
||||
if (!rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(server, service),
|
||||
server_env, silent))
|
||||
{
|
||||
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy service files
|
||||
std::cout << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_config(server, service) << std::endl;
|
||||
if (!rsync_tree_to_remote(localpath::service(server, service), remotepath::service_config(server, service),
|
||||
server_env, silent))
|
||||
{
|
||||
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Run install script
|
||||
{
|
||||
server_env.run_remote_template_command(service, "install", {}, silent, {});
|
||||
}
|
||||
|
||||
// print health tick
|
||||
std::cout << "Health: " << healthtick(server, service) << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// get_arch : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
std::string get_arch()
|
||||
{
|
||||
// determine the architecture of the system
|
||||
std::string arch;
|
||||
#ifdef __aarch64__
|
||||
arch = "arm64";
|
||||
#elif __x86_64__
|
||||
arch = "amd64";
|
||||
#endif
|
||||
return arch;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// update_dropshell
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
std::string _exec(const char *cmd)
|
||||
{
|
||||
char buffer[128];
|
||||
std::string result = "";
|
||||
FILE *pipe = popen(cmd, "r");
|
||||
if (!pipe)
|
||||
{
|
||||
throw std::runtime_error("popen() failed!");
|
||||
}
|
||||
while (!feof(pipe))
|
||||
{
|
||||
if (fgets(buffer, 128, pipe) != nullptr)
|
||||
result += buffer;
|
||||
}
|
||||
pclose(pipe);
|
||||
return trim(result);
|
||||
}
|
||||
|
||||
int update_dropshell()
|
||||
{
|
||||
// determine path to this executable
|
||||
std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe");
|
||||
std::filesystem::path parent_path = dropshell_path.parent_path();
|
||||
|
||||
// determine the architecture of the system
|
||||
std::string arch = get_arch();
|
||||
|
||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||
|
||||
// download new version, preserve permissions and ownership
|
||||
std::string bash_script;
|
||||
bash_script += "docker run --rm -v " + parent_path.string() + ":/target";
|
||||
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
||||
bash_script += " sh -c \"";
|
||||
bash_script += " curl -fsSL " + url + " -o /target/dropshell_temp &&";
|
||||
bash_script += " chmod --reference=/target/dropshell /target/dropshell_temp &&";
|
||||
bash_script += " chown --reference=/target/dropshell /target/dropshell_temp";
|
||||
bash_script += "\"";
|
||||
|
||||
std::string cmd = "bash -c '" + bash_script + "'";
|
||||
int rval = system(cmd.c_str());
|
||||
if (rval != 0)
|
||||
{
|
||||
std::cerr << "Failed to download new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check if the new version is the same as the old version
|
||||
uint64_t new_hash = hash_file(parent_path / "dropshell_temp");
|
||||
uint64_t old_hash = hash_file(parent_path / "dropshell");
|
||||
if (new_hash == old_hash)
|
||||
{
|
||||
std::cout << "Confirmed dropshell is the latest version." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string runvercmd = (parent_path / "dropshell").string() + " version";
|
||||
std::string currentver = _exec(runvercmd.c_str());
|
||||
runvercmd = (parent_path / "dropshell_temp").string() + " version";
|
||||
std::string newver = _exec(runvercmd.c_str());
|
||||
|
||||
if (currentver >= newver)
|
||||
{
|
||||
std::cout << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl;
|
||||
std::cout << "No update needed." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
std::string bash_script_2 = "docker run --rm -v " + parent_path.string() + ":/target gitea.jde.nz/public/debian-curl:latest " +
|
||||
"sh -c \"mv /target/dropshell_temp /target/dropshell\"";
|
||||
rval = system(bash_script_2.c_str());
|
||||
if (rval != 0)
|
||||
{
|
||||
std::cerr << "Failed to install new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::cout << "Successfully updated " << dropshell_path << " to the latest " << arch << " version." << std::endl;
|
||||
|
||||
// execute the new version
|
||||
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
|
||||
std::cerr << "Failed to execute new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int install_local_agent()
|
||||
{
|
||||
std::vector<std::filesystem::path> paths = {
|
||||
gConfig().get_local_template_cache_path(),
|
||||
gConfig().get_local_backup_path(),
|
||||
gConfig().get_local_tempfiles_path(),
|
||||
localpath::agent()};
|
||||
for (auto &p : gConfig().get_local_server_definition_paths())
|
||||
paths.push_back(p);
|
||||
|
||||
for (auto &p : paths)
|
||||
if (!std::filesystem::exists(p))
|
||||
{
|
||||
std::cout << "Creating directory: " << p << std::endl;
|
||||
std::filesystem::create_directories(p);
|
||||
}
|
||||
|
||||
// download bb64.
|
||||
if (!std::filesystem::exists(localpath::agent() + "bb64"))
|
||||
{
|
||||
std::string cmd = "cd " + localpath::agent() + " && curl -fsSL -o bb64 https://gitea.jde.nz/public/bb64/releases/download/latest/bb64.amd64 && chmod a+x bb64";
|
||||
int ret = system(cmd.c_str());
|
||||
if (EXITSTATUSCHECK(ret))
|
||||
std::cout << "Downloaded bb64 to " << localpath::agent() << std::endl;
|
||||
else
|
||||
std::cerr << "Failed to download bb64 to " << localpath::agent() << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout << "Updating bb64..." << std::endl;
|
||||
system((localpath::agent() + "bb64 -u").c_str()); // update.
|
||||
}
|
||||
|
||||
|
||||
std::cout << "Creating local agent files..." << std::endl;
|
||||
recreate_agent::recreate_tree(localpath::agent());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_host()
|
||||
{
|
||||
// update dropshell.
|
||||
// install the local dropshell agent.
|
||||
|
||||
int rval = update_dropshell();
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
||||
rval = install_local_agent();
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
||||
std::cout << "Installation complete." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int install_server(const std::string &server)
|
||||
{
|
||||
// install the dropshell agent on the given server.
|
||||
std::cout << "Installing dropshell agent on " << server << std::endl;
|
||||
|
||||
std::string agent_path = remotepath::agent(server);
|
||||
if (agent_path.empty())
|
||||
{
|
||||
std::cerr << "Failed to get agent path for " << server << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
std::cerr << "Invalid server environment for " << server << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// first install bb64.
|
||||
std::cout << "Installing bb64 on " << server << std::endl
|
||||
<< std::flush;
|
||||
std::string remote_cmd =
|
||||
"ssh -p " + server_env.get_SSH_INFO().port + " " + server_env.get_SSH_INFO().user + "@" + server_env.get_SSH_INFO().host +
|
||||
" 'mkdir -p " + quote(agent_path) + " && curl -fsSL \"https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh\" | bash -s -- " +
|
||||
quote(agent_path) + " " + quote("$(id -u " + server_env.get_SSH_USER() + "):$(id -g " + server_env.get_SSH_USER() + ")") + "'";
|
||||
|
||||
std::cout << "Executing: " << remote_cmd << std::endl;
|
||||
if (!execute_local_command(remote_cmd, nullptr, cMode::Silent))
|
||||
std::cerr << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl;
|
||||
else
|
||||
std::cout << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl;
|
||||
|
||||
// now create the agent.
|
||||
// copy across from the local agent files.
|
||||
#pragma message("TODO: copy across from the local agent files.")
|
||||
|
||||
return 0; // NOTIMPL
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// install command implementation
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int install_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{ // install host
|
||||
return install_host();
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1)
|
||||
{ // install server
|
||||
return install_server(server);
|
||||
}
|
||||
|
||||
// install service(s)
|
||||
if (safearg(ctx.args, 1) == "*")
|
||||
{
|
||||
// install all services on the server
|
||||
bool okay = true;
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!install_service(server, service.service_name, false))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
else
|
||||
{ // install the specific service.
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return install_service(server, service, false) ? 0 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
184
source/src/commands/list.cpp
Normal file
184
source/src/commands/list.cpp
Normal file
@ -0,0 +1,184 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "tableprint.hpp"
|
||||
#include "transwarp.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int list_handler(const CommandContext& ctx);
|
||||
void show_server_details(const std::string& server_name);
|
||||
void list_servers();
|
||||
|
||||
static std::vector<std::string> list_name_list={"list","ls","info","-l"};
|
||||
|
||||
// Static registration
|
||||
struct ListCommandRegister {
|
||||
ListCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
list_name_list,
|
||||
list_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
0, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"list [SERVER] [SERVICE]",
|
||||
"List server or service information and status",
|
||||
// heredoc
|
||||
R"(
|
||||
List details for servers and services controller by dropshell.
|
||||
list list all servers.
|
||||
list server list all services for the given server.
|
||||
list server service list the given service details on the given server.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} list_command_register;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// list command handler
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
int list_handler(const CommandContext& ctx) {
|
||||
if (ctx.args.size() == 0) {
|
||||
list_servers();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx.args.size() == 1) {
|
||||
show_server_details(ctx.args[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::cout << "List handler called with " << ctx.args.size() << " args\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// https://github.com/bloomen/transwarp?tab=readme-ov-file#range-functions
|
||||
void list_servers() {
|
||||
auto servers = get_configured_servers();
|
||||
|
||||
if (servers.empty()) {
|
||||
std::cout << "No servers found" << std::endl;
|
||||
std::cout << "Please run 'dropshell edit' to set up dropshell." << std::endl;
|
||||
std::cout << "Then run 'dropshell create-server' to create a server." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
tableprint tp("All DropShell Servers");
|
||||
tp.add_row({"Name", "User", "Address", "Health", "Ports"});
|
||||
|
||||
std::cout << "Checking "<<servers.size() << " servers: " << std::flush;
|
||||
int checked = 0;
|
||||
|
||||
transwarp::parallel exec{servers.size()};
|
||||
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const ServerInfo& server) {
|
||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server.name);
|
||||
|
||||
std::set<int> ports_used;
|
||||
std::string serviceticks = "";
|
||||
for (const auto& [service_name, service_status] : status) {
|
||||
ports_used.insert(service_status.ports.begin(), service_status.ports.end());
|
||||
serviceticks += HealthStatus2String(service_status.health) + " ";
|
||||
}
|
||||
std::string ports_used_str = "";
|
||||
for (const auto& port : ports_used)
|
||||
ports_used_str += std::to_string(port) + " ";
|
||||
|
||||
tp.add_row({server.name, server.ssh_user, server.ssh_host, serviceticks, ports_used_str});
|
||||
++checked;
|
||||
// print out a tick character for each server checked.
|
||||
std::cout << checked << " ✓ " << std::flush;
|
||||
});
|
||||
task->wait();
|
||||
std::cout << std::endl << std::endl;
|
||||
tp.print();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void show_server_details(const std::string& server_name) {
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
//---------------------
|
||||
// Check if server is reachable via SSH
|
||||
std::string ssh_address = env.get_SSH_HOST();
|
||||
std::string ssh_user = env.get_SSH_USER();
|
||||
std::string ssh_port = env.get_SSH_PORT();
|
||||
if (!ssh_address.empty()) {
|
||||
std::cout << std::endl << "Server Status:" << std::endl;
|
||||
std::cout << std::string(40, '-') << std::endl;
|
||||
|
||||
// Try to connect to the server
|
||||
std::string cmd = "ssh -o ConnectTimeout=5 " + ssh_user + "@" + ssh_address + " -p " + ssh_port + " 'echo connected' 2>/dev/null";
|
||||
int result = system(cmd.c_str());
|
||||
if (result == 0) {
|
||||
std::cout << "Status: Online" << std::endl;
|
||||
|
||||
// // Get uptime if possible
|
||||
// cmd = "ssh " + ssh_address + " 'uptime' 2>/dev/null";
|
||||
// int rval = system(cmd.c_str());
|
||||
// if (rval != 0) {
|
||||
// std::cout << "Error: Failed to get uptime" << std::endl;
|
||||
// }
|
||||
} else {
|
||||
std::cout << "Status: Offline" << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
//---------------------
|
||||
{
|
||||
std::cout << std::endl;
|
||||
tableprint tp("Server Configuration: " + server_name, true);
|
||||
tp.add_row({"Key", "Value"});
|
||||
for (const auto& [key, value] : env.get_variables()) {
|
||||
tp.add_row({key, value});
|
||||
}
|
||||
tp.print();
|
||||
}
|
||||
|
||||
//---------------------
|
||||
// list services, and run healthcheck on each
|
||||
{
|
||||
tableprint tp("Services: " + server_name, false);
|
||||
tp.add_row({"Status", "Service", "Ports"});
|
||||
|
||||
|
||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server_name);
|
||||
|
||||
std::set<int> ports_used;
|
||||
std::string serviceticks = "";
|
||||
for (const auto& [service_name, service_status] : status) {
|
||||
std::string healthy = HealthStatus2String(service_status.health);
|
||||
|
||||
std::string ports_str = "";
|
||||
for (const auto& port : service_status.ports)
|
||||
ports_str += std::to_string(port) + " ";
|
||||
|
||||
tp.add_row({healthy, service_name, ports_str});
|
||||
} // end of for (const auto& service : services)
|
||||
tp.print();
|
||||
} // end of list services
|
||||
} // end of show_server_details
|
||||
|
||||
|
||||
} // namespace dropshell
|
106
source/src/commands/nuke.cpp
Normal file
106
source/src/commands/nuke.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "config.hpp"
|
||||
#include "services.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
#pragma message ("TODO: Fix issues with Nuke below.")
|
||||
|
||||
/*
|
||||
|
||||
j@twelve:~/code/dropshell$ ds nuke localhost test-squashkiwi
|
||||
---------------------------------------
|
||||
| Nuking test-squashkiwi on localhost |
|
||||
---------------------------------------
|
||||
---------------------------------------------
|
||||
| Uninstalling test-squashkiwi on localhost |
|
||||
---------------------------------------------
|
||||
Service is not installed: test-squashkiwi
|
||||
bash: line 1: cd: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template: Permission denied
|
||||
bash: line 1: /home/dropshell/dropshell_deploy/services/test-squashkiwi/template/nuke.sh: Permission denied
|
||||
Warning: Failed to run nuke script: test-squashkiwi
|
||||
|
||||
*/
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
int nuke_handler(const CommandContext& ctx);
|
||||
static std::vector<std::string> nuke_name_list={"nuke"};
|
||||
|
||||
// Static registration
|
||||
struct NukeCommandRegister {
|
||||
NukeCommandRegister() {
|
||||
CommandRegistry::instance().register_command({
|
||||
nuke_name_list,
|
||||
nuke_handler,
|
||||
std_autocomplete,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
2, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"nuke SERVER [SERVICE|*] ",
|
||||
"Nuke a service on a server. Destroys everything, both local and remote!",
|
||||
// heredoc
|
||||
R"(
|
||||
Nuke a service on a server. Destroys everything, both local and remote!
|
||||
nuke SERVER SERVICE nuke the given service on the given server.
|
||||
nuke SERVER * nuke all services on the given server.
|
||||
)"
|
||||
});
|
||||
}
|
||||
} nuke_command_register;
|
||||
|
||||
int nuke_handler(const CommandContext &ctx)
|
||||
{
|
||||
ASSERT(ctx.args.size() > 1, "Usage: nuke <server> <service>");
|
||||
ASSERT(gConfig().is_config_set(), "No configuration found. Please run 'dropshell config' to set up your configuration.");
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
|
||||
maketitle("Nuking " + service + " on " + server);
|
||||
|
||||
server_env_manager server_env(server);
|
||||
LocalServiceInfo service_info;
|
||||
|
||||
if (server_env.is_valid())
|
||||
{
|
||||
service_info = get_service_info(server, service);
|
||||
if (!SIvalid(service_info))
|
||||
std::cerr << "Warning: Invalid service: " << service << std::endl;
|
||||
|
||||
if (!uninstall_service(server, service, false))
|
||||
std::cerr << "Warning: Failed to uninstall service: " << service << std::endl;
|
||||
|
||||
// run the nuke script on the remote server if it exists.
|
||||
if (gTemplateManager().template_command_exists(service_info.template_name, "nuke"))
|
||||
{
|
||||
if (!server_env.run_remote_template_command(service, "nuke", {}, false, {}))
|
||||
std::cerr << "Warning: Failed to run nuke script: " << service << std::endl;
|
||||
}
|
||||
}
|
||||
else
|
||||
std::cerr << "Warning: Invalid server: " << server << std::endl;
|
||||
|
||||
// remove the local service directory
|
||||
std::string local_service_path = service_info.local_service_path;
|
||||
if (local_service_path.empty() || !std::filesystem::exists(local_service_path))
|
||||
{
|
||||
std::cerr << "Warning: Local service directory not found: " << local_service_path << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto ret = std::filesystem::remove_all(local_service_path);
|
||||
if (ret != 0)
|
||||
std::cerr << "Warning: Failed to remove local service directory" << std::endl;
|
||||
|
||||
return ret == 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
31
source/src/commands/shared_commands.hpp
Normal file
31
source/src/commands/shared_commands.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef SHARED_COMMANDS_HPP
|
||||
#define SHARED_COMMANDS_HPP
|
||||
|
||||
#include "servers.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// defined in install.cpp
|
||||
bool rsync_tree_to_remote(
|
||||
const std::string &local_path,
|
||||
const std::string &remote_path,
|
||||
server_env_manager &server_env,
|
||||
bool silent);
|
||||
|
||||
// defined in install.cpp
|
||||
bool install_service(const std::string& server, const std::string& service, bool silent);
|
||||
bool uninstall_service(const std::string& server, const std::string& service, bool silent);
|
||||
std::string get_arch();
|
||||
|
||||
// defined in health.cpp
|
||||
std::string healthtick(const std::string& server, const std::string& service);
|
||||
std::string HealthStatus2String(HealthStatus status);
|
||||
|
||||
// defined in standard_autocomplete.cpp
|
||||
void std_autocomplete(const CommandContext& ctx);
|
||||
void std_autocomplete_allowallservices(const CommandContext& ctx);
|
||||
|
||||
|
||||
} // namespace dropshell
|
||||
#endif
|
36
source/src/commands/standard_autocomplete.cpp
Normal file
36
source/src/commands/standard_autocomplete.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
#include "shared_commands.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
#include "servers.hpp"
|
||||
#include "services.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
void std_autocomplete(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() == 0) { // just the command, no args yet.
|
||||
// list servers
|
||||
std::vector<ServerInfo> servers = get_configured_servers();
|
||||
for (const auto& server : servers) {
|
||||
std::cout << server.name << std::endl;
|
||||
}
|
||||
}
|
||||
else if (ctx.args.size() == 1) {
|
||||
// list services
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(ctx.args[0]);
|
||||
for (const auto& service : services) {
|
||||
std::cout << service.service_name << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void std_autocomplete_allowallservices(const CommandContext &ctx)
|
||||
{
|
||||
std_autocomplete(ctx);
|
||||
if (ctx.args.size() == 1)
|
||||
std::cout << "*" << std::endl;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
108
source/src/commands/uninstall.cpp
Normal file
108
source/src/commands/uninstall.cpp
Normal file
@ -0,0 +1,108 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "directories.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
|
||||
int uninstall_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> uninstall_name_list = {"uninstall", "remove"};
|
||||
|
||||
// Static registration
|
||||
struct UninstallCommandRegister
|
||||
{
|
||||
UninstallCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({uninstall_name_list,
|
||||
uninstall_handler,
|
||||
std_autocomplete_allowallservices,
|
||||
false, // hidden
|
||||
true, // requires_config
|
||||
true, // requires_install
|
||||
1, // min_args (after command)
|
||||
2, // max_args (after command)
|
||||
"uninstall SERVER [SERVICE|*]",
|
||||
"Uninstall a service on a server. Does not remove configuration or user data.",
|
||||
// heredoc
|
||||
R"(
|
||||
Uninstall a service on a server. Does not remove configuration or user data.
|
||||
uninstall SERVER SERVICE uninstall the given service on the given server.
|
||||
uninstall SERVER * uninstall all services on the given server.
|
||||
)"});
|
||||
}
|
||||
} uninstall_command_register;
|
||||
|
||||
int uninstall_handler(const CommandContext &ctx)
|
||||
{
|
||||
if (ctx.args.size() < 1)
|
||||
{
|
||||
std::cerr << "Error: uninstall requires a server and (optionally) a service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string server = safearg(ctx.args, 0);
|
||||
|
||||
if (ctx.args.size() == 1)
|
||||
{
|
||||
// uninstall all services on the server
|
||||
bool okay = true;
|
||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||
for (const auto &service : services)
|
||||
{
|
||||
if (!uninstall_service(server, service.service_name, false))
|
||||
okay = false;
|
||||
}
|
||||
return okay ? 0 : 1;
|
||||
}
|
||||
|
||||
std::string service = safearg(ctx.args, 1);
|
||||
return uninstall_service(server, service, false) ? 0 : 1;
|
||||
}
|
||||
|
||||
bool uninstall_service(const std::string &server, const std::string &service, bool silent)
|
||||
{
|
||||
if (!silent)
|
||||
maketitle("Uninstalling " + service + " on " + server);
|
||||
|
||||
server_env_manager server_env(server);
|
||||
if (!server_env.is_valid())
|
||||
{
|
||||
std::cerr << "Invalid server: " << server << std::endl;
|
||||
return false; // should never hit this.
|
||||
}
|
||||
|
||||
// 2. Check if service directory exists on server
|
||||
if (!server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||
{
|
||||
std::cerr << "Service is not installed: " << service << std::endl;
|
||||
return true; // Nothing to uninstall
|
||||
}
|
||||
|
||||
// 3. Run uninstall script if it exists
|
||||
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
|
||||
if (gTemplateManager().template_command_exists(service, "uninstall"))
|
||||
if (server_env.check_remote_file_exists(uninstall_script))
|
||||
if (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
|
||||
if (!silent)
|
||||
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||
|
||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||
if (server_env.remove_remote_dir(remotepath::service(server, service), silent))
|
||||
{
|
||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||
if (!silent)
|
||||
std::cout << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
||||
}
|
||||
else if (!silent)
|
||||
std::cerr << "Warning: Failed to remove remote service directory" << std::endl;
|
||||
|
||||
if (!silent)
|
||||
std::cout << "Completed service " << service << " uninstall on " << server << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
45
source/src/commands/version.cpp
Normal file
45
source/src/commands/version.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "command_registry.hpp"
|
||||
#include "version.hpp"
|
||||
namespace dropshell {
|
||||
|
||||
int version_handler(const CommandContext &ctx);
|
||||
|
||||
static std::vector<std::string> version_name_list = {"version","v","ver","-v","-ver","--version"};
|
||||
|
||||
void version_autocomplete(const CommandContext &ctx)
|
||||
{
|
||||
}
|
||||
|
||||
// Static registration
|
||||
struct VersionCommandRegister
|
||||
{
|
||||
VersionCommandRegister()
|
||||
{
|
||||
CommandRegistry::instance().register_command({version_name_list,
|
||||
version_handler,
|
||||
version_autocomplete,
|
||||
false, // hidden
|
||||
false, // requires_config
|
||||
false, // requires_install
|
||||
0, // min_args (after command)
|
||||
0, // max_args (after command)
|
||||
"version",
|
||||
"Uninstall a service on a server. Does not remove configuration or user data.",
|
||||
// heredoc
|
||||
R"(
|
||||
Uninstall a service on a server. Does not remove configuration or user data.
|
||||
uninstall <server> <service> uninstall the given service on the given server.
|
||||
uninstall <server> uninstall all services on the given server.
|
||||
)"});
|
||||
}
|
||||
} version_command_register;
|
||||
|
||||
|
||||
int version_handler(const CommandContext &ctx)
|
||||
{
|
||||
std::cout << VERSION << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
} // namespace dropshell
|
166
source/src/config.cpp
Normal file
166
source/src/config.cpp
Normal file
@ -0,0 +1,166 @@
|
||||
#include "utils/directories.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/json.hpp"
|
||||
#include <filesystem>
|
||||
#include "utils/execute.hpp"
|
||||
namespace dropshell {
|
||||
|
||||
|
||||
config & gConfig() {
|
||||
static config *globalConfig = new config();
|
||||
return *globalConfig;
|
||||
}
|
||||
|
||||
|
||||
config::config() : mIsConfigSet(false) {
|
||||
}
|
||||
|
||||
config::~config() {
|
||||
}
|
||||
|
||||
bool config::load_config() { // load json config file.
|
||||
std::string config_path = localfile::dropshell_json();
|
||||
if (config_path.empty() || !std::filesystem::exists(config_path))
|
||||
return false;
|
||||
|
||||
std::ifstream config_file(config_path);
|
||||
if (!config_file.is_open())
|
||||
return false;
|
||||
|
||||
try {
|
||||
mConfig = nlohmann::json::parse(config_file);
|
||||
}
|
||||
catch (nlohmann::json::parse_error& ex)
|
||||
{
|
||||
std::cerr << "Error: Failed to parse config file: " << ex.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
mIsConfigSet = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool config::save_config(bool create_aux_directories)
|
||||
{
|
||||
std::string config_path = localfile::dropshell_json();
|
||||
if (config_path.empty())
|
||||
return false;
|
||||
|
||||
std::filesystem::create_directories(get_parent(config_path));
|
||||
|
||||
std::ofstream config_file(config_path);
|
||||
if (!config_file.is_open())
|
||||
return false;
|
||||
|
||||
if (!mIsConfigSet)
|
||||
{
|
||||
std::string homedir = localpath::current_user_home();
|
||||
std::string dropshell_base = homedir + "/.dropshell";
|
||||
mConfig["tempfiles"] = dropshell_base + "/tmp";
|
||||
mConfig["backups"] = dropshell_base + "/backups";
|
||||
|
||||
mConfig["template_cache"] = dropshell_base + "/template_cache";
|
||||
mConfig["template_registry_URLs"] = {
|
||||
"https://templates.dropshell.app"
|
||||
};
|
||||
mConfig["template_local_paths"] = {
|
||||
dropshell_base + "/local_templates"
|
||||
};
|
||||
|
||||
mConfig["server_definition_paths"] = {
|
||||
dropshell_base + "/servers"
|
||||
};
|
||||
mConfig["template_upload_registry_url"] = "https://templates.dropshell.app";
|
||||
mConfig["template_upload_registry_token"] = "SECRETTOKEN";
|
||||
}
|
||||
|
||||
config_file << mConfig.dump(4);
|
||||
config_file.close();
|
||||
|
||||
if (create_aux_directories) {
|
||||
std::vector<std::filesystem::path> paths = {
|
||||
get_local_template_cache_path(),
|
||||
get_local_backup_path(),
|
||||
get_local_tempfiles_path()
|
||||
};
|
||||
for (auto & p : get_local_server_definition_paths())
|
||||
paths.push_back(p);
|
||||
|
||||
for (auto & p : paths)
|
||||
if (!std::filesystem::exists(p))
|
||||
{
|
||||
std::cout << "Creating directory: " << p << std::endl;
|
||||
std::filesystem::create_directories(p);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool config::is_config_set() const
|
||||
{
|
||||
return mIsConfigSet;
|
||||
}
|
||||
|
||||
bool config::is_agent_installed()
|
||||
{
|
||||
return std::filesystem::exists(localpath::agent() + "/bb64");
|
||||
}
|
||||
|
||||
std::string config::get_local_tempfiles_path() {
|
||||
return mConfig["tempfiles"];
|
||||
}
|
||||
|
||||
std::string config::get_local_backup_path() {
|
||||
return mConfig["backups"];
|
||||
}
|
||||
|
||||
std::string config::get_local_template_cache_path() {
|
||||
return mConfig["template_cache"];
|
||||
}
|
||||
|
||||
std::vector<std::string> config::get_template_registry_urls() {
|
||||
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
||||
std::vector<std::string> urls;
|
||||
for (auto &url : template_registry_urls) {
|
||||
urls.push_back(url);
|
||||
}
|
||||
return urls;
|
||||
}
|
||||
|
||||
std::vector<std::string> config::get_template_local_paths()
|
||||
{
|
||||
nlohmann::json template_local_paths = mConfig["template_local_paths"];
|
||||
std::vector<std::string> paths;
|
||||
for (auto &path : template_local_paths) {
|
||||
if (path.is_string() && !path.empty())
|
||||
paths.push_back(path);
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
std::vector<std::string> config::get_local_server_definition_paths() {
|
||||
nlohmann::json server_definition_paths = mConfig["server_definition_paths"];
|
||||
|
||||
std::vector<std::string> paths;
|
||||
for (auto &path : server_definition_paths) {
|
||||
if (path.is_string() && !path.empty())
|
||||
paths.push_back(path);
|
||||
else
|
||||
std::cerr << "Warning: Invalid server definition path: " << path << std::endl;
|
||||
}
|
||||
return paths;
|
||||
}
|
||||
|
||||
std::string config::get_template_upload_registry_url() {
|
||||
return mConfig["template_upload_registry_url"];
|
||||
}
|
||||
|
||||
std::string config::get_template_upload_registry_token() {
|
||||
return mConfig["template_upload_registry_token"];
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
38
source/src/config.hpp
Normal file
38
source/src/config.hpp
Normal file
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "utils/json.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
class config {
|
||||
public:
|
||||
config();
|
||||
~config();
|
||||
|
||||
bool load_config();
|
||||
bool save_config(bool create_aux_directories);
|
||||
|
||||
bool is_config_set() const;
|
||||
static bool is_agent_installed();
|
||||
|
||||
std::string get_local_tempfiles_path();
|
||||
std::string get_local_backup_path();
|
||||
std::string get_local_template_cache_path();
|
||||
std::vector<std::string> get_template_registry_urls();
|
||||
std::vector<std::string> get_template_local_paths();
|
||||
std::vector<std::string> get_local_server_definition_paths();
|
||||
|
||||
std::string get_template_upload_registry_url();
|
||||
std::string get_template_upload_registry_token();
|
||||
|
||||
private:
|
||||
nlohmann::json mConfig;
|
||||
bool mIsConfigSet;
|
||||
};
|
||||
|
||||
|
||||
config & gConfig();
|
||||
|
||||
} // namespace dropshell
|
3744
source/src/contrib/transwarp.hpp
Normal file
3744
source/src/contrib/transwarp.hpp
Normal file
File diff suppressed because it is too large
Load Diff
7343
source/src/contrib/xxhash.hpp
Normal file
7343
source/src/contrib/xxhash.hpp
Normal file
File diff suppressed because it is too large
Load Diff
16
source/src/dropshell-completion.bash
Executable file
16
source/src/dropshell-completion.bash
Executable file
@ -0,0 +1,16 @@
|
||||
#!/bin/bash
|
||||
|
||||
_dropshell_completions() {
|
||||
local cur
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
|
||||
# call dropshell to get the list of possiblities for the current argument. Supply all previous arguments.
|
||||
local completions=($(dropshell autocomplete "${COMP_WORDS[@]:1:${COMP_CWORD}-1}"))
|
||||
COMPREPLY=( $(compgen -W "${completions[*]}" -- ${cur}) )
|
||||
return 0
|
||||
}
|
||||
|
||||
# Register the completion function
|
||||
complete -F _dropshell_completions dropshell
|
||||
complete -F _dropshell_completions ds
|
262
source/src/main.cpp
Normal file
262
source/src/main.cpp
Normal file
@ -0,0 +1,262 @@
|
||||
#include "version.hpp"
|
||||
#include "config.hpp"
|
||||
#include "service_runner.hpp"
|
||||
#include "services.hpp"
|
||||
#include "servers.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "autocomplete.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
#include "command_registry.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
#include <chrono>
|
||||
#include <assert.hpp>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
namespace dropshell {
|
||||
|
||||
extern const std::string VERSION;
|
||||
extern const std::string RELEASE_DATE;
|
||||
extern const std::string AUTHOR;
|
||||
extern const std::string LICENSE;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
try {
|
||||
// silently attempt to load the config file and templates.
|
||||
gConfig().load_config();
|
||||
if (gConfig().is_config_set())
|
||||
gTemplateManager().load_sources();
|
||||
|
||||
|
||||
// process the command line arguments.
|
||||
std::vector<std::string> args(argv, argv + argc);
|
||||
|
||||
if (args.size() < 2)
|
||||
args.push_back("help");
|
||||
ASSERT(args.size() > 1, "No command provided, logic error.");
|
||||
|
||||
CommandContext ctx{args[0], args[1], std::vector<std::string>(args.begin() + 2, args.end())};
|
||||
|
||||
if (ctx.command == "autocomplete") {
|
||||
CommandRegistry::instance().autocomplete(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const CommandInfo* info = CommandRegistry::instance().find_command(ctx.command);
|
||||
if (!info) {
|
||||
std::cerr << "Unknown command: " << ctx.command << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (info->requires_config && !gConfig().is_config_set()) {
|
||||
std::cerr << "Valid dropshell configuration required for command: " << ctx.command << std::endl;
|
||||
std::cerr << "Please run 'dropshell edit' to set up the dropshell configuration." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (info->requires_install && !gConfig().is_agent_installed()) {
|
||||
std::cerr << "Dropshell agent not installed for command: " << ctx.command << std::endl;
|
||||
std::cerr << "Please run 'dropshell install' to install the local dropshell agent." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int arg_count = ctx.args.size();
|
||||
if (arg_count < info->min_args || (info->max_args != -1 && arg_count > info->max_args)) {
|
||||
std::cerr << "Invalid number of arguments for command: " << ctx.command << std::endl;
|
||||
std::cerr << "Usage: " << std::endl;
|
||||
std::cout << " ";
|
||||
print_left_aligned(info->help_usage,30);
|
||||
std::cout << info->help_description << std::endl;
|
||||
return 1;
|
||||
}
|
||||
return info->handler(ctx);
|
||||
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
struct ServerAndServices {
|
||||
std::string server_name;
|
||||
std::vector<LocalServiceInfo> servicelist;
|
||||
};
|
||||
|
||||
bool getCLIServices(const std::string & arg2, const std::string & arg3,
|
||||
ServerAndServices & server_and_services)
|
||||
{
|
||||
if (arg2.empty()) return false;
|
||||
server_and_services.server_name = arg2;
|
||||
|
||||
if (arg3.empty()) {
|
||||
server_and_services.servicelist = get_server_services_info(arg2);
|
||||
} else {
|
||||
server_and_services.servicelist.push_back(get_service_info(arg2, arg3));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void printversion() {
|
||||
maketitle("DropShell version " + VERSION);
|
||||
std::cout << "Release date: " << RELEASE_DATE << std::endl;
|
||||
std::cout << "Author: " << AUTHOR << std::endl;
|
||||
std::cout << "License: " << LICENSE << std::endl;
|
||||
}
|
||||
|
||||
auto command_match = [](const std::string& cmd_list, int argc, char* argv[]) -> bool {
|
||||
std::istringstream iss(cmd_list);
|
||||
std::string cmd_item;
|
||||
while (iss >> cmd_item) {
|
||||
if (cmd_item == safearg(argc, argv, 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
#define BOOLEXIT(CMD_LIST, RUNCMD) { \
|
||||
if (command_match(CMD_LIST, argc, argv)) { \
|
||||
return (RUNCMD) ? 0 : 1; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define HAPPYEXIT(CMD_LIST, RUNCMD) { \
|
||||
if (command_match(CMD_LIST, argc, argv)) { \
|
||||
RUNCMD; \
|
||||
return 0; \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
int old_main(int argc, char* argv[]) {
|
||||
HAPPYEXIT("hash", hash_demo_raw(safearg(argc,argv,2)))
|
||||
HAPPYEXIT("version", printversion())
|
||||
BOOLEXIT("test-template", gTemplateManager().test_template(safearg(argc,argv,2)))
|
||||
ASSERT(safearg(argc,argv,1) != "assert", "Hello! Here is an assert.");
|
||||
|
||||
try {
|
||||
// silently attempt to load the config file and templates.
|
||||
gConfig().load_config();
|
||||
if (gConfig().is_config_set())
|
||||
gTemplateManager().load_sources();
|
||||
|
||||
std::string cmd = argv[1];
|
||||
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// from here we require the config file to be loaded.
|
||||
if (!gConfig().is_config_set())
|
||||
return die("Please run 'dropshell edit' to set up the dropshell configuration.");
|
||||
|
||||
|
||||
const std::vector<std::string> & server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||
if (server_definition_paths.size()>1) { // only show if there are multiple.
|
||||
std::cout << "Server definition paths: ";
|
||||
for (auto & dir : server_definition_paths)
|
||||
std::cout << "["<< dir << "] ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
if (gTemplateManager().is_loaded() && gTemplateManager().get_source_count() > 0)
|
||||
gTemplateManager().print_sources();
|
||||
|
||||
HAPPYEXIT("templates", gTemplateManager().list_templates());
|
||||
|
||||
if (cmd == "create-template") {
|
||||
if (argc < 3) return die("Error: create-template requires a template name");
|
||||
return (gTemplateManager().create_template(argv[2])) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (cmd == "create-server") {
|
||||
if (argc < 3) return die("Error: create-server requires a server name");
|
||||
return (create_server(argv[2])) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (cmd == "create-service") {
|
||||
if (argc < 5) return die("Error: not enough arguments.\ndropshell create-service server template service");
|
||||
return (create_service(argv[2], argv[3], argv[4])) ? 0 : 1;
|
||||
}
|
||||
|
||||
if (cmd == "ssh" && argc < 4) {
|
||||
if (argc < 3) return die("Error: ssh requires a server name and optionally service name");
|
||||
service_runner::interactive_ssh(argv[2], "bash");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// handle running a command.
|
||||
std::set<std::string> commands;
|
||||
get_all_used_commands(commands);
|
||||
|
||||
autocomplete::merge_commands(commands, autocomplete::service_commands_require_config); // handled by service_runner, but not in template_shell_commands.
|
||||
|
||||
if (commands.count(cmd)) {
|
||||
std::set<std::string> safe_commands = {"nuke", "fullnuke"};
|
||||
if (safe_commands.count(cmd) && argc < 4)
|
||||
return die("Error: "+cmd+" requires a server name and service name. For safety, can't run on all services.");
|
||||
|
||||
// get all the services to run the command on.
|
||||
ServerAndServices server_and_services;
|
||||
if (!getCLIServices(safearg(argc, argv, 2), safearg(argc, argv, 3), server_and_services))
|
||||
return die("Error: "+cmd+" command requires server name and optionally service name");
|
||||
|
||||
// run the command on each service.
|
||||
for (const auto& service_info : server_and_services.servicelist) {
|
||||
if (!SIvalid(service_info))
|
||||
std::cerr<<"Error: Unable to get service information."<<std::endl;
|
||||
else {
|
||||
service_runner runner(server_and_services.server_name, service_info.service_name);
|
||||
if (!runner.isValid())
|
||||
return die("Error: Failed to initialize service");
|
||||
|
||||
std::vector<std::string> additional_args;
|
||||
for (int i=4; i<argc; i++)
|
||||
additional_args.push_back(argv[i]);
|
||||
if (!runner.run_command(cmd, additional_args))
|
||||
return die(cmd+" failed on service "+service_info.service_name);
|
||||
}
|
||||
}
|
||||
|
||||
// success!
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Unknown command
|
||||
std::cerr << "Error: Unknown command '" << cmd << "'" << std::endl;
|
||||
std::cerr << "Valid commands: ";
|
||||
for (const auto& command : commands) {
|
||||
if (!command.empty() && command[0]!='_')
|
||||
std::cerr << command << " ";
|
||||
}
|
||||
std::cerr << std::endl;
|
||||
return 1;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
return dropshell::main(argc, argv);
|
||||
}
|
231
source/src/server_env_manager.cpp
Normal file
231
source/src/server_env_manager.cpp
Normal file
@ -0,0 +1,231 @@
|
||||
#include "server_env_manager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "services.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/json.hpp"
|
||||
#include "utils/execute.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <wordexp.h> // For potential shell-like expansion if needed
|
||||
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
server_env_manager::server_env_manager(const std::string& server_name) : mValid(false), mServerName(server_name) {
|
||||
if (server_name.empty())
|
||||
return;
|
||||
|
||||
// Construct the full path to server.env
|
||||
std::string server_env_path = localfile::server_json(server_name);
|
||||
|
||||
// Check if file exists
|
||||
if (!std::filesystem::exists(server_env_path)) {
|
||||
std::cerr << "Server environment file not found: " + server_env_path << " for server " << server_name << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Use envmanager to handle the environment file
|
||||
nlohmann::json server_env_json = nlohmann::json::parse(std::ifstream(server_env_path));
|
||||
if (server_env_json.empty()) {
|
||||
std::cerr << "Error: Failed to parse server environment file: " + server_env_path << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// get the variables from the json
|
||||
for (const auto& var : server_env_json.items()) {
|
||||
std::string value;
|
||||
if (var.value().is_string())
|
||||
value = var.value();
|
||||
else if (var.value().is_number_integer())
|
||||
value = std::to_string(var.value().get<int>());
|
||||
else if (var.value().is_boolean())
|
||||
value = var.value() ? "true" : "false";
|
||||
else
|
||||
value = var.value().dump();
|
||||
|
||||
mVariables[var.key()] = replace_with_environment_variables_like_bash(value);
|
||||
}
|
||||
|
||||
// Verify required variables exist
|
||||
for (const auto& var : {"SSH_HOST", "SSH_USER", "SSH_PORT", "DROPSHELL_DIR"}) {
|
||||
if (mVariables.find(var) == mVariables.end()) {
|
||||
// Print the variables identified in the file
|
||||
std::cout << "Variables identified in the file:" << std::endl;
|
||||
for (const auto& v : mVariables) {
|
||||
std::cout << " " << v.first << std::endl;
|
||||
}
|
||||
throw std::runtime_error("Missing required variable: " + std::string(var));
|
||||
}
|
||||
}
|
||||
mValid = true;
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Failed to parse server environment file: " + std::string(e.what()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool server_env_manager::create_server_env(const std::string &server_env_path, const std::string &SSH_HOST, const std::string &SSH_USER, const std::string &SSH_PORT, const std::string &DROPSHELL_DIR)
|
||||
{
|
||||
nlohmann::json server_env_json;
|
||||
server_env_json["SSH_HOST"] = SSH_HOST;
|
||||
server_env_json["SSH_USER"] = SSH_USER;
|
||||
server_env_json["SSH_PORT"] = SSH_PORT;
|
||||
server_env_json["DROPSHELL_DIR"] = DROPSHELL_DIR;
|
||||
|
||||
try {
|
||||
std::ofstream server_env_file(server_env_path);
|
||||
server_env_file << server_env_json.dump(4);
|
||||
server_env_file.close();
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Failed to create server environment file: " + std::string(e.what()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string server_env_manager::get_variable(const std::string& name) const {
|
||||
auto it = mVariables.find(name);
|
||||
if (it == mVariables.end()) {
|
||||
return "";
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
std::optional<sCommand> server_env_manager::construct_standard_template_run_cmd(const std::string &service_name, const std::string &command, const std::vector<std::string> args, const bool silent) const
|
||||
{
|
||||
if (command.empty())
|
||||
return std::nullopt;
|
||||
|
||||
std::string remote_service_template_path = remotepath::service_template(mServerName,service_name);
|
||||
std::string script_path = remote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
std::map<std::string, std::string> env_vars;
|
||||
if (!get_all_service_env_vars(mServerName, service_name, env_vars)) {
|
||||
std::cerr << "Error: Failed to get all service env vars for " << service_name << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::string argstr = "";
|
||||
for (const auto& arg : args) {
|
||||
argstr += " " + quote(dequote(trim(arg)));
|
||||
}
|
||||
|
||||
sCommand sc(
|
||||
remote_service_template_path,
|
||||
quote(script_path) + argstr + (silent ? " > /dev/null 2>&1" : ""),
|
||||
env_vars
|
||||
);
|
||||
|
||||
if (sc.empty()) {
|
||||
std::cerr << "Error: Failed to construct command for " << service_name << " " << command << std::endl;
|
||||
return std::nullopt;
|
||||
}
|
||||
return sc;
|
||||
}
|
||||
|
||||
|
||||
bool server_env_manager::check_remote_dir_exists(const std::string &dir_path) const
|
||||
{
|
||||
sCommand scommand("", "test -d " + quote(dir_path),{});
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand, cMode::Silent);
|
||||
}
|
||||
|
||||
bool server_env_manager::check_remote_file_exists(const std::string& file_path) const {
|
||||
sCommand scommand("", "test -f " + quote(file_path),{});
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand, cMode::Silent);
|
||||
}
|
||||
|
||||
bool server_env_manager::check_remote_items_exist(const std::vector<std::string> &file_paths) const
|
||||
{
|
||||
// convert file_paths to a single string, separated by spaces
|
||||
std::string file_paths_str;
|
||||
std::string file_names_str;
|
||||
for (const auto& file_path : file_paths) {
|
||||
file_paths_str += quote(file_path) + " ";
|
||||
file_names_str += std::filesystem::path(file_path).filename().string() + " ";
|
||||
}
|
||||
// check if all items in the vector exist on the remote server, in a single command.
|
||||
sCommand scommand("", "for item in " + file_paths_str + "; do test -f $item; done",{});
|
||||
|
||||
bool okay = execute_ssh_command(get_SSH_INFO(), scommand, cMode::Silent);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Required items not found on remote server: " << file_names_str << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_env_manager::remove_remote_dir(const std::string &dir_path, bool silent) const
|
||||
{
|
||||
std::filesystem::path path(dir_path);
|
||||
std::filesystem::path parent_path = path.parent_path();
|
||||
std::string target_dir = path.filename().string();
|
||||
|
||||
if (parent_path.empty())
|
||||
parent_path="/";
|
||||
|
||||
if (target_dir.empty())
|
||||
return false;
|
||||
|
||||
if (!silent)
|
||||
std::cout << "Removing remote directory " << target_dir << " in " << parent_path << " on " << mServerName << std::endl;
|
||||
std::string remote_cmd =
|
||||
"docker run --rm -v " + quote(parent_path.string()) + ":/parent " +
|
||||
" alpine rm -rf \"/parent/" + target_dir + "\"";
|
||||
|
||||
if (!silent)
|
||||
std::cout << "Running command: " << remote_cmd << std::endl;
|
||||
|
||||
sCommand scommand("", remote_cmd,{});
|
||||
cMode mode = (silent ? cMode::Silent : cMode::Defaults);
|
||||
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand, mode);
|
||||
}
|
||||
|
||||
bool server_env_manager::run_remote_template_command(const std::string &service_name, const std::string &command, std::vector<std::string> args, bool silent, std::map<std::string, std::string> extra_env_vars) const
|
||||
{
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, silent);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto& [key, value] : extra_env_vars)
|
||||
scommand->add_env_var(key, value);
|
||||
|
||||
if (scommand->get_command_to_run().empty())
|
||||
return false;
|
||||
cMode mode = (command=="ssh") ? (cMode::Interactive) : cMode::Silent;
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand.value(), mode);
|
||||
}
|
||||
|
||||
bool server_env_manager::run_remote_template_command_and_capture_output(const std::string &service_name, const std::string &command, std::vector<std::string> args, std::string &output, bool silent, std::map<std::string, std::string> extra_env_vars) const
|
||||
{
|
||||
auto scommand = construct_standard_template_run_cmd(service_name, command, args, false);
|
||||
if (!scommand.has_value())
|
||||
return false;
|
||||
|
||||
// add the extra env vars to the command
|
||||
for (const auto& [key, value] : extra_env_vars)
|
||||
scommand->add_env_var(key, value);
|
||||
|
||||
cMode mode = cMode::CaptureOutput;
|
||||
return execute_ssh_command(get_SSH_INFO(), scommand.value(), mode, &output);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// base64 <<< "FOO=BAR WHEE=YAY bash ./test.sh"
|
||||
// echo YmFzaCAtYyAnRk9PPUJBUiBXSEVFPVlBWSBiYXNoIC4vdGVzdC5zaCcK | base64 -d | bash
|
||||
|
||||
|
||||
} // namespace dropshell
|
77
source/src/server_env_manager.hpp
Normal file
77
source/src/server_env_manager.hpp
Normal file
@ -0,0 +1,77 @@
|
||||
// server_env.hpp
|
||||
//
|
||||
// read the server.env file and provide a class to access the variables
|
||||
|
||||
#ifndef __SERVER_ENV_HPP
|
||||
#define __SERVER_ENV_HPP
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "utils/execute.hpp"
|
||||
#include <optional>
|
||||
namespace dropshell {
|
||||
|
||||
class server_env_manager;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
// reads path / server.env and provides a class to access the variables.
|
||||
// each env file is required to have the following variables:
|
||||
// SSH_HOST
|
||||
// SSH_USER
|
||||
// SSH_PORT
|
||||
// the following replacements are made in the values:
|
||||
// ${USER} -> the username of the user running dropshell
|
||||
class server_env_manager {
|
||||
public:
|
||||
server_env_manager(const std::string& server_name);
|
||||
|
||||
static bool create_server_env(
|
||||
const std::string& server_env_path,
|
||||
const std::string& SSH_HOST,
|
||||
const std::string& SSH_USER,
|
||||
const std::string& SSH_PORT,
|
||||
const std::string& DROPSHELL_DIR);
|
||||
|
||||
std::string get_variable(const std::string& name) const;
|
||||
|
||||
// trivial getters.
|
||||
const std::map<std::string, std::string>& get_variables() const { return mVariables; }
|
||||
std::string get_SSH_HOST() const { return get_variable("SSH_HOST"); }
|
||||
std::string get_SSH_USER() const { return get_variable("SSH_USER"); }
|
||||
std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); }
|
||||
std::string get_DROPSHELL_DIR() const { return get_variable("DROPSHELL_DIR"); }
|
||||
sSSHInfo get_SSH_INFO() const { return sSSHInfo{get_SSH_HOST(), get_SSH_USER(), get_SSH_PORT(), get_server_name()}; }
|
||||
bool is_valid() const { return mValid; }
|
||||
std::string get_server_name() const { return mServerName; }
|
||||
|
||||
// helper functions
|
||||
public:
|
||||
bool check_remote_dir_exists(const std::string &dir_path) const;
|
||||
bool check_remote_file_exists(const std::string& file_path) const;
|
||||
bool check_remote_items_exist(const std::vector<std::string>& file_paths) const;
|
||||
|
||||
bool remove_remote_dir(const std::string &dir_path, bool silent) const;
|
||||
|
||||
bool run_remote_template_command(const std::string& service_name, const std::string& command,
|
||||
std::vector<std::string> args, bool silent, std::map<std::string, std::string> extra_env_vars) const;
|
||||
bool run_remote_template_command_and_capture_output(const std::string& service_name, const std::string& command,
|
||||
std::vector<std::string> args, std::string & output, bool silent, std::map<std::string, std::string> extra_env_vars) const;
|
||||
|
||||
private:
|
||||
std::optional<sCommand> construct_standard_template_run_cmd(const std::string& service_name, const std::string& command, const std::vector<std::string> args, const bool silent) const;
|
||||
|
||||
private:
|
||||
std::string mServerName;
|
||||
std::map<std::string, std::string> mVariables;
|
||||
bool mValid;
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
|
||||
#endif // __SERVER_ENV_HPP
|
129
source/src/servers.cpp
Normal file
129
source/src/servers.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "servers.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "service_runner.hpp"
|
||||
#include "utils/tableprint.hpp"
|
||||
#include "utils/envmanager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "services.hpp"
|
||||
#include "config.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "contrib/transwarp.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
std::vector<ServerInfo> get_configured_servers() {
|
||||
std::vector<ServerInfo> servers;
|
||||
|
||||
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty())
|
||||
return servers;
|
||||
|
||||
for (auto servers_dir : lsdp) {
|
||||
if (!servers_dir.empty() && std::filesystem::exists(servers_dir)) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(servers_dir)) {
|
||||
if (std::filesystem::is_directory(entry)) {
|
||||
std::string server_name = entry.path().filename().string();
|
||||
|
||||
if (server_name.empty() || server_name[0]=='.' || server_name[0]=='_')
|
||||
continue;
|
||||
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl;
|
||||
continue;
|
||||
}
|
||||
servers.push_back({
|
||||
server_name,
|
||||
env.get_SSH_HOST(),
|
||||
env.get_SSH_USER(),
|
||||
env.get_SSH_PORT()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return servers;
|
||||
}
|
||||
|
||||
ServerInfo get_server_info(const std::string &server_name)
|
||||
{
|
||||
std::vector<std::string> lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty())
|
||||
return ServerInfo();
|
||||
|
||||
for (auto &config_dir : lsdp) {
|
||||
std::string server_dir = config_dir + "/" + server_name;
|
||||
if (std::filesystem::exists(server_dir)) {
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << server_dir << std::endl;
|
||||
continue;
|
||||
}
|
||||
return ServerInfo({server_name, env.get_SSH_HOST(), env.get_SSH_USER(), env.get_SSH_PORT()});
|
||||
}
|
||||
}
|
||||
return ServerInfo();
|
||||
}
|
||||
|
||||
|
||||
bool create_server(const std::string &server_name)
|
||||
{
|
||||
// 1. check if server name already exists
|
||||
std::string server_existing_dir = localpath::server(server_name);
|
||||
if (!server_existing_dir.empty()) {
|
||||
std::cerr << "Error: Server name already exists: " << server_name << std::endl;
|
||||
std::cerr << "Current server path: " << server_existing_dir << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. create a new directory in the user config directory
|
||||
auto lsdp = gConfig().get_local_server_definition_paths();
|
||||
if (lsdp.empty() || lsdp[0].empty()) {
|
||||
std::cerr << "Error: Local server definition path not found" << std::endl;
|
||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string server_dir = lsdp[0] + "/" + server_name;
|
||||
std::filesystem::create_directory(server_dir);
|
||||
|
||||
// 3. create a template server.env file in the server directory
|
||||
std::string user = getenv("USER");
|
||||
std::string server_env_path = server_dir + "/server.env";
|
||||
std::ofstream server_env_file(server_env_path);
|
||||
server_env_file << "SSH_HOST=" << server_name << std::endl;
|
||||
server_env_file << "SSH_USER=" << user << std::endl;
|
||||
server_env_file << "SSH_PORT=" << 22 << std::endl;
|
||||
server_env_file << std::endl;
|
||||
server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl;
|
||||
server_env_file.close();
|
||||
|
||||
// 4. add dropshell-agent service to server
|
||||
create_service(server_name, "dropshell-agent", "dropshell-agent", true); // silently create service.
|
||||
|
||||
std::cout << "Server created successfully: " << server_name << std::endl;
|
||||
std::cout << "Please complete the installation:" <<std::endl;
|
||||
std::cout << "1) edit the server configuration: dropshell edit " << server_name << std::endl;
|
||||
std::cout << "2) test ssh is working: dropshell ssh " << server_name << std::endl;
|
||||
std::cout << "3) install dropshell-agent: dropshell install " << server_name << " dropshell-agent" << std::endl;
|
||||
std::cout << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
void get_all_used_commands(std::set<std::string> &commands)
|
||||
{
|
||||
std::vector<ServerInfo> servers = get_configured_servers();
|
||||
for (const auto& server : servers)
|
||||
{
|
||||
auto services = dropshell::get_server_services_info(server.name);
|
||||
for (const auto& service : services)
|
||||
commands.merge(dropshell::get_used_commands(server.name, service.service_name));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
29
source/src/servers.hpp
Normal file
29
source/src/servers.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef SERVERS_HPP
|
||||
#define SERVERS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "service_runner.hpp" // for ServiceStatus
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// Server information structure
|
||||
struct ServerInfo {
|
||||
std::string name;
|
||||
std::string ssh_host;
|
||||
std::string ssh_user;
|
||||
std::string ssh_port;
|
||||
};
|
||||
|
||||
std::vector<ServerInfo> get_configured_servers();
|
||||
|
||||
ServerInfo get_server_info(const std::string& server_name);
|
||||
|
||||
bool create_server(const std::string& server_name);
|
||||
|
||||
void get_all_used_commands(std::set<std::string> &commands);
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
#endif // SERVERS_HPP
|
551
source/src/service_runner.cpp
Normal file
551
source/src/service_runner.cpp
Normal file
@ -0,0 +1,551 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <filesystem>
|
||||
#include <unistd.h>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "service_runner.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "services.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "command_registry.hpp"
|
||||
#include "shared_commands.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
static const std::string magic_string = "-_-";
|
||||
|
||||
service_runner::service_runner(const std::string& server_name, const std::string& service_name) :
|
||||
mServerEnv(server_name), mServer(server_name), mService(service_name), mValid(false)
|
||||
{
|
||||
if (server_name.empty() || service_name.empty())
|
||||
return;
|
||||
|
||||
// Initialize server environment
|
||||
if (!mServerEnv.is_valid())
|
||||
return;
|
||||
|
||||
mServiceInfo = get_service_info(server_name, service_name);
|
||||
if (mServiceInfo.service_name.empty())
|
||||
return;
|
||||
|
||||
mService = mServiceInfo.service_name;
|
||||
|
||||
mValid = !mServiceInfo.local_template_path.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool service_runner::nuke(bool silent)
|
||||
{
|
||||
maketitle("Nuking " + mService + " (" + mServiceInfo.template_name + ") on " + mServer);
|
||||
|
||||
if (!mServerEnv.is_valid()) return false; // should never hit this.
|
||||
|
||||
std::string remote_service_path = remotepath::service(mServer, mService);
|
||||
bool okay = mServerEnv.run_remote_template_command("dropshell-agent", "_nuke_other", {mService, remote_service_path}, silent, {});
|
||||
if (!okay)
|
||||
{
|
||||
std::cerr << "Warning: Nuke script failed" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
||||
|
||||
if (!silent) {
|
||||
std::cout << "There's nothing left on the remote server." << std::endl;
|
||||
std::cout << "You can remove the local files with:" << std::endl;
|
||||
std::cout << " rm -rf " << localpath::service(mServer,mService) << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool service_runner::fullnuke()
|
||||
{
|
||||
if (!nuke(true))
|
||||
{
|
||||
std::cerr << "Warning: Nuke script failed, aborting fullnuke!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string local_service_path = mServiceInfo.local_service_path;
|
||||
if (local_service_path.empty() || !fs::exists(local_service_path)) {
|
||||
std::cerr << "Error: Service directory not found: " << local_service_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string rm_cmd = "rm -rf " + quote(local_service_path);
|
||||
if (!execute_local_command(rm_cmd, nullptr, cMode::Silent)) {
|
||||
std::cerr << "Failed to remove service directory" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Run a command on the service.
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool service_runner::run_command(const std::string& command, std::vector<std::string> additional_args, std::map<std::string, std::string> env_vars) {
|
||||
if (!mServerEnv.is_valid()) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name);
|
||||
if (!tinfo.is_set()) {
|
||||
std::cerr << "Error: Template '" << mServiceInfo.template_name << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (command == "fullnuke")
|
||||
return fullnuke();
|
||||
|
||||
if (command == "nuke")
|
||||
{
|
||||
std::cout << "Nuking " << mService << " (" << mServiceInfo.template_name << ") on " << mServer << std::endl;
|
||||
return nuke();
|
||||
}
|
||||
|
||||
if (!gTemplateManager().template_command_exists(mServiceInfo.template_name, command)) {
|
||||
std::cout << "No command script for " << mServiceInfo.template_name << " : " << command << std::endl;
|
||||
return true; // nothing to run.
|
||||
}
|
||||
|
||||
// install doesn't require anything on the server yet.
|
||||
if (command == "install")
|
||||
return install_service(mServer, mService, false);
|
||||
|
||||
std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh";
|
||||
|
||||
// Check if service directory exists
|
||||
if (!mServerEnv.check_remote_dir_exists(remotepath::service(mServer, mService))) {
|
||||
std::cerr << "Error: Service is not installed: " << mService << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if command script exists
|
||||
if (!mServerEnv.check_remote_file_exists(script_path)) {
|
||||
std::cerr << "Error: Remote command script not found: " << script_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if env file exists
|
||||
if (!mServerEnv.check_remote_file_exists(remotefile::service_env(mServer, mService))) {
|
||||
std::cerr << "Error: Service config file not found: " << remotefile::service_env(mServer, mService) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// if (command == "uninstall")
|
||||
// return uninstall();
|
||||
|
||||
if (command == "ssh") {
|
||||
interactive_ssh_service();
|
||||
return true;
|
||||
}
|
||||
if (command == "restore") {
|
||||
if (additional_args.size() < 1) {
|
||||
std::cerr << "Error: restore requires a backup file:" << std::endl;
|
||||
std::cerr << "dropshell restore <server> <service> <backup-file>" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return restore(additional_args[0], false);
|
||||
}
|
||||
if (command == "backup") {
|
||||
return backup(false);
|
||||
}
|
||||
|
||||
// Run the generic command
|
||||
std::vector<std::string> args; // not passed through yet.
|
||||
return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars);
|
||||
}
|
||||
|
||||
|
||||
std::map<std::string, ServiceStatus> service_runner::get_all_services_status(std::string server_name)
|
||||
{
|
||||
std::map<std::string, ServiceStatus> status;
|
||||
|
||||
std::string command = "_allservicesstatus";
|
||||
std::string service_name = "dropshell-agent";
|
||||
|
||||
if (!gTemplateManager().template_command_exists(service_name, "shared/"+command))
|
||||
{
|
||||
std::cerr << "Error: " << service_name << " does not contain the " << command << " script" << std::endl;
|
||||
return status;
|
||||
}
|
||||
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment" << std::endl;
|
||||
return status;
|
||||
}
|
||||
|
||||
std::string output;
|
||||
if (!env.run_remote_template_command_and_capture_output(service_name, "shared/"+command, {}, output, true, {}))
|
||||
return status;
|
||||
|
||||
std::stringstream ss(output);
|
||||
std::string line;
|
||||
while (std::getline(ss, line)) {
|
||||
std::string key, value;
|
||||
std::size_t pos = line.find("=");
|
||||
if (pos != std::string::npos) {
|
||||
key = dequote(trim(line.substr(0, pos)));
|
||||
value = dequote(trim(line.substr(pos + 1)));
|
||||
|
||||
// decode key, it's of format SERVICENAME_[HEALTH|PORTS]
|
||||
std::string service_name = key.substr(0, key.find_last_of("_"));
|
||||
std::string status_type = key.substr(key.find_last_of("_") + 1);
|
||||
|
||||
if (status_type == "HEALTH") { // healthy|unhealthy|unknown
|
||||
if (value == "healthy")
|
||||
status[service_name].health = HealthStatus::HEALTHY;
|
||||
else if (value == "unhealthy")
|
||||
status[service_name].health = HealthStatus::UNHEALTHY;
|
||||
else if (value == "unknown")
|
||||
status[service_name].health = HealthStatus::UNKNOWN;
|
||||
else
|
||||
status[service_name].health = HealthStatus::ERROR;
|
||||
} else if (status_type == "PORTS") { // port1,port2,port3
|
||||
std::vector<std::string> ports = string2multi(value);
|
||||
for (const auto& port : ports) {
|
||||
if (port!="unknown")
|
||||
status[service_name].ports.push_back(str2int(port));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) {
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
if (serverpath.empty()) {
|
||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
server_env_manager env(server_name);
|
||||
if (!env.is_valid()) {
|
||||
std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
sCommand scommand("", "bash",{});
|
||||
return execute_ssh_command(env.get_SSH_INFO(), scommand, cMode::Interactive);
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool service_runner::interactive_ssh_service()
|
||||
{
|
||||
std::set<std::string> used_commands = get_used_commands(mServer, mService);
|
||||
if (used_commands.find("ssh") == used_commands.end()) {
|
||||
std::cerr << "Error: "<< mService <<" does not support ssh" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> args; // not passed through yet.
|
||||
return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {});
|
||||
}
|
||||
|
||||
bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent)
|
||||
{
|
||||
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||
return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
|
||||
bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent)
|
||||
{
|
||||
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||
return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||
}
|
||||
|
||||
bool service_runner::restore(std::string backup_file, bool silent)
|
||||
{
|
||||
if (backup_file.empty()) {
|
||||
std::cerr << "Error: not enough arguments. dropshell restore <server> <service> <backup-file>" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
|
||||
if (backup_file == "latest") {
|
||||
// get the latest backup file from the server
|
||||
backup_file = get_latest_backup_file(mServer, mService);
|
||||
}
|
||||
|
||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string();
|
||||
|
||||
if (! std::filesystem::exists(local_backup_file_path)) {
|
||||
std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// split the backup filename into parts based on the magic string
|
||||
std::vector<std::string> parts = dropshell::split(backup_file, magic_string);
|
||||
if (parts.size() != 4) {
|
||||
std::cerr << "Error: Backup file format is incompatible, - in one of the names?" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string backup_server_name = parts[0];
|
||||
std::string backup_template_name = parts[1];
|
||||
std::string backup_service_name = parts[2];
|
||||
std::string backup_datetime = parts[3];
|
||||
|
||||
if (backup_template_name != mServiceInfo.template_name) {
|
||||
std::cerr << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string nicedate = std::string(backup_datetime).substr(0, 10);
|
||||
|
||||
std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "<<backup_server_name<<", onto "<<mServer<<"/"<<mService<<std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "*** ALL DATA FOR "<<mServer<<"/"<<mService<<" WILL BE OVERWRITTEN! ***"<<std::endl;
|
||||
|
||||
// run the restore script
|
||||
std::cout << "OK, here goes..." << std::endl;
|
||||
|
||||
{ // backup existing service
|
||||
maketitle("1) Backing up old service... ");
|
||||
if (!backup(true)) // silent=true
|
||||
{
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "Error: Backup failed, restore aborted." << std::endl;
|
||||
std::cerr << "You can try using dropshell install "<<mServer<<" "<<mService<<" to install the service afresh." << std::endl;
|
||||
std::cerr << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "Backup complete." << std::endl;
|
||||
}
|
||||
|
||||
{ // uninstall service, then nuke it.
|
||||
maketitle("2) Uninstalling old service...");
|
||||
// if (!uninstall(true))
|
||||
// return false;
|
||||
|
||||
maketitle("3) Nuking old service...");
|
||||
// if (!nuke(true))
|
||||
// return false;
|
||||
}
|
||||
|
||||
|
||||
{ // restore service from backup
|
||||
maketitle("4) Restoring service data from backup...");
|
||||
std::string remote_backups_dir = remotepath::backups(mServer);
|
||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_file;
|
||||
|
||||
// Copy backup file from local to server
|
||||
if (!scp_file_to_remote(local_backup_file_path, remote_backup_file_path, silent)) {
|
||||
std::cerr << "Failed to copy backup file from local to server" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||
mServerEnv.run_remote_template_command(mService, "restore", {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||
|
||||
|
||||
{ // installing fresh service
|
||||
maketitle("5) Non-destructive install of fresh service...");
|
||||
if (!install_service(mServer, mService, true))
|
||||
return false;
|
||||
}
|
||||
|
||||
bool healthy = false;
|
||||
{// healthcheck the service
|
||||
maketitle("6) Healthchecking service...");
|
||||
std::string green_tick = "\033[32m✓\033[0m";
|
||||
std::string red_cross = "\033[31m✗\033[0m";
|
||||
healthy= (mServerEnv.run_remote_template_command(mService, "status", {}, silent, {}));
|
||||
if (!silent)
|
||||
std::cout << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||
}
|
||||
|
||||
return healthy;
|
||||
}
|
||||
|
||||
|
||||
bool name_breaks_backups(std::string name)
|
||||
{
|
||||
// if name contains -_-, return true
|
||||
return name.find("-_-") != std::string::npos;
|
||||
}
|
||||
|
||||
// backup the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// 1. run backup.sh on the server
|
||||
// 2. create a backup file with format server-service-datetime.tgz
|
||||
// 3. store it in the server's DROPSHELL_DIR/backups folder
|
||||
// 4. copy it to the local user_dir/backups folder
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Backup the service.
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
bool service_runner::backup(bool silent) {
|
||||
auto service_info = get_service_info(mServer, mService);
|
||||
if (service_info.local_service_path.empty()) {
|
||||
std::cerr << "Error: Service not found" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const std::string command = "backup";
|
||||
|
||||
if (!gTemplateManager().template_command_exists(service_info.template_name, command)) {
|
||||
std::cout << "No backup script for " << service_info.template_name << std::endl;
|
||||
return true; // nothing to back up.
|
||||
}
|
||||
|
||||
// Check if basic installed stuff is in place.
|
||||
std::string remote_service_template_path = remotepath::service_template(mServer, mService);
|
||||
std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
||||
std::string remote_service_config_path = remotepath::service_config(mServer, mService);
|
||||
if (!mServerEnv.check_remote_items_exist({
|
||||
remotepath::service(mServer, mService),
|
||||
remote_command_script_file,
|
||||
remotefile::service_env(mServer, mService)})
|
||||
)
|
||||
{
|
||||
std::cerr << "Error: Required service directories not found on remote server" << std::endl;
|
||||
std::cerr << "Is the service installed?" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create backups directory on server if it doesn't exist
|
||||
std::string remote_backups_dir = remotepath::backups(mServer);
|
||||
if (!silent) std::cout << "Remote backups directory on "<< mServer <<": " << remote_backups_dir << std::endl;
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
||||
if (!execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand("",mkdir_cmd, {}), cMode::Silent)) {
|
||||
std::cerr << "Failed to create backups directory on server" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create backups directory locally if it doesn't exist
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty()) {
|
||||
std::cerr << "Error: Local backups directory not found" << std::endl;
|
||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!std::filesystem::exists(local_backups_dir))
|
||||
std::filesystem::create_directories(local_backups_dir);
|
||||
|
||||
// Get current datetime for backup filename
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto time = std::chrono::system_clock::to_time_t(now);
|
||||
std::stringstream datetime;
|
||||
datetime << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
|
||||
|
||||
if (name_breaks_backups(mServer)) {std::cerr << "Error: Server name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
||||
if (name_breaks_backups(mService)) {std::cerr << "Error: Service name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
||||
if (name_breaks_backups(service_info.template_name)) {std::cerr << "Error: Service template name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
||||
|
||||
// Construct backup filename
|
||||
std::string backup_filename = mServer + magic_string + service_info.template_name + magic_string + mService + magic_string + datetime.str() + ".tgz";
|
||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_filename;
|
||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_filename).string();
|
||||
|
||||
// assert that the backup filename is valid - -_- appears exactly 3 times in local_backup_file_path.
|
||||
ASSERT(3 == count_substring(magic_string, local_backup_file_path), "Invalid backup filename");
|
||||
|
||||
{ // Run backup script
|
||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||
if (!mServerEnv.run_remote_template_command(mService, command, {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) {
|
||||
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy backup file from server to local
|
||||
if (!scp_file_from_remote(remote_backup_file_path, local_backup_file_path, silent)) {
|
||||
std::cerr << "Failed to copy backup file from server" << std::endl;
|
||||
return false;
|
||||
}
|
||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||
|
||||
if (!silent) {
|
||||
std::cout << "Backup created successfully. Restore with:"<<std::endl;
|
||||
std::cout << " dropshell restore " << mServer << " " << mService << " " << backup_filename << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
cRemoteTempFolder::cRemoteTempFolder(const server_env_manager &server_env) : mServerEnv(server_env)
|
||||
{
|
||||
std::string p = remotepath::temp_files(server_env.get_server_name()) + "/" + random_alphanumeric_string(10);
|
||||
std::string mkdir_cmd = "mkdir -p " + quote(p);
|
||||
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Silent))
|
||||
std::cerr << "Failed to create temp directory on server" << std::endl;
|
||||
else
|
||||
mPath = p;
|
||||
}
|
||||
|
||||
cRemoteTempFolder::~cRemoteTempFolder()
|
||||
{
|
||||
std::string rm_cmd = "rm -rf " + quote(mPath);
|
||||
execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand("", rm_cmd, {}), cMode::Silent);
|
||||
}
|
||||
|
||||
std::string cRemoteTempFolder::path() const
|
||||
{
|
||||
return mPath;
|
||||
}
|
||||
|
||||
// Helper function to get the latest backup file for a given server and service
|
||||
std::string service_runner::get_latest_backup_file(const std::string& server, const std::string& service) {
|
||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) {
|
||||
std::cerr << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||
return "";
|
||||
}
|
||||
|
||||
// Get the template name for this service
|
||||
LocalServiceInfo info = get_service_info(server, service);
|
||||
if (info.template_name.empty()) {
|
||||
std::cerr << "Error: Could not determine template name for service: " << service << std::endl;
|
||||
return "";
|
||||
}
|
||||
|
||||
// Build the expected prefix for backup files
|
||||
std::string prefix = server + magic_string + info.template_name + magic_string + service + magic_string;
|
||||
std::string latest_file;
|
||||
std::string latest_datetime;
|
||||
|
||||
std::cout << "Looking for backup files in " << local_backups_dir << std::endl;
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(local_backups_dir)) {
|
||||
if (!entry.is_regular_file()) continue;
|
||||
std::string filename = entry.path().filename().string();
|
||||
if (filename.rfind(prefix, 0) == 0) { // starts with prefix
|
||||
// Extract the datetime part
|
||||
size_t dt_start = prefix.size();
|
||||
size_t dt_end = filename.find(".tgz", dt_start);
|
||||
if (dt_end == std::string::npos) continue;
|
||||
std::string datetime = filename.substr(dt_start, dt_end - dt_start);
|
||||
std::cout << "Found backup file: " << filename << " with datetime: " << datetime << std::endl;
|
||||
if (datetime > latest_datetime) {
|
||||
latest_datetime = datetime;
|
||||
latest_file = filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (latest_file.empty()) {
|
||||
std::cerr << "Error: No backup files found for " << server << ", " << service << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Latest backup file: " << latest_file << std::endl;
|
||||
return latest_file;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
102
source/src/service_runner.hpp
Normal file
102
source/src/service_runner.hpp
Normal file
@ -0,0 +1,102 @@
|
||||
// server_service.hpp
|
||||
//
|
||||
// manage a service on a server
|
||||
//
|
||||
|
||||
#ifndef SERVICE_RUNNER_HPP
|
||||
#define SERVICE_RUNNER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "server_env_manager.hpp"
|
||||
#include "services.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/hash.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
typedef enum HealthStatus {
|
||||
HEALTHY,
|
||||
UNHEALTHY,
|
||||
NOTINSTALLED,
|
||||
ERROR,
|
||||
UNKNOWN
|
||||
} HealthStatus;
|
||||
|
||||
typedef struct ServiceStatus {
|
||||
HealthStatus health;
|
||||
std::vector<int> ports;
|
||||
} ServiceStatus;
|
||||
|
||||
|
||||
class service_runner {
|
||||
public:
|
||||
service_runner(const std::string& server_name, const std::string& service_name);
|
||||
bool isValid() const { return mValid; }
|
||||
|
||||
// run a command over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// first check that the command corresponds to a valid .sh file in the service directory
|
||||
// then run the command, passing the {service_name}.env file as an argument
|
||||
// do a lot of checks, such as:
|
||||
// checking that we can ssh to the server.
|
||||
// checking whether the service directory exists on the server.
|
||||
// checking that the command exists in the service directory.
|
||||
// checking that the command is a valid .sh file.
|
||||
// checking that the {service_name}.env file exists in the service directory.
|
||||
bool run_command(const std::string& command, std::vector<std::string> additional_args={}, std::map<std::string, std::string> env_vars={});
|
||||
|
||||
// check health of service. Silent.
|
||||
// 1. run status.sh on the server
|
||||
// 2. return the output of the status.sh script
|
||||
|
||||
HealthStatus is_healthy();
|
||||
|
||||
std::string healthtick();
|
||||
std::string healthmark();
|
||||
|
||||
public:
|
||||
// backup and restore
|
||||
bool backup(bool silent=false);
|
||||
bool restore(std::string backup_file, bool silent=false);
|
||||
|
||||
// nuke the service
|
||||
bool nuke(bool silent=false); // nukes all data for this service on the remote server
|
||||
bool fullnuke(); // nuke all data for this service on the remote server, and then nukes all the local service definitionfiles
|
||||
|
||||
// launch an interactive ssh session on a server or service
|
||||
// replaces the current dropshell process with the ssh process
|
||||
bool interactive_ssh_service();
|
||||
|
||||
bool scp_file_to_remote(const std::string& local_path, const std::string& remote_path, bool silent=false);
|
||||
bool scp_file_from_remote(const std::string& remote_path, const std::string& local_path, bool silent=false);
|
||||
public:
|
||||
// utility functions
|
||||
static std::string get_latest_backup_file(const std::string& server, const std::string& service);
|
||||
static bool interactive_ssh(const std::string & server_name, const std::string & command);
|
||||
static std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
|
||||
|
||||
private:
|
||||
std::string mServer;
|
||||
server_env_manager mServerEnv;
|
||||
LocalServiceInfo mServiceInfo;
|
||||
std::string mService;
|
||||
bool mValid;
|
||||
|
||||
// Helper methods
|
||||
public:
|
||||
};
|
||||
|
||||
class cRemoteTempFolder {
|
||||
public:
|
||||
cRemoteTempFolder(const server_env_manager & server_env); // create a temp folder on the remote server
|
||||
~cRemoteTempFolder(); // delete the temp folder on the remote server
|
||||
std::string path() const; // get the path to the temp folder on the remote server
|
||||
private:
|
||||
std::string mPath;
|
||||
const server_env_manager & mServerEnv;
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
#endif // SERVICE_RUNNER_HPP
|
282
source/src/services.cpp
Normal file
282
source/src/services.cpp
Normal file
@ -0,0 +1,282 @@
|
||||
#include "services.hpp"
|
||||
#include "utils/envmanager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include "servers.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
bool SIvalid(const LocalServiceInfo& service_info) {
|
||||
return !service_info.service_name.empty() &&
|
||||
!service_info.template_name.empty() &&
|
||||
!service_info.local_service_path.empty() &&
|
||||
!service_info.local_template_path.empty();
|
||||
}
|
||||
|
||||
std::vector<LocalServiceInfo> get_server_services_info(const std::string& server_name) {
|
||||
std::vector<LocalServiceInfo> services;
|
||||
|
||||
if (server_name.empty())
|
||||
return services;
|
||||
|
||||
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||
if (local_server_definition_paths.empty()) {
|
||||
std::cerr << "Error: No local server definition paths found" << std::endl;
|
||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return services;
|
||||
}
|
||||
|
||||
for (const auto& server_definition_path : local_server_definition_paths) {
|
||||
fs::path serverpath = server_definition_path + "/" + server_name;
|
||||
if (fs::exists(serverpath)) // service is on that server...
|
||||
for (const auto& entry : fs::directory_iterator(serverpath)) {
|
||||
if (fs::is_directory(entry)) {
|
||||
std::string dirname = entry.path().filename().string();
|
||||
if (dirname.empty() || dirname[0] == '.' || dirname[0] == '_')
|
||||
continue;
|
||||
auto service = get_service_info(server_name, dirname);
|
||||
if (!service.local_service_path.empty())
|
||||
services.push_back(service);
|
||||
else
|
||||
std::cerr << "Warning: Failed to get service info for " << dirname << " on server " << server_name << std::endl;
|
||||
}
|
||||
} // end of for
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
LocalServiceInfo get_service_info(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
LocalServiceInfo service;
|
||||
|
||||
if (server_name.empty() || service_name.empty())
|
||||
return LocalServiceInfo();
|
||||
|
||||
service.service_name = service_name;
|
||||
|
||||
service.local_service_path = localpath::service(server_name, service_name);
|
||||
if (service.local_service_path.empty())
|
||||
return LocalServiceInfo();
|
||||
|
||||
|
||||
// now set the template name and path.
|
||||
std::map<std::string, std::string> variables;
|
||||
if (!get_all_service_env_vars(server_name, service_name, variables))
|
||||
return LocalServiceInfo();
|
||||
|
||||
// confirm TEMPLATE is defined.
|
||||
auto it = variables.find("TEMPLATE");
|
||||
if (it == variables.end()) {
|
||||
std::cerr << "Error: TEMPLATE variable not defined in service " << service_name << " on server " << server_name << std::endl;
|
||||
return LocalServiceInfo();
|
||||
}
|
||||
service.template_name = it->second;
|
||||
|
||||
template_info tinfo = gTemplateManager().get_template_info(service.template_name);
|
||||
if (!tinfo.is_set()) {
|
||||
std::cerr << "Error: Template '" << service.template_name << "' not found" << std::endl;
|
||||
return LocalServiceInfo();
|
||||
}
|
||||
|
||||
// find the template path
|
||||
service.local_template_path = tinfo.local_template_path();
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
std::set<std::string> get_used_commands(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::set<std::string> commands;
|
||||
|
||||
if (server_name.empty() || service_name.empty())
|
||||
return commands;
|
||||
|
||||
auto service_info = get_service_info(server_name, service_name);
|
||||
if (service_info.local_template_path.empty()) {
|
||||
std::cerr << "Error: Service not found: " << service_name << std::endl;
|
||||
return commands;
|
||||
}
|
||||
|
||||
// iterate over all files in the template path, and add the command name to the set.
|
||||
// commands are .sh files that don't begin with _
|
||||
for (const auto& entry : fs::directory_iterator(service_info.local_template_path)) {
|
||||
if (fs::is_regular_file(entry) && entry.path().extension() == ".sh" && (entry.path().filename().string().rfind("_", 0) != 0))
|
||||
commands.insert(entry.path().stem().string());
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
std::set<std::string> list_backups(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::set<std::string> backups;
|
||||
|
||||
if (server_name.empty() || service_name.empty())
|
||||
return backups;
|
||||
|
||||
// need to find the template for the service.
|
||||
auto service_info = get_service_info(server_name, service_name);
|
||||
if (service_info.local_template_path.empty()) {
|
||||
std::cerr << "Error: Service not found: " << service_name << std::endl;
|
||||
return backups;
|
||||
}
|
||||
|
||||
std::string backups_dir = gConfig().get_local_backup_path();
|
||||
if (backups_dir.empty())
|
||||
return backups;
|
||||
|
||||
if (fs::exists(backups_dir)) {
|
||||
for (const auto& entry : fs::directory_iterator(backups_dir)) {
|
||||
if (fs::is_regular_file(entry) && entry.path().extension() == ".tgz")
|
||||
if (entry.path().filename().string().find(service_info.template_name) != std::string::npos)
|
||||
{
|
||||
backups.insert(entry.path().filename().string());
|
||||
}
|
||||
}
|
||||
}
|
||||
return backups;
|
||||
}
|
||||
|
||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, bool silent)
|
||||
{
|
||||
if (server_name.empty() || template_name.empty() || service_name.empty())
|
||||
return false;
|
||||
|
||||
std::string service_dir = localpath::service(server_name, service_name);
|
||||
|
||||
if (service_dir.empty())
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
||||
std::cerr << "Please check the server name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all servers with 'dropshell servers'" << std::endl;
|
||||
std::cerr << "You can create a new server with 'dropshell create-server " << server_name << "'" << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fs::exists(service_dir))
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Service already exists: " << service_name << std::endl;
|
||||
std::cerr << "Current service path: " << service_dir << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template_info tinfo = gTemplateManager().get_template_info(template_name);
|
||||
if (!tinfo.is_set())
|
||||
{
|
||||
if (!silent)
|
||||
{
|
||||
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
|
||||
std::cerr << "Please check the template name is correct and try again" << std::endl;
|
||||
std::cerr << "You can list all templates with 'dropshell templates'" << std::endl;
|
||||
std::cerr << "You can create a new template with 'dropshell create-template " << template_name << "'" << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// check template is all good.
|
||||
if (!gTemplateManager().test_template(tinfo.local_template_path()))
|
||||
{
|
||||
if (!silent)
|
||||
std::cerr << "Error: Template '" << template_name << "' is not valid" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create the service directory
|
||||
fs::create_directory(service_dir);
|
||||
|
||||
// copy the template config files to the service directory
|
||||
recursive_copy(tinfo.local_template_path()/"config", service_dir);
|
||||
|
||||
if (!silent)
|
||||
{
|
||||
std::cout << "Service " << service_name <<" created successfully"<<std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << "To complete the installation, please:" << std::endl;
|
||||
std::cout << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
|
||||
std::cout << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool get_all_service_env_vars(const std::string &server_name, const std::string &service_name, std::map<std::string, std::string> & all_env_vars)
|
||||
{
|
||||
all_env_vars.clear();
|
||||
|
||||
if (localpath::service(server_name, service_name).empty() || !fs::exists(localpath::service(server_name, service_name)))
|
||||
{
|
||||
std::cerr << "Error: Service not found: " << service_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// add in some handy variables.
|
||||
all_env_vars["CONFIG_PATH"] = remotepath::service_config(server_name,service_name);
|
||||
all_env_vars["SERVER"] = server_name;
|
||||
all_env_vars["SERVICE"] = service_name;
|
||||
all_env_vars["AGENT_PATH"] = remotepath::agent(server_name);
|
||||
|
||||
ServerInfo server_info = get_server_info(server_name);
|
||||
if (server_info.ssh_host.empty())
|
||||
std::cerr << "Error: Server " << server_name << " not found - ssh_host empty, so HOST_NAME not set" << std::endl;
|
||||
all_env_vars["HOST_NAME"] = server_info.ssh_host;
|
||||
|
||||
// Lambda function to load environment variables from a file
|
||||
auto load_env_file = [&all_env_vars](const std::string& file) {
|
||||
if (!file.empty() && std::filesystem::exists(file)) {
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(file);
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
else
|
||||
std::cout << "Warning: Expected environment file not found: " << file << std::endl;
|
||||
};
|
||||
|
||||
// Load environment files
|
||||
load_env_file(localfile::service_env(server_name, service_name));
|
||||
load_env_file(localfile::template_info_env(server_name, service_name));
|
||||
|
||||
// determine template name.
|
||||
auto it = all_env_vars.find("TEMPLATE");
|
||||
if (it == all_env_vars.end()) {
|
||||
std::cerr << std::endl << std::endl;
|
||||
std::cerr << "Error: TEMPLATE variable not defined in service " << service_name << " on server " << server_name << std::endl;
|
||||
std::cerr << "The TEMPLATE variable is required to determine the template name." << std::endl;
|
||||
std::cerr << "Please check the service.env file and the .template_info.env file in:" << std::endl;
|
||||
std::cerr << " " << localpath::service(server_name, service_name) << std::endl << std::endl;
|
||||
return false;
|
||||
}
|
||||
template_info tinfo = gTemplateManager().get_template_info(it->second);
|
||||
if (!tinfo.is_set()) {
|
||||
std::cerr << "Error: Template '" << it->second << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string default_env_file = tinfo.local_template_path()/"_default.env";
|
||||
if (!fs::exists(default_env_file)) {
|
||||
std::cerr << "Error: Template default env file '" << default_env_file << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
load_env_file(default_env_file);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
34
source/src/services.hpp
Normal file
34
source/src/services.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef SERVICES_HPP
|
||||
#define SERVICES_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
struct LocalServiceInfo {
|
||||
std::string service_name;
|
||||
std::string template_name;
|
||||
std::string local_service_path;
|
||||
std::string local_template_path;
|
||||
};
|
||||
|
||||
bool SIvalid(const LocalServiceInfo& service_info);
|
||||
|
||||
std::vector<LocalServiceInfo> get_server_services_info(const std::string& server_name);
|
||||
|
||||
LocalServiceInfo get_service_info(const std::string& server_name, const std::string& service_name);
|
||||
std::set<std::string> get_used_commands(const std::string& server_name, const std::string& service_name);
|
||||
|
||||
// get all env vars for a given service
|
||||
bool get_all_service_env_vars(const std::string& server_name, const std::string& service_name, std::map<std::string, std::string> & all_env_vars);
|
||||
|
||||
// list all backups for a given service (across all servers)
|
||||
std::set<std::string> list_backups(const std::string& server_name, const std::string& service_name);
|
||||
|
||||
bool create_service(const std::string& server_name, const std::string& template_name, const std::string& service_name, bool silent=false);
|
||||
} // namespace dropshell
|
||||
|
||||
#endif
|
359
source/src/templates.cpp
Normal file
359
source/src/templates.cpp
Normal file
@ -0,0 +1,359 @@
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <map>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
#include "utils/envmanager.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "templates.hpp"
|
||||
#include "config.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// template_source_local
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
std::set<std::string> template_source_local::get_template_list() {
|
||||
std::set<std::string> templates;
|
||||
|
||||
// Helper function to add templates from a directory
|
||||
auto add_templates_from_dir = [&templates](const std::string& dir_path) {
|
||||
if (!std::filesystem::exists(dir_path))
|
||||
return;
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(dir_path))
|
||||
if (entry.is_directory())
|
||||
templates.insert(entry.path().filename().string());
|
||||
};
|
||||
|
||||
add_templates_from_dir(mLocalPath);
|
||||
return templates;
|
||||
}
|
||||
|
||||
bool template_source_local::has_template(const std::string& template_name) {
|
||||
std::filesystem::path path = mLocalPath / template_name;
|
||||
return (std::filesystem::exists(path));
|
||||
}
|
||||
|
||||
bool template_source_local::template_command_exists(const std::string& template_name, const std::string& command) {
|
||||
std::filesystem::path path = mLocalPath / template_name / (command+".sh");
|
||||
return std::filesystem::exists(path);
|
||||
}
|
||||
|
||||
template_info template_source_local::get_template_info(const std::string& template_name) {
|
||||
std::filesystem::path path = mLocalPath / template_name;
|
||||
|
||||
if (!std::filesystem::exists(path))
|
||||
return template_info();
|
||||
|
||||
return template_info(
|
||||
template_name,
|
||||
mLocalPath.string(),
|
||||
path
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// template_source_registry
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
std::set<std::string> template_source_registry::get_template_list()
|
||||
{
|
||||
#pragma message("TODO: Implement template_source_registry::get_template_list")
|
||||
return std::set<std::string>();
|
||||
}
|
||||
|
||||
bool template_source_registry::has_template(const std::string& template_name)
|
||||
{
|
||||
#pragma message("TODO: Implement template_source_registry::has_template")
|
||||
return false;
|
||||
}
|
||||
|
||||
template_info template_source_registry::get_template_info(const std::string& template_name)
|
||||
{
|
||||
#pragma message("TODO: Implement template_source_registry::get_template_info")
|
||||
return template_info();
|
||||
}
|
||||
|
||||
bool template_source_registry::template_command_exists(const std::string& template_name, const std::string& command)
|
||||
{
|
||||
#pragma message("TODO: Implement template_source_registry::template_command_exists")
|
||||
return false;
|
||||
}
|
||||
|
||||
std::filesystem::path template_source_registry::get_cache_dir()
|
||||
{
|
||||
#pragma message("TODO: Implement template_source_registry::get_cache_dir")
|
||||
return std::filesystem::path();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// template_manager
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
void template_manager::list_templates() const {
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
auto templates = get_template_list();
|
||||
|
||||
if (templates.empty()) {
|
||||
std::cout << "No templates found." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Available templates:" << std::endl;
|
||||
|
||||
// print templates.
|
||||
std::cout << std::string(60, '-') << std::endl;
|
||||
bool first = true;
|
||||
for (const auto& t : templates) {
|
||||
std::cout << (first ? "" : ", ") << t;
|
||||
first = false;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << std::string(60, '-') << std::endl;
|
||||
}
|
||||
|
||||
std::set<std::string> template_manager::get_template_list() const
|
||||
{
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
std::set<std::string> templates;
|
||||
for (const auto& source : mSources) {
|
||||
auto source_templates = source->get_template_list();
|
||||
templates.insert(source_templates.begin(), source_templates.end());
|
||||
}
|
||||
return templates;
|
||||
}
|
||||
|
||||
bool template_manager::has_template(const std::string &template_name) const
|
||||
{
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
template_source_interface* source = get_source(template_name);
|
||||
if (!source)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template_info template_manager::get_template_info(const std::string &template_name) const
|
||||
{
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
template_source_interface* source = get_source(template_name);
|
||||
if (source)
|
||||
return source->get_template_info(template_name);
|
||||
|
||||
// fail
|
||||
return template_info();
|
||||
}
|
||||
|
||||
bool template_manager::template_command_exists(const std::string &template_name, const std::string &command) const
|
||||
{
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
template_source_interface* source = get_source(template_name);
|
||||
if (!source) {
|
||||
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return source->template_command_exists(template_name, command);
|
||||
}
|
||||
|
||||
bool template_manager::create_template(const std::string &template_name) const
|
||||
{
|
||||
// 1. Create a new directory in the user templates directory
|
||||
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||
|
||||
if (local_server_definition_paths.empty()) {
|
||||
std::cerr << "Error: No local server definition paths found" << std::endl;
|
||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto info = get_template_info(template_name);
|
||||
if (info.is_set()) {
|
||||
std::cerr << "Error: Template '" << template_name << "' already exists at " << info.locationID() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto local_template_paths = gConfig().get_template_local_paths();
|
||||
if (local_template_paths.empty()) {
|
||||
std::cerr << "Error: No local template paths found" << std::endl;
|
||||
std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string new_template_path = local_template_paths[0] + "/" + template_name;
|
||||
|
||||
// Create the new template directory
|
||||
std::filesystem::create_directories(new_template_path);
|
||||
|
||||
// 2. Copy the example template from the system templates directory
|
||||
auto example_info = gTemplateManager().get_template_info("example-nginx");
|
||||
if (!example_info.is_set()) {
|
||||
std::cerr << "Error: Example template not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::string example_template_path = example_info.local_template_path();
|
||||
|
||||
// Copy all files from example template to new template
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(example_template_path)) {
|
||||
std::string relative_path = entry.path().string().substr(example_template_path.length());
|
||||
std::string target_path = new_template_path + relative_path;
|
||||
|
||||
if (entry.is_directory()) {
|
||||
std::filesystem::create_directory(target_path);
|
||||
} else {
|
||||
std::filesystem::copy_file(entry.path(), target_path);
|
||||
}
|
||||
}
|
||||
|
||||
// modify the TEMPLATE=example line in the .template_info.env file to TEMPLATE=<template_name>
|
||||
std::string search_string = "TEMPLATE=";
|
||||
std::string replacement_line = "TEMPLATE=" + template_name;
|
||||
std::string service_env_path = new_template_path + "/config/.template_info.env";
|
||||
if (!replace_line_in_file(service_env_path, search_string, replacement_line)) {
|
||||
std::cerr << "Error: Failed to replace TEMPLATE= line in the .template_info.env file" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. Print out the README.txt file and the path
|
||||
std::string readme_path = new_template_path + "/README.txt";
|
||||
if (std::filesystem::exists(readme_path)) {
|
||||
std::cout << "\nREADME contents:" << std::endl;
|
||||
std::cout << std::string(60, '-') << std::endl;
|
||||
|
||||
std::ifstream readme_file(readme_path);
|
||||
if (readme_file.is_open()) {
|
||||
std::string line;
|
||||
while (std::getline(readme_file, line)) {
|
||||
std::cout << line << std::endl;
|
||||
}
|
||||
readme_file.close();
|
||||
}
|
||||
std::cout << std::string(60, '-') << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << "Template '" << template_name << "' created at " << new_template_path << std::endl;
|
||||
return test_template(new_template_path);
|
||||
}
|
||||
|
||||
void template_manager::load_sources()
|
||||
{
|
||||
ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty).");
|
||||
ASSERT(gConfig().is_config_set(), "Config not set.");
|
||||
ASSERT(!mLoaded, "Template manager already loaded.");
|
||||
auto local_template_paths = gConfig().get_template_local_paths();
|
||||
if (local_template_paths.empty())
|
||||
return;
|
||||
for (const auto& path : local_template_paths)
|
||||
mSources.push_back(std::make_unique<template_source_local>(path));
|
||||
|
||||
auto registry_urls = gConfig().get_template_registry_urls();
|
||||
for (const auto& url : registry_urls)
|
||||
mSources.push_back(std::make_unique<template_source_registry>(url));
|
||||
|
||||
mLoaded = true;
|
||||
}
|
||||
|
||||
void template_manager::print_sources() const
|
||||
{
|
||||
std::cout << "Template sources: ";
|
||||
for (const auto& source : mSources) {
|
||||
std::cout << "[" << source->get_description() << "] ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
bool template_manager::required_file(std::string path, std::string template_name)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cerr << "Error: " << path << " file not found in template - REQUIRED." << template_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template_source_interface *template_manager::get_source(const std::string &template_name) const
|
||||
{
|
||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||
for (const auto& source : mSources) {
|
||||
if (source->has_template(template_name)) {
|
||||
return source.get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool template_manager::test_template(const std::string &template_path)
|
||||
{
|
||||
std::string template_name = std::filesystem::path(template_path).filename().string();
|
||||
|
||||
std::vector<std::string> required_files = {
|
||||
"config/service.env",
|
||||
"config/.template_info.env",
|
||||
"_default.env",
|
||||
"install.sh",
|
||||
"uninstall.sh",
|
||||
"nuke.sh"
|
||||
};
|
||||
|
||||
for (const auto& file : required_files) {
|
||||
if (!required_file(template_path + "/" + file, template_name))
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// check TEMPLATE= line.
|
||||
std::map<std::string, std::string> all_env_vars;
|
||||
std::vector<std::string> env_files = {
|
||||
"config/service.env",
|
||||
"config/.template_info.env"
|
||||
};
|
||||
for (const auto& file : env_files) {
|
||||
{ // load service.env from the service on this machine.
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(template_path + "/" + file);
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
}
|
||||
|
||||
// determine template name.
|
||||
auto it = all_env_vars.find("TEMPLATE");
|
||||
if (it == all_env_vars.end()) {
|
||||
std::cerr << "Error: TEMPLATE variable not found in " << template_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string env_template_name = it->second;
|
||||
if (env_template_name.empty()) {
|
||||
std::cerr << "Error: TEMPLATE variable is empty in " << template_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (env_template_name != template_name) {
|
||||
std::cerr << "Error: TEMPLATE variable is wrong in " << template_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template_manager & gTemplateManager()
|
||||
{
|
||||
static template_manager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
110
source/src/templates.hpp
Normal file
110
source/src/templates.hpp
Normal file
@ -0,0 +1,110 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
|
||||
#include "utils/json.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
typedef enum template_source_type {
|
||||
TEMPLATE_SOURCE_TYPE_LOCAL,
|
||||
TEMPLATE_SOURCE_TYPE_REGISTRY,
|
||||
TEMPLATE_SOURCE_NOT_SET
|
||||
} template_source_type;
|
||||
|
||||
class template_info {
|
||||
public:
|
||||
template_info() : mIsSet(false) {}
|
||||
template_info(const std::string& template_name, const std::string& location_id, const std::filesystem::path& local_template_path) : mTemplateName(template_name), mLocationID(location_id), mTemplateLocalPath(local_template_path), mIsSet(true) {}
|
||||
virtual ~template_info() {}
|
||||
bool is_set() { return mIsSet; }
|
||||
std::string name() { return mTemplateName; }
|
||||
std::string locationID() { return mLocationID; }
|
||||
std::filesystem::path local_template_path() { return mTemplateLocalPath; }
|
||||
private:
|
||||
std::string mTemplateName;
|
||||
std::string mLocationID;
|
||||
std::filesystem::path mTemplateLocalPath; // source or cache.
|
||||
bool mIsSet;
|
||||
};
|
||||
|
||||
class template_source_interface {
|
||||
public:
|
||||
virtual ~template_source_interface() {}
|
||||
virtual std::set<std::string> get_template_list() = 0;
|
||||
virtual bool has_template(const std::string& template_name) = 0;
|
||||
virtual template_info get_template_info(const std::string& template_name) = 0;
|
||||
virtual bool template_command_exists(const std::string& template_name,const std::string& command) = 0;
|
||||
|
||||
virtual std::string get_description() = 0;
|
||||
};
|
||||
|
||||
class template_source_registry : public template_source_interface {
|
||||
public:
|
||||
template_source_registry(std::string URL) : mURL(URL) {}
|
||||
|
||||
~template_source_registry() {}
|
||||
|
||||
std::set<std::string> get_template_list();
|
||||
bool has_template(const std::string& template_name);
|
||||
template_info get_template_info(const std::string& template_name);
|
||||
bool template_command_exists(const std::string& template_name,const std::string& command);
|
||||
|
||||
std::string get_description() { return "Registry: " + mURL; }
|
||||
private:
|
||||
std::filesystem::path get_cache_dir();
|
||||
private:
|
||||
std::string mURL;
|
||||
std::vector<nlohmann::json> mTemplates; // cached list.
|
||||
};
|
||||
|
||||
class template_source_local : public template_source_interface {
|
||||
public:
|
||||
template_source_local(std::string local_path) : mLocalPath(local_path) {}
|
||||
~template_source_local() {}
|
||||
std::set<std::string> get_template_list();
|
||||
bool has_template(const std::string& template_name);
|
||||
template_info get_template_info(const std::string& template_name);
|
||||
bool template_command_exists(const std::string& template_name,const std::string& command);
|
||||
|
||||
std::string get_description() { return "Local: " + mLocalPath.string(); }
|
||||
private:
|
||||
std::filesystem::path mLocalPath;
|
||||
};
|
||||
|
||||
class template_manager {
|
||||
public:
|
||||
template_manager() : mLoaded(false) {}
|
||||
~template_manager() {}
|
||||
|
||||
std::set<std::string> get_template_list() const;
|
||||
bool has_template(const std::string& template_name) const;
|
||||
template_info get_template_info(const std::string& template_name) const;
|
||||
|
||||
bool template_command_exists(const std::string& template_name,const std::string& command) const;
|
||||
bool create_template(const std::string& template_name) const;
|
||||
static bool test_template(const std::string& template_path);
|
||||
|
||||
void list_templates() const;
|
||||
|
||||
void load_sources();
|
||||
void print_sources() const;
|
||||
|
||||
bool is_loaded() const { return mLoaded; }
|
||||
int get_source_count() const { return mSources.size(); }
|
||||
|
||||
private:
|
||||
static bool required_file(std::string path, std::string template_name);
|
||||
template_source_interface* get_source(const std::string& template_name) const;
|
||||
|
||||
private:
|
||||
bool mLoaded;
|
||||
mutable std::vector<std::unique_ptr<template_source_interface>> mSources;
|
||||
};
|
||||
|
||||
template_manager & gTemplateManager();
|
||||
|
||||
|
||||
} // namespace dropshell
|
11
source/src/utils/assert.hpp
Normal file
11
source/src/utils/assert.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef ASSERT_HPP
|
||||
#define ASSERT_HPP
|
||||
|
||||
|
||||
#define ASSERT(condition, message) \
|
||||
if (!(condition)) { \
|
||||
std::cerr << "Assertion failed: " << message << std::endl; \
|
||||
std::exit(1); \
|
||||
}
|
||||
|
||||
#endif // ASSERT_HPP
|
42
source/src/utils/b64ed.cpp
Normal file
42
source/src/utils/b64ed.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include "b64ed.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
// Custom base64 encoding/decoding tables
|
||||
static const std::string custom_base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+_";
|
||||
|
||||
std::string base64_encode(const std::string &in) {
|
||||
std::string out;
|
||||
int val = 0, valb = -6;
|
||||
for (unsigned char c : in) {
|
||||
val = (val << 8) + c;
|
||||
valb += 8;
|
||||
while (valb >= 0) {
|
||||
out.push_back(custom_base64_chars[(val >> valb) & 0x3F]);
|
||||
valb -= 6;
|
||||
}
|
||||
}
|
||||
if (valb > -6) out.push_back(custom_base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
|
||||
while (out.size() % 4) out.push_back('=');
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string base64_decode(const std::string &in) {
|
||||
std::vector<int> T(256, -1);
|
||||
for (int i = 0; i < 64; i++) T[custom_base64_chars[i]] = i;
|
||||
std::string out;
|
||||
int val = 0, valb = -8;
|
||||
for (unsigned char c : in) {
|
||||
if (T[c] == -1) break;
|
||||
val = (val << 6) + T[c];
|
||||
valb += 6;
|
||||
if (valb >= 0) {
|
||||
out.push_back(char((val >> valb) & 0xFF));
|
||||
valb -= 8;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
9
source/src/utils/b64ed.hpp
Normal file
9
source/src/utils/b64ed.hpp
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef B64ED_HPP
|
||||
#define B64ED_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
std::string base64_decode(const std::string &in);
|
||||
std::string base64_encode(const std::string &in);
|
||||
|
||||
#endif
|
180
source/src/utils/directories.cpp
Normal file
180
source/src/utils/directories.cpp
Normal file
@ -0,0 +1,180 @@
|
||||
#include "directories.hpp"
|
||||
#include "config.hpp"
|
||||
#include "server_env_manager.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
|
||||
namespace localfile {
|
||||
|
||||
std::string dropshell_json() {
|
||||
// Try ~/.config/dropshell/dropshell.json
|
||||
std::string homedir = localpath::current_user_home();
|
||||
if (!homedir.empty()) {
|
||||
fs::path user_path = fs::path(homedir) / ".config" / "dropshell" / "dropshell.json";
|
||||
return user_path.string();
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::string server_json(const std::string &server_name) {
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / "server.json").string());
|
||||
}
|
||||
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name) {
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / "service.env").string());
|
||||
}
|
||||
|
||||
std::string template_info_env(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string servicepath = localpath::service(server_name, service_name);
|
||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / ".template_info.env").string());
|
||||
}
|
||||
|
||||
} // namespace localfile
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
|
||||
namespace localpath {
|
||||
std::string server(const std::string &server_name) {
|
||||
for (std::filesystem::path dir : gConfig().get_local_server_definition_paths())
|
||||
if (fs::exists(dir / server_name))
|
||||
return dir / server_name;
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string service(const std::string &server_name, const std::string &service_name) {
|
||||
std::string serverpath = localpath::server(server_name);
|
||||
return ((serverpath.empty() || service_name.empty()) ? "" : (serverpath+"/"+service_name));
|
||||
}
|
||||
|
||||
std::string remote_versions(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string template_cache_path = gConfig().get_local_template_cache_path();
|
||||
return ((template_cache_path.empty() || service_name.empty()) ? "" :
|
||||
(template_cache_path+"/remote_versions/"+service_name+".json"));
|
||||
}
|
||||
std::string agent(){
|
||||
return current_user_home() + "/.local/dropshell_agent";
|
||||
}
|
||||
std::string current_user_home(){
|
||||
char * homedir = std::getenv("HOME");
|
||||
if (homedir)
|
||||
{
|
||||
std::filesystem::path homedir_path(homedir);
|
||||
return fs::canonical(homedir_path).string();
|
||||
}
|
||||
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
|
||||
return std::string();
|
||||
}
|
||||
} // namespace localpath
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// remote paths
|
||||
// DROPSHELL_DIR
|
||||
// |-- backups
|
||||
// |-- temp_files
|
||||
// |-- agent
|
||||
// |-- services
|
||||
// |-- service name
|
||||
// |-- config
|
||||
// |-- service.env
|
||||
// |-- template
|
||||
// |-- (script files)
|
||||
// |-- config
|
||||
// |-- service.env
|
||||
// |-- (other config files for specific server&service)
|
||||
|
||||
|
||||
namespace remotefile {
|
||||
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
return remotepath::service_config(server_name, service_name) + "/service.env";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace remotepath {
|
||||
std::string DROPSHELL_DIR(const std::string &server_name)
|
||||
{
|
||||
return server_env_manager(server_name).get_DROPSHELL_DIR();
|
||||
}
|
||||
|
||||
std::string services(const std::string &server_name)
|
||||
{
|
||||
std::string dsp = DROPSHELL_DIR(server_name);
|
||||
return (dsp.empty() ? "" : (dsp + "/services"));
|
||||
}
|
||||
|
||||
std::string service(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string services_path = services(server_name);
|
||||
return (services_path.empty() ? "" : (services_path + "/" + service_name));
|
||||
}
|
||||
|
||||
std::string service_config(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string service_path = service(server_name, service_name);
|
||||
return (service_path.empty() ? "" : (service_path + "/config"));
|
||||
}
|
||||
|
||||
std::string service_template(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string service_path = service(server_name, service_name);
|
||||
return (service_path.empty() ? "" : (service_path + "/template"));
|
||||
}
|
||||
|
||||
std::string backups(const std::string &server_name)
|
||||
{
|
||||
std::string dsp = DROPSHELL_DIR(server_name);
|
||||
return (dsp.empty() ? "" : (dsp + "/backups"));
|
||||
}
|
||||
|
||||
std::string temp_files(const std::string &server_name)
|
||||
{
|
||||
std::string dsp = DROPSHELL_DIR(server_name);
|
||||
return (dsp.empty() ? "" : (dsp + "/temp_files"));
|
||||
}
|
||||
|
||||
std::string agent(const std::string &server_name)
|
||||
{
|
||||
std::string dsp = DROPSHELL_DIR(server_name);
|
||||
return (dsp.empty() ? "" : (dsp + "/agent"));
|
||||
}
|
||||
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name)
|
||||
{
|
||||
std::string service_path = service_config(server_name, service_name);
|
||||
return (service_path.empty() ? "" : (service_path + "/service.env"));
|
||||
}
|
||||
} // namespace remotepath
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------
|
||||
// Utility functions
|
||||
|
||||
std::string get_parent(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.parent_path().string();
|
||||
}
|
||||
|
||||
std::string get_child(const std::filesystem::path path)
|
||||
{
|
||||
if (path.empty())
|
||||
return std::string();
|
||||
return path.filename().string();
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
103
source/src/utils/directories.hpp
Normal file
103
source/src/utils/directories.hpp
Normal file
@ -0,0 +1,103 @@
|
||||
#ifndef DIRECTORIES_HPP
|
||||
#define DIRECTORIES_HPP
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// all functions return empty string on failure
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
// local user config directories
|
||||
|
||||
// ~/.config/dropshell/dropshell.json
|
||||
// ~/.local/dropshell_agent/bb64
|
||||
|
||||
// server_definition_path
|
||||
// |-- <server_name>
|
||||
// |-- server.json
|
||||
// |-- services
|
||||
// |-- <service_name>
|
||||
// |-- service.env
|
||||
// |-- .template_info.env
|
||||
// |-- (...other config files for specific server&service...)
|
||||
|
||||
// backup path
|
||||
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
|
||||
|
||||
// temp files path
|
||||
|
||||
// template cache path
|
||||
// |-- templates
|
||||
// | |-- <template_name>.json
|
||||
// | |-- <template_name>
|
||||
// | |-- (...script files...)
|
||||
// | |-- _default.env
|
||||
// | |-- config
|
||||
// | |-- service.env
|
||||
// | |-- .template_info.env
|
||||
// | |-- (...other service config files...)
|
||||
// |-- remote_versions
|
||||
// | |-- server_name-service_name.json
|
||||
|
||||
namespace localfile {
|
||||
// ~/.config/dropshell/dropshell.json
|
||||
std::string dropshell_json();
|
||||
std::string server_json(const std::string &server_name);
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name);
|
||||
std::string template_info_env(const std::string &server_name, const std::string &service_name);
|
||||
} // namespace localfile
|
||||
|
||||
namespace localpath {
|
||||
std::string server(const std::string &server_name);
|
||||
std::string service(const std::string &server_name, const std::string &service_name);
|
||||
|
||||
std::string remote_versions(const std::string &server_name, const std::string &service_name);
|
||||
|
||||
std::string agent();
|
||||
std::string current_user_home();
|
||||
} // namespace local
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
// remote paths
|
||||
// DROPSHELL_DIR
|
||||
// |-- backups
|
||||
// |-- temp_files
|
||||
// |-- agent
|
||||
// | |-- bb64
|
||||
// | |-- (other agent files, including _allservicesstatus.sh)
|
||||
// |-- services
|
||||
// |-- service name
|
||||
// |-- config
|
||||
// |-- service.env
|
||||
// |-- template
|
||||
// |-- (script files)
|
||||
// |-- config
|
||||
// |-- service.env
|
||||
// |-- (other config files for specific server&service)
|
||||
|
||||
namespace remotefile {
|
||||
std::string service_env(const std::string &server_name, const std::string &service_name);
|
||||
} // namespace remotefile
|
||||
|
||||
namespace remotepath {
|
||||
std::string DROPSHELL_DIR(const std::string &server_name);
|
||||
std::string services(const std::string &server_name);
|
||||
std::string service(const std::string &server_name, const std::string &service_name);
|
||||
std::string service_config(const std::string &server_name, const std::string &service_name);
|
||||
std::string service_template(const std::string &server_name, const std::string &service_name);
|
||||
std::string backups(const std::string &server_name);
|
||||
std::string temp_files(const std::string &server_name);
|
||||
std::string agent(const std::string &server_name);
|
||||
} // namespace remotepath
|
||||
|
||||
//------------------------------------------------------------------------------------------------
|
||||
// utility functions
|
||||
std::string get_parent(const std::filesystem::path path);
|
||||
std::string get_child(const std::filesystem::path path);
|
||||
} // namespace dropshell
|
||||
|
||||
|
||||
#endif // DIRECTORIES_HPP
|
89
source/src/utils/envmanager.cpp
Normal file
89
source/src/utils/envmanager.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
#include "envmanager.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <regex>
|
||||
#include <cstdlib> // For std::getenv
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
envmanager::envmanager(std::string path) : m_path(path) {
|
||||
}
|
||||
|
||||
envmanager::~envmanager() {
|
||||
}
|
||||
|
||||
bool envmanager::load() {
|
||||
std::ifstream file(m_path);
|
||||
if (!file.is_open()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m_variables.clear();
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
line=trim(line);
|
||||
// Skip empty lines and comments
|
||||
if (line.empty() || line[0] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t pos = line.find('=');
|
||||
if (pos != std::string::npos) {
|
||||
std::string key = line.substr(0, pos);
|
||||
std::string value = line.substr(pos + 1);
|
||||
|
||||
// trim whitespace from the key and value
|
||||
m_variables[dequote(trim(key))] = dequote(trim(value));
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
void envmanager::save() {
|
||||
std::ofstream file(m_path);
|
||||
if (!file.is_open()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& pair : m_variables) {
|
||||
file << pair.first << "=" << quote(pair.second) << std::endl;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
||||
std::string envmanager::get_variable(std::string key) const {
|
||||
key = dequote(trim(key));
|
||||
|
||||
// Use case-insensitive comparison to find the key
|
||||
for (const auto& pair : m_variables) {
|
||||
if (pair.first == key) {
|
||||
return pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
void envmanager::get_all_variables(std::map<std::string, std::string>& variables) const {
|
||||
variables = m_variables;
|
||||
}
|
||||
|
||||
void envmanager::add_variables(std::map<std::string, std::string> variables) {
|
||||
for (auto& pair : variables) {
|
||||
set_variable(pair.first, pair.second);
|
||||
}
|
||||
}
|
||||
|
||||
void envmanager::set_variable(std::string key, std::string value) {
|
||||
m_variables[dequote(trim(key))] = dequote(trim(value));
|
||||
}
|
||||
|
||||
void envmanager::clear_variables() {
|
||||
m_variables.clear();
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
47
source/src/utils/envmanager.hpp
Normal file
47
source/src/utils/envmanager.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef ENV_MANAGER_HPP
|
||||
#define ENV_MANAGER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
namespace dropshell {
|
||||
|
||||
// envmanager is a class that manages the environment files for the application.
|
||||
// it is responsible for loading the environment files, and providing a class to access the variables.
|
||||
// it can also save the environment files.
|
||||
class envmanager {
|
||||
public:
|
||||
envmanager(std::string path);
|
||||
~envmanager();
|
||||
|
||||
// load all variables from the environment file
|
||||
bool load();
|
||||
|
||||
// save all variables to the environment file
|
||||
void save();
|
||||
|
||||
// get variables from the environment files. Trim whitespace from the values.
|
||||
// keys are case-sensitive.
|
||||
std::string get_variable(std::string key) const;
|
||||
void get_all_variables(std::map<std::string, std::string>& variables) const;
|
||||
|
||||
// add variables to the environment files.
|
||||
// trim whitespace from the values.
|
||||
void add_variables(std::map<std::string, std::string> variables);
|
||||
void set_variable(std::string key, std::string value);
|
||||
void clear_variables();
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
std::map<std::string, std::string> m_variables;
|
||||
};
|
||||
|
||||
// utility functions
|
||||
std::string trim(std::string str);
|
||||
std::string dequote(std::string str);
|
||||
std::string multi2string(std::vector<std::string> values);
|
||||
std::vector<std::string> string2multi(std::string values);
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
#endif
|
179
source/src/utils/execute.cpp
Normal file
179
source/src/utils/execute.cpp
Normal file
@ -0,0 +1,179 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
#include <sstream>
|
||||
#include "utils/assert.hpp"
|
||||
|
||||
#include "execute.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
#include "utils/b64ed.hpp"
|
||||
#include "config.hpp"
|
||||
#include "utils/directories.hpp"
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
bool EXITSTATUSCHECK(int ret)
|
||||
{
|
||||
return (ret != -1 && WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); // ret is -1 if the command failed to execute.
|
||||
}
|
||||
|
||||
bool execute_local_command_interactive(const sCommand &command)
|
||||
{
|
||||
if (command.get_command_to_run().empty())
|
||||
return false;
|
||||
std::string full_command = command.construct_cmd(localpath::agent()); // Get the command string
|
||||
|
||||
pid_t pid = fork();
|
||||
|
||||
if (pid == -1)
|
||||
{
|
||||
// Fork failed
|
||||
perror("fork failed");
|
||||
return false;
|
||||
}
|
||||
else if (pid == 0)
|
||||
{
|
||||
int rval = system(full_command.c_str());
|
||||
exit(rval);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Parent process
|
||||
int ret;
|
||||
// Wait for the child process to complete
|
||||
waitpid(pid, &ret, 0);
|
||||
|
||||
return EXITSTATUSCHECK(ret);
|
||||
}
|
||||
}
|
||||
|
||||
bool execute_local_command_and_capture_output(const sCommand &command, std::string *output)
|
||||
{
|
||||
ASSERT(output != nullptr, "Output string must be provided");
|
||||
if (command.get_command_to_run().empty())
|
||||
return false;
|
||||
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1";
|
||||
FILE *pipe = popen(full_cmd.c_str(), "r");
|
||||
if (!pipe)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
char buffer[128];
|
||||
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
|
||||
{
|
||||
(*output) += buffer;
|
||||
}
|
||||
int ret = pclose(pipe);
|
||||
return EXITSTATUSCHECK(ret);
|
||||
}
|
||||
|
||||
bool execute_local_command(std::string command, std::string *output, cMode mode)
|
||||
{
|
||||
return execute_local_command("", command, {}, output, mode);
|
||||
}
|
||||
|
||||
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
|
||||
{
|
||||
sCommand command(directory_to_run_in, command_to_run, env_vars);
|
||||
|
||||
if (hasFlag(mode, cMode::Interactive))
|
||||
{
|
||||
ASSERT(!hasFlag(mode, cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together");
|
||||
ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together");
|
||||
|
||||
return execute_local_command_interactive(command);
|
||||
}
|
||||
|
||||
if (hasFlag(mode, cMode::CaptureOutput))
|
||||
{
|
||||
ASSERT(output != nullptr, "Capture output mode requires an output string to be provided");
|
||||
ASSERT(!hasFlag(mode, cMode::Silent), "Silent mode is not allowed with capture output mode");
|
||||
|
||||
return execute_local_command_and_capture_output(command, output);
|
||||
}
|
||||
|
||||
if (command.get_command_to_run().empty())
|
||||
return false;
|
||||
bool silent = hasFlag(mode, cMode::Silent);
|
||||
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1" + (silent ? " > /dev/null" : "");
|
||||
int ret = system(full_cmd.c_str());
|
||||
|
||||
bool ok = EXITSTATUSCHECK(ret);
|
||||
if (!ok && !silent)
|
||||
{
|
||||
std::cerr << "Error: Failed to execute command: " << std::endl;
|
||||
std::cerr << full_cmd << std::endl;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool execute_ssh_command(const sSSHInfo &ssh_info, const sCommand &remote_command, cMode mode, std::string *output)
|
||||
{
|
||||
if (remote_command.get_command_to_run().empty())
|
||||
return false;
|
||||
|
||||
ASSERT(!(hasFlag(mode, cMode::CaptureOutput) && output == nullptr), "Capture output mode must be used with an output string");
|
||||
|
||||
std::stringstream ssh_cmd;
|
||||
ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "")
|
||||
<< ssh_info.user << "@" << ssh_info.host;
|
||||
|
||||
std::string remote_agent_path = remotepath::agent(ssh_info.server_ID);
|
||||
|
||||
bool rval = execute_local_command(
|
||||
"", // directory to run in
|
||||
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path), // local command to run
|
||||
{}, // environment variables
|
||||
output, // output string
|
||||
mode // mode
|
||||
);
|
||||
|
||||
if (!rval && !hasFlag(mode, cMode::Silent))
|
||||
{
|
||||
std::cerr << std::endl
|
||||
<< std::endl;
|
||||
std::cerr << "Error: Failed to execute ssh command:" << std::endl;
|
||||
std::cerr << "\033[90m" << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path) << "\033[0m" << std::endl;
|
||||
std::cerr << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
return rval;
|
||||
}
|
||||
|
||||
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const
|
||||
{
|
||||
if (command.empty())
|
||||
return "";
|
||||
std::string encoded = base64_encode(dequote(trim(command)));
|
||||
std::string commandstr = agent_path + "/bb64 " + encoded;
|
||||
return commandstr;
|
||||
}
|
||||
|
||||
std::string sCommand::construct_cmd(std::string agent_path) const
|
||||
{
|
||||
if (mCmd.empty())
|
||||
return "";
|
||||
|
||||
// need to construct to change directory and set environment variables
|
||||
std::string cmdstr;
|
||||
|
||||
if (!mDir.empty())
|
||||
cmdstr += "cd " + quote(mDir) + " && ";
|
||||
|
||||
if (!mVars.empty())
|
||||
for (const auto &env_var : mVars)
|
||||
cmdstr += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
|
||||
|
||||
cmdstr += mCmd;
|
||||
|
||||
if (!agent_path.empty())
|
||||
cmdstr = makesafecmd(agent_path, cmdstr);
|
||||
|
||||
return cmdstr;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
74
source/src/utils/execute.hpp
Normal file
74
source/src/utils/execute.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#ifndef EXECUTE_HPP
|
||||
#define EXECUTE_HPP
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
class sCommand;
|
||||
|
||||
// mode bitset
|
||||
enum class cMode {
|
||||
Defaults = 0,
|
||||
Interactive = 1,
|
||||
Silent = 2,
|
||||
CaptureOutput = 4
|
||||
};
|
||||
|
||||
inline cMode operator&(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & static_cast<int>(rhs));}
|
||||
inline cMode operator+(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) | static_cast<int>(rhs));}
|
||||
inline cMode operator-(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & ~static_cast<int>(rhs));}
|
||||
inline cMode operator|(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) | static_cast<int>(rhs));}
|
||||
inline cMode operator|=(cMode & lhs, cMode rhs) {return lhs = lhs | rhs;}
|
||||
inline bool hasFlag(cMode mode, cMode flag) {return (mode & flag) == flag;}
|
||||
|
||||
|
||||
typedef struct sSSHInfo {
|
||||
std::string host;
|
||||
std::string user;
|
||||
std::string port;
|
||||
std::string server_ID; // dropshell name for server.
|
||||
} sSSHInfo;
|
||||
|
||||
bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
||||
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars, std::string * output = nullptr, cMode mode = cMode::Defaults);
|
||||
bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// class to hold a command to run on the remote server.
|
||||
class sCommand {
|
||||
public:
|
||||
sCommand(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> & env_vars) :
|
||||
mDir(directory_to_run_in), mCmd(command_to_run), mVars(env_vars) {}
|
||||
|
||||
std::string get_directory_to_run_in() const { return mDir; }
|
||||
std::string get_command_to_run() const { return mCmd; }
|
||||
const std::map<std::string, std::string>& get_env_vars() const { return mVars; }
|
||||
|
||||
void add_env_var(const std::string& key, const std::string& value) { mVars[key] = value; }
|
||||
|
||||
bool empty() const { return mCmd.empty(); }
|
||||
|
||||
std::string construct_cmd(std::string agent_path) const;
|
||||
|
||||
private:
|
||||
std::string makesafecmd(std::string agent_path, const std::string& command) const;
|
||||
|
||||
private:
|
||||
std::string mDir;
|
||||
std::string mCmd;
|
||||
std::map<std::string, std::string> mVars;
|
||||
};
|
||||
|
||||
bool EXITSTATUSCHECK(int ret);
|
||||
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
|
||||
|
||||
#endif
|
131
source/src/utils/hash.cpp
Normal file
131
source/src/utils/hash.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
#include "utils/hash.hpp"
|
||||
|
||||
#define XXH_INLINE_ALL
|
||||
#include "contrib/xxhash.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
uint64_t hash_file(const std::string &path) {
|
||||
// Create hash state
|
||||
XXH64_state_t* const state = XXH64_createState();
|
||||
if (state == nullptr) {
|
||||
std::cerr << "Failed to create hash state" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialize state with seed 0
|
||||
XXH64_hash_t const seed = 0; /* or any other value */
|
||||
if (XXH64_reset(state, seed) == XXH_ERROR) return 0;
|
||||
|
||||
// Open file
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Failed to open file: " << path << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read file in chunks and update hash
|
||||
const size_t buffer_size = 4096;
|
||||
char buffer[buffer_size];
|
||||
while (file.read(buffer, buffer_size)) {
|
||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||
std::cerr << "Failed to update hash" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle any remaining bytes
|
||||
if (file.gcount() > 0) {
|
||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||
std::cerr << "Failed to update hash" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Get final hash
|
||||
XXH64_hash_t hash = XXH64_digest(state);
|
||||
XXH64_freeState(state);
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint64_t hash_directory_recursive(const std::string &path) {
|
||||
// Create hash state
|
||||
XXH64_state_t* const state = XXH64_createState();
|
||||
if (state == nullptr) {
|
||||
std::cerr << "Failed to create hash state" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Initialize state with seed 0
|
||||
XXH64_hash_t const seed = 0; /* or any other value */
|
||||
if (XXH64_reset(state, seed) == XXH_ERROR) {
|
||||
std::cerr << "Failed to reset hash state" << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
// Iterate through all files in directory recursively
|
||||
for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
|
||||
if (entry.is_regular_file()) {
|
||||
// Get file hash
|
||||
XXH64_hash_t file_hash = hash_file(entry.path().string());
|
||||
XXH64_update(state, &file_hash, sizeof(file_hash));
|
||||
}
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& e) {
|
||||
std::cerr << "Filesystem error: " << e.what() << std::endl;
|
||||
XXH64_freeState(state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get final hash
|
||||
XXH64_hash_t hash = XXH64_digest(state);
|
||||
XXH64_freeState(state);
|
||||
return hash;
|
||||
}
|
||||
|
||||
uint64_t hash_path(const std::string &path) {
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cerr << "Path does not exist: " << path << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (std::filesystem::is_directory(path)) {
|
||||
return hash_directory_recursive(path);
|
||||
} else if (std::filesystem::is_regular_file(path)) {
|
||||
return hash_file(path);
|
||||
} else {
|
||||
std::cerr << "Path is neither a file nor a directory: " << path << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void hash_demo(const std::string & path)
|
||||
{
|
||||
std::cout << "Hashing path: " << path << std::endl;
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
XXH64_hash_t hash = hash_path(path);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
||||
}
|
||||
|
||||
int hash_demo_raw(const std::string & path)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::cout << 0 <<std::endl; return 1;
|
||||
}
|
||||
XXH64_hash_t hash = hash_path(path);
|
||||
std::cout << hash << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
22
source/src/utils/hash.hpp
Normal file
22
source/src/utils/hash.hpp
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef HASH_HPP
|
||||
#define HASH_HPP
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
uint64_t hash_file(const std::string &path);
|
||||
|
||||
uint64_t hash_directory_recursive(const std::string &path);
|
||||
|
||||
uint64_t hash_path(const std::string &path);
|
||||
|
||||
void hash_demo(const std::string & path);
|
||||
|
||||
int hash_demo_raw(const std::string & path);
|
||||
|
||||
} // namespace dropshell
|
||||
|
||||
|
||||
#endif
|
25578
source/src/utils/json.hpp
Normal file
25578
source/src/utils/json.hpp
Normal file
File diff suppressed because it is too large
Load Diff
187
source/src/utils/json_fwd.hpp
Normal file
187
source/src/utils/json_fwd.hpp
Normal file
@ -0,0 +1,187 @@
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.12.0
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
||||
|
||||
#include <cstdint> // int64_t, uint64_t
|
||||
#include <map> // map
|
||||
#include <memory> // allocator
|
||||
#include <string> // string
|
||||
#include <vector> // vector
|
||||
|
||||
// #include <nlohmann/detail/abi_macros.hpp>
|
||||
// __ _____ _____ _____
|
||||
// __| | __| | | | JSON for Modern C++
|
||||
// | | |__ | | | | | | version 3.12.0
|
||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
|
||||
|
||||
// This file contains all macro definitions affecting or depending on the ABI
|
||||
|
||||
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
|
||||
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
|
||||
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
|
||||
#warning "Already included a different version of the library!"
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
|
||||
#define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
|
||||
|
||||
#ifndef JSON_DIAGNOSTICS
|
||||
#define JSON_DIAGNOSTICS 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_DIAGNOSTIC_POSITIONS
|
||||
#define JSON_DIAGNOSTIC_POSITIONS 0
|
||||
#endif
|
||||
|
||||
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
|
||||
#endif
|
||||
|
||||
#if JSON_DIAGNOSTICS
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
|
||||
#endif
|
||||
|
||||
#if JSON_DIAGNOSTIC_POSITIONS
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
|
||||
#endif
|
||||
|
||||
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
|
||||
#else
|
||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
|
||||
#endif
|
||||
|
||||
// Construct the namespace ABI tags component
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
|
||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
|
||||
|
||||
#define NLOHMANN_JSON_ABI_TAGS \
|
||||
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
||||
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
|
||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
|
||||
|
||||
// Construct the namespace version component
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
|
||||
_v ## major ## _ ## minor ## _ ## patch
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
|
||||
|
||||
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION
|
||||
#else
|
||||
#define NLOHMANN_JSON_NAMESPACE_VERSION \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
|
||||
NLOHMANN_JSON_VERSION_MINOR, \
|
||||
NLOHMANN_JSON_VERSION_PATCH)
|
||||
#endif
|
||||
|
||||
// Combine namespace components
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
|
||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
|
||||
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE
|
||||
#define NLOHMANN_JSON_NAMESPACE \
|
||||
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION)
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
|
||||
namespace nlohmann \
|
||||
{ \
|
||||
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
||||
NLOHMANN_JSON_ABI_TAGS, \
|
||||
NLOHMANN_JSON_NAMESPACE_VERSION) \
|
||||
{
|
||||
#endif
|
||||
|
||||
#ifndef NLOHMANN_JSON_NAMESPACE_END
|
||||
#define NLOHMANN_JSON_NAMESPACE_END \
|
||||
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
|
||||
} // namespace nlohmann
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
@brief namespace for Niels Lohmann
|
||||
@see https://github.com/nlohmann
|
||||
@since version 1.0.0
|
||||
*/
|
||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||
|
||||
/*!
|
||||
@brief default JSONSerializer template argument
|
||||
|
||||
This serializer ignores the template arguments and uses ADL
|
||||
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
|
||||
for serialization.
|
||||
*/
|
||||
template<typename T = void, typename SFINAE = void>
|
||||
struct adl_serializer;
|
||||
|
||||
/// a class to store JSON values
|
||||
/// @sa https://json.nlohmann.me/api/basic_json/
|
||||
template<template<typename U, typename V, typename... Args> class ObjectType =
|
||||
std::map,
|
||||
template<typename U, typename... Args> class ArrayType = std::vector,
|
||||
class StringType = std::string, class BooleanType = bool,
|
||||
class NumberIntegerType = std::int64_t,
|
||||
class NumberUnsignedType = std::uint64_t,
|
||||
class NumberFloatType = double,
|
||||
template<typename U> class AllocatorType = std::allocator,
|
||||
template<typename T, typename SFINAE = void> class JSONSerializer =
|
||||
adl_serializer,
|
||||
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
|
||||
class CustomBaseClass = void>
|
||||
class basic_json;
|
||||
|
||||
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
|
||||
/// @sa https://json.nlohmann.me/api/json_pointer/
|
||||
template<typename RefStringType>
|
||||
class json_pointer;
|
||||
|
||||
/*!
|
||||
@brief default specialization
|
||||
@sa https://json.nlohmann.me/api/json/
|
||||
*/
|
||||
using json = basic_json<>;
|
||||
|
||||
/// @brief a minimal map-like container that preserves insertion order
|
||||
/// @sa https://json.nlohmann.me/api/ordered_map/
|
||||
template<class Key, class T, class IgnoredLess, class Allocator>
|
||||
struct ordered_map;
|
||||
|
||||
/// @brief specialization that maintains the insertion order of object keys
|
||||
/// @sa https://json.nlohmann.me/api/ordered_json/
|
||||
using ordered_json = basic_json<nlohmann::ordered_map>;
|
||||
|
||||
NLOHMANN_JSON_NAMESPACE_END
|
||||
|
||||
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
292
source/src/utils/tableprint.cpp
Normal file
292
source/src/utils/tableprint.cpp
Normal file
@ -0,0 +1,292 @@
|
||||
#include "tableprint.hpp"
|
||||
#include <iomanip>
|
||||
#include <algorithm>
|
||||
#include <locale>
|
||||
#include <cwchar>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <codecvt>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
enum kTextColors {
|
||||
kTextColor_Default,
|
||||
kTextColor_Red,
|
||||
kTextColor_Green,
|
||||
kTextColor_Yellow,
|
||||
kTextColor_Blue,
|
||||
kTextColor_Magenta,
|
||||
kTextColor_Cyan,
|
||||
kTextColor_White,
|
||||
kTextColor_DarkGrey,
|
||||
kTextColor_DarkYellow,
|
||||
kTextColor_LightGrey
|
||||
};
|
||||
|
||||
struct coloredText {
|
||||
std::string text;
|
||||
kTextColors color;
|
||||
};
|
||||
|
||||
const std::map<std::string, coloredText> kReplacements = {
|
||||
{":tick:", {"+", kTextColor_Green}},
|
||||
{":cross:", {"x", kTextColor_Red}},
|
||||
{":warning:", {"!", kTextColor_Yellow}},
|
||||
{":info:", {"i", kTextColor_Blue}},
|
||||
{":check:", {"+", kTextColor_Green}},
|
||||
{":x:", {"x", kTextColor_Red}},
|
||||
{":error:", {"!", kTextColor_Red}},
|
||||
{":question:", {"?", kTextColor_DarkGrey}},
|
||||
{":greytick:", {"+", kTextColor_LightGrey}},
|
||||
{":greycross:", {"x", kTextColor_LightGrey}}
|
||||
};
|
||||
|
||||
// Helper function to get ANSI color code
|
||||
std::string get_color_code(kTextColors color) {
|
||||
switch (color) {
|
||||
case kTextColor_Red: return "\033[1;31m";
|
||||
case kTextColor_Green: return "\033[1;32m";
|
||||
case kTextColor_Yellow: return "\033[1;33m";
|
||||
case kTextColor_Blue: return "\033[1;34m";
|
||||
case kTextColor_Magenta: return "\033[1;35m";
|
||||
case kTextColor_Cyan: return "\033[1;36m";
|
||||
case kTextColor_White: return "\033[1;37m";
|
||||
case kTextColor_DarkGrey: return "\033[90m";
|
||||
case kTextColor_DarkYellow: return "\033[38;5;142m";
|
||||
case kTextColor_LightGrey: return "\033[38;5;250m";
|
||||
default: return "\033[0m";
|
||||
}
|
||||
}
|
||||
|
||||
int get_codepoints(const std::string& str) {
|
||||
int num_code_points = 0;
|
||||
for (char byte: str) {
|
||||
if((byte & 0xC0) != 0x80) {
|
||||
num_code_points++;
|
||||
}
|
||||
}
|
||||
return num_code_points;
|
||||
}
|
||||
|
||||
int get_visible_length(const std::string& str) {
|
||||
int length = get_codepoints(str);
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < str.length()) {
|
||||
for (const auto& [key, value] : kReplacements) {
|
||||
if (str.compare(pos, key.length(), key) == 0) {
|
||||
length = length - get_codepoints(key) + get_codepoints(value.text);
|
||||
pos += key.length();
|
||||
break;
|
||||
}
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
std::string process_cell(std::string str,std::string rowcolor) {
|
||||
std::string result;
|
||||
|
||||
size_t pos = 0;
|
||||
while (pos < str.length()) {
|
||||
bool found_replacement = false;
|
||||
for (const auto& [key, value] : kReplacements) {
|
||||
if (str.compare(pos, key.length(), key) == 0) {
|
||||
found_replacement = true;
|
||||
result += get_color_code(value.color) + value.text + rowcolor;
|
||||
pos += key.length();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found_replacement) {
|
||||
result += str[pos];
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string width_print_centered(std::string str,int width, std::string rowcolor) {
|
||||
size_t padding = (width - get_visible_length(str));
|
||||
size_t lpad = padding/2;
|
||||
size_t rpad = padding - lpad;
|
||||
std::ostringstream oss;
|
||||
oss << rowcolor << std::string(lpad, ' ') << process_cell(str, rowcolor) <<
|
||||
std::string(rpad, ' ') << "\033[0m";
|
||||
|
||||
// std::cout << "str = "<<str <<std::endl;
|
||||
// std::cout << "width = "<<width <<std::endl;
|
||||
// std::cout << "padding = "<<padding <<std::endl;
|
||||
// std::cout << "get_visible_length(str) = "<<get_visible_length(str) <<std::endl;
|
||||
// std::cout << "get_codepoints(str) = "<<get_codepoints(str) <<std::endl;
|
||||
// std::cout << "oss.str() = ["<<oss.str() <<"]"<<std::endl;
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
std::string width_print_left(std::string str,int width, std::string rowcolor) {
|
||||
size_t padding = (width - get_visible_length(str));
|
||||
size_t lpad = (padding>1 ? 1 : 0);
|
||||
size_t rpad = padding - lpad;
|
||||
std::ostringstream oss;
|
||||
oss << rowcolor << std::string(lpad, ' ') << process_cell(str, rowcolor)<< std::string(rpad, ' ') << "\033[0m";
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
tableprint::tableprint(const std::string title, bool compact) : title(title), mCompact(compact) {
|
||||
// Set locale for wide character support
|
||||
std::setlocale(LC_ALL, "");
|
||||
}
|
||||
|
||||
tableprint::~tableprint() {}
|
||||
|
||||
void tableprint::set_title(const std::string title) {
|
||||
this->title = title;
|
||||
}
|
||||
|
||||
void tableprint::add_row(const std::vector<std::string>& row) {
|
||||
std::vector<std::string> trimmed_row;
|
||||
for (const auto& cell : row) {
|
||||
// Trim whitespace from start and end
|
||||
auto start = cell.find_first_not_of(" \t");
|
||||
auto end = cell.find_last_not_of(" \t");
|
||||
if (start == std::string::npos) {
|
||||
trimmed_row.push_back("");
|
||||
} else {
|
||||
trimmed_row.push_back(cell.substr(start, end - start + 1));
|
||||
}
|
||||
}
|
||||
rows.push_back(trimmed_row);
|
||||
}
|
||||
|
||||
void tableprint::print() {
|
||||
if (rows.empty()) return;
|
||||
|
||||
// Calculate column widths based on header text
|
||||
std::vector<size_t> col_widths(rows[0].size(), 0);
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
col_widths[i] = rows[0][i].length();
|
||||
}
|
||||
|
||||
// Adjust widths for any cells with replacements
|
||||
for (size_t row_idx = 1; row_idx < rows.size(); ++row_idx) {
|
||||
const auto& row = rows[row_idx];
|
||||
for (size_t i = 0; i < row.size(); ++i) {
|
||||
int l = get_visible_length(row[i]);
|
||||
if (l > col_widths[i])
|
||||
col_widths[i] = l;
|
||||
}
|
||||
}
|
||||
|
||||
// Debug output
|
||||
// std::cerr << "Column widths: ";
|
||||
// for (size_t width : col_widths) {
|
||||
// std::cerr << width << " ";
|
||||
// }
|
||||
// std::cerr << std::endl;
|
||||
|
||||
// Calculate total table width
|
||||
size_t total_width = 0;
|
||||
for (size_t width : col_widths) {
|
||||
total_width += width + 2; // +2 for padding
|
||||
}
|
||||
total_width += col_widths.size() - 1; // Add space for vertical borders
|
||||
|
||||
// Print title if it exists
|
||||
if (!title.empty()) {
|
||||
std::cout << "\033[90m"; // Dark grey color for borders
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
std::cout << std::string(col_widths[i] + 2, '-');
|
||||
if (i < rows[0].size() - 1) std::cout << "-";
|
||||
}
|
||||
std::cout << "+" << std::endl;
|
||||
|
||||
std::cout << "|"; // White color for title
|
||||
size_t title_width = 0;
|
||||
for (size_t width : col_widths) {
|
||||
title_width += width + 2; // +2 for padding
|
||||
}
|
||||
title_width += col_widths.size() - 1; // Add space for vertical borders
|
||||
|
||||
std::cout << width_print_centered(title,title_width,"\033[1;37m");
|
||||
std::cout << "\033[90m|" << std::endl;
|
||||
|
||||
// Use └─┴─┘ for bottom of title box to connect with table
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
std::cout << std::string(col_widths[i] + 2, '-');
|
||||
if (i < rows[0].size() - 1) std::cout << "-";
|
||||
}
|
||||
std::cout << "+" << std::endl;
|
||||
} else {
|
||||
// Print top border if no title
|
||||
std::cout << "\033[90m"; // Dark grey color for borders
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
std::cout << std::string(col_widths[i] + 2, '-');
|
||||
if (i < rows[0].size() - 1) std::cout << "+";
|
||||
}
|
||||
std::cout << "+" << std::endl;
|
||||
}
|
||||
|
||||
// Print header
|
||||
std::cout << "|";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
std::cout << width_print_centered(rows[0][i],col_widths[i]+2,"\033[1;36m");
|
||||
if (i < rows[0].size() - 1) {
|
||||
std::cout << "\033[90m|\033[1;36m"; // Border color then back to cyan
|
||||
} else {
|
||||
std::cout << "\033[90m|"; // Just border color for last column
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Print header separator
|
||||
if (!mCompact) {
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
for (size_t j = 0; j < col_widths[i] + 2; ++j) {
|
||||
std::cout << "-";
|
||||
}
|
||||
if (i < rows[0].size() - 1) std::cout << "+";
|
||||
}
|
||||
std::cout << "+" << std::endl;
|
||||
}
|
||||
|
||||
// Print rows
|
||||
for (size_t row_idx = 1; row_idx < rows.size(); ++row_idx) {
|
||||
const auto& row = rows[row_idx];
|
||||
std::cout << "|";
|
||||
for (size_t i = 0; i < row.size(); ++i) {
|
||||
// Set the appropriate color for the row
|
||||
std::string rowcolor = (row_idx % 2 == 1) ? "\033[38;5;142m" : "\033[38;5;250m";
|
||||
std::cout << width_print_left(row[i],col_widths[i]+2,rowcolor);
|
||||
std::cout << "\033[90m" << "|";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// Print row separator if not the last row
|
||||
if (row_idx < rows.size() - 1 && !mCompact) {
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
for (size_t j = 0; j < col_widths[i] + 2; ++j) {
|
||||
std::cout << "-";
|
||||
}
|
||||
if (i < rows[0].size() - 1) std::cout << "+";
|
||||
}
|
||||
std::cout << "+" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// Print bottom border
|
||||
std::cout << "+";
|
||||
for (size_t i = 0; i < rows[0].size(); ++i) {
|
||||
for (size_t j = 0; j < col_widths[i] + 2; ++j) {
|
||||
std::cout << "-";
|
||||
}
|
||||
if (i < rows[0].size() - 1) std::cout << "+";
|
||||
}
|
||||
std::cout << "+" << "\033[0m" << std::endl; // Reset color
|
||||
}
|
25
source/src/utils/tableprint.hpp
Normal file
25
source/src/utils/tableprint.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
# ifndef TABLEPRINT_HPP
|
||||
# define TABLEPRINT_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
// tableprint is a class that prints a table of strings.
|
||||
// formatted to look nice with colored headings and rows.
|
||||
// converts :tick: to a green tick and :cross: to a red cross.
|
||||
// assumes the first row is the header.
|
||||
class tableprint {
|
||||
public:
|
||||
tableprint(const std::string title = "", bool compact = false);
|
||||
~tableprint();
|
||||
void add_row(const std::vector<std::string>& row);
|
||||
void print();
|
||||
void set_title(const std::string title);
|
||||
private:
|
||||
std::vector<std::vector<std::string>> rows;
|
||||
std::string title;
|
||||
bool mCompact;
|
||||
};
|
||||
|
||||
# endif
|
374
source/src/utils/utils.cpp
Normal file
374
source/src/utils/utils.cpp
Normal file
@ -0,0 +1,374 @@
|
||||
#include "utils.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
#include <random>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
void maketitle(const std::string& title) {
|
||||
std::cout << std::string(title.length() + 4, '-') << std::endl;
|
||||
std::cout << "| " << title << " |" << std::endl;
|
||||
std::cout << std::string(title.length() + 4, '-') << std::endl;
|
||||
}
|
||||
|
||||
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line) {
|
||||
std::ifstream input_file(file_path);
|
||||
std::vector<std::string> file_lines;
|
||||
std::string line;
|
||||
|
||||
if (!input_file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
while (std::getline(input_file, line)) {
|
||||
if (line.find(search_string) != std::string::npos)
|
||||
file_lines.push_back(replacement_line);
|
||||
else
|
||||
file_lines.push_back(line);
|
||||
}
|
||||
input_file.close();
|
||||
|
||||
std::ofstream output_file(file_path);
|
||||
if (!output_file.is_open())
|
||||
{
|
||||
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
|
||||
return false;
|
||||
}
|
||||
for (const auto& modified_line : file_lines)
|
||||
output_file << modified_line << "\n";
|
||||
output_file.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string trim(std::string str) {
|
||||
// Trim leading whitespace
|
||||
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}));
|
||||
|
||||
// Trim trailing whitespace
|
||||
str.erase(std::find_if(str.rbegin(), str.rend(), [](unsigned char ch) {
|
||||
return !std::isspace(ch);
|
||||
}).base(), str.end());
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string dequote(std::string str)
|
||||
{
|
||||
if (str.length() < 2)
|
||||
return str;
|
||||
if (str.front() == '"' && str.back() == '"') {
|
||||
return str.substr(1, str.length() - 2);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string quote(std::string str)
|
||||
{
|
||||
return "\""+str+"\"";
|
||||
}
|
||||
|
||||
std::string halfquote(std::string str)
|
||||
{
|
||||
return "'" + str + "'";
|
||||
}
|
||||
|
||||
std::string escapequotes(std::string str)
|
||||
{
|
||||
return std::regex_replace(str, std::regex("\""), "\\\"");
|
||||
}
|
||||
|
||||
std::string multi2string(std::vector<std::string> values)
|
||||
{
|
||||
std::string result;
|
||||
for (const auto& value : values) {
|
||||
// remove any " contained in the string value, if present
|
||||
result += dequote(trim(value)) + ",";
|
||||
}
|
||||
if (!result.empty())
|
||||
result.pop_back(); // Remove the last comma
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> string2multi(std::string values)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
|
||||
values = dequote(trim(values));
|
||||
|
||||
// Return values separated by commas, but ignore commas within quotes
|
||||
bool inside_quotes = false;
|
||||
std::string current_item;
|
||||
|
||||
for (char c : values) {
|
||||
if (c == '"') {
|
||||
inside_quotes = !inside_quotes;
|
||||
} else if (c == ',' && !inside_quotes) {
|
||||
if (!current_item.empty()) {
|
||||
std::string final = dequote(trim(current_item));
|
||||
if (!final.empty())
|
||||
result.push_back(final);
|
||||
current_item.clear();
|
||||
}
|
||||
} else {
|
||||
current_item += c;
|
||||
}
|
||||
}
|
||||
|
||||
// Add the last item if not empty
|
||||
if (!current_item.empty()) {
|
||||
std::string final = dequote(trim(current_item));
|
||||
if (!final.empty())
|
||||
result.push_back(final);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int str2int(const std::string &str)
|
||||
{
|
||||
try {
|
||||
return std::stoi(str);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: Invalid integer string: [" << str << "]" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void recursive_copy(const std::string & source, const std::string & destination) {
|
||||
try {
|
||||
if (std::filesystem::is_directory(source)) {
|
||||
if (!std::filesystem::exists(destination)) {
|
||||
std::filesystem::create_directory(destination);
|
||||
}
|
||||
for (const auto& entry : std::filesystem::directory_iterator(source)) {
|
||||
recursive_copy(entry.path(), destination / entry.path().filename());
|
||||
}
|
||||
} else if (std::filesystem::is_regular_file(source)) {
|
||||
std::filesystem::copy(source, destination, std::filesystem::copy_options::overwrite_existing);
|
||||
}
|
||||
} catch (const std::filesystem::filesystem_error& ex) {
|
||||
std::cerr << "Error copying " << source << " to " << destination << ": " << ex.what() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void ensure_directories_exist(std::vector<std::string> directories)
|
||||
{
|
||||
for (const auto& directory : directories) {
|
||||
if (!std::filesystem::exists(directory)) {
|
||||
std::filesystem::create_directories(directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
|
||||
void constructLps(const std::string &pat, std::vector<int> &lps) {
|
||||
|
||||
// len stores the length of longest prefix which
|
||||
// is also a suffix for the previous index
|
||||
int len = 0;
|
||||
|
||||
// lps[0] is always 0
|
||||
lps[0] = 0;
|
||||
|
||||
int i = 1;
|
||||
while (i < pat.length()) {
|
||||
|
||||
// If characters match, increment the size of lps
|
||||
if (pat[i] == pat[len]) {
|
||||
len++;
|
||||
lps[i] = len;
|
||||
i++;
|
||||
}
|
||||
|
||||
// If there is a mismatch
|
||||
else {
|
||||
if (len != 0) {
|
||||
|
||||
// Update len to the previous lps value
|
||||
// to avoid reduntant comparisons
|
||||
len = lps[len - 1];
|
||||
}
|
||||
else {
|
||||
|
||||
// If no matching prefix found, set lps[i] to 0
|
||||
lps[i] = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<int> search(const std::string &pat, const std::string &txt) {
|
||||
int n = txt.length();
|
||||
int m = pat.length();
|
||||
|
||||
std::vector<int> lps(m);
|
||||
std::vector<int> res;
|
||||
|
||||
constructLps(pat, lps);
|
||||
|
||||
// Pointers i and j, for traversing
|
||||
// the text and pattern
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
while (i < n) {
|
||||
|
||||
// If characters match, move both pointers forward
|
||||
if (txt[i] == pat[j]) {
|
||||
i++;
|
||||
j++;
|
||||
|
||||
// If the entire pattern is matched
|
||||
// store the start index in result
|
||||
if (j == m) {
|
||||
res.push_back(i - j);
|
||||
|
||||
// Use LPS of previous index to
|
||||
// skip unnecessary comparisons
|
||||
j = lps[j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
// If there is a mismatch
|
||||
else {
|
||||
|
||||
// Use lps value of previous index
|
||||
// to avoid redundant comparisons
|
||||
if (j != 0)
|
||||
j = lps[j - 1];
|
||||
else
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int count_substring(const std::string &substring, const std::string &text) {
|
||||
std::vector<int> positions = search(substring, text);
|
||||
return positions.size();
|
||||
}
|
||||
|
||||
std::vector<std::string> split(const std::string& str, const std::string& delimiter) {
|
||||
std::vector<std::string> tokens;
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
|
||||
while ((end = str.find(delimiter, start)) != std::string::npos) {
|
||||
tokens.push_back(str.substr(start, end - start));
|
||||
start = end + delimiter.length();
|
||||
}
|
||||
|
||||
// Add the last token
|
||||
tokens.push_back(str.substr(start));
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
std::string replace_with_environment_variables_like_bash(std::string str) {
|
||||
// Combined regex pattern for both ${var} and $var formats
|
||||
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
|
||||
std::string result = str;
|
||||
std::smatch match;
|
||||
|
||||
while (std::regex_search(result, match, var_pattern)) {
|
||||
// match[1] will contain capture from ${var} format
|
||||
// match[2] will contain capture from $var format
|
||||
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
|
||||
|
||||
// Get value from system environment variables
|
||||
const char* env_value = std::getenv(var_name.c_str());
|
||||
std::string value = env_value ? env_value : "";
|
||||
|
||||
result = result.replace(match.position(), match.length(), value);
|
||||
}
|
||||
|
||||
// dequote the result
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string random_alphanumeric_string(int length)
|
||||
{
|
||||
static std::mt19937 generator(std::random_device{}());
|
||||
static const std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
std::uniform_int_distribution<> distribution(0, chars.size() - 1);
|
||||
std::string random_string;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
random_string += chars[distribution(generator)];
|
||||
}
|
||||
|
||||
return random_string;
|
||||
}
|
||||
|
||||
std::string requote(std::string str) {
|
||||
return quote(trim(dequote(trim(str))));
|
||||
}
|
||||
|
||||
|
||||
int die(const std::string & msg) {
|
||||
std::cerr << msg << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string safearg(const std::vector<std::string> & args, int index)
|
||||
{
|
||||
if (index<0 || index >= args.size()) return "";
|
||||
return args[index];
|
||||
}
|
||||
|
||||
std::string safearg(int argc, char *argv[], int index)
|
||||
{
|
||||
if (index<0 || index >= argc) return "";
|
||||
return argv[index];
|
||||
}
|
||||
|
||||
|
||||
void print_left_aligned(const std::string & str, int width) {
|
||||
std::cout << left_align(str, width);
|
||||
}
|
||||
|
||||
void print_centered(const std::string & str, int width) {
|
||||
std::cout << center_align(str, width);
|
||||
}
|
||||
|
||||
void print_right_aligned(const std::string & str, int width) {
|
||||
std::cout << right_align(str, width);
|
||||
}
|
||||
|
||||
|
||||
std::string left_align(const std::string & str, int width) {
|
||||
if (static_cast<int>(str.size()) >= width)
|
||||
return str;
|
||||
return str + std::string(width - str.size(), ' ');
|
||||
}
|
||||
|
||||
std::string right_align(const std::string & str, int width) {
|
||||
if (static_cast<int>(str.size()) >= width)
|
||||
return str;
|
||||
return std::string(width - str.size(), ' ') + str;
|
||||
}
|
||||
|
||||
std::string center_align(const std::string & str, int width) {
|
||||
int pad = width - static_cast<int>(str.size());
|
||||
if (pad <= 0)
|
||||
return str;
|
||||
int pad_left = pad / 2;
|
||||
int pad_right = pad - pad_left;
|
||||
return std::string(pad_left, ' ') + str + std::string(pad_right, ' ');
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace dropshell
|
56
source/src/utils/utils.hpp
Normal file
56
source/src/utils/utils.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
/**
|
||||
* Prints a formatted title surrounded by a box of dashes.
|
||||
*
|
||||
* @param title The title string to display
|
||||
*/
|
||||
void maketitle(const std::string& title);
|
||||
|
||||
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);
|
||||
|
||||
|
||||
// utility functions
|
||||
std::string trim(std::string str);
|
||||
std::string dequote(std::string str);
|
||||
std::string quote(std::string str);
|
||||
std::string halfquote(std::string str);
|
||||
std::string requote(std::string str);
|
||||
std::string escapequotes(std::string str);
|
||||
|
||||
std::string multi2string(std::vector<std::string> values);
|
||||
std::vector<std::string> string2multi(std::string values);
|
||||
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
|
||||
|
||||
int str2int(const std::string & str);
|
||||
|
||||
void recursive_copy(const std::string & source, const std::string & destination);
|
||||
|
||||
void ensure_directories_exist(std::vector<std::string> directories);
|
||||
|
||||
// KMP algorithm
|
||||
std::vector<int> search(const std::string &pat, const std::string &txt);
|
||||
int count_substring(const std::string &substring, const std::string &text);
|
||||
|
||||
std::string replace_with_environment_variables_like_bash(std::string str);
|
||||
|
||||
std::string random_alphanumeric_string(int length);
|
||||
|
||||
int die(const std::string & msg);
|
||||
std::string safearg(int argc, char *argv[], int index);
|
||||
std::string safearg(const std::vector<std::string> & args, int index);
|
||||
|
||||
void print_left_aligned(const std::string & str, int width);
|
||||
void print_centered(const std::string & str, int width);
|
||||
void print_right_aligned(const std::string & str, int width);
|
||||
|
||||
std::string left_align(const std::string & str, int width);
|
||||
std::string right_align(const std::string & str, int width);
|
||||
std::string center_align(const std::string & str, int width);
|
||||
|
||||
} // namespace dropshell
|
21
source/src/version.hpp.in
Normal file
21
source/src/version.hpp.in
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
/*
|
||||
|
||||
version.hpp is automatically generated by the build system, from version.hpp.in.
|
||||
|
||||
DO NOT EDIT VERSION.HPP!
|
||||
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
// Version information
|
||||
const std::string VERSION = "@PROJECT_VERSION@";
|
||||
const std::string RELEASE_DATE = "@RELEASE_DATE@";
|
||||
const std::string AUTHOR = "j842";
|
||||
const std::string LICENSE = "MIT";
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user