Compare commits
46 Commits
2025.0513.
...
2025.0518.
Author | SHA1 | Date | |
---|---|---|---|
828171c977 | |||
668cef5a05 | |||
e7c6d38273 | |||
d0152c3ec7 | |||
ebb101e381 | |||
dc2f694ebe | |||
038d08e638 | |||
27f86e95e7 | |||
891f0d023f | |||
91f706ffcd | |||
0e1ac9ddd8 | |||
e5aaa57259 | |||
cf42ce5304 | |||
1d3bb634f0 | |||
203068048d | |||
5bf93dc954 | |||
583bb18676 | |||
4c0bca4def | |||
4efccf7793 | |||
f362c1699b | |||
4147d4b97f | |||
399fe1d549 | |||
67da992326 | |||
82af6a6af7 | |||
985153377f | |||
93e563948f | |||
9eb9707c2e | |||
49b1475ffd | |||
a68e31eb6b | |||
5be6a3e038 | |||
707e973130 | |||
2cc00244c6 | |||
22e37b212a | |||
ba866494cd | |||
d8d73058de | |||
b396441271 | |||
2397c665a5 | |||
a828100878 | |||
fcc517a115 | |||
47dcfca5f2 | |||
283b88effc | |||
97776b4642 | |||
89095cdc50 | |||
8d5296d9ea | |||
524d3df1b2 | |||
54dce4862f |
29
README.md
29
README.md
@ -1,3 +1,32 @@
|
|||||||
# Dropshell
|
# Dropshell
|
||||||
|
|
||||||
A system management tool for server operations, written in C++.
|
A system management tool for server operations, written in C++.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```
|
||||||
|
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/install.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
This installs as dropshell, with a symlink ds if the ds command does not already exist.
|
||||||
|
|
||||||
|
## Installation of Agent
|
||||||
|
|
||||||
|
Install the Agent on each server you wish to manage. Supports amd64 (x86_64) and arm64 (aarch64) architectures.
|
||||||
|
|
||||||
|
```
|
||||||
|
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/server_autosetup.sh | sudo bash
|
||||||
|
```
|
||||||
|
|
||||||
|
Manual steps:
|
||||||
|
1. `apt install curl wget jq`
|
||||||
|
1. `curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh && rm get-docker.sh`
|
||||||
|
1. `useradd -m dropshell && usermod -aG docker dropshell && chsh -s /bin/bash dropshell`
|
||||||
|
1. Put appropriate ssh keys in `/home/dropshell/.ssh/authorized_keys`
|
||||||
|
1. Test ssh'ing into the server.
|
||||||
|
|
||||||
|
|
||||||
|
## Install Services
|
||||||
|
|
||||||
|
Set up a server and install a service:
|
||||||
|
1. `ds create-server SERVERNAME`
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
#!/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"
|
|
14
install.sh
14
install.sh
@ -3,6 +3,12 @@ set -e
|
|||||||
|
|
||||||
# download and install dropshell
|
# download and install dropshell
|
||||||
|
|
||||||
|
# Check if running as root
|
||||||
|
if [ "$EUID" -ne 0 ]; then
|
||||||
|
echo "Please run this script as root (use sudo)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# 1. Determine architecture
|
# 1. Determine architecture
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
@ -33,8 +39,12 @@ fi
|
|||||||
|
|
||||||
chmod +x "$TMPDIR/dropshell"
|
chmod +x "$TMPDIR/dropshell"
|
||||||
|
|
||||||
docker run --rm -v "$TMPDIR:/tmp" -v /usr/local/bin:/target alpine sh -c "cp /tmp/dropshell /target/dropshell && ln -s /target/dropshell /target/ds"
|
cp "$TMPDIR/dropshell" /usr/local/bin/dropshell
|
||||||
rm "$TMPDIR/dropshell"
|
if [ -f /usr/local/bin/ds ]; then
|
||||||
|
rm -f /usr/local/bin/ds
|
||||||
|
fi
|
||||||
|
ln -s /usr/local/bin/dropshell /usr/local/bin/ds
|
||||||
|
rm -rf "$TMPDIR"
|
||||||
|
|
||||||
echo "dropshell installed successfully to /usr/local/bin/dropshell"
|
echo "dropshell installed successfully to /usr/local/bin/dropshell"
|
||||||
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# build amd64 and arm64 versions of dropshell, to:
|
|
||||||
# build/dropshell.amd64
|
|
||||||
# build/dropshell.arm64
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# Build for amd64
|
|
||||||
echo "Building for amd64..."
|
|
||||||
cmake -B build_amd64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=x86-64" .
|
|
||||||
cmake --build build_amd64 --target dropshell --config Release
|
|
||||||
mkdir -p build
|
|
||||||
cp build_amd64/dropshell build/dropshell.amd64
|
|
||||||
|
|
||||||
# Build for arm64
|
|
||||||
echo "Building for arm64..."
|
|
||||||
cmake -B build_arm64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS="-march=armv8-a" -DCMAKE_SYSTEM_PROCESSOR=aarch64 -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ .
|
|
||||||
cmake --build build_arm64 --target dropshell --config Release
|
|
||||||
mkdir -p build
|
|
||||||
cp build_arm64/dropshell build/dropshell.arm64
|
|
||||||
|
|
||||||
echo "Builds complete:"
|
|
||||||
ls -lh build/dropshell.*
|
|
||||||
|
|
@ -90,18 +90,6 @@ chsh -s /bin/bash dropshell
|
|||||||
|
|
||||||
#--------------------------------
|
#--------------------------------
|
||||||
|
|
||||||
# download dropshell
|
|
||||||
|
|
||||||
# determine if x86_64 or arm64
|
|
||||||
ARCH=$(uname -m)
|
|
||||||
|
|
||||||
# check is aarch64 or x86_64 and error if neither
|
|
||||||
if [ "$ARCH" != "aarch64" ] && [ "$ARCH" != "x86_64" ]; then
|
|
||||||
echo "Unsupported architecture: $ARCH"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
echo "Installation complete."
|
echo "Installation complete."
|
||||||
|
|
||||||
#--------------------------------
|
#--------------------------------
|
@ -1,7 +1,7 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(dropshell VERSION 1.0.0 LANGUAGES CXX)
|
project(dropshell VERSION 1.0.0 LANGUAGES CXX)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
# Set default build type to Release if not specified
|
# Set default build type to Release if not specified
|
||||||
@ -25,7 +25,7 @@ string(TIMESTAMP RELEASE_DATE "%Y-%m-%d")
|
|||||||
# Configure version.hpp file
|
# Configure version.hpp file
|
||||||
configure_file(
|
configure_file(
|
||||||
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.hpp.in"
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/version.hpp.in"
|
||||||
"${CMAKE_CURRENT_BINARY_DIR}/src/version.hpp"
|
"${CMAKE_CURRENT_BINARY_DIR}/src/autogen/version.hpp"
|
||||||
@ONLY
|
@ONLY
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,17 +36,27 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
|
|||||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||||
file(GLOB_RECURSE HEADERS "src/*.hpp")
|
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
|
||||||
add_executable(dropshell ${SOURCES})
|
add_executable(dropshell ${SOURCES})
|
||||||
|
add_dependencies(dropshell run_createagent)
|
||||||
|
|
||||||
# Set include directories
|
# Set include directories
|
||||||
# build dir goes first so that we can use the generated version.hpp
|
# build dir goes first so that we can use the generated version.hpp
|
||||||
target_include_directories(dropshell PRIVATE
|
target_include_directories(dropshell PRIVATE
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src>
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/src/autogen>
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/utils
|
${CMAKE_CURRENT_SOURCE_DIR}/src/utils
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/contrib
|
${CMAKE_CURRENT_SOURCE_DIR}/src/contrib
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/src/commands
|
${CMAKE_CURRENT_SOURCE_DIR}/src/commands
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
@ -9,20 +9,30 @@
|
|||||||
# Get all services on the server
|
# Get all services on the server
|
||||||
SCRIPT_DIR="$(dirname "$0")"
|
SCRIPT_DIR="$(dirname "$0")"
|
||||||
|
|
||||||
|
# //------------------------------------------------------------------------------------------------
|
||||||
|
# // remote paths
|
||||||
# // DROPSHELL_DIR
|
# // DROPSHELL_DIR
|
||||||
# // |-- backups
|
# // |-- backups
|
||||||
|
# // |-- temp_files
|
||||||
|
# // |-- agent
|
||||||
|
# // | |-- bb64
|
||||||
|
# // | |-- (other agent files, including _allservicesstatus.sh)
|
||||||
# // |-- services
|
# // |-- services
|
||||||
# // |-- service name
|
# // |-- service name
|
||||||
# // |-- config <-- this is passed as argument to all scripts
|
|
||||||
# // |-- service.env
|
|
||||||
# // |-- template
|
|
||||||
# // |-- (script files)
|
|
||||||
# // |-- shared
|
|
||||||
# // |-- _allservicesstatus.sh
|
|
||||||
# // |-- config
|
# // |-- config
|
||||||
# // |-- service.env
|
# // |-- service.env (actual service config)
|
||||||
|
# // |-- .template_info.env
|
||||||
|
# // |-- template
|
||||||
|
# // |-- _default.env
|
||||||
|
# // |-- (script files)
|
||||||
|
# // |-- config
|
||||||
|
# // |-- service.env (default service config)
|
||||||
|
# // |-- .template_info.env
|
||||||
# // |-- (other config files for specific server&service)
|
# // |-- (other config files for specific server&service)
|
||||||
|
|
||||||
|
# Get all services on the server
|
||||||
|
SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../services/")
|
||||||
|
|
||||||
CURRENT_OUTPUT=""
|
CURRENT_OUTPUT=""
|
||||||
CURRENT_EXIT_CODE=0
|
CURRENT_EXIT_CODE=0
|
||||||
|
|
||||||
@ -33,6 +43,15 @@ load_dotenv(){
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_check_required_env_vars_allservicesstatus() {
|
||||||
|
local required_vars=("$@")
|
||||||
|
for var in "${required_vars[@]}"; do
|
||||||
|
if [ -z "${!var}" ]; then
|
||||||
|
_die "Required environment variable $var is not set"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
function run_command() {
|
function run_command() {
|
||||||
local service_path=$1
|
local service_path=$1
|
||||||
local command=$2
|
local command=$2
|
||||||
@ -46,14 +65,18 @@ function run_command() {
|
|||||||
# run the command in a subshell to prevent environment changes
|
# run the command in a subshell to prevent environment changes
|
||||||
CURRENT_OUTPUT=$(
|
CURRENT_OUTPUT=$(
|
||||||
set -a
|
set -a
|
||||||
|
|
||||||
load_dotenv "${service_path}/template/_default.env"
|
load_dotenv "${service_path}/template/_default.env"
|
||||||
load_dotenv "${service_path}/config/service.env"
|
load_dotenv "${service_path}/config/service.env"
|
||||||
set +a
|
load_dotenv "${service_path}/config/.template_info.env"
|
||||||
|
|
||||||
# update the main variables.
|
# update the main variables.
|
||||||
export CONFIG_PATH="${service_path}/config"
|
CONFIG_PATH="${service_path}/config"
|
||||||
# SERVER is correct
|
SERVICE="${SERVICE_NAME}"
|
||||||
export SERVICE="${SERVICE_NAME}"
|
|
||||||
|
set +a
|
||||||
|
|
||||||
|
_check_required_env_vars_allservicesstatus "CONFIG_PATH" "SERVER" "SERVICE" "AGENT_PATH" "HOST_NAME" "TEMPLATE"
|
||||||
|
|
||||||
if [ "$capture_output" = "true" ]; then
|
if [ "$capture_output" = "true" ]; then
|
||||||
# Capture and return output
|
# Capture and return output
|
||||||
@ -75,8 +98,7 @@ function command_exists() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get all services on the server
|
|
||||||
SERVICES_PATH=$(realpath "${SCRIPT_DIR}/../../../")
|
|
||||||
|
|
||||||
# Get all service names
|
# Get all service names
|
||||||
SERVICE_NAMES=$(ls "${SERVICES_PATH}")
|
SERVICE_NAMES=$(ls "${SERVICES_PATH}")
|
@ -28,20 +28,10 @@
|
|||||||
|
|
||||||
# Prints an error message in red and exits with status code 1.
|
# Prints an error message in red and exits with status code 1.
|
||||||
_die() {
|
_die() {
|
||||||
echo -e "\033[91mError: $1\033[0m"
|
echo -e "Error: $1"
|
||||||
exit 1
|
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.
|
# Creates/starts a container, verifying it runs.
|
||||||
_create_and_start_container() {
|
_create_and_start_container() {
|
||||||
if [ -z "$1" ] || [ -z "$2" ]; then
|
if [ -z "$1" ] || [ -z "$2" ]; then
|
||||||
@ -55,9 +45,7 @@ _create_and_start_container() {
|
|||||||
_is_container_running $container_name && return 0
|
_is_container_running $container_name && return 0
|
||||||
_start_container $container_name
|
_start_container $container_name
|
||||||
else
|
else
|
||||||
_grey_start
|
|
||||||
$run_cmd
|
$run_cmd
|
||||||
_grey_end
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! _is_container_running $container_name; then
|
if ! _is_container_running $container_name; then
|
||||||
@ -180,4 +168,4 @@ _root_remove_tree() {
|
|||||||
|
|
||||||
|
|
||||||
# Load autocommands
|
# Load autocommands
|
||||||
source "${AGENT_PATH}/_autocommands.sh"
|
source "${AGENT_PATH}/datacommands.sh"
|
@ -155,16 +155,16 @@ _autocommandparse() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
autocreate() {
|
datacreate() {
|
||||||
_autocommandparse create none "$@"
|
_autocommandparse create none "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
autonuke() {
|
datanuke() {
|
||||||
_autocommandparse nuke none "$@"
|
_autocommandparse nuke none "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
autobackup() {
|
databackup() {
|
||||||
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
||||||
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
|
BACKUP_TEMP_PATH="$TEMP_DIR/backup"
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ autobackup() {
|
|||||||
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
|
tar zcvf "$BACKUP_FILE" -C "$BACKUP_TEMP_PATH" .
|
||||||
}
|
}
|
||||||
|
|
||||||
autorestore() {
|
datarestore() {
|
||||||
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
_check_required_env_vars "BACKUP_FILE" "TEMP_DIR"
|
||||||
BACKUP_TEMP_PATH="$TEMP_DIR/restore"
|
BACKUP_TEMP_PATH="$TEMP_DIR/restore"
|
||||||
|
|
7
source/agent/selftest.sh
Executable file
7
source/agent/selftest.sh
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Running remote agent self-test..."
|
||||||
|
|
||||||
|
echo "Completed remote agent self-test."
|
||||||
|
|
||||||
|
exit 0
|
@ -9,15 +9,11 @@ GREEN='\033[0;32m'
|
|||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
NC='\033[0m' # No Color
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
# Parse command line arguments
|
JOBS=4
|
||||||
AUTO_INSTALL=false
|
# Determine number of CPU cores for parallel build
|
||||||
for arg in "$@"; do
|
if command -v nproc >/dev/null 2>&1; then
|
||||||
case $arg in
|
JOBS=$(nproc)
|
||||||
--auto-install)
|
fi
|
||||||
AUTO_INSTALL=true
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
|
|
||||||
# Function to print status messages
|
# Function to print status messages
|
||||||
@ -33,6 +29,9 @@ print_warning() {
|
|||||||
echo -e "${YELLOW}[!] $1${NC}"
|
echo -e "${YELLOW}[!] $1${NC}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ensure we have latest dehydrate.
|
||||||
|
dehydrate -u
|
||||||
|
|
||||||
# Check if build directory exists, if not create it
|
# Check if build directory exists, if not create it
|
||||||
if [ ! -d "build" ]; then
|
if [ ! -d "build" ]; then
|
||||||
print_status "Creating build directory..."
|
print_status "Creating build directory..."
|
||||||
@ -79,7 +78,7 @@ cmake .. -DCMAKE_BUILD_TYPE=Debug
|
|||||||
|
|
||||||
# Build the project
|
# Build the project
|
||||||
print_status "Building project..."
|
print_status "Building project..."
|
||||||
make -j$(nproc)
|
make -j"$JOBS"
|
||||||
|
|
||||||
# Check if build was successful
|
# Check if build was successful
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
@ -90,8 +89,6 @@ else
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if user wants to install
|
|
||||||
if [ $AUTO_INSTALL = true ]; then
|
|
||||||
print_status "Auto-installing dropshell..."
|
print_status "Auto-installing dropshell..."
|
||||||
sudo make install
|
sudo make install
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
@ -100,16 +97,7 @@ if [ $AUTO_INSTALL = true ]; then
|
|||||||
print_error "Installation failed!"
|
print_error "Installation failed!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
print_status "Installing dropshell..."
|
|
||||||
sudo make install
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
print_status "Installation successful!"
|
|
||||||
else
|
|
||||||
print_error "Installation failed!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Return to original directory
|
# Return to original directory
|
||||||
cd ..
|
cd ..
|
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"
|
59
source/multibuild.sh
Executable file
59
source/multibuild.sh
Executable file
@ -0,0 +1,59 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# build amd64 and arm64 versions of dropshell, to:
|
||||||
|
# build/dropshell.amd64
|
||||||
|
# build/dropshell.arm64
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
|
rm -f $SCRIPT_DIR/build_amd64/dropshell $SCRIPT_DIR/build_arm64/dropshell $SCRIPT_DIR/output/dropshell.amd64 $SCRIPT_DIR/output/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
|
||||||
|
|
||||||
|
PREV_PWD=$PWD
|
||||||
|
cd $SCRIPT_DIR
|
||||||
|
|
||||||
|
# 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 output
|
||||||
|
cp build_amd64/dropshell output/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 output
|
||||||
|
cp build_arm64/dropshell output/dropshell.arm64
|
||||||
|
|
||||||
|
if [ ! -f output/dropshell.amd64 ]; then
|
||||||
|
echo "output/dropshell.amd64 not found!" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f output/dropshell.arm64 ]; then
|
||||||
|
echo "output/dropshell.arm64 not found!" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Builds complete:"
|
||||||
|
ls -lh output/dropshell.*
|
||||||
|
|
||||||
|
cd $PREV_PWD
|
@ -1,6 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
|
# directory of this script
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
echo "Script directory: $SCRIPT_DIR"
|
||||||
|
|
||||||
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
||||||
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
|
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
|
||||||
TOKEN="$GITEA_TOKEN_DEPLOY"
|
TOKEN="$GITEA_TOKEN_DEPLOY"
|
||||||
@ -11,27 +15,32 @@ else
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
$SCRIPT_DIR/multibuild.sh
|
||||||
|
BUILD_DIR=$SCRIPT_DIR/build
|
||||||
|
|
||||||
./multibuild.sh
|
OLD_PWD=$PWD
|
||||||
|
cd $SCRIPT_DIR
|
||||||
|
|
||||||
if [ ! -f "build/dropshell.amd64" ]; then
|
|
||||||
echo "build/dropshell.amd64 not found!" >&2
|
if [ ! -f "output/dropshell.amd64" ]; then
|
||||||
|
echo "output/dropshell.amd64 not found!" >&2
|
||||||
echo "Please run multibuild.sh first." >&2
|
echo "Please run multibuild.sh first." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f "build/dropshell.arm64" ]; then
|
if [ ! -f "output/dropshell.arm64" ]; then
|
||||||
echo "build/dropshell.arm64 not found!" >&2
|
echo "output/dropshell.arm64 not found!" >&2
|
||||||
echo "Please run multibuild.sh first." >&2
|
echo "Please run multibuild.sh first." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TAG=$(./build/dropshell.amd64 --version)
|
TAG=$("$SCRIPT_DIR/output/dropshell.amd64" --version)
|
||||||
|
[ -z "$TAG" ] && echo "Failed to get version from dropshell.amd64" >&2 && exit 1
|
||||||
|
|
||||||
echo "Publishing dropshell version $TAG"
|
echo "Publishing dropshell version $TAG"
|
||||||
|
|
||||||
# make sure we've commited.
|
# make sure we've commited.
|
||||||
git add . && git commit -m "dropshell release $TAG" && git push
|
git add "$SCRIPT_DIR/../" && git commit -m "dropshell release $TAG" && git push
|
||||||
|
|
||||||
|
|
||||||
# Find repo info from .git/config
|
# Find repo info from .git/config
|
||||||
@ -72,9 +81,11 @@ if [ -z "$RELEASE_ID" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Upload binaries and install.sh
|
# Upload binaries and install.sh
|
||||||
for FILE in dropshell.amd64 dropshell.arm64 install.sh; do
|
for FILE in dropshell.amd64 dropshell.arm64 install.sh server_autosetup.sh; do
|
||||||
if [ -f "build/$FILE" ]; then
|
if [ -f "output/$FILE" ]; then
|
||||||
filetoupload="build/$FILE"
|
filetoupload="output/$FILE"
|
||||||
|
elif [ -f "../$FILE" ]; then
|
||||||
|
filetoupload="../$FILE"
|
||||||
elif [ -f "$FILE" ]; then
|
elif [ -f "$FILE" ]; then
|
||||||
filetoupload="$FILE"
|
filetoupload="$FILE"
|
||||||
else
|
else
|
||||||
@ -93,3 +104,5 @@ for FILE in dropshell.amd64 dropshell.arm64 install.sh; do
|
|||||||
done
|
done
|
||||||
|
|
||||||
echo "Published dropshell version $TAG to $REPO_URL (tag $TAG) with binaries."
|
echo "Published dropshell version $TAG to $REPO_URL (tag $TAG) with binaries."
|
||||||
|
|
||||||
|
cd $OLD_PWD
|
439
source/src/autogen/_agent.cpp
Normal file
439
source/src/autogen/_agent.cpp
Normal file
@ -0,0 +1,439 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
THIS FILE IS AUTO-GENERATED BY DEHYDRATE.
|
||||||
|
DO NOT EDIT THIS FILE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "_agent.hpp"
|
||||||
|
namespace recreate_agent {
|
||||||
|
|
||||||
|
|
||||||
|
// Tiny dependency-free FNV-1a 64-bit hash
|
||||||
|
static uint64_t fnv1a_64(const void* data, size_t len) {
|
||||||
|
const uint8_t* p = static_cast<const uint8_t*>(data);
|
||||||
|
uint64_t h = 0xcbf29ce484222325ULL;
|
||||||
|
for (size_t i = 0; i < len; ++i)
|
||||||
|
h = (h ^ p[i]) * 0x100000001b3ULL;
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Base64 decoding function - no dependencies
|
||||||
|
static void base64_decode(const char* encoded_data, size_t encoded_len, unsigned char* output, size_t* output_len) {
|
||||||
|
const char* base64_chars =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
size_t out_pos = 0;
|
||||||
|
int val = 0, valb = -8;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < encoded_len; i++) {
|
||||||
|
char c = encoded_data[i];
|
||||||
|
if (c == '=') break;
|
||||||
|
|
||||||
|
// Find position in base64_chars
|
||||||
|
const char* pos = strchr(base64_chars, c);
|
||||||
|
if (pos == nullptr) continue; // Skip invalid characters
|
||||||
|
|
||||||
|
val = (val << 6) + static_cast<int>(pos - base64_chars);
|
||||||
|
valb += 6;
|
||||||
|
if (valb >= 0) {
|
||||||
|
output[out_pos++] = static_cast<unsigned char>((val >> valb) & 0xFF);
|
||||||
|
valb -= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*output_len = out_pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Utility function to recreate a file with proper permissions
|
||||||
|
static bool _recreate_file_(const std::filesystem::path& outpath, uint64_t file_hash, std::filesystem::perms file_perms, const unsigned char* filedata, size_t filedata_len) {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
bool needs_write = false;
|
||||||
|
|
||||||
|
// Check if file exists and has correct hash
|
||||||
|
if (fs::exists(outpath)) {
|
||||||
|
// Check content hash
|
||||||
|
std::ifstream in(outpath, std::ios::binary);
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << in.rdbuf();
|
||||||
|
std::string data = oss.str();
|
||||||
|
uint64_t existing_hash = fnv1a_64(data.data(), data.size());
|
||||||
|
needs_write = existing_hash != file_hash;
|
||||||
|
} else {
|
||||||
|
needs_write = true; // File doesn't exist, need to create it
|
||||||
|
}
|
||||||
|
|
||||||
|
bool needs_permission_update = true;
|
||||||
|
if (!needs_write) { // we always update permissions if the file is written or changed. Othewise we check.
|
||||||
|
fs::perms current_perms = fs::status(outpath).permissions();
|
||||||
|
needs_permission_update = current_perms != file_perms;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needs_write) {
|
||||||
|
fs::create_directories(outpath.parent_path());
|
||||||
|
std::ofstream out(outpath, std::ios::binary);
|
||||||
|
out.write(reinterpret_cast<const char*>(filedata), filedata_len);
|
||||||
|
out.close();
|
||||||
|
// Set the file permissions
|
||||||
|
fs::permissions(outpath, file_perms);
|
||||||
|
|
||||||
|
if (!fs::exists(outpath)) {
|
||||||
|
std::cout << "[dehydrate] " << outpath.filename() << ": created\n";
|
||||||
|
} else {
|
||||||
|
std::cout << "[dehydrate] " << outpath.filename() << ": updated (hash changed)\n";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needs_permission_update) {
|
||||||
|
// Update only permissions
|
||||||
|
fs::permissions(outpath, file_perms);
|
||||||
|
std::cout << "[dehydrate] " << outpath.filename() << ": updated (permissions changed)\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool recreate_tree(std::string destination_folder) {
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
bool any_written = false;
|
||||||
|
{
|
||||||
|
// File: selftest.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "selftest.sh";
|
||||||
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKCmVjaG8gIlJ1bm5pbmcgcmVtb3RlIGFnZW50IHNlbGYtdGVzdC4uLiIKCmVj"\
|
||||||
|
"aG8gIkNvbXBsZXRlZCByZW1vdGUgYWdlbnQgc2VsZi10ZXN0LiIKCmV4aXQgMAo=";
|
||||||
|
|
||||||
|
// Decode Base64 data
|
||||||
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
|
unsigned char* decoded_data = new unsigned char[decoded_size];
|
||||||
|
size_t actual_size;
|
||||||
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
|
bool file_written = _recreate_file_(outpath, 11594895391899191874ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// File: datacommands.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "datacommands.sh";
|
||||||
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKCiMgVGhpcyBzY3JpcHQgY29udGFpbnMgdGhlIGNvbW1vbiBjb2RlIGZvciB0"\
|
||||||
|
"aGUgYXV0b2NvbW1hbmRzLgoKTVlJRD0kKGlkIC11KQpNWUdSUD0kKGlkIC1nKQoKX2F1dG9jb21t"\
|
||||||
|
"YW5kcnVuX3ZvbHVtZSgpIHsKICAgIGxvY2FsIGNvbW1hbmQ9IiQxIgogICAgbG9jYWwgdm9sdW1l"\
|
||||||
|
"X25hbWU9IiQyIgogICAgbG9jYWwgYmFja3VwX2ZvbGRlcj0iJDMiCgogICAgY2FzZSAiJGNvbW1h"\
|
||||||
|
"bmQiIGluCiAgICAgICAgY3JlYXRlKQogICAgICAgICAgICBlY2hvICJDcmVhdGluZyB2b2x1bWUg"\
|
||||||
|
"JHt2b2x1bWVfbmFtZX0iCiAgICAgICAgICAgIGRvY2tlciB2b2x1bWUgY3JlYXRlICR7dm9sdW1l"\
|
||||||
|
"X25hbWV9CiAgICAgICAgICAgIDs7CiAgICAgICAgbnVrZSkKICAgICAgICAgICAgZWNobyAiTnVr"\
|
||||||
|
"aW5nIHZvbHVtZSAke3ZvbHVtZV9uYW1lfSIKICAgICAgICAgICAgZG9ja2VyIHZvbHVtZSBybSAk"\
|
||||||
|
"e3ZvbHVtZV9uYW1lfQogICAgICAgICAgICA7OwogICAgICAgIGJhY2t1cCkKICAgICAgICAgICAg"\
|
||||||
|
"ZWNobyAiQmFja2luZyB1cCB2b2x1bWUgJHt2b2x1bWVfbmFtZX0iCiAgICAgICAgICAgIGRvY2tl"\
|
||||||
|
"ciBydW4gLS1ybSAtdiAke3ZvbHVtZV9uYW1lfTovdm9sdW1lIC12ICR7YmFja3VwX2ZvbGRlcn06"\
|
||||||
|
"L2JhY2t1cCBkZWJpYW4gYmFzaCAtYyAidGFyIC1jenZmIC9iYWNrdXAvYmFja3VwLnRneiAtQyAv"\
|
||||||
|
"dm9sdW1lIC4gJiYgY2hvd24gLVIgJE1ZSUQ6JE1ZR1JQIC9iYWNrdXAiCiAgICAgICAgICAgIDs7"\
|
||||||
|
"CiAgICAgICAgcmVzdG9yZSkKICAgICAgICAgICAgZWNobyAiUmVzdG9yaW5nIHZvbHVtZSAke3Zv"\
|
||||||
|
"bHVtZV9uYW1lfSIKICAgICAgICAgICAgZG9ja2VyIHZvbHVtZSBybSAke3ZvbHVtZV9uYW1lfQog"\
|
||||||
|
"ICAgICAgICAgICBkb2NrZXIgdm9sdW1lIGNyZWF0ZSAke3ZvbHVtZV9uYW1lfQogICAgICAgICAg"\
|
||||||
|
"ICBkb2NrZXIgcnVuIC0tcm0gLXYgJHt2b2x1bWVfbmFtZX06L3ZvbHVtZSAtdiAke2JhY2t1cF9m"\
|
||||||
|
"b2xkZXJ9Oi9iYWNrdXAgZGViaWFuIGJhc2ggLWMgInRhciAteHp2ZiAvYmFja3VwL2JhY2t1cC50"\
|
||||||
|
"Z3ogLUMgL3ZvbHVtZSAtLXN0cmlwLWNvbXBvbmVudHM9MSIKICAgICAgICAgICAgOzsKICAgIGVz"\
|
||||||
|
"YWMKfSAgIAoKX2F1dG9jb21tYW5kcnVuX3BhdGgoKSB7CiAgICBsb2NhbCBjb21tYW5kPSIkMSIK"\
|
||||||
|
"ICAgIGxvY2FsIHBhdGg9IiQyIgogICAgbG9jYWwgYmFja3VwX2ZvbGRlcj0iJDMiCgogICAgY2Fz"\
|
||||||
|
"ZSAiJGNvbW1hbmQiIGluCiAgICAgICAgY3JlYXRlKQogICAgICAgICAgICBlY2hvICJDcmVhdGlu"\
|
||||||
|
"ZyBwYXRoICR7cGF0aH0iCiAgICAgICAgICAgIG1rZGlyIC1wICR7cGF0aH0KICAgICAgICAgICAg"\
|
||||||
|
"OzsKICAgICAgICBudWtlKQogICAgICAgICAgICBlY2hvICJOdWtpbmcgcGF0aCAke3BhdGh9Igog"\
|
||||||
|
"ICAgICAgICAgICBsb2NhbCBwYXRoX3BhcmVudD0kKGRpcm5hbWUgJHtwYXRofSkKICAgICAgICAg"\
|
||||||
|
"ICAgbG9jYWwgcGF0aF9jaGlsZD0kKGJhc2VuYW1lICR7cGF0aH0pCiAgICAgICAgICAgIGlmIFsg"\
|
||||||
|
"LWQgIiR7cGF0aF9wYXJlbnR9LyR7cGF0aF9jaGlsZH0iIF07IHRoZW4KICAgICAgICAgICAgICAg"\
|
||||||
|
"IGRvY2tlciBydW4gLS1ybSAtdiAke3BhdGhfcGFyZW50fTovdm9sdW1lIGRlYmlhbiBiYXNoIC1j"\
|
||||||
|
"ICJybSAtcmYgL3ZvbHVtZS8ke3BhdGhfY2hpbGR9IiB8fCBlY2hvICJGYWlsZWQgdG8gbnVrZSBw"\
|
||||||
|
"YXRoICR7cGF0aH0iCiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIGVjaG8gIlBhdGgg"\
|
||||||
|
"JHtwYXRofSBkb2VzIG5vdCBleGlzdCAtIG5vdGhpbmcgdG8gbnVrZSIKICAgICAgICAgICAgZmkK"\
|
||||||
|
"ICAgICAgICAgICAgOzsKICAgICAgICBiYWNrdXApCiAgICAgICAgICAgIGVjaG8gIkJhY2tpbmcg"\
|
||||||
|
"dXAgcGF0aCAke3BhdGh9IgogICAgICAgICAgICBpZiBbIC1kICIke3BhdGh9IiBdOyB0aGVuCiAg"\
|
||||||
|
"ICAgICAgICAgICAgICBkb2NrZXIgcnVuIC0tcm0gLXYgJHtwYXRofTovcGF0aCAtdiAke2JhY2t1"\
|
||||||
|
"cF9mb2xkZXJ9Oi9iYWNrdXAgZGViaWFuIGJhc2ggLWMgInRhciAtY3p2ZiAvYmFja3VwL2JhY2t1"\
|
||||||
|
"cC50Z3ogLUMgL3BhdGggLiAmJiBjaG93biAtUiAkTVlJRDokTVlHUlAgL2JhY2t1cCIKICAgICAg"\
|
||||||
|
"ICAgICAgZWxzZQogICAgICAgICAgICAgICAgZWNobyAiUGF0aCAke3BhdGh9IGRvZXMgbm90IGV4"\
|
||||||
|
"aXN0IC0gbm90aGluZyB0byBiYWNrdXAiCiAgICAgICAgICAgIGZpCiAgICAgICAgICAgIDs7CiAg"\
|
||||||
|
"ICAgICAgcmVzdG9yZSkKICAgICAgICAgICAgZWNobyAiUmVzdG9yaW5nIHBhdGggJHtwYXRofSIK"\
|
||||||
|
"ICAgICAgICAgICAgdGFyIC14enZmICR7YmFja3VwX2ZvbGRlcn0vYmFja3VwLnRneiAtQyAke3Bh"\
|
||||||
|
"dGh9IC0tc3RyaXAtY29tcG9uZW50cz0xCiAgICAgICAgICAgIDs7CiAgICBlc2FjCn0KCl9hdXRv"\
|
||||||
|
"Y29tbWFuZHJ1bl9maWxlKCkgewogICAgbG9jYWwgY29tbWFuZD0iJDEiCiAgICBsb2NhbCBmaWxl"\
|
||||||
|
"cGF0aD0iJDIiCiAgICBsb2NhbCBiYWNrdXBfZm9sZGVyPSIkMyIKCiAgICBjYXNlICIkY29tbWFu"\
|
||||||
|
"ZCIgaW4KICAgICAgICBjcmVhdGUpCiAgICAgICAgICAgIDs7CiAgICAgICAgbnVrZSkKICAgICAg"\
|
||||||
|
"ICAgICAgcm0gLWYgJHtmaWxlcGF0aH0KICAgICAgICAgICAgOzsKICAgICAgICBiYWNrdXApCiAg"\
|
||||||
|
"ICAgICAgICAgIGVjaG8gIkJhY2tpbmcgdXAgZmlsZSAke2ZpbGVwYXRofSIKICAgICAgICAgICAg"\
|
||||||
|
"bG9jYWwgZmlsZV9wYXJlbnQ9JChkaXJuYW1lICR7ZmlsZXBhdGh9KQogICAgICAgICAgICBsb2Nh"\
|
||||||
|
"bCBmaWxlX25hbWU9JChiYXNlbmFtZSAke2ZpbGVwYXRofSkKICAgICAgICAgICAgaWYgWyAtZiAi"\
|
||||||
|
"JHtmaWxlX3BhcmVudH0vJHtmaWxlX25hbWV9IiBdOyB0aGVuCiAgICAgICAgICAgICAgICBkb2Nr"\
|
||||||
|
"ZXIgcnVuIC0tcm0gLXYgJHtmaWxlX3BhcmVudH06L3ZvbHVtZSAtdiAke2JhY2t1cF9mb2xkZXJ9"\
|
||||||
|
"Oi9iYWNrdXAgZGViaWFuIGJhc2ggLWMgImNwIC92b2x1bWUvJHtmaWxlX25hbWV9IC9iYWNrdXAv"\
|
||||||
|
"JHtmaWxlX25hbWV9ICYmIGNob3duIC1SICRNWUlEOiRNWUdSUCAvYmFja3VwIgogICAgICAgICAg"\
|
||||||
|
"ICBlbHNlCiAgICAgICAgICAgICAgICBlY2hvICJGaWxlICR7ZmlsZXBhdGh9IGRvZXMgbm90IGV4"\
|
||||||
|
"aXN0IC0gbm90aGluZyB0byBiYWNrdXAiCiAgICAgICAgICAgIGZpCiAgICAgICAgICAgIDs7CiAg"\
|
||||||
|
"ICAgICAgcmVzdG9yZSkKICAgICAgICAgICAgZWNobyAiUmVzdG9yaW5nIGZpbGUgJHtmaWxlcGF0"\
|
||||||
|
"aH0iCiAgICAgICAgICAgIGxvY2FsIGZpbGVfbmFtZT0kKGJhc2VuYW1lICR7ZmlsZXBhdGh9KQog"\
|
||||||
|
"ICAgICAgICAgICBjcCAke2JhY2t1cF9mb2xkZXJ9LyR7ZmlsZV9uYW1lfSAke2ZpbGVwYXRofQog"\
|
||||||
|
"ICAgICAgICAgICA7OwogICAgZXNhYwp9CgpfYXV0b2NvbW1hbmRwYXJzZSgpIHsKICAgICMgZmly"\
|
||||||
|
"c3QgYXJndW1lbnQgaXMgdGhlIGNvbW1hbmQKICAgICMgaWYgdGhlIGNvbW1hbmQgaXMgYmFja3Vw"\
|
||||||
|
"IG9yIHJlc3RvcmUsIHRoZW4gdGhlIGxhc3QgdHdvIGFyZ3VtZW50cyBhcmUgdGhlIGJhY2t1cCBm"\
|
||||||
|
"aWxlIGFuZCB0aGUgdGVtcG9yYXJ5IHBhdGgKICAgICMgYWxsIG90aGVyIGFyZ3VtZW50cyBhcmUg"\
|
||||||
|
"b2YgZm9ybToKICAgICMga2V5PXZhbHVlCiAgICAjIHdoZXJlIGtleSBjYW4gYmUgb25lIG9mIHZv"\
|
||||||
|
"bHVtZSwgcGF0aCBvciBmaWxlLgogICAgIyB2YWx1ZSBpcyB0aGUgcGF0aCBvciB2b2x1bWUgbmFt"\
|
||||||
|
"ZS4KCiAgICAjIHdlIGl0ZXJhdGUgb3ZlciB0aGUga2V5PXZhbHVlIGFyZ3VtZW50cywgYW5kIGZv"\
|
||||||
|
"ciBlYWNoIHdlIGNhbGw6CiAgICAjICAgIGF1dG9ydW4gPGNvbW1hbmQ+IDxiYWNrdXBmaWxlPiA8"\
|
||||||
|
"a2V5PiA8dmFsdWU+CgogICAgbG9jYWwgY29tbWFuZD0iJDEiCiAgICBzaGlmdAoKICAgIGxvY2Fs"\
|
||||||
|
"IGJhY2t1cF90ZW1wX3BhdGg9IiQxIgogICAgc2hpZnQKCiAgICBlY2hvICJhdXRvY29tbWFuZHBh"\
|
||||||
|
"cnNlOiBjb21tYW5kPSRjb21tYW5kIGJhY2t1cF90ZW1wX3BhdGg9JGJhY2t1cF90ZW1wX3BhdGgi"\
|
||||||
|
"CgogICAgIyBFeHRyYWN0IHRoZSBiYWNrdXAgZmlsZSBhbmQgdGVtcCBwYXRoIChsYXN0IHR3byBh"\
|
||||||
|
"cmd1bWVudHMpCiAgICBsb2NhbCBhcmdzPSgiJEAiKQogICAgbG9jYWwgYXJnX2NvdW50PSR7I2Fy"\
|
||||||
|
"Z3NbQF19CiAgICAKICAgICMgUHJvY2VzcyBhbGwga2V5PXZhbHVlIHBhaXJzCiAgICBmb3IgKChp"\
|
||||||
|
"PTA7IGk8JGFyZ19jb3VudDsgaSsrKSk7IGRvCiAgICAgICAgbG9jYWwgcGFpcj0iJHthcmdzWyRp"\
|
||||||
|
"XX0iCiAgICAgICAgCiAgICAgICAgIyBTa2lwIGlmIG5vdCBpbiBrZXk9dmFsdWUgZm9ybWF0CiAg"\
|
||||||
|
"ICAgICAgaWYgW1sgIiRwYWlyIiAhPSAqIj0iKiBdXTsgdGhlbgogICAgICAgICAgICBjb250aW51"\
|
||||||
|
"ZQogICAgICAgIGZpCiAgICAgICAgCiAgICAgICAgbG9jYWwga2V5PSIke3BhaXIlJT0qfSIKICAg"\
|
||||||
|
"ICAgICBsb2NhbCB2YWx1ZT0iJHtwYWlyIyo9fSIKCiAgICAgICAgIyBjcmVhdGUgYmFja3VwIGZv"\
|
||||||
|
"bGRlciB1bmlxdWUgdG8ga2V5L3ZhbHVlLgogICAgICAgIGxvY2FsIGJmb2xkZXI9JChlY2hvICIk"\
|
||||||
|
"e2tleX1fJHt2YWx1ZX0iIHwgdHIgLWNkICdbOmFsbnVtOl1fLScpCiAgICAgICAgbG9jYWwgdGFy"\
|
||||||
|
"Z2V0cGF0aD0iJHtiYWNrdXBfdGVtcF9wYXRofS8ke2Jmb2xkZXJ9IgogICAgICAgIG1rZGlyIC1w"\
|
||||||
|
"ICR7dGFyZ2V0cGF0aH0KCiAgICAgICAgIyBLZXkgbXVzdCBiZSBvbmUgb2Ygdm9sdW1lLCBwYXRo"\
|
||||||
|
"IG9yIGZpbGUKICAgICAgICBjYXNlICIka2V5IiBpbgogICAgICAgICAgICB2b2x1bWUpCiAgICAg"\
|
||||||
|
"ICAgICAgICAgICBfYXV0b2NvbW1hbmRydW5fdm9sdW1lICIkY29tbWFuZCIgIiR2YWx1ZSIgIiR0"\
|
||||||
|
"YXJnZXRwYXRoIgogICAgICAgICAgICAgICAgOzsKICAgICAgICAgICAgcGF0aCkKICAgICAgICAg"\
|
||||||
|
"ICAgICAgIF9hdXRvY29tbWFuZHJ1bl9wYXRoICIkY29tbWFuZCIgIiR2YWx1ZSIgIiR0YXJnZXRw"\
|
||||||
|
"YXRoIgogICAgICAgICAgICAgICAgOzsKICAgICAgICAgICAgZmlsZSkKICAgICAgICAgICAgICAg"\
|
||||||
|
"IF9hdXRvY29tbWFuZHJ1bl9maWxlICIkY29tbWFuZCIgIiR2YWx1ZSIgIiR0YXJnZXRwYXRoIgog"\
|
||||||
|
"ICAgICAgICAgICAgICAgOzsKICAgICAgICAgICAgKikKICAgICAgICAgICAgICAgIF9kaWUgIlVu"\
|
||||||
|
"a25vd24ga2V5ICRrZXkgcGFzc2VkIHRvIGF1dG8ke2NvbW1hbmR9LiBXZSBvbmx5IHN1cHBvcnQg"\
|
||||||
|
"dm9sdW1lLCBwYXRoIGFuZCBmaWxlLiIKICAgICAgICAgICAgICAgIDs7CiAgICAgICAgZXNhYwog"\
|
||||||
|
"ICAgZG9uZQp9CgoKZGF0YWNyZWF0ZSgpIHsKICAgIF9hdXRvY29tbWFuZHBhcnNlIGNyZWF0ZSBu"\
|
||||||
|
"b25lICIkQCIKfQoKCmRhdGFudWtlKCkgewogICAgX2F1dG9jb21tYW5kcGFyc2UgbnVrZSBub25l"\
|
||||||
|
"ICIkQCIKfQoKZGF0YWJhY2t1cCgpIHsKICAgIF9jaGVja19yZXF1aXJlZF9lbnZfdmFycyAiQkFD"\
|
||||||
|
"S1VQX0ZJTEUiICJURU1QX0RJUiIKICAgIEJBQ0tVUF9URU1QX1BBVEg9IiRURU1QX0RJUi9iYWNr"\
|
||||||
|
"dXAiCgoKICAgIG1rZGlyIC1wICIkQkFDS1VQX1RFTVBfUEFUSCIKICAgIGVjaG8gIl9hdXRvY29t"\
|
||||||
|
"bWFuZHBhcnNlIFtiYWNrdXBdIFskQkFDS1VQX1RFTVBfUEFUSF0gWyRAXSIKICAgIF9hdXRvY29t"\
|
||||||
|
"bWFuZHBhcnNlIGJhY2t1cCAiJEJBQ0tVUF9URU1QX1BBVEgiICIkQCIKCiAgICB0YXIgemN2ZiAi"\
|
||||||
|
"JEJBQ0tVUF9GSUxFIiAtQyAiJEJBQ0tVUF9URU1QX1BBVEgiIC4KfQoKZGF0YXJlc3RvcmUoKSB7"\
|
||||||
|
"CiAgICBfY2hlY2tfcmVxdWlyZWRfZW52X3ZhcnMgIkJBQ0tVUF9GSUxFIiAiVEVNUF9ESVIiCiAg"\
|
||||||
|
"ICBCQUNLVVBfVEVNUF9QQVRIPSIkVEVNUF9ESVIvcmVzdG9yZSIKCiAgICBlY2hvICJfYXV0b2Nv"\
|
||||||
|
"bW1hbmRwYXJzZSBbcmVzdG9yZV0gWyRCQUNLVVBfVEVNUF9QQVRIXSBbJEBdIgoKICAgIG1rZGly"\
|
||||||
|
"IC1wICIkQkFDS1VQX1RFTVBfUEFUSCIKICAgIHRhciB6eHZmICIkQkFDS1VQX0ZJTEUiIC1DICIk"\
|
||||||
|
"QkFDS1VQX1RFTVBfUEFUSCIgLS1zdHJpcC1jb21wb25lbnRzPTEKCiAgICBfYXV0b2NvbW1hbmRw"\
|
||||||
|
"YXJzZSByZXN0b3JlICIkQkFDS1VQX1RFTVBfUEFUSCIgIiRAIgp9Cg==";
|
||||||
|
|
||||||
|
// Decode Base64 data
|
||||||
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
|
unsigned char* decoded_data = new unsigned char[decoded_size];
|
||||||
|
size_t actual_size;
|
||||||
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
|
bool file_written = _recreate_file_(outpath, 6443138635497166205ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// File: _allservicesstatus.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "_allservicesstatus.sh";
|
||||||
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKCiMgVGhpcyBzY3JpcHQgY2hlY2tzIEFMTCBzZXJ2aWNlcyBvbiB0aGUgc2Vy"\
|
||||||
|
"dmVyIGFuZCByZXR1cm5zIGEgc3RhdHVzIGZvciBlYWNoLgoKIyBSZXR1cm4gZm9ybWF0IGlzIHNp"\
|
||||||
|
"bXBsZSBFTlYgd2l0aCB0aGUgZm9sbG93aW5nIGZvcm1hdDoKIyBTRVJWSUNFX05BTUVfSEVBTFRI"\
|
||||||
|
"PWhlYWx0aHl8dW5oZWFsdGh5fHVua25vd24KIyBTRVJWSUNFX05BTUVfUE9SVFM9cG9ydDEscG9y"\
|
||||||
|
"dDIscG9ydDMKCiMgR2V0IGFsbCBzZXJ2aWNlcyBvbiB0aGUgc2VydmVyClNDUklQVF9ESVI9IiQo"\
|
||||||
|
"ZGlybmFtZSAiJDAiKSIKCiAgICAjIC8vLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tCiAgICAjIC8vIHJlbW90ZSBwYXRocwogICAgIyAvLyBEUk9QU0hFTExfRElSCiAgICAj"\
|
||||||
|
"IC8vICAgfC0tIGJhY2t1cHMKICAgICMgLy8gICB8LS0gdGVtcF9maWxlcwogICAgIyAvLyAgIHwt"\
|
||||||
|
"LSBhZ2VudAogICAgIyAvLyAgIHwgICB8LS0gYmI2NAogICAgIyAvLyAgIHwgICB8LS0gKG90aGVy"\
|
||||||
|
"IGFnZW50IGZpbGVzLCBpbmNsdWRpbmcgX2FsbHNlcnZpY2Vzc3RhdHVzLnNoKQogICAgIyAvLyAg"\
|
||||||
|
"IHwtLSBzZXJ2aWNlcwogICAgIyAvLyAgICAgICB8LS0gc2VydmljZSBuYW1lCiAgICAjIC8vICAg"\
|
||||||
|
"ICAgICAgICB8LS0gY29uZmlnCiAgICAjIC8vICAgICAgICAgICAgICAgfC0tIHNlcnZpY2UuZW52"\
|
||||||
|
"IChhY3R1YWwgc2VydmljZSBjb25maWcpCiAgICAjIC8vICAgICAgICAgICAgICAgfC0tIC50ZW1w"\
|
||||||
|
"bGF0ZV9pbmZvLmVudgogICAgIyAvLyAgICAgICAgICAgfC0tIHRlbXBsYXRlCiAgICAjIC8vICAg"\
|
||||||
|
"ICAgICAgICAgICAgfC0tIF9kZWZhdWx0LmVudgogICAgIyAvLyAgICAgICAgICAgICAgIHwtLSAo"\
|
||||||
|
"c2NyaXB0IGZpbGVzKQogICAgIyAvLyAgICAgICAgICAgICAgIHwtLSBjb25maWcKICAgICMgLy8g"\
|
||||||
|
"ICAgICAgICAgICAgICAgICAgfC0tIHNlcnZpY2UuZW52IChkZWZhdWx0IHNlcnZpY2UgY29uZmln"\
|
||||||
|
"KQogICAgIyAvLyAgICAgICAgICAgICAgICAgICB8LS0gLnRlbXBsYXRlX2luZm8uZW52CiAgICAj"\
|
||||||
|
"IC8vICAgICAgICAgICAgICAgICAgIHwtLSAob3RoZXIgY29uZmlnIGZpbGVzIGZvciBzcGVjaWZp"\
|
||||||
|
"YyBzZXJ2ZXImc2VydmljZSkKCiMgR2V0IGFsbCBzZXJ2aWNlcyBvbiB0aGUgc2VydmVyClNFUlZJ"\
|
||||||
|
"Q0VTX1BBVEg9JChyZWFscGF0aCAiJHtTQ1JJUFRfRElSfS8uLi9zZXJ2aWNlcy8iKQoKQ1VSUkVO"\
|
||||||
|
"VF9PVVRQVVQ9IiIKQ1VSUkVOVF9FWElUX0NPREU9MAoKbG9hZF9kb3RlbnYoKXsKICAgIGxvY2Fs"\
|
||||||
|
"IGZpbGVfcGF0aD0kMQogICAgaWYgWyAtZiAiJHtmaWxlX3BhdGh9IiBdOyB0aGVuCiAgICAgICAg"\
|
||||||
|
"c291cmNlICIke2ZpbGVfcGF0aH0iCiAgICBmaQp9CgpfY2hlY2tfcmVxdWlyZWRfZW52X3ZhcnNf"\
|
||||||
|
"YWxsc2VydmljZXNzdGF0dXMoKSB7CiAgICBsb2NhbCByZXF1aXJlZF92YXJzPSgiJEAiKQogICAg"\
|
||||||
|
"Zm9yIHZhciBpbiAiJHtyZXF1aXJlZF92YXJzW0BdfSI7IGRvCiAgICAgICAgaWYgWyAteiAiJHsh"\
|
||||||
|
"dmFyfSIgXTsgdGhlbgogICAgICAgICAgICBfZGllICJSZXF1aXJlZCBlbnZpcm9ubWVudCB2YXJp"\
|
||||||
|
"YWJsZSAkdmFyIGlzIG5vdCBzZXQiCiAgICAgICAgZmkKICAgIGRvbmUKfQoKZnVuY3Rpb24gcnVu"\
|
||||||
|
"X2NvbW1hbmQoKSB7CiAgICBsb2NhbCBzZXJ2aWNlX3BhdGg9JDEKICAgIGxvY2FsIGNvbW1hbmQ9"\
|
||||||
|
"JDIKICAgIGxvY2FsIGNhcHR1cmVfb3V0cHV0PSR7MzotZmFsc2V9ICAjIGRlZmF1bHQgdG8gZmFs"\
|
||||||
|
"c2UgaWYgbm90IHNwZWNpZmllZAoKICAgICMgY2hlY2sgaWYgdGhlIGNvbW1hbmQgaXMgYSBmaWxl"\
|
||||||
|
"CiAgICBpZiBbICEgLWYgIiR7c2VydmljZV9wYXRofS90ZW1wbGF0ZS8ke2NvbW1hbmR9LnNoIiBd"\
|
||||||
|
"OyB0aGVuCiAgICAgICAgcmV0dXJuOwogICAgZmkKCiAgICAjIHJ1biB0aGUgY29tbWFuZCBpbiBh"\
|
||||||
|
"IHN1YnNoZWxsIHRvIHByZXZlbnQgZW52aXJvbm1lbnQgY2hhbmdlcwogICAgQ1VSUkVOVF9PVVRQ"\
|
||||||
|
"VVQ9JCgKICAgICAgICBzZXQgLWEKCiAgICAgICAgbG9hZF9kb3RlbnYgIiR7c2VydmljZV9wYXRo"\
|
||||||
|
"fS90ZW1wbGF0ZS9fZGVmYXVsdC5lbnYiCiAgICAgICAgbG9hZF9kb3RlbnYgIiR7c2VydmljZV9w"\
|
||||||
|
"YXRofS9jb25maWcvc2VydmljZS5lbnYiCiAgICAgICAgbG9hZF9kb3RlbnYgIiR7c2VydmljZV9w"\
|
||||||
|
"YXRofS9jb25maWcvLnRlbXBsYXRlX2luZm8uZW52IgoKICAgICAgICAjIHVwZGF0ZSB0aGUgbWFp"\
|
||||||
|
"biB2YXJpYWJsZXMuCiAgICAgICAgQ09ORklHX1BBVEg9IiR7c2VydmljZV9wYXRofS9jb25maWci"\
|
||||||
|
"CiAgICAgICAgU0VSVklDRT0iJHtTRVJWSUNFX05BTUV9IgoKICAgICAgICBzZXQgK2EKCiAgICAg"\
|
||||||
|
"ICAgX2NoZWNrX3JlcXVpcmVkX2Vudl92YXJzX2FsbHNlcnZpY2Vzc3RhdHVzICJDT05GSUdfUEFU"\
|
||||||
|
"SCIgIlNFUlZFUiIgIlNFUlZJQ0UiICJBR0VOVF9QQVRIIiAiSE9TVF9OQU1FIiAiVEVNUExBVEUi"\
|
||||||
|
"CgogICAgICAgIGlmIFsgIiRjYXB0dXJlX291dHB1dCIgPSAidHJ1ZSIgXTsgdGhlbgogICAgICAg"\
|
||||||
|
"ICAgICAjIENhcHR1cmUgYW5kIHJldHVybiBvdXRwdXQKICAgICAgICAgICAgYmFzaCAiJHtzZXJ2"\
|
||||||
|
"aWNlX3BhdGh9L3RlbXBsYXRlLyR7Y29tbWFuZH0uc2giIDI+JjEKICAgICAgICBlbHNlCiAgICAg"\
|
||||||
|
"ICAgICAgICMgUnVuIHNpbGVudGx5IGFuZCByZXR1cm4gZXhpdCBjb2RlCiAgICAgICAgICAgIGJh"\
|
||||||
|
"c2ggIiR7c2VydmljZV9wYXRofS90ZW1wbGF0ZS8ke2NvbW1hbmR9LnNoIiA+IC9kZXYvbnVsbCAy"\
|
||||||
|
"PiYxCiAgICAgICAgZmkKICAgICkKICAgIENVUlJFTlRfRVhJVF9DT0RFPSQ/Cn0KCmZ1bmN0aW9u"\
|
||||||
|
"IGNvbW1hbmRfZXhpc3RzKCkgewogICAgbG9jYWwgc2VydmljZV9wYXRoPSQxCiAgICBsb2NhbCBj"\
|
||||||
|
"b21tYW5kPSQyCiAgICBpZiBbICEgLWYgIiR7c2VydmljZV9wYXRofS90ZW1wbGF0ZS8ke2NvbW1h"\
|
||||||
|
"bmR9LnNoIiBdOyB0aGVuCiAgICAgICAgcmV0dXJuIDEKICAgIGZpCiAgICByZXR1cm4gMAp9CgoK"\
|
||||||
|
"CiMgR2V0IGFsbCBzZXJ2aWNlIG5hbWVzClNFUlZJQ0VfTkFNRVM9JChscyAiJHtTRVJWSUNFU19Q"\
|
||||||
|
"QVRIfSIpCgojIEl0ZXJhdGUgb3ZlciBhbGwgc2VydmljZSBuYW1lcwpmb3IgU0VSVklDRV9OQU1F"\
|
||||||
|
"IGluICR7U0VSVklDRV9OQU1FU307IGRvCgogICAgU0VSVklDRV9QQVRIPSQocmVhbHBhdGggIiR7"\
|
||||||
|
"U0VSVklDRVNfUEFUSH0vJHtTRVJWSUNFX05BTUV9IikKCiAgICAjLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0KICAgICMgR2V0IHRoZSBzZXJ2aWNlIGhlYWx0aAogICAgaWYgISBjb21t"\
|
||||||
|
"YW5kX2V4aXN0cyAiJHtTRVJWSUNFX1BBVEh9IiAic3RhdHVzIjsgdGhlbgogICAgICAgIFNFUlZJ"\
|
||||||
|
"Q0VfSEVBTFRIPSJ1bmtub3duIgogICAgZWxzZQogICAgICAgIHJ1bl9jb21tYW5kICIke1NFUlZJ"\
|
||||||
|
"Q0VfUEFUSH0iICJzdGF0dXMiICJmYWxzZSIKICAgICAgICBpZiBbICIke0NVUlJFTlRfRVhJVF9D"\
|
||||||
|
"T0RFfSIgLWVxIDAgXTsgdGhlbgogICAgICAgICAgICBTRVJWSUNFX0hFQUxUSD0iaGVhbHRoeSIK"\
|
||||||
|
"ICAgICAgICBlbHNlCiAgICAgICAgICAgIFNFUlZJQ0VfSEVBTFRIPSJ1bmhlYWx0aHkiCiAgICAg"\
|
||||||
|
"ICAgZmkKICAgIGZpCgogICAgIy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCiAgICAj"\
|
||||||
|
"IEdldCB0aGUgc2VydmljZSBwb3J0cwogICAgaWYgISBjb21tYW5kX2V4aXN0cyAiJHtTRVJWSUNF"\
|
||||||
|
"X1BBVEh9IiAicG9ydHMiOyB0aGVuCiAgICAgICAgU0VSVklDRV9QT1JUUz0iIgogICAgZWxzZQog"\
|
||||||
|
"ICAgICAgIHJ1bl9jb21tYW5kICIke1NFUlZJQ0VfUEFUSH0iICJwb3J0cyIgInRydWUiCiAgICAg"\
|
||||||
|
"ICAgU0VSVklDRV9QT1JUUz0iJHtDVVJSRU5UX09VVFBVVH0iCiAgICBmaQoKICAgICMtLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQogICAgIyByZXR1cm4gdGhlIGhlYWx0aCBhbmQgcG9y"\
|
||||||
|
"dHMKICAgIGVjaG8gIiR7U0VSVklDRV9OQU1FfV9IRUFMVEg9JHtTRVJWSUNFX0hFQUxUSH0iCiAg"\
|
||||||
|
"ICBlY2hvICIke1NFUlZJQ0VfTkFNRX1fUE9SVFM9JHtTRVJWSUNFX1BPUlRTfSIKZG9uZQo=";
|
||||||
|
|
||||||
|
// Decode Base64 data
|
||||||
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
|
unsigned char* decoded_data = new unsigned char[decoded_size];
|
||||||
|
size_t actual_size;
|
||||||
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
|
bool file_written = _recreate_file_(outpath, 4383289270743338040ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// File: common.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "common.sh";
|
||||||
|
static const char filedata_base64[] = "IyBDT01NT04gRlVOQ1RJT05TCiMgSkRFCiMgMjAyNS0wNS0wMwoKIyBUaGlzIGZpbGUgaXMgYXZh"\
|
||||||
|
"aWxhYmxlIFRPICoqKkFMTCoqKiB0ZW1wbGF0ZXMsIGFzICR7QUdFTlRfUEFUSH0vX2NvbW1vbi5z"\
|
||||||
|
"aAoKIyAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgoj"\
|
||||||
|
"IHN1bW1hcnkgb2YgZnVuY3Rpb25zOgojICAgX2RpZSAibWVzc2FnZSIgICAgICAgICAgICAgICAg"\
|
||||||
|
"ICAgICAgICAgOiBQcmludHMgYW4gZXJyb3IgbWVzc2FnZSBpbiByZWQgYW5kIGV4aXRzIHdpdGgg"\
|
||||||
|
"c3RhdHVzIGNvZGUgMS4KIyAgIF9ncmV5X3N0YXJ0ICAgICAgICAgICAgICAgICAgICAgICAgICAg"\
|
||||||
|
"IDogU3dpdGNoZXMgdGVybWluYWwgb3V0cHV0IGNvbG9yIHRvIGdyZXkuCiMgICBfZ3JleV9lbmQg"\
|
||||||
|
"ICAgICAgICAgICAgICAgICAgICAgICAgICAgICA6IFJlc2V0cyB0ZXJtaW5hbCBvdXRwdXQgY29s"\
|
||||||
|
"b3IgZnJvbSBncmV5LgojICAgX2NyZWF0ZV9hbmRfc3RhcnRfY29udGFpbmVyICI8cnVuX2NtZD4i"\
|
||||||
|
"IDxjb250YWluZXJfbmFtZT4gOiBDcmVhdGVzL3N0YXJ0cyBhIGNvbnRhaW5lciwgdmVyaWZ5aW5n"\
|
||||||
|
"IGl0IHJ1bnMuCiMgICBfY3JlYXRlX2ZvbGRlciA8Zm9sZGVyX3BhdGg+ICAgICAgICAgICA6IENy"\
|
||||||
|
"ZWF0ZXMgYSBkaXJlY3RvcnkgaWYgaXQgZG9lc24ndCBleGlzdCAoY2htb2QgNzc3KS4KIyAgIF9j"\
|
||||||
|
"aGVja19kb2NrZXJfaW5zdGFsbGVkICAgICAgICAgICAgICAgOiBDaGVja3MgaWYgRG9ja2VyIGlz"\
|
||||||
|
"IGluc3RhbGxlZCwgcnVubmluZywgYW5kIHVzZXIgaGFzIHBlcm1pc3Npb24uIFJldHVybnMgMSBv"\
|
||||||
|
"biBmYWlsdXJlLgojICAgX2lzX2NvbnRhaW5lcl9leGlzdHMgPGNvbnRhaW5lcl9uYW1lPiA6IENo"\
|
||||||
|
"ZWNrcyBpZiBhIGNvbnRhaW5lciAoYW55IHN0YXRlKSBleGlzdHMuIFJldHVybnMgMSBpZiBub3Qg"\
|
||||||
|
"Zm91bmQuCiMgICBfaXNfY29udGFpbmVyX3J1bm5pbmcgPGNvbnRhaW5lcl9uYW1lPjogQ2hlY2tz"\
|
||||||
|
"IGlmIGEgY29udGFpbmVyIGlzIGN1cnJlbnRseSBydW5uaW5nLiBSZXR1cm5zIDEgaWYgbm90IHJ1"\
|
||||||
|
"bm5pbmcuCiMgICBfZ2V0X2NvbnRhaW5lcl9pZCA8Y29udGFpbmVyX25hbWU+ICAgIDogUHJpbnRz"\
|
||||||
|
"IHRoZSBJRCBvZiB0aGUgbmFtZWQgY29udGFpbmVyLgojICAgX2dldF9jb250YWluZXJfc3RhdHVz"\
|
||||||
|
"IDxjb250YWluZXJfbmFtZT46IFByaW50cyB0aGUgc3RhdHVzIHN0cmluZyBvZiB0aGUgbmFtZWQg"\
|
||||||
|
"Y29udGFpbmVyLgojICAgX3N0YXJ0X2NvbnRhaW5lciA8Y29udGFpbmVyX25hbWU+ICAgICA6IFN0"\
|
||||||
|
"YXJ0cyBhbiBleGlzdGluZywgc3RvcHBlZCBjb250YWluZXIuCiMgICBfc3RvcF9jb250YWluZXIg"\
|
||||||
|
"PGNvbnRhaW5lcl9uYW1lPiAgICAgIDogU3RvcHMgYSBydW5uaW5nIGNvbnRhaW5lci4KIyAgIF9y"\
|
||||||
|
"ZW1vdmVfY29udGFpbmVyIDxjb250YWluZXJfbmFtZT4gICAgOiBTdG9wcyAoaWYgbmVlZGVkKSBh"\
|
||||||
|
"bmQgcmVtb3ZlcyBhIGNvbnRhaW5lci4KIyAgIF9nZXRfY29udGFpbmVyX2xvZ3MgPGNvbnRhaW5l"\
|
||||||
|
"cl9uYW1lPiAgOiBQcmludHMgdGhlIGxvZ3MgZm9yIGEgY29udGFpbmVyLgojICAgX2NoZWNrX3Jl"\
|
||||||
|
"cXVpcmVkX2Vudl92YXJzICJWQVIxIiAuLi4gICAgOiBDaGVja3MgaWYgbGlzdGVkIGVudmlyb25t"\
|
||||||
|
"ZW50IHZhcmlhYmxlcyBhcmUgc2V0OyBjYWxscyBfZGllKCkgaWYgYW55IGFyZSBtaXNzaW5nLgoj"\
|
||||||
|
"ICAgX3Jvb3RfcmVtb3ZlX3RyZWUgPHBhdGg+ICAgICAgICAgICAgICA6IFJlbW92ZXMgYSBwYXRo"\
|
||||||
|
"IHVzaW5nIGEgcm9vdCBEb2NrZXIgY29udGFpbmVyIChmb3IgcGVybWlzc2lvbnMpLgoKIyAtLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgojIFByaW50cyBh"\
|
||||||
|
"biBlcnJvciBtZXNzYWdlIGluIHJlZCBhbmQgZXhpdHMgd2l0aCBzdGF0dXMgY29kZSAxLgpfZGll"\
|
||||||
|
"KCkgewogICAgZWNobyAtZSAiRXJyb3I6ICQxIgogICAgZXhpdCAxCn0KCiMgQ3JlYXRlcy9zdGFy"\
|
||||||
|
"dHMgYSBjb250YWluZXIsIHZlcmlmeWluZyBpdCBydW5zLgpfY3JlYXRlX2FuZF9zdGFydF9jb250"\
|
||||||
|
"YWluZXIoKSB7CiAgICBpZiBbIC16ICIkMSIgXSB8fCBbIC16ICIkMiIgXTsgdGhlbgogICAgICAg"\
|
||||||
|
"IF9kaWUgIlRlbXBsYXRlIGVycm9yOiBjcmVhdGVfYW5kX3N0YXJ0X2NvbnRhaW5lciA8cnVuX2Nt"\
|
||||||
|
"ZD4gPGNvbnRhaW5lcl9uYW1lPiIKICAgIGZpCgogICAgbG9jYWwgcnVuX2NtZD0iJDEiCiAgICBs"\
|
||||||
|
"b2NhbCBjb250YWluZXJfbmFtZT0iJDIiCgogICAgaWYgX2lzX2NvbnRhaW5lcl9leGlzdHMgJGNv"\
|
||||||
|
"bnRhaW5lcl9uYW1lOyB0aGVuCiAgICAgICAgX2lzX2NvbnRhaW5lcl9ydW5uaW5nICRjb250YWlu"\
|
||||||
|
"ZXJfbmFtZSAmJiByZXR1cm4gMAogICAgICAgIF9zdGFydF9jb250YWluZXIgJGNvbnRhaW5lcl9u"\
|
||||||
|
"YW1lCiAgICBlbHNlCiAgICAgICAgJHJ1bl9jbWQKICAgIGZpCgogICAgaWYgISBfaXNfY29udGFp"\
|
||||||
|
"bmVyX3J1bm5pbmcgJGNvbnRhaW5lcl9uYW1lOyB0aGVuCiAgICAgICAgX2RpZSAiQ29udGFpbmVy"\
|
||||||
|
"ICR7Y29udGFpbmVyX25hbWV9IGZhaWxlZCB0byBzdGFydCIKICAgIGZpCgogICAgSUQ9JChfZ2V0"\
|
||||||
|
"X2NvbnRhaW5lcl9pZCAkY29udGFpbmVyX25hbWUpCiAgICBlY2hvICJDb250YWluZXIgJHtjb250"\
|
||||||
|
"YWluZXJfbmFtZX0gaXMgcnVubmluZyB3aXRoIElEICR7SUR9Igp9CgojIENyZWF0ZXMgYSBkaXJl"\
|
||||||
|
"Y3RvcnkgaWYgaXQgZG9lc24ndCBleGlzdCAoY2htb2QgNzc3KS4KX2NyZWF0ZV9mb2xkZXIoKSB7"\
|
||||||
|
"CiAgICBsb2NhbCBmb2xkZXI9IiQxIgogICAgaWYgWyAtZCAiJGZvbGRlciIgXTsgdGhlbgogICAg"\
|
||||||
|
"ICAgIHJldHVybiAwCiAgICBmaQogICAgaWYgISBta2RpciAtcCAiJGZvbGRlciI7IHRoZW4KICAg"\
|
||||||
|
"ICAgICBfZGllICJGYWlsZWQgdG8gY3JlYXRlIGZvbGRlcjogJGZvbGRlciIKICAgIGZpCiAgICBj"\
|
||||||
|
"aG1vZCA3NzcgIiRmb2xkZXIiCiAgICBlY2hvICJGb2xkZXIgY3JlYXRlZDogJGZvbGRlciIKfQoK"\
|
||||||
|
"IyBDaGVja3MgaWYgRG9ja2VyIGlzIGluc3RhbGxlZCwgcnVubmluZywgYW5kIHVzZXIgaGFzIHBl"\
|
||||||
|
"cm1pc3Npb24uIFJldHVybnMgMSBvbiBmYWlsdXJlLgpfY2hlY2tfZG9ja2VyX2luc3RhbGxlZCgp"\
|
||||||
|
"IHsKICAgIGlmICEgY29tbWFuZCAtdiBkb2NrZXIgJj4gL2Rldi9udWxsOyB0aGVuCiAgICAgICAg"\
|
||||||
|
"ZWNobyAiRG9ja2VyIGlzIG5vdCBpbnN0YWxsZWQiCiAgICAgICAgcmV0dXJuIDEKICAgIGZpCgog"\
|
||||||
|
"ICAgIyBjaGVjayBpZiBkb2NrZXIgZGFlbW9uIGlzIHJ1bm5pbmcKICAgIGlmICEgZG9ja2VyIGlu"\
|
||||||
|
"Zm8gJj4gL2Rldi9udWxsOyB0aGVuCiAgICAgICAgZWNobyAiRG9ja2VyIGRhZW1vbiBpcyBub3Qg"\
|
||||||
|
"cnVubmluZyIKICAgICAgICByZXR1cm4gMQogICAgZmkKCiAgICAjIGNoZWNrIGlmIHVzZXIgaGFz"\
|
||||||
|
"IHBlcm1pc3Npb24gdG8gcnVuIGRvY2tlcgogICAgaWYgISBkb2NrZXIgcnVuIC0tcm0gaGVsbG8t"\
|
||||||
|
"d29ybGQgJj4gL2Rldi9udWxsOyB0aGVuCiAgICAgICAgZWNobyAiVXNlciBkb2VzIG5vdCBoYXZl"\
|
||||||
|
"IHBlcm1pc3Npb24gdG8gcnVuIGRvY2tlciIKICAgICAgICByZXR1cm4gMQogICAgZmkKCiAgICBy"\
|
||||||
|
"ZXR1cm4gMAp9CgojIENoZWNrcyBpZiBhIGNvbnRhaW5lciAoYW55IHN0YXRlKSBleGlzdHMuIFJl"\
|
||||||
|
"dHVybnMgMSBpZiBub3QgZm91bmQuCl9pc19jb250YWluZXJfZXhpc3RzKCkgewogICAgaWYgISBk"\
|
||||||
|
"b2NrZXIgcHMgLWEgLS1mb3JtYXQgInt7Lk5hbWVzfX0iIHwgZ3JlcCAtcSAiXiQxJCI7IHRoZW4K"\
|
||||||
|
"ICAgICAgICByZXR1cm4gMQogICAgZmkKICAgIHJldHVybiAwCn0KCiMgQ2hlY2tzIGlmIGEgY29u"\
|
||||||
|
"dGFpbmVyIGlzIGN1cnJlbnRseSBydW5uaW5nLiBSZXR1cm5zIDEgaWYgbm90IHJ1bm5pbmcuCl9p"\
|
||||||
|
"c19jb250YWluZXJfcnVubmluZygpIHsKICAgIGlmICEgZG9ja2VyIHBzIC0tZm9ybWF0ICJ7ey5O"\
|
||||||
|
"YW1lc319IiB8IGdyZXAgLXEgIl4kMSQiOyB0aGVuCiAgICAgICAgcmV0dXJuIDEKICAgIGZpCiAg"\
|
||||||
|
"ICByZXR1cm4gMAp9CgojIFByaW50cyB0aGUgSUQgb2YgdGhlIG5hbWVkIGNvbnRhaW5lci4KX2dl"\
|
||||||
|
"dF9jb250YWluZXJfaWQoKSB7CiAgICBkb2NrZXIgcHMgLS1mb3JtYXQgInt7LklEfX0iIC0tZmls"\
|
||||||
|
"dGVyICJuYW1lPSQxIgp9CgojIFByaW50cyB0aGUgc3RhdHVzIHN0cmluZyBvZiB0aGUgbmFtZWQg"\
|
||||||
|
"Y29udGFpbmVyLgpfZ2V0X2NvbnRhaW5lcl9zdGF0dXMoKSB7CiAgICBkb2NrZXIgcHMgLS1mb3Jt"\
|
||||||
|
"YXQgInt7LlN0YXR1c319IiAtLWZpbHRlciAibmFtZT0kMSIKfQoKIyBTdGFydHMgYW4gZXhpc3Rp"\
|
||||||
|
"bmcsIHN0b3BwZWQgY29udGFpbmVyLgpfc3RhcnRfY29udGFpbmVyKCkgewogICAgX2lzX2NvbnRh"\
|
||||||
|
"aW5lcl9leGlzdHMgJDEgfHwgcmV0dXJuIDEKICAgIF9pc19jb250YWluZXJfcnVubmluZyAkMSAm"\
|
||||||
|
"JiByZXR1cm4gMAogICAgZG9ja2VyIHN0YXJ0ICQxCn0KCiMgU3RvcHMgYSBydW5uaW5nIGNvbnRh"\
|
||||||
|
"aW5lci4KX3N0b3BfY29udGFpbmVyKCkgewogICAgX2lzX2NvbnRhaW5lcl9ydW5uaW5nICQxIHx8"\
|
||||||
|
"IHJldHVybiAwOwogICAgZG9ja2VyIHN0b3AgJDEKfSAgIAoKIyBTdG9wcyAoaWYgbmVlZGVkKSBh"\
|
||||||
|
"bmQgcmVtb3ZlcyBhIGNvbnRhaW5lci4KX3JlbW92ZV9jb250YWluZXIoKSB7CiAgICBfc3RvcF9j"\
|
||||||
|
"b250YWluZXIgJDEKICAgIF9pc19jb250YWluZXJfZXhpc3RzICQxIHx8IHJldHVybiAwOwogICAg"\
|
||||||
|
"ZG9ja2VyIHJtICQxCn0KCiMgUHJpbnRzIHRoZSBsb2dzIGZvciBhIGNvbnRhaW5lci4KX2dldF9j"\
|
||||||
|
"b250YWluZXJfbG9ncygpIHsKICAgIGlmICEgX2lzX2NvbnRhaW5lcl9leGlzdHMgJDE7IHRoZW4K"\
|
||||||
|
"ICAgICAgICBlY2hvICJDb250YWluZXIgJDEgZG9lcyBub3QgZXhpc3QiCiAgICAgICAgcmV0dXJu"\
|
||||||
|
"IDEKICAgIGZpCgogICAgZG9ja2VyIGxvZ3MgJDEKfQoKIyBDaGVja3MgaWYgbGlzdGVkIGVudmly"\
|
||||||
|
"b25tZW50IHZhcmlhYmxlcyBhcmUgc2V0OyBjYWxscyBfZGllKCkgaWYgYW55IGFyZSBtaXNzaW5n"\
|
||||||
|
"LgpfY2hlY2tfcmVxdWlyZWRfZW52X3ZhcnMoKSB7CiAgICBsb2NhbCByZXF1aXJlZF92YXJzPSgi"\
|
||||||
|
"JEAiKQogICAgZm9yIHZhciBpbiAiJHtyZXF1aXJlZF92YXJzW0BdfSI7IGRvCiAgICAgICAgaWYg"\
|
||||||
|
"WyAteiAiJHshdmFyfSIgXTsgdGhlbgogICAgICAgICAgICBfZGllICJSZXF1aXJlZCBlbnZpcm9u"\
|
||||||
|
"bWVudCB2YXJpYWJsZSAkdmFyIGlzIG5vdCBzZXQiCiAgICAgICAgZmkKICAgIGRvbmUKfQoKIyBS"\
|
||||||
|
"ZW1vdmVzIGEgcGF0aCB1c2luZyBhIHJvb3QgRG9ja2VyIGNvbnRhaW5lciAoZm9yIHBlcm1pc3Np"\
|
||||||
|
"b25zKS4KX3Jvb3RfcmVtb3ZlX3RyZWUoKSB7CiAgICBsb2NhbCB0b19yZW1vdmU9IiQxIgogICAg"\
|
||||||
|
"cGFyZW50PSQoZGlybmFtZSAiJHRvX3JlbW92ZSIpCiAgICBhYnNfcGFyZW50PSQocmVhbHBhdGgg"\
|
||||||
|
"IiRwYXJlbnQiKQogICAgY2hpbGQ9JChiYXNlbmFtZSAiJHRvX3JlbW92ZSIpCiAgICBkb2NrZXIg"\
|
||||||
|
"cnVuIC0tcm0gLXYgIiRhYnNfcGFyZW50IjovZGF0YSBhbHBpbmUgcm0gLXJmICIvZGF0YS8kY2hp"\
|
||||||
|
"bGQiCn0KCgojIExvYWQgYXV0b2NvbW1hbmRzCnNvdXJjZSAiJHtBR0VOVF9QQVRIfS9kYXRhY29t"\
|
||||||
|
"bWFuZHMuc2gi";
|
||||||
|
|
||||||
|
// Decode Base64 data
|
||||||
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
|
unsigned char* decoded_data = new unsigned char[decoded_size];
|
||||||
|
size_t actual_size;
|
||||||
|
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &actual_size);
|
||||||
|
|
||||||
|
bool file_written = _recreate_file_(outpath, 6967493376886731479ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
return any_written;
|
||||||
|
}
|
||||||
|
}
|
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);
|
||||||
|
}
|
11
source/src/autogen/version.hpp
Normal file
11
source/src/autogen/version.hpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// version.hpp (dummy for linter/IntelliSense)
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace dropshell {
|
||||||
|
extern const std::string VERSION;
|
||||||
|
extern const std::string RELEASE_DATE;
|
||||||
|
extern const std::string AUTHOR;
|
||||||
|
extern const std::string LICENSE;
|
||||||
|
}
|
133
source/src/commands/create-service.cpp
Normal file
133
source/src/commands/create-service.cpp
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
#include "command_registry.hpp"
|
||||||
|
#include "directories.hpp"
|
||||||
|
#include "shared_commands.hpp"
|
||||||
|
#include "templates.hpp"
|
||||||
|
|
||||||
|
#include "utils/assert.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int create_service_handler(const CommandContext &ctx);
|
||||||
|
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, bool silent);
|
||||||
|
void create_service_autocomplete(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> create_service_name_list = {"create-service"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct CreateServiceCommandRegister
|
||||||
|
{
|
||||||
|
CreateServiceCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({create_service_name_list,
|
||||||
|
create_service_handler,
|
||||||
|
create_service_autocomplete,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
3, // min_args (after command)
|
||||||
|
3, // max_args (after command)
|
||||||
|
"create-service SERVER SERVICE TEMPLATE",
|
||||||
|
"Create a service on a server.",
|
||||||
|
// heredoc
|
||||||
|
R"(
|
||||||
|
Create a service on a server.
|
||||||
|
create-service SERVER SERVICE TEMPLATE create the given service on the given server.
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} create_service_command_register;
|
||||||
|
|
||||||
|
int create_service_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
std::string template_name = safearg(ctx.args, 2);
|
||||||
|
|
||||||
|
return create_service(server, template_name, service, false) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void create_service_autocomplete(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
if (ctx.args.size() < 2)
|
||||||
|
shared_commands::std_autocomplete(ctx);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ctx.args.size() == 2)
|
||||||
|
{
|
||||||
|
std::set<std::string> templates = gTemplateManager().get_template_list();
|
||||||
|
for (const auto &template_name : templates)
|
||||||
|
std::cout << template_name << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (std::filesystem::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
|
||||||
|
std::filesystem::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
@ -23,7 +23,7 @@ struct EditCommandRegister {
|
|||||||
CommandRegistry::instance().register_command({
|
CommandRegistry::instance().register_command({
|
||||||
edit_name_list,
|
edit_name_list,
|
||||||
edit_handler,
|
edit_handler,
|
||||||
std_autocomplete,
|
shared_commands::std_autocomplete,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
false, // requires_config
|
false, // requires_config
|
||||||
false, // requires_install
|
false, // requires_install
|
||||||
@ -102,8 +102,6 @@ int edit_config()
|
|||||||
|
|
||||||
gConfig().save_config(true);
|
gConfig().save_config(true);
|
||||||
|
|
||||||
// make sure we have executables.
|
|
||||||
|
|
||||||
std::cout << "Successfully edited config file at " << config_file << std::endl;
|
std::cout << "Successfully edited config file at " << config_file << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -113,25 +111,22 @@ int edit_config()
|
|||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
int edit_server(const std::string &server_name)
|
int edit_server(const std::string &server_name)
|
||||||
{
|
{
|
||||||
std::string serverpath = localpath::server(server_name);
|
if (localpath::server(server_name).empty()) {
|
||||||
if (serverpath.empty()) {
|
|
||||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream aftertext;
|
std::string config_file = localfile::server_json(server_name);
|
||||||
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)) {
|
if (!edit_file(config_file, true)) {
|
||||||
std::cerr << "Error: Failed to edit server.env" << std::endl;
|
std::cerr << "Error: Failed to edit server config" << std::endl;
|
||||||
std::cerr << "You can manually edit this file at: " << config_file << std::endl;
|
std::cerr << "You can manually edit this file at: " << config_file << std::endl;
|
||||||
std::cerr << "After editing, " << aftertext.str() << std::endl;
|
return 1;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
std::cout << aftertext.str() << std::endl;
|
std::cout << "If you have changed DROPSHELL_DIR, you should manually move the files to the new location NOW." << std::endl
|
||||||
|
<< "You can ssh in to the remote server with: dropshell ssh "<<server_name<< std::endl
|
||||||
|
<< "Once moved, reinstall all services with: dropshell install " << server_name << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -22,7 +22,7 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
CommandRegistry::instance().register_command({health_name_list,
|
CommandRegistry::instance().register_command({health_name_list,
|
||||||
health_handler,
|
health_handler,
|
||||||
std_autocomplete_allowallservices,
|
shared_commands::std_autocomplete_allowall,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
true, // requires_config
|
true, // requires_config
|
||||||
true, // requires_install
|
true, // requires_install
|
||||||
@ -36,72 +36,7 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
} health_command_register;
|
} 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
|
// health command implementation
|
||||||
@ -121,7 +56,7 @@ namespace dropshell
|
|||||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||||
transwarp::parallel exec{services.size()};
|
transwarp::parallel exec{services.size()};
|
||||||
auto task = transwarp::for_each(exec, services.begin(), services.end(), [&](const LocalServiceInfo& service) {
|
auto task = transwarp::for_each(exec, services.begin(), services.end(), [&](const LocalServiceInfo& service) {
|
||||||
std::string status = healthtick(server, service.service_name);
|
std::string status = shared_commands::healthtick(server, service.service_name);
|
||||||
std::cout << status << " " << service.service_name << " (" << service.template_name << ")" << std::endl << std::flush;
|
std::cout << status << " " << service.service_name << " (" << service.template_name << ")" << std::endl << std::flush;
|
||||||
});
|
});
|
||||||
task->wait();
|
task->wait();
|
||||||
@ -130,7 +65,7 @@ namespace dropshell
|
|||||||
// get service status
|
// get service status
|
||||||
std::string service = safearg(ctx.args, 1);
|
std::string service = safearg(ctx.args, 1);
|
||||||
LocalServiceInfo service_info = get_service_info(server, service);
|
LocalServiceInfo service_info = get_service_info(server, service);
|
||||||
std::cout << healthtick(server, service) << " " << service << " (" << service_info.template_name << ")" << std::endl << std::flush;
|
std::cout << shared_commands::healthtick(server, service) << " " << service << " (" << service_info.template_name << ")" << std::endl << std::flush;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -57,10 +57,13 @@ void help_autocomplete(const CommandContext& ctx) {
|
|||||||
void show_command(const std::string& cmd) {
|
void show_command(const std::string& cmd) {
|
||||||
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
|
||||||
if (!cmd_info)
|
if (!cmd_info)
|
||||||
|
{
|
||||||
std::cout << "Unknown command: " << cmd << std::endl;
|
std::cout << "Unknown command: " << cmd << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << " ";
|
std::cout << " ";
|
||||||
print_left_aligned(cmd_info->help_usage, 30);
|
print_left_aligned(cmd_info->help_usage, 32);
|
||||||
std::cout << cmd_info->help_description << std::endl;
|
std::cout << cmd_info->help_description << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +122,16 @@ int help_handler(const CommandContext& ctx) {
|
|||||||
{
|
{
|
||||||
// show more!
|
// show more!
|
||||||
show_command("list");
|
show_command("list");
|
||||||
|
std::cout << std::endl;
|
||||||
|
show_command("install");
|
||||||
|
show_command("uninstall");
|
||||||
|
show_command("nuke");
|
||||||
|
std::cout << std::endl;
|
||||||
|
show_command("start");
|
||||||
|
show_command("stop");
|
||||||
|
std::cout << std::endl;
|
||||||
|
show_command("ssh");
|
||||||
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
@ -5,6 +5,8 @@
|
|||||||
#include "templates.hpp"
|
#include "templates.hpp"
|
||||||
#include "shared_commands.hpp"
|
#include "shared_commands.hpp"
|
||||||
#include "utils/hash.hpp"
|
#include "utils/hash.hpp"
|
||||||
|
#include "autogen/_agent.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -27,14 +29,14 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
CommandRegistry::instance().register_command({install_name_list,
|
CommandRegistry::instance().register_command({install_name_list,
|
||||||
install_handler,
|
install_handler,
|
||||||
std_autocomplete_allowallservices,
|
shared_commands::std_autocomplete_allowall,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
false, // requires_config
|
false, // requires_config
|
||||||
false, // requires_install
|
false, // requires_install
|
||||||
0, // min_args (after command)
|
0, // min_args (after command)
|
||||||
2, // max_args (after command)
|
2, // max_args (after command)
|
||||||
"install SERVER [SERVICE]",
|
"install [SERVER] [SERVICE|all]",
|
||||||
"Install/reinstall service(s). Safe/non-destructive way to update.",
|
"Install/reinstall host, remote servers, or service(s). Safe/non-destructive way to update.",
|
||||||
// heredoc
|
// heredoc
|
||||||
R"(
|
R"(
|
||||||
Install components on a server. This is safe to re-run (non-destructive) and used to update
|
Install components on a server. This is safe to re-run (non-destructive) and used to update
|
||||||
@ -42,7 +44,7 @@ namespace dropshell
|
|||||||
|
|
||||||
install (re)install dropshell components on this computer.
|
install (re)install dropshell components on this computer.
|
||||||
install SERVER (re)install dropshell agent on the given server.
|
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.
|
install SERVER [SERVICE|all] (re)install the given service (or all services) on the given server.
|
||||||
|
|
||||||
Note you need to create the service first with:
|
Note you need to create the service first with:
|
||||||
dropshell create-service <server> <template> <service>
|
dropshell create-service <server> <template> <service>
|
||||||
@ -50,23 +52,6 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
} install_command_register;
|
} 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::None));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// install service over ssh : SHARED COMMAND
|
// install service over ssh : SHARED COMMAND
|
||||||
@ -91,6 +76,12 @@ namespace dropshell
|
|||||||
if (!tinfo.is_set())
|
if (!tinfo.is_set())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (!tinfo.template_valid())
|
||||||
|
{
|
||||||
|
std::cerr << "Template is not valid: " << service_info.template_name << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Create service directory
|
// Create service directory
|
||||||
std::string remote_service_path = remotepath::service(server, service);
|
std::string remote_service_path = remotepath::service(server, service);
|
||||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path);
|
std::string mkdir_cmd = "mkdir -p " + quote(remote_service_path);
|
||||||
@ -111,7 +102,7 @@ namespace dropshell
|
|||||||
// Copy template files
|
// Copy template files
|
||||||
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
std::cout << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
||||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_template(server, service) << "/" << 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),
|
if (!shared_commands::rsync_tree_to_remote(tinfo.local_template_path().string(), remotepath::service_template(server, service),
|
||||||
server_env, silent))
|
server_env, silent))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
||||||
@ -121,7 +112,7 @@ namespace dropshell
|
|||||||
// Copy service files
|
// Copy service files
|
||||||
std::cout << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
std::cout << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
||||||
<< std::string(8, ' ') << "[REMOTE] " << remotepath::service_config(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),
|
if (!shared_commands::rsync_tree_to_remote(localpath::service(server, service), remotepath::service_config(server, service),
|
||||||
server_env, silent))
|
server_env, silent))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
||||||
@ -130,47 +121,36 @@ namespace dropshell
|
|||||||
|
|
||||||
// Run install script
|
// Run install script
|
||||||
{
|
{
|
||||||
|
std::cout << "Running " << service_info.template_name << " install script on " << server << "..." << std::endl;
|
||||||
server_env.run_remote_template_command(service, "install", {}, silent, {});
|
server_env.run_remote_template_command(service, "install", {}, silent, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
// print health tick
|
// print health tick
|
||||||
std::cout << "Health: " << healthtick(server, service) << std::endl;
|
std::cout << "Health: " << shared_commands::healthtick(server, service) << std::endl;
|
||||||
return true;
|
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
|
// update_dropshell
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
std::string _exec(const char *cmd)
|
||||||
std::string _exec(const char* cmd) {
|
{
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
std::string result = "";
|
std::string result = "";
|
||||||
FILE *pipe = popen(cmd, "r");
|
FILE *pipe = popen(cmd, "r");
|
||||||
if (!pipe) {
|
if (!pipe)
|
||||||
|
{
|
||||||
throw std::runtime_error("popen() failed!");
|
throw std::runtime_error("popen() failed!");
|
||||||
}
|
}
|
||||||
while (!feof(pipe)) {
|
while (!feof(pipe))
|
||||||
|
{
|
||||||
if (fgets(buffer, 128, pipe) != nullptr)
|
if (fgets(buffer, 128, pipe) != nullptr)
|
||||||
result += buffer;
|
result += buffer;
|
||||||
}
|
}
|
||||||
pclose(pipe);
|
pclose(pipe);
|
||||||
return result;
|
return trim(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_dropshell()
|
int update_dropshell()
|
||||||
@ -180,7 +160,7 @@ namespace dropshell
|
|||||||
std::filesystem::path parent_path = dropshell_path.parent_path();
|
std::filesystem::path parent_path = dropshell_path.parent_path();
|
||||||
|
|
||||||
// determine the architecture of the system
|
// determine the architecture of the system
|
||||||
std::string arch = get_arch();
|
std::string arch = shared_commands::get_arch();
|
||||||
|
|
||||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||||
|
|
||||||
@ -225,7 +205,6 @@ namespace dropshell
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
||||||
std::string bash_script_2 = "docker run --rm -v " + parent_path.string() + ":/target gitea.jde.nz/public/debian-curl:latest " +
|
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\"";
|
"sh -c \"mv /target/dropshell_temp /target/dropshell\"";
|
||||||
rval = system(bash_script_2.c_str());
|
rval = system(bash_script_2.c_str());
|
||||||
@ -243,20 +222,115 @@ namespace dropshell
|
|||||||
return -1;
|
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 for the host architecture.
|
||||||
|
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 local bb64 to " << localpath::agent() << std::endl;
|
||||||
|
else
|
||||||
|
std::cerr << "Failed to download local bb64 to " << localpath::agent() << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Updating local bb64..." << std::endl;
|
||||||
|
system((localpath::agent() + "bb64 -u").c_str()); // update.
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Creating local files to copy to remote agents..." << std::endl;
|
||||||
|
recreate_agent::recreate_tree(localpath::files_for_remote_agent());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int install_host()
|
int install_host()
|
||||||
{
|
{
|
||||||
// update dropshell.
|
// update dropshell.
|
||||||
// install the local dropshell agent.
|
// install the local dropshell agent.
|
||||||
|
|
||||||
return update_dropshell();
|
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)
|
int install_server(const std::string &server)
|
||||||
{
|
{
|
||||||
// install the dropshell agent on the given 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// now create the agent.
|
||||||
|
// copy across from the local agent files.
|
||||||
|
std::cout << "Copying local agent files to remote server... " << std::flush;
|
||||||
|
shared_commands::rsync_tree_to_remote(localpath::files_for_remote_agent(), agent_path, server_env, false);
|
||||||
|
std::cout << "done." << std::endl;
|
||||||
|
|
||||||
|
// add in bb64. We can't use execute_remote_command() here, as that relies on bb64 which we're installing!
|
||||||
|
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;
|
||||||
|
|
||||||
|
// just test all is ok
|
||||||
|
|
||||||
|
// run the self-test.
|
||||||
|
std::string output;
|
||||||
|
bool okay = execute_ssh_command(server_env.get_SSH_INFO(), sCommand(agent_path, "./selftest.sh", {}), cMode::Defaults, &output);
|
||||||
|
if (!okay)
|
||||||
|
{
|
||||||
|
std::cerr << "ERROR: Failed to install remote agent on " << server << std::endl;
|
||||||
|
std::cerr << "ERROR: Output: " << output << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << output << std::endl;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
@ -269,6 +343,12 @@ namespace dropshell
|
|||||||
return install_host();
|
return install_host();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!gConfig().is_config_set())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Dropshell is not configured. Please run 'dropshell edit' to configure it." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
std::string server = safearg(ctx.args, 0);
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
|
||||||
if (ctx.args.size() == 1)
|
if (ctx.args.size() == 1)
|
||||||
@ -277,9 +357,10 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
|
|
||||||
// install service(s)
|
// install service(s)
|
||||||
if (safearg(ctx.args, 1) == "*")
|
if (safearg(ctx.args, 1) == "all")
|
||||||
{
|
{
|
||||||
// install all services on the server
|
// install all services on the server
|
||||||
|
maketitle("Installing all services on " + server);
|
||||||
bool okay = true;
|
bool okay = true;
|
||||||
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||||
for (const auto &service : services)
|
for (const auto &service : services)
|
@ -6,6 +6,8 @@
|
|||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
#include "tableprint.hpp"
|
#include "tableprint.hpp"
|
||||||
#include "transwarp.hpp"
|
#include "transwarp.hpp"
|
||||||
|
#include "server_env_manager.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -28,7 +30,7 @@ struct ListCommandRegister {
|
|||||||
CommandRegistry::instance().register_command({
|
CommandRegistry::instance().register_command({
|
||||||
list_name_list,
|
list_name_list,
|
||||||
list_handler,
|
list_handler,
|
||||||
std_autocomplete,
|
shared_commands::std_autocomplete,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
true, // requires_config
|
true, // requires_config
|
||||||
true, // requires_install
|
true, // requires_install
|
||||||
@ -86,13 +88,13 @@ void list_servers() {
|
|||||||
|
|
||||||
transwarp::parallel exec{servers.size()};
|
transwarp::parallel exec{servers.size()};
|
||||||
auto task = transwarp::for_each(exec, servers.begin(), servers.end(), [&](const ServerInfo& server) {
|
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::map<std::string, shared_commands::ServiceStatus> status = shared_commands::get_all_services_status(server.name);
|
||||||
|
|
||||||
std::set<int> ports_used;
|
std::set<int> ports_used;
|
||||||
std::string serviceticks = "";
|
std::string serviceticks = "";
|
||||||
for (const auto& [service_name, service_status] : status) {
|
for (const auto& [service_name, service_status] : status) {
|
||||||
ports_used.insert(service_status.ports.begin(), service_status.ports.end());
|
ports_used.insert(service_status.ports.begin(), service_status.ports.end());
|
||||||
serviceticks += HealthStatus2String(service_status.health) + " ";
|
serviceticks += shared_commands::HealthStatus2String(service_status.health) + " ";
|
||||||
}
|
}
|
||||||
std::string ports_used_str = "";
|
std::string ports_used_str = "";
|
||||||
for (const auto& port : ports_used)
|
for (const auto& port : ports_used)
|
||||||
@ -160,21 +162,21 @@ void show_server_details(const std::string& server_name) {
|
|||||||
// list services, and run healthcheck on each
|
// list services, and run healthcheck on each
|
||||||
{
|
{
|
||||||
tableprint tp("Services: " + server_name, false);
|
tableprint tp("Services: " + server_name, false);
|
||||||
tp.add_row({"Status", "Service", "Ports"});
|
tp.add_row({"Status", "Service", "Template","Ports"});
|
||||||
|
|
||||||
|
|
||||||
std::map<std::string, ServiceStatus> status = service_runner::get_all_services_status(server_name);
|
std::map<std::string, shared_commands::ServiceStatus> status = shared_commands::get_all_services_status(server_name);
|
||||||
|
|
||||||
std::set<int> ports_used;
|
std::set<int> ports_used;
|
||||||
std::string serviceticks = "";
|
std::string serviceticks = "";
|
||||||
for (const auto& [service_name, service_status] : status) {
|
for (const auto& [service_name, service_status] : status) {
|
||||||
std::string healthy = HealthStatus2String(service_status.health);
|
std::string healthy = shared_commands::HealthStatus2String(service_status.health);
|
||||||
|
|
||||||
std::string ports_str = "";
|
std::string ports_str = "";
|
||||||
for (const auto& port : service_status.ports)
|
for (const auto& port : service_status.ports)
|
||||||
ports_str += std::to_string(port) + " ";
|
ports_str += std::to_string(port) + " ";
|
||||||
|
|
||||||
tp.add_row({healthy, service_name, ports_str});
|
tp.add_row({healthy, service_name, get_service_info(server_name,service_name).template_name, ports_str});
|
||||||
} // end of for (const auto& service : services)
|
} // end of for (const auto& service : services)
|
||||||
tp.print();
|
tp.print();
|
||||||
} // end of list services
|
} // end of list services
|
149
source/src/commands/nuke.cpp
Normal file
149
source/src/commands/nuke.cpp
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
#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/utils.hpp"
|
||||||
|
|
||||||
|
#include "utils/assert.hpp"
|
||||||
|
|
||||||
|
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,
|
||||||
|
shared_commands::std_autocomplete,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
2, // min_args (after command)
|
||||||
|
2, // max_args (after command)
|
||||||
|
"nuke SERVER SERVICE|all",
|
||||||
|
"Nuke a service on a server. Destroys everything, both local and remote!",
|
||||||
|
// heredoc
|
||||||
|
R"(
|
||||||
|
Nuke a service.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
nuke SERVER SERVICE nuke the given service on the given server.
|
||||||
|
nuke SERVER all nuke all services on the given server.
|
||||||
|
|
||||||
|
Note: This command is destructive and will destroy all data and all configuration,
|
||||||
|
both on the dropshell host and on the remote server.
|
||||||
|
|
||||||
|
Use with caution!
|
||||||
|
)"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} nuke_command_register;
|
||||||
|
|
||||||
|
int nuke_one(std::string server, std::string service)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
|
||||||
|
// step 1 - nuke on remote server.
|
||||||
|
if (server_env.is_valid())
|
||||||
|
{
|
||||||
|
LocalServiceInfo service_info;
|
||||||
|
|
||||||
|
service_info = get_service_info(server, service);
|
||||||
|
if (!SIvalid(service_info))
|
||||||
|
std::cerr << "Warning: Invalid service: " << service << std::endl;
|
||||||
|
|
||||||
|
if (server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||||
|
{
|
||||||
|
// run the nuke script on the remote server if it exists.
|
||||||
|
// otherwise just uninstall.
|
||||||
|
if (gTemplateManager().template_command_exists(service_info.template_name, "nuke"))
|
||||||
|
{
|
||||||
|
std::cout << "Running nuke script for " << service << " on " << server << std::endl;
|
||||||
|
if (!server_env.run_remote_template_command(service, "nuke", {}, false, {}))
|
||||||
|
std::cerr << "Warning: Failed to run nuke script: " << service << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "No nuke script found for " << service << " on " << server << std::endl;
|
||||||
|
std::cout << "Running uninstall script instead and will clean directories." << std::endl;
|
||||||
|
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||||
|
std::cerr << "Warning: Failed to uninstall service: " << service << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the service directory from the server, running in a docker container as root.
|
||||||
|
if (server_env.remove_remote_dir(remotepath::service(server, service), true))
|
||||||
|
{
|
||||||
|
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||||
|
std::cout << "Remote service directory removed: " << remotepath::service(server, service) << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cerr << "Warning: Failed to remove remote service directory" << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cerr << "Warning: Service not found on remote server: " << remotepath::service(server, service) << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cerr << "Warning: Can't nuke the remote service as the server is invalid: " << server << std::endl;
|
||||||
|
|
||||||
|
// step 2 - nuke the local service directory.
|
||||||
|
std::string local_service_path = localpath::service(server, service);
|
||||||
|
if (local_service_path.empty() || !std::filesystem::exists(local_service_path))
|
||||||
|
{
|
||||||
|
std::cerr << "Warning: Local service directory not found: " << local_service_path << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto itemsdeleted = std::filesystem::remove_all(local_service_path);
|
||||||
|
if (itemsdeleted == 0)
|
||||||
|
std::cerr << "Error: Failed to remove local service directory" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Nuked service " << service << " on server " << server << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nuke_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
ASSERT(ctx.args.size() == 2, "Usage: nuke SERVER SERVICE|all (requires 2 args - you supplied " + std::to_string(ctx.args.size()) + ")");
|
||||||
|
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);
|
||||||
|
|
||||||
|
if (service == "all")
|
||||||
|
{
|
||||||
|
int rval = 0;
|
||||||
|
|
||||||
|
// iterate through all service folders in the server directory.
|
||||||
|
std::string server_path = localpath::server(server);
|
||||||
|
if (server_path.empty())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server not found: " << server << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(server_path))
|
||||||
|
{
|
||||||
|
if (entry.is_directory() && entry.path().filename().string().find(".") != 0)
|
||||||
|
{
|
||||||
|
std::string service_name = entry.path().filename().string();
|
||||||
|
rval |= nuke_one(server, service_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rval;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return nuke_one(server, service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
247
source/src/commands/shared_commands.cpp
Normal file
247
source/src/commands/shared_commands.cpp
Normal file
@ -0,0 +1,247 @@
|
|||||||
|
#include "shared_commands.hpp"
|
||||||
|
#include "utils/assert.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
#include "server_env_manager.hpp"
|
||||||
|
#include "directories.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
#include "servers.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// std_autocomplete : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// std_autocomplete_allowall : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
void std_autocomplete_allowall(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
std_autocomplete(ctx);
|
||||||
|
if (ctx.args.size() == 1)
|
||||||
|
std::cout << "all" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// cRemoteTempFolder : SHARED CLASS
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// get_all_services_status : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name)
|
||||||
|
{
|
||||||
|
std::map<std::string, ServiceStatus> 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 (!execute_ssh_command(env.get_SSH_INFO(), sCommand(remotepath::agent(server_name), "./_allservicesstatus.sh", {{"HOST_NAME", server_name}, {"SERVER", server_name}, {"AGENT_PATH", remotepath::agent(server_name)}}), cMode::CaptureOutput, &output))
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// healthtick : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// HealthStatus2String : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
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:";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// is_healthy : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// healthmark : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
std::string healthmark(const std::string &server, const std::string &service)
|
||||||
|
{
|
||||||
|
HealthStatus status = is_healthy(server, service);
|
||||||
|
return HealthStatus2String(status);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
|
} // namespace dropshell
|
63
source/src/commands/shared_commands.hpp
Normal file
63
source/src/commands/shared_commands.hpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#ifndef SHARED_COMMANDS_HPP
|
||||||
|
#define SHARED_COMMANDS_HPP
|
||||||
|
|
||||||
|
#include "servers.hpp"
|
||||||
|
#include "command_registry.hpp"
|
||||||
|
#include "server_env_manager.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
|
typedef enum HealthStatus
|
||||||
|
{
|
||||||
|
HEALTHY,
|
||||||
|
UNHEALTHY,
|
||||||
|
NOTINSTALLED,
|
||||||
|
ERROR,
|
||||||
|
UNKNOWN
|
||||||
|
} HealthStatus;
|
||||||
|
|
||||||
|
typedef struct ServiceStatus
|
||||||
|
{
|
||||||
|
HealthStatus health;
|
||||||
|
std::vector<int> ports;
|
||||||
|
} ServiceStatus;
|
||||||
|
|
||||||
|
// expose routines used by multiple commands.
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool rsync_tree_to_remote(
|
||||||
|
const std::string &local_path,
|
||||||
|
const std::string &remote_path,
|
||||||
|
server_env_manager &server_env,
|
||||||
|
bool silent);
|
||||||
|
|
||||||
|
std::string get_arch();
|
||||||
|
|
||||||
|
std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
|
||||||
|
|
||||||
|
std::string healthtick(const std::string &server, const std::string &service);
|
||||||
|
std::string HealthStatus2String(HealthStatus status);
|
||||||
|
HealthStatus is_healthy(const std::string &server, const std::string &service);
|
||||||
|
std::string healthmark(const std::string &server, const std::string &service);
|
||||||
|
|
||||||
|
void std_autocomplete(const CommandContext &ctx);
|
||||||
|
void std_autocomplete_allowall(const CommandContext &ctx);
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace shared_commands
|
||||||
|
} // namespace dropshell
|
||||||
|
#endif
|
109
source/src/commands/ssh.cpp
Normal file
109
source/src/commands/ssh.cpp
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
#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 "templates.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int ssh_handler(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> ssh_name_list = {"ssh"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct SSHCommandRegister
|
||||||
|
{
|
||||||
|
SSHCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({ssh_name_list,
|
||||||
|
ssh_handler,
|
||||||
|
shared_commands::std_autocomplete,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
1, // min_args (after command)
|
||||||
|
2, // max_args (after command)
|
||||||
|
"ssh SERVER",
|
||||||
|
"SSH into a server, or into a docker container for a service.",
|
||||||
|
R"(
|
||||||
|
|
||||||
|
ssh SERVER SERVICE SSH into a docker container for a service.
|
||||||
|
ssh SERVER SSH into a server.
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} ssh_command_register;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool ssh_into_server(const std::string &server)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server " << server << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
execute_ssh_command(server_env.get_SSH_INFO(), sCommand(remotepath::DROPSHELL_DIR(server), "ls --color && bash", {}), cMode::Interactive);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ssh_into_service(const std::string &server, const std::string &service)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server " << server << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalServiceInfo sinfo = get_service_info(server, service);
|
||||||
|
if (!SIvalid(sinfo))
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Service " << service << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gTemplateManager().has_template(sinfo.template_name))
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Template " << sinfo.template_name << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!gTemplateManager().template_command_exists(sinfo.template_name, "ssh"))
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Template " << sinfo.template_name << " does not have an ssh command" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
server_env.run_remote_template_command(service,"ssh",{},false,{}); // explicitly supports interactive ssh!
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ssh_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() < 2)
|
||||||
|
{
|
||||||
|
// ssh into the server
|
||||||
|
return ssh_into_server(server) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
|
||||||
|
// ssh into the specific service.
|
||||||
|
return ssh_into_service(server, service) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
91
source/src/commands/start.cpp
Normal file
91
source/src/commands/start.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#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"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int start_handler(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> start_name_list = {"start", "start-service"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct StartCommandRegister
|
||||||
|
{
|
||||||
|
StartCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({start_name_list,
|
||||||
|
start_handler,
|
||||||
|
shared_commands::std_autocomplete_allowall,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
1, // min_args (after command)
|
||||||
|
2, // max_args (after command)
|
||||||
|
"start SERVER SERVICE|all",
|
||||||
|
"Start a service or all services on a server.",
|
||||||
|
R"(
|
||||||
|
|
||||||
|
start SERVER SERVICE Starts the given service on the given server.
|
||||||
|
start SERVER all Starts all services on the given server.
|
||||||
|
|
||||||
|
Note: This command will not create any data or configuration.
|
||||||
|
It will simply start the service on the remote server.
|
||||||
|
Stop the service with stop, or uninstall with uninstall.
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} start_command_register;
|
||||||
|
|
||||||
|
bool start_service(const std::string &server, const std::string &service)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server " << server << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// run the start script.
|
||||||
|
bool started = server_env.run_remote_template_command(service, "start", {}, false, {});
|
||||||
|
|
||||||
|
if (started)
|
||||||
|
{
|
||||||
|
std::cout << "Service " << service << " on server " << server << " started." << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::cerr << "Error: Failed to start service " << service << " on server " << server << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int start_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
if (ctx.args.size() < 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server name and service name are both required" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
|
||||||
|
if (service == "all")
|
||||||
|
{
|
||||||
|
// install all services on the server
|
||||||
|
maketitle("Stopping all services on " + server);
|
||||||
|
bool okay = true;
|
||||||
|
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||||
|
for (const auto &service : services)
|
||||||
|
okay &= start_service(server, service.service_name);
|
||||||
|
return okay ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the specific service.
|
||||||
|
return start_service(server, service) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
91
source/src/commands/stop.cpp
Normal file
91
source/src/commands/stop.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#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"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int stop_handler(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> stop_name_list = {"stop", "stop-service"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct StopCommandRegister
|
||||||
|
{
|
||||||
|
StopCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({stop_name_list,
|
||||||
|
stop_handler,
|
||||||
|
shared_commands::std_autocomplete_allowall,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
1, // min_args (after command)
|
||||||
|
2, // max_args (after command)
|
||||||
|
"stop SERVER SERVICE|all",
|
||||||
|
"Stop a service or all services on a server.",
|
||||||
|
R"(
|
||||||
|
|
||||||
|
stop SERVER SERVICE Stops the given service on the given server.
|
||||||
|
stop SERVER all Stops all services on the given server.
|
||||||
|
|
||||||
|
Note: This command will not destroy any data or configuration.
|
||||||
|
It will simply stop the service on the remote server.
|
||||||
|
Restart the service with start, or update and start it with install.
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} stop_command_register;
|
||||||
|
|
||||||
|
bool stop_service(const std::string &server, const std::string &service)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server " << server << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// run the stop script.
|
||||||
|
bool stopped = server_env.run_remote_template_command(service, "stop", {}, false, {});
|
||||||
|
|
||||||
|
if (stopped)
|
||||||
|
{
|
||||||
|
std::cout << "Service " << service << " on server " << server << " stopped." << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
std::cerr << "Error: Failed to stop service " << service << " on server " << server << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int stop_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
if (ctx.args.size() < 2)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Server name and service name are both required" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
|
||||||
|
if (service == "all")
|
||||||
|
{
|
||||||
|
// install all services on the server
|
||||||
|
maketitle("Stopping all services on " + server);
|
||||||
|
bool okay = true;
|
||||||
|
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||||
|
for (const auto &service : services)
|
||||||
|
okay &= stop_service(server, service.service_name);
|
||||||
|
return okay ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop the specific service.
|
||||||
|
return stop_service(server, service) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
@ -4,6 +4,8 @@
|
|||||||
#include "templates.hpp"
|
#include "templates.hpp"
|
||||||
|
|
||||||
#include "utils/assert.hpp"
|
#include "utils/assert.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
|
||||||
namespace dropshell
|
namespace dropshell
|
||||||
{
|
{
|
||||||
@ -19,51 +21,27 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
CommandRegistry::instance().register_command({uninstall_name_list,
|
CommandRegistry::instance().register_command({uninstall_name_list,
|
||||||
uninstall_handler,
|
uninstall_handler,
|
||||||
std_autocomplete_allowallservices,
|
shared_commands::std_autocomplete_allowall,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
true, // requires_config
|
true, // requires_config
|
||||||
true, // requires_install
|
true, // requires_install
|
||||||
1, // min_args (after command)
|
2, // min_args (after command)
|
||||||
2, // max_args (after command)
|
2, // max_args (after command)
|
||||||
"uninstall SERVER [SERVICE|*]",
|
"uninstall SERVER SERVICE|all",
|
||||||
"Uninstall a service on a server. Does not remove configuration or user data.",
|
"Uninstall a service on a server. Does not remove configuration or user data.",
|
||||||
// heredoc
|
// heredoc
|
||||||
R"(
|
R"(
|
||||||
Uninstall a service on a server. Does not remove configuration or user data.
|
Uninstall a service, leaving all configuration and data intact.
|
||||||
uninstall SERVER SERVICE uninstall the given service on the given server.
|
|
||||||
uninstall SERVER * uninstall all services on the given server.
|
uninstall SERVER SERVICE Uninstall the given service on the given server.
|
||||||
|
uninstall SERVER all Uninstall all services on the given server.
|
||||||
|
|
||||||
|
Update and reinstall the service with install, or delete all configuration and data with nuke.
|
||||||
)"});
|
)"});
|
||||||
}
|
}
|
||||||
} uninstall_command_register;
|
} uninstall_command_register;
|
||||||
|
|
||||||
int uninstall_handler(const CommandContext &ctx)
|
bool uninstall_service(const std::string &server, const std::string &service, bool silent = false)
|
||||||
{
|
|
||||||
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)
|
if (!silent)
|
||||||
maketitle("Uninstalling " + service + " on " + server);
|
maketitle("Uninstalling " + service + " on " + server);
|
||||||
@ -84,8 +62,6 @@ namespace dropshell
|
|||||||
|
|
||||||
// 3. Run uninstall script if it exists
|
// 3. Run uninstall script if it exists
|
||||||
std::string uninstall_script = remotepath::service_template(server, service) + "/uninstall.sh";
|
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 (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
|
||||||
if (!silent)
|
if (!silent)
|
||||||
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
|
std::cerr << "Warning: Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||||
@ -105,4 +81,31 @@ namespace dropshell
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int uninstall_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
if (ctx.args.size() < 1)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: uninstall requires a server and a service (or all)" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
|
||||||
|
if (safearg(ctx.args, 1) == "all")
|
||||||
|
{
|
||||||
|
// 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))
|
||||||
|
okay = false;
|
||||||
|
}
|
||||||
|
return okay ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
return uninstall_service(server, service) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -61,7 +61,6 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
std::string dropshell_base = homedir + "/.dropshell";
|
std::string dropshell_base = homedir + "/.dropshell";
|
||||||
mConfig["tempfiles"] = dropshell_base + "/tmp";
|
mConfig["tempfiles"] = dropshell_base + "/tmp";
|
||||||
mConfig["backups"] = dropshell_base + "/backups";
|
mConfig["backups"] = dropshell_base + "/backups";
|
||||||
mConfig["executables"] = dropshell_base + "/executables";
|
|
||||||
|
|
||||||
mConfig["template_cache"] = dropshell_base + "/template_cache";
|
mConfig["template_cache"] = dropshell_base + "/template_cache";
|
||||||
mConfig["template_registry_URLs"] = {
|
mConfig["template_registry_URLs"] = {
|
||||||
@ -85,8 +84,7 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
std::vector<std::filesystem::path> paths = {
|
std::vector<std::filesystem::path> paths = {
|
||||||
get_local_template_cache_path(),
|
get_local_template_cache_path(),
|
||||||
get_local_backup_path(),
|
get_local_backup_path(),
|
||||||
get_local_tempfiles_path(),
|
get_local_tempfiles_path()
|
||||||
get_local_executables_path()
|
|
||||||
};
|
};
|
||||||
for (auto & p : get_local_server_definition_paths())
|
for (auto & p : get_local_server_definition_paths())
|
||||||
paths.push_back(p);
|
paths.push_back(p);
|
||||||
@ -99,17 +97,6 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// also make sure the executables are in the path.
|
|
||||||
std::string executables_path = get_local_executables_path();
|
|
||||||
|
|
||||||
// download bb64.
|
|
||||||
std::string cmd = "cd " + executables_path + " && 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 " << executables_path << std::endl;
|
|
||||||
else
|
|
||||||
std::cerr << "Failed to download bb64 to " << executables_path << std::endl;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,11 +122,6 @@ std::string config::get_local_template_cache_path() {
|
|||||||
return mConfig["template_cache"];
|
return mConfig["template_cache"];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_local_executables_path()
|
|
||||||
{
|
|
||||||
return mConfig["executables"];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> config::get_template_registry_urls() {
|
std::vector<std::string> config::get_template_registry_urls() {
|
||||||
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
||||||
std::vector<std::string> urls;
|
std::vector<std::string> urls;
|
@ -20,7 +20,6 @@ class config {
|
|||||||
std::string get_local_tempfiles_path();
|
std::string get_local_tempfiles_path();
|
||||||
std::string get_local_backup_path();
|
std::string get_local_backup_path();
|
||||||
std::string get_local_template_cache_path();
|
std::string get_local_template_cache_path();
|
||||||
std::string get_local_executables_path();
|
|
||||||
std::vector<std::string> get_template_registry_urls();
|
std::vector<std::string> get_template_registry_urls();
|
||||||
std::vector<std::string> get_template_local_paths();
|
std::vector<std::string> get_template_local_paths();
|
||||||
std::vector<std::string> get_local_server_definition_paths();
|
std::vector<std::string> get_local_server_definition_paths();
|
263
source/src/main.cpp
Normal file
263
source/src/main.cpp
Normal file
@ -0,0 +1,263 @@
|
|||||||
|
#include "version.hpp"
|
||||||
|
#include "config.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 "output.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 << "(" << ctx.args.size() << " args provided, " << ctx.command << " requires " << (info->min_args) << " to " << (info->max_args) << " args)" << std::endl;
|
||||||
|
std::cerr << "Usage: " << std::endl;
|
||||||
|
std::cerr << " ";
|
||||||
|
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);
|
||||||
|
}
|
@ -183,11 +183,11 @@ bool server_env_manager::remove_remote_dir(const std::string &dir_path, bool sil
|
|||||||
"docker run --rm -v " + quote(parent_path.string()) + ":/parent " +
|
"docker run --rm -v " + quote(parent_path.string()) + ":/parent " +
|
||||||
" alpine rm -rf \"/parent/" + target_dir + "\"";
|
" alpine rm -rf \"/parent/" + target_dir + "\"";
|
||||||
|
|
||||||
if (!silent)
|
// if (!silent)
|
||||||
std::cout << "Running command: " << remote_cmd << std::endl;
|
// std::cout << "Running command: " << remote_cmd << std::endl;
|
||||||
|
|
||||||
sCommand scommand("", remote_cmd,{});
|
sCommand scommand("", remote_cmd,{});
|
||||||
cMode mode = (silent ? cMode::Silent : cMode::None);
|
cMode mode = (silent ? cMode::Silent : cMode::Defaults);
|
||||||
|
|
||||||
return execute_ssh_command(get_SSH_INFO(), scommand, mode);
|
return execute_ssh_command(get_SSH_INFO(), scommand, mode);
|
||||||
}
|
}
|
||||||
@ -204,7 +204,7 @@ bool server_env_manager::run_remote_template_command(const std::string &service_
|
|||||||
|
|
||||||
if (scommand->get_command_to_run().empty())
|
if (scommand->get_command_to_run().empty())
|
||||||
return false;
|
return false;
|
||||||
cMode mode = (command=="ssh") ? (cMode::Interactive) : cMode::Silent;
|
cMode mode = (command=="ssh") ? (cMode::Interactive) : (silent ? cMode::Silent : cMode::Defaults);
|
||||||
return execute_ssh_command(get_SSH_INFO(), scommand.value(), mode);
|
return execute_ssh_command(get_SSH_INFO(), scommand.value(), mode);
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
|||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
#include "server_env_manager.hpp"
|
#include "server_env_manager.hpp"
|
||||||
#include "service_runner.hpp"
|
|
||||||
#include "utils/tableprint.hpp"
|
#include "utils/tableprint.hpp"
|
||||||
#include "utils/envmanager.hpp"
|
#include "utils/envmanager.hpp"
|
||||||
#include "utils/directories.hpp"
|
#include "utils/directories.hpp"
|
||||||
@ -103,14 +102,11 @@ bool create_server(const std::string &server_name)
|
|||||||
server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl;
|
server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl;
|
||||||
server_env_file.close();
|
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 << "Server created successfully: " << server_name << std::endl;
|
||||||
std::cout << "Please complete the installation:" <<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 << "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 << "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 << "3) install the server: dropshell install " << server_name << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
@ -2,10 +2,9 @@
|
|||||||
#define SERVERS_HPP
|
#define SERVERS_HPP
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <set>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "service_runner.hpp" // for ServiceStatus
|
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
// Server information structure
|
// Server information structure
|
@ -10,7 +10,6 @@
|
|||||||
#include "utils/assert.hpp"
|
#include "utils/assert.hpp"
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "service_runner.hpp"
|
|
||||||
#include "server_env_manager.hpp"
|
#include "server_env_manager.hpp"
|
||||||
#include "templates.hpp"
|
#include "templates.hpp"
|
||||||
#include "services.hpp"
|
#include "services.hpp"
|
||||||
@ -19,6 +18,75 @@
|
|||||||
#include "command_registry.hpp"
|
#include "command_registry.hpp"
|
||||||
#include "shared_commands.hpp"
|
#include "shared_commands.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
namespace dropshell {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace dropshell
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
@ -53,12 +121,6 @@ bool service_runner::nuke(bool silent)
|
|||||||
if (!mServerEnv.is_valid()) return false; // should never hit this.
|
if (!mServerEnv.is_valid()) return false; // should never hit this.
|
||||||
|
|
||||||
std::string remote_service_path = remotepath::service(mServer, mService);
|
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;
|
std::cout << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
||||||
|
|
||||||
@ -123,8 +185,8 @@ bool service_runner::run_command(const std::string& command, std::vector<std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
// install doesn't require anything on the server yet.
|
// install doesn't require anything on the server yet.
|
||||||
if (command == "install")
|
// if (command == "install")
|
||||||
return install_service(mServer, mService, false);
|
// return install_service(mServer, mService, false);
|
||||||
|
|
||||||
std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh";
|
std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh";
|
||||||
|
|
||||||
@ -171,63 +233,6 @@ bool service_runner::run_command(const std::string& command, std::vector<std::st
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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) {
|
bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) {
|
||||||
std::string serverpath = localpath::server(server_name);
|
std::string serverpath = localpath::server(server_name);
|
||||||
@ -262,13 +267,13 @@ bool service_runner::interactive_ssh_service()
|
|||||||
bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent)
|
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" : "");
|
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::None));
|
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)
|
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" : "");
|
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::None));
|
return execute_local_command(scp_cmd, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool service_runner::restore(std::string backup_file, bool silent)
|
bool service_runner::restore(std::string backup_file, bool silent)
|
||||||
@ -353,16 +358,16 @@ bool service_runner::restore(std::string backup_file, bool silent)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
shared_commands::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()}});
|
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
|
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
|
|
||||||
{ // installing fresh service
|
// { // installing fresh service
|
||||||
maketitle("5) Non-destructive install of fresh service...");
|
// maketitle("5) Non-destructive install of fresh service...");
|
||||||
if (!install_service(mServer, mService, true))
|
// if (!install_service(mServer, mService, true))
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool healthy = false;
|
bool healthy = false;
|
||||||
{// healthcheck the service
|
{// healthcheck the service
|
||||||
@ -460,7 +465,7 @@ bool service_runner::backup(bool silent) {
|
|||||||
ASSERT(3 == count_substring(magic_string, local_backup_file_path), "Invalid backup filename");
|
ASSERT(3 == count_substring(magic_string, local_backup_file_path), "Invalid backup filename");
|
||||||
|
|
||||||
{ // Run backup script
|
{ // Run backup script
|
||||||
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
shared_commands::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()}})) {
|
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;
|
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -480,27 +485,6 @@ bool service_runner::backup(bool silent) {
|
|||||||
return true;
|
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
|
// 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 service_runner::get_latest_backup_file(const std::string& server, const std::string& service) {
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
std::string local_backups_dir = gConfig().get_local_backup_path();
|
@ -6,6 +6,7 @@
|
|||||||
#include "utils/utils.hpp"
|
#include "utils/utils.hpp"
|
||||||
#include "server_env_manager.hpp"
|
#include "server_env_manager.hpp"
|
||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
|
#include "assert.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
@ -68,6 +69,12 @@ LocalServiceInfo get_service_info(const std::string &server_name, const std::str
|
|||||||
if (service.local_service_path.empty())
|
if (service.local_service_path.empty())
|
||||||
return LocalServiceInfo();
|
return LocalServiceInfo();
|
||||||
|
|
||||||
|
// check the service directory exists.
|
||||||
|
if (!fs::exists(service.local_service_path))
|
||||||
|
{
|
||||||
|
std::cerr << "Error: Service directory not found: " << service.local_service_path << std::endl;
|
||||||
|
return LocalServiceInfo();
|
||||||
|
}
|
||||||
|
|
||||||
// now set the template name and path.
|
// now set the template name and path.
|
||||||
std::map<std::string, std::string> variables;
|
std::map<std::string, std::string> variables;
|
||||||
@ -146,73 +153,6 @@ std::set<std::string> list_backups(const std::string &server_name, const std::st
|
|||||||
return backups;
|
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)
|
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)
|
||||||
@ -221,19 +161,20 @@ bool get_all_service_env_vars(const std::string &server_name, const std::string
|
|||||||
|
|
||||||
if (localpath::service(server_name, service_name).empty() || !fs::exists(localpath::service(server_name, service_name)))
|
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;
|
std::cerr << "Error: Service not found: " << service_name << " on server " << server_name << std::endl;
|
||||||
return false;
|
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);
|
ServerInfo server_info = get_server_info(server_name);
|
||||||
if (server_info.ssh_host.empty())
|
if (server_info.ssh_host.empty())
|
||||||
std::cerr << "Error: Server " << server_name << " not found - ssh_host empty, so HOST_NAME not set" << std::endl;
|
std::cerr << "Error: Server " << server_name << " not found - ssh_host empty, so HOST_NAME not set" << std::endl;
|
||||||
|
|
||||||
|
// add in some handy variables.
|
||||||
|
// if we change these, we also need to update agent/_allservicesstatus.sh
|
||||||
|
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);
|
||||||
all_env_vars["HOST_NAME"] = server_info.ssh_host;
|
all_env_vars["HOST_NAME"] = server_info.ssh_host;
|
||||||
|
|
||||||
// Lambda function to load environment variables from a file
|
// Lambda function to load environment variables from a file
|
@ -27,8 +27,6 @@ namespace dropshell {
|
|||||||
|
|
||||||
// list all backups for a given service (across all servers)
|
// 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);
|
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
|
} // namespace dropshell
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -296,6 +296,12 @@
|
|||||||
|
|
||||||
bool template_manager::test_template(const std::string &template_path)
|
bool template_manager::test_template(const std::string &template_path)
|
||||||
{
|
{
|
||||||
|
if (template_path.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!std::filesystem::exists(template_path))
|
||||||
|
return false;
|
||||||
|
|
||||||
std::string template_name = std::filesystem::path(template_path).filename().string();
|
std::string template_name = std::filesystem::path(template_path).filename().string();
|
||||||
|
|
||||||
std::vector<std::string> required_files = {
|
std::vector<std::string> required_files = {
|
||||||
@ -303,13 +309,22 @@
|
|||||||
"config/.template_info.env",
|
"config/.template_info.env",
|
||||||
"_default.env",
|
"_default.env",
|
||||||
"install.sh",
|
"install.sh",
|
||||||
"uninstall.sh",
|
"uninstall.sh"
|
||||||
"nuke.sh"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto& file : required_files) {
|
for (const auto& file : required_files) {
|
||||||
if (!required_file(template_path + "/" + file, template_name))
|
if (!required_file(template_path + "/" + file, template_name))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// check if file is executable, if it ends in .sh
|
||||||
|
std::string suffix=".sh";
|
||||||
|
if (file.find(suffix) == file.size() - suffix.size())
|
||||||
|
{
|
||||||
|
std::filesystem::path path = template_path + "/" + file;
|
||||||
|
auto perms = std::filesystem::status(path).permissions();
|
||||||
|
if ((perms & std::filesystem::perms::owner_exec) == std::filesystem::perms::none)
|
||||||
|
std::cerr << "Error: " << file << " is not executable" << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
@ -356,4 +371,13 @@
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template_info::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),
|
||||||
|
mTemplateValid(template_manager::test_template(local_template_path.string())),
|
||||||
|
mIsSet(!template_name.empty() && !location_id.empty() && !local_template_path.empty())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -17,16 +17,18 @@ typedef enum template_source_type {
|
|||||||
class template_info {
|
class template_info {
|
||||||
public:
|
public:
|
||||||
template_info() : mIsSet(false) {}
|
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) {}
|
template_info(const std::string& template_name, const std::string& location_id, const std::filesystem::path& local_template_path);
|
||||||
virtual ~template_info() {}
|
virtual ~template_info() {}
|
||||||
bool is_set() { return mIsSet; }
|
bool is_set() { return mIsSet; }
|
||||||
std::string name() { return mTemplateName; }
|
std::string name() { return mTemplateName; }
|
||||||
std::string locationID() { return mLocationID; }
|
std::string locationID() { return mLocationID; }
|
||||||
std::filesystem::path local_template_path() { return mTemplateLocalPath; }
|
std::filesystem::path local_template_path() { return mTemplateLocalPath; }
|
||||||
|
bool template_valid() { return mTemplateValid; }
|
||||||
private:
|
private:
|
||||||
std::string mTemplateName;
|
std::string mTemplateName;
|
||||||
std::string mLocationID;
|
std::string mLocationID;
|
||||||
std::filesystem::path mTemplateLocalPath; // source or cache.
|
std::filesystem::path mTemplateLocalPath; // source or cache.
|
||||||
|
bool mTemplateValid;
|
||||||
bool mIsSet;
|
bool mIsSet;
|
||||||
};
|
};
|
||||||
|
|
@ -66,7 +66,12 @@ namespace localpath {
|
|||||||
std::string agent(){
|
std::string agent(){
|
||||||
return current_user_home() + "/.local/dropshell_agent";
|
return current_user_home() + "/.local/dropshell_agent";
|
||||||
}
|
}
|
||||||
std::string current_user_home(){
|
std::string files_for_remote_agent()
|
||||||
|
{
|
||||||
|
return agent() + "/files_for_remote_agent";
|
||||||
|
}
|
||||||
|
std::string current_user_home()
|
||||||
|
{
|
||||||
char * homedir = std::getenv("HOME");
|
char * homedir = std::getenv("HOME");
|
||||||
if (homedir)
|
if (homedir)
|
||||||
{
|
{
|
||||||
@ -78,23 +83,27 @@ namespace localpath {
|
|||||||
}
|
}
|
||||||
} // namespace localpath
|
} // namespace localpath
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------
|
||||||
// remote paths
|
// remote paths
|
||||||
// DROPSHELL_DIR
|
// DROPSHELL_DIR
|
||||||
// |-- backups
|
// |-- backups
|
||||||
// |-- temp_files
|
// |-- temp_files
|
||||||
// |-- agent
|
// |-- agent
|
||||||
|
// | |-- bb64
|
||||||
|
// | |-- (other agent files, including _allservicesstatus.sh)
|
||||||
// |-- services
|
// |-- services
|
||||||
// |-- service name
|
// |-- service name
|
||||||
// |-- config
|
// |-- config
|
||||||
// |-- service.env
|
// |-- service.env (actual service config)
|
||||||
// |-- template
|
// |-- template
|
||||||
|
// |-- _default.env
|
||||||
// |-- (script files)
|
// |-- (script files)
|
||||||
// |-- config
|
// |-- config
|
||||||
// |-- service.env
|
// |-- service.env (default service config)
|
||||||
// |-- (other config files for specific server&service)
|
// |-- (other config files for specific server&service)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace remotefile {
|
namespace remotefile {
|
||||||
|
|
||||||
std::string service_env(const std::string &server_name, const std::string &service_name)
|
std::string service_env(const std::string &server_name, const std::string &service_name)
|
@ -13,6 +13,11 @@ namespace dropshell {
|
|||||||
|
|
||||||
// ~/.config/dropshell/dropshell.json
|
// ~/.config/dropshell/dropshell.json
|
||||||
|
|
||||||
|
// ~/.local/dropshell_agent
|
||||||
|
// |-- bb64 (only used locally, as it's for the local machine's architecture!)
|
||||||
|
// |-- files_for_remote_agent
|
||||||
|
// |-- (other agent files, including _allservicesstatus.sh)
|
||||||
|
|
||||||
// server_definition_path
|
// server_definition_path
|
||||||
// |-- <server_name>
|
// |-- <server_name>
|
||||||
// |-- server.json
|
// |-- server.json
|
||||||
@ -27,8 +32,6 @@ namespace dropshell {
|
|||||||
|
|
||||||
// temp files path
|
// temp files path
|
||||||
|
|
||||||
// executables path
|
|
||||||
|
|
||||||
// template cache path
|
// template cache path
|
||||||
// |-- templates
|
// |-- templates
|
||||||
// | |-- <template_name>.json
|
// | |-- <template_name>.json
|
||||||
@ -57,6 +60,7 @@ namespace dropshell {
|
|||||||
std::string remote_versions(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 agent();
|
||||||
|
std::string files_for_remote_agent();
|
||||||
std::string current_user_home();
|
std::string current_user_home();
|
||||||
} // namespace local
|
} // namespace local
|
||||||
|
|
||||||
@ -67,14 +71,19 @@ namespace dropshell {
|
|||||||
// |-- backups
|
// |-- backups
|
||||||
// |-- temp_files
|
// |-- temp_files
|
||||||
// |-- agent
|
// |-- agent
|
||||||
|
// | |-- bb64
|
||||||
|
// | |-- (other agent files, including _allservicesstatus.sh)
|
||||||
// |-- services
|
// |-- services
|
||||||
// |-- service name
|
// |-- service name
|
||||||
// |-- config
|
// |-- config
|
||||||
// |-- service.env
|
// |-- service.env (actual service config)
|
||||||
|
// |-- .template_info.env
|
||||||
// |-- template
|
// |-- template
|
||||||
|
// |-- _default.env
|
||||||
// |-- (script files)
|
// |-- (script files)
|
||||||
// |-- config
|
// |-- config
|
||||||
// |-- service.env
|
// |-- service.env (default service config)
|
||||||
|
// |-- .template_info.env
|
||||||
// |-- (other config files for specific server&service)
|
// |-- (other config files for specific server&service)
|
||||||
|
|
||||||
namespace remotefile {
|
namespace remotefile {
|
@ -13,6 +13,7 @@
|
|||||||
#include "utils/b64ed.hpp"
|
#include "utils/b64ed.hpp"
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "utils/directories.hpp"
|
#include "utils/directories.hpp"
|
||||||
|
#include "utils/output.hpp"
|
||||||
|
|
||||||
namespace dropshell
|
namespace dropshell
|
||||||
{
|
{
|
||||||
@ -21,11 +22,15 @@ namespace dropshell
|
|||||||
return (ret != -1 && WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); // ret is -1 if the command failed to execute.
|
return (ret != -1 && WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); // ret is -1 if the command failed to execute.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// execute_local_command_interactive
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
bool execute_local_command_interactive(const sCommand &command)
|
bool execute_local_command_interactive(const sCommand &command)
|
||||||
{
|
{
|
||||||
if (command.get_command_to_run().empty())
|
if (command.get_command_to_run().empty())
|
||||||
return false;
|
return false;
|
||||||
std::string full_command = command.construct_cmd(gConfig().get_local_executables_path()); // Get the command string
|
std::string full_command = command.construct_cmd(localpath::agent()); // Get the command string
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
@ -51,12 +56,15 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// execute_local_command_and_capture_output
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
bool execute_local_command_and_capture_output(const sCommand &command, std::string *output)
|
bool execute_local_command_and_capture_output(const sCommand &command, std::string *output)
|
||||||
{
|
{
|
||||||
ASSERT(output != nullptr, "Output string must be provided");
|
ASSERT(output != nullptr, "Output string must be provided");
|
||||||
if (command.get_command_to_run().empty())
|
if (command.get_command_to_run().empty())
|
||||||
return false;
|
return false;
|
||||||
std::string full_cmd = command.construct_cmd(gConfig().get_local_executables_path()) + " 2>&1";
|
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1";
|
||||||
FILE *pipe = popen(full_cmd.c_str(), "r");
|
FILE *pipe = popen(full_cmd.c_str(), "r");
|
||||||
if (!pipe)
|
if (!pipe)
|
||||||
{
|
{
|
||||||
@ -71,6 +79,9 @@ namespace dropshell
|
|||||||
return EXITSTATUSCHECK(ret);
|
return EXITSTATUSCHECK(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// execute_local_command
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
bool execute_local_command(std::string command, std::string *output, cMode mode)
|
bool execute_local_command(std::string command, std::string *output, cMode mode)
|
||||||
{
|
{
|
||||||
return execute_local_command("", command, {}, output, mode);
|
return execute_local_command("", command, {}, output, mode);
|
||||||
@ -99,18 +110,26 @@ namespace dropshell
|
|||||||
if (command.get_command_to_run().empty())
|
if (command.get_command_to_run().empty())
|
||||||
return false;
|
return false;
|
||||||
bool silent = hasFlag(mode, cMode::Silent);
|
bool silent = hasFlag(mode, cMode::Silent);
|
||||||
std::string full_cmd = command.construct_cmd(gConfig().get_local_executables_path()) + " 2>&1" + (silent ? " > /dev/null" : "");
|
std::string full_cmd = command.construct_cmd(localpath::agent()) + " 2>&1" + (silent ? " > /dev/null" : "");
|
||||||
int ret = system(full_cmd.c_str());
|
|
||||||
|
int ret=0;
|
||||||
|
{
|
||||||
|
SwitchColour sc(sColour::DEBUG, std::cerr);
|
||||||
|
ret = system(full_cmd.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
bool ok = EXITSTATUSCHECK(ret);
|
bool ok = EXITSTATUSCHECK(ret);
|
||||||
if (!ok && !silent)
|
if (!ok && !silent)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Failed to execute command: " << std::endl;
|
PrintError("Error: Failed to execute command: ");
|
||||||
std::cerr << full_cmd << std::endl;
|
PrintError(full_cmd);
|
||||||
}
|
}
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// execute_ssh_command
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
bool execute_ssh_command(const sSSHInfo &ssh_info, const sCommand &remote_command, cMode mode, std::string *output)
|
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())
|
if (remote_command.get_command_to_run().empty())
|
||||||
@ -144,6 +163,9 @@ namespace dropshell
|
|||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// makesafecmd
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const
|
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const
|
||||||
{
|
{
|
||||||
if (command.empty())
|
if (command.empty())
|
||||||
@ -153,6 +175,9 @@ namespace dropshell
|
|||||||
return commandstr;
|
return commandstr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
|
// construct_cmd
|
||||||
|
// ----------------------------------------------------------------------------------------------------------
|
||||||
std::string sCommand::construct_cmd(std::string agent_path) const
|
std::string sCommand::construct_cmd(std::string agent_path) const
|
||||||
{
|
{
|
||||||
if (mCmd.empty())
|
if (mCmd.empty())
|
@ -10,7 +10,7 @@ class sCommand;
|
|||||||
|
|
||||||
// mode bitset
|
// mode bitset
|
||||||
enum class cMode {
|
enum class cMode {
|
||||||
None = 0,
|
Defaults = 0,
|
||||||
Interactive = 1,
|
Interactive = 1,
|
||||||
Silent = 2,
|
Silent = 2,
|
||||||
CaptureOutput = 4
|
CaptureOutput = 4
|
||||||
@ -31,9 +31,9 @@ typedef struct sSSHInfo {
|
|||||||
std::string server_ID; // dropshell name for server.
|
std::string server_ID; // dropshell name for server.
|
||||||
} sSSHInfo;
|
} sSSHInfo;
|
||||||
|
|
||||||
bool execute_local_command(std::string command, std::string * output = nullptr, cMode mode = cMode::None);
|
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::None);
|
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::None, std::string * output = nullptr);
|
bool execute_ssh_command(const sSSHInfo & ssh_info, const sCommand & remote_command, cMode mode = cMode::Defaults, std::string * output = nullptr);
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
119
source/src/utils/output.cpp
Normal file
119
source/src/utils/output.cpp
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
#include "output.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// Mutex to synchronize output
|
||||||
|
std::mutex output_mutex;
|
||||||
|
|
||||||
|
// ANSI colour codes
|
||||||
|
constexpr const char* GREY = "\033[90m";
|
||||||
|
constexpr const char* RESET = "\033[0m";
|
||||||
|
constexpr const char* DEBUG_COLOUR = "\033[36m"; // Cyan
|
||||||
|
constexpr const char* INFO_COLOUR = "\033[32m"; // Green
|
||||||
|
constexpr const char* WARNING_COLOUR = "\033[33m"; // Yellow
|
||||||
|
constexpr const char* ERROR_COLOUR = "\033[31m"; // Red
|
||||||
|
|
||||||
|
// Tag and colour for each stream
|
||||||
|
struct StreamInfo {
|
||||||
|
const char* tag;
|
||||||
|
const char* colour;
|
||||||
|
};
|
||||||
|
|
||||||
|
const StreamInfo stream_infos[] = {
|
||||||
|
{"[DBG]", DEBUG_COLOUR},
|
||||||
|
{"[INF]", INFO_COLOUR},
|
||||||
|
{"[WRN]", WARNING_COLOUR},
|
||||||
|
{"[ERR]", ERROR_COLOUR}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Custom streambuf to prefix and colour each line
|
||||||
|
class PrefixStreambuf : public std::streambuf {
|
||||||
|
public:
|
||||||
|
PrefixStreambuf(std::ostream& dest, const char* tag, const char* colour)
|
||||||
|
: dest_(dest), tag_(tag), colour_(colour), at_line_start_(true) {}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int overflow(int c) override {
|
||||||
|
std::lock_guard<std::mutex> lock(output_mutex);
|
||||||
|
if (c == EOF) return !EOF;
|
||||||
|
if (at_line_start_ && c != '\n') {
|
||||||
|
dest_ << GREY << tag_ << RESET << ' ' << colour_;
|
||||||
|
at_line_start_ = false;
|
||||||
|
}
|
||||||
|
dest_.put(static_cast<char>(c));
|
||||||
|
if (c == '\n') {
|
||||||
|
dest_ << RESET;
|
||||||
|
at_line_start_ = true;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::ostream& dest_;
|
||||||
|
const char* tag_;
|
||||||
|
const char* colour_;
|
||||||
|
bool at_line_start_;
|
||||||
|
};
|
||||||
|
|
||||||
|
PrefixStreambuf debug_buf(std::clog, stream_infos[0].tag, stream_infos[0].colour);
|
||||||
|
PrefixStreambuf info_buf(std::clog, stream_infos[1].tag, stream_infos[1].colour);
|
||||||
|
PrefixStreambuf warning_buf(std::clog, stream_infos[2].tag, stream_infos[2].colour);
|
||||||
|
PrefixStreambuf error_buf(std::cerr, stream_infos[3].tag, stream_infos[3].colour);
|
||||||
|
|
||||||
|
std::ostream debug_stream(&debug_buf);
|
||||||
|
std::ostream info_stream(&info_buf);
|
||||||
|
std::ostream warning_stream(&warning_buf);
|
||||||
|
std::ostream error_stream(&error_buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& debug = debug_stream;
|
||||||
|
std::ostream& info = info_stream;
|
||||||
|
std::ostream& warning = warning_stream;
|
||||||
|
std::ostream& error = error_stream;
|
||||||
|
|
||||||
|
void SetColour(sColour colour, std::ostream& os) {
|
||||||
|
switch (colour) {
|
||||||
|
case sColour::RESET:
|
||||||
|
os << RESET;
|
||||||
|
break;
|
||||||
|
case sColour::DEBUG:
|
||||||
|
os << DEBUG_COLOUR;
|
||||||
|
break;
|
||||||
|
case sColour::INFO:
|
||||||
|
os << INFO_COLOUR;
|
||||||
|
break;
|
||||||
|
case sColour::WARNING:
|
||||||
|
os << WARNING_COLOUR;
|
||||||
|
break;
|
||||||
|
case sColour::ERROR:
|
||||||
|
os << ERROR_COLOUR;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintDebug(const std::string& msg) {
|
||||||
|
std::lock_guard<std::mutex> lock(output_mutex);
|
||||||
|
debug << msg << '\n';
|
||||||
|
}
|
||||||
|
void PrintInfo(const std::string& msg) {
|
||||||
|
std::lock_guard<std::mutex> lock(output_mutex);
|
||||||
|
info << msg << '\n';
|
||||||
|
}
|
||||||
|
void PrintWarning(const std::string& msg) {
|
||||||
|
std::lock_guard<std::mutex> lock(output_mutex);
|
||||||
|
warning << msg << '\n';
|
||||||
|
}
|
||||||
|
void PrintError(const std::string& msg) {
|
||||||
|
std::lock_guard<std::mutex> lock(output_mutex);
|
||||||
|
error << msg << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
SwitchColour::SwitchColour(sColour colour, std::ostream& os) : os_(os), colour_(colour)
|
||||||
|
{
|
||||||
|
SetColour(colour_, os_);
|
||||||
|
}
|
||||||
|
|
||||||
|
SwitchColour::~SwitchColour()
|
||||||
|
{
|
||||||
|
SetColour(sColour::RESET, os_);
|
||||||
|
}
|
84
source/src/utils/output.hpp
Normal file
84
source/src/utils/output.hpp
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
#ifndef OUTPUT_HPP
|
||||||
|
#define OUTPUT_HPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
output.hpp and output.cpp - simple output helpers.
|
||||||
|
|
||||||
|
Defines ostreams:
|
||||||
|
|
||||||
|
debug, info, warning, error.
|
||||||
|
|
||||||
|
These ostreams can be used with C++23 print and println, e.g.
|
||||||
|
std::println(debug, "funny variable: {}={}","my_var",my_var);
|
||||||
|
|
||||||
|
Also defines a few helper functions:
|
||||||
|
|
||||||
|
PrintDebug(const std::string& msg); // equivalent to std::println(debug, msg);
|
||||||
|
PrintInfo(const std::string& msg); // equivalent to std::println(info, msg);
|
||||||
|
PrintWarning(const std::string& msg); // equivalent to std::println(warning, msg);
|
||||||
|
PrintError(const std::string& msg); // equivalent to std::println(error, msg);
|
||||||
|
|
||||||
|
Output for these streams for each line is formatted as:
|
||||||
|
[DBG] <message>
|
||||||
|
[INF] <message>
|
||||||
|
[WRN] <message>
|
||||||
|
[ERR] <message>
|
||||||
|
|
||||||
|
The output is coloured, and the tag is printed in grey.
|
||||||
|
|
||||||
|
In addition, when not using any of the above, helper routines for coloring the output of cout and cerr are provided.
|
||||||
|
|
||||||
|
SetColour(std::ostream& os, sColour colour);
|
||||||
|
|
||||||
|
Where sColour is an enum:
|
||||||
|
enum class sColour {
|
||||||
|
RESET,
|
||||||
|
DEBUG,
|
||||||
|
INFO,
|
||||||
|
WARNING,
|
||||||
|
ERROR
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Output streams for different log levels
|
||||||
|
extern std::ostream& debug;
|
||||||
|
extern std::ostream& info;
|
||||||
|
extern std::ostream& warning;
|
||||||
|
extern std::ostream& error;
|
||||||
|
|
||||||
|
// Enum for colours
|
||||||
|
enum class sColour {
|
||||||
|
RESET,
|
||||||
|
DEBUG,
|
||||||
|
INFO,
|
||||||
|
WARNING,
|
||||||
|
ERROR
|
||||||
|
};
|
||||||
|
|
||||||
|
// Set colour for a stream
|
||||||
|
void SetColour(sColour colour, std::ostream& os = std::cerr);
|
||||||
|
|
||||||
|
// Helper print functions
|
||||||
|
void PrintDebug(const std::string& msg);
|
||||||
|
void PrintInfo(const std::string& msg);
|
||||||
|
void PrintWarning(const std::string& msg);
|
||||||
|
void PrintError(const std::string& msg);
|
||||||
|
|
||||||
|
class SwitchColour
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SwitchColour(sColour colour, std::ostream& os = std::cerr);
|
||||||
|
~SwitchColour();
|
||||||
|
private:
|
||||||
|
std::ostream& os_;
|
||||||
|
sColour colour_;
|
||||||
|
};
|
||||||
|
#endif // OUTPUT_HPP
|
@ -1,5 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
version.hpp is automatically generated by the build system, from version.hpp.in.
|
||||||
|
|
||||||
|
DO NOT EDIT VERSION.HPP!
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
@ -1,106 +0,0 @@
|
|||||||
#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 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
|
|
@ -1,31 +0,0 @@
|
|||||||
#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
|
|
@ -1,36 +0,0 @@
|
|||||||
#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
|
|
262
src/main.cpp
262
src/main.cpp
@ -1,262 +0,0 @@
|
|||||||
#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);
|
|
||||||
}
|
|
@ -1,102 +0,0 @@
|
|||||||
// 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
|
|
@ -1,15 +0,0 @@
|
|||||||
#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
|
|
Reference in New Issue
Block a user