Compare commits

...

14 Commits

Author SHA1 Message Date
b3a57f13dc dropshell release 2025.0521.2125
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-21 21:25:39 +12:00
270d6ef792 Tidying
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-21 20:51:05 +12:00
9063edb45f create-server
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-21 19:30:10 +12:00
fc6b310b89 dropshell release 2025.0521.1908
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-21 19:08:48 +12:00
7a710b525f dropshell release 2025.0521.1906
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-21 19:06:50 +12:00
1b16741288 .
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 23:09:43 +12:00
d8236a58df dropshell release 2025.0519.1727
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 17:28:01 +12:00
625de98890 Fix generated files in build, so they don't have to exist when the build starts.
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 11:24:42 +12:00
7c1b51a93c Don't include autogen in commits
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 11:19:47 +12:00
1be7af571f Remove autogen files. 2025-05-19 11:18:36 +12:00
1439ec2f79 Add dehydrate to build prereq's.
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 11:16:57 +12:00
6c3c35bf89 dropshell release 2025.0519.0021
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-19 00:21:34 +12:00
548ffea6f9 dropshell release 2025.0518.2308
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-18 23:08:44 +12:00
434a2bc6da dropshell release 2025.0518.2245
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
2025-05-18 22:45:25 +12:00
37 changed files with 1258 additions and 1170 deletions

1
.gitignore vendored
View File

@ -7,6 +7,7 @@ out/
bin/ bin/
lib/ lib/
output/ output/
autogen/
# Compiled Object files # Compiled Object files
*.o *.o

View File

@ -2,6 +2,7 @@ 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 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_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
@ -48,6 +49,19 @@ add_custom_target(run_createagent ALL
add_executable(dropshell ${SOURCES}) add_executable(dropshell ${SOURCES})
add_dependencies(dropshell run_createagent) add_dependencies(dropshell run_createagent)
# Mark the generated files as GENERATED so CMake knows they'll be created during build
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-remote.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-local.cpp
PROPERTIES GENERATED TRUE
)
# Explicitly add the generated agent files, as they might not be in the source directory when globbed at the start.
target_sources(dropshell PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-remote.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/autogen/_agent-local.cpp
)
# 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

View File

@ -0,0 +1,83 @@
#!/bin/bash
SCRIPT_DIR=$(dirname "$0")
echo "Installing dropshell host agent on this computer..."
# Prints an error message in red and exits with status code 1.
_die() {
echo -e "Error: $1"
exit 1
}
# Checks if listed environment variables are set; calls _die() if any are missing.
_check_required_env_vars() {
local required_vars=("$@")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
_die "Required environment variable $var is not set"
fi
done
}
# Checks if Docker is installed, running, and user has permission. Returns 1 on failure.
_check_docker_installed() {
if ! command -v docker &> /dev/null; then
echo "Docker is not installed"
return 1
fi
# check if docker daemon is running
if ! docker info &> /dev/null; then
echo "Docker daemon is not running"
return 1
fi
# check if user has permission to run docker
if ! docker run --rm hello-world &> /dev/null; then
echo "User does not have permission to run docker"
return 1
fi
return 0
}
function install_bb64() {
# check curl installed
if ! command -v curl &> /dev/null; then
_die "Curl is not installed. Curl is required for agent installation."
fi
curl -fsSL "https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh" | bash -s -- "$AGENT_LOCAL_PATH" "$(id -u $USER):$(id -g $USER)"
# test result code from curl
if [ $? -ne 0 ]; then
_die "Failed to install bb64. Curl returned non-zero exit code."
fi
# test if bb64 is installed
"$AGENT_LOCAL_PATH/bb64" -v
if [ $? -ne 0 ]; then
_die "bb64 did not install correctly."
fi
echo "bb64 installed successfully."
return 0;
}
#-------------------------------------------------------------------------
set -a
AGENT_LOCAL_PATH="$SCRIPT_DIR"
set +a
_check_required_env_vars "AGENT_LOCAL_PATH"
echo "Installing host agent into $AGENT_LOCAL_PATH"
_check_docker_installed || _die "Docker is required."
install_bb64

View File

@ -0,0 +1,87 @@
#!/bin/bash
# This script is used to install the dropshell agent on a remote server.
SCRIPT_DIR=$(dirname "$0")
set -a
AGENT_PATH="$SCRIPT_DIR"
set +a
if [ -f "$SCRIPT_DIR/common.sh" ]; then
source "$SCRIPT_DIR/common.sh"
else
echo "Error: common.sh not found in $SCRIPT_DIR"
exit 1
fi
_check_required_env_vars "AGENT_PATH"
function install_bb64() {
curl -fsSL "https://gitea.jde.nz/public/bb64/releases/download/latest/install.sh" | bash -s -- "$AGENT_PATH" "$(id -u $USER):$(id -g $USER)"
# test result code from curl
if [ $? -ne 0 ]; then
_die "Failed to install bb64. Curl returned non-zero exit code."
fi
# test if bb64 is installed
VER=$("$AGENT_PATH/bb64" -v)
if [ $? -ne 0 ]; then
_die "bb64 did not install correctly."
fi
echo "bb64 v$VER installed."
return 0;
}
#-------------------------------------------------------------------------
# Check pre-requisites
# Check if curl is installed
if ! command -v curl &> /dev/null; then
_die "Curl is not installed. Curl is required for agent installation."
fi
# check docker installation
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Docker is required for agent installation."
exit 1
fi
#-------------------------------------------------------------------------
echo "Installing dropshell agent..."
install_bb64
#-------------------------------------------------------------------------
# confirm we're in a good state.
required_files=(
"$AGENT_PATH/bb64"
"$AGENT_PATH/_allservicesstatus.sh"
"$AGENT_PATH/common.sh"
"$AGENT_PATH/datacommands.sh"
)
# check if all files exist
for file in "${required_files[@]}"; do
if [ ! -f "$file" ]; then
_die "$file does not exist. Please check the installation."
fi
done
#-------------------------------------------------------------------------
echo "Completed dropshell agent installation."
#-------------------------------------------------------------------------
exit 0

View File

@ -12,6 +12,10 @@ _autocommandrun_volume() {
case "$command" in case "$command" in
create) create)
if docker volume ls | grep -q ${volume_name}; then
echo "Volume ${volume_name} already exists - leaving unchanged"
return
fi
echo "Creating volume ${volume_name}" echo "Creating volume ${volume_name}"
docker volume create ${volume_name} docker volume create ${volume_name}
;; ;;
@ -39,6 +43,10 @@ _autocommandrun_path() {
case "$command" in case "$command" in
create) create)
if [ -d "${path}" ]; then
echo "Path ${path} already exists - unchanged"
return
fi
echo "Creating path ${path}" echo "Creating path ${path}"
mkdir -p ${path} mkdir -p ${path}
;; ;;
@ -47,7 +55,7 @@ _autocommandrun_path() {
local path_parent=$(dirname ${path}) local path_parent=$(dirname ${path})
local path_child=$(basename ${path}) local path_child=$(basename ${path})
if [ -d "${path_parent}/${path_child}" ]; then if [ -d "${path_parent}/${path_child}" ]; then
docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rf /volume/${path_child}" || echo "Failed to nuke path ${path}" docker run --rm -v ${path_parent}:/volume debian bash -c "rm -rfv /volume/${path_child}" || echo "Failed to nuke path ${path}"
else else
echo "Path ${path} does not exist - nothing to nuke" echo "Path ${path} does not exist - nothing to nuke"
fi fi
@ -61,8 +69,14 @@ _autocommandrun_path() {
fi fi
;; ;;
restore) restore)
echo "Restoring path ${path}" if [ ! -f "${backup_folder}/backup.tgz" ]; then
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1 echo "Backup file ${backup_folder}/backup.tgz does not exist - nothing to restore"
else
echo "Clearing existing data in path ${path}"
docker run --rm -v ${path}:/path debian bash -c "rm -rfv /path/{*,.*}"
echo "Restoring path ${path} from backup file ${backup_folder}/backup.tgz"
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1
fi
;; ;;
esac esac
} }
@ -74,6 +88,12 @@ _autocommandrun_file() {
case "$command" in case "$command" in
create) create)
filepath_parent=$(dirname ${filepath})
filepath_child=$(basename ${filepath})
if [ ! -d "${filepath_parent}" ]; then
echo "Parent directory ${filepath_parent} of ${filepath_child} does not exist - creating"
mkdir -p ${filepath_parent}
fi
;; ;;
nuke) nuke)
rm -f ${filepath} rm -f ${filepath}
@ -91,7 +111,8 @@ _autocommandrun_file() {
restore) restore)
echo "Restoring file ${filepath}" echo "Restoring file ${filepath}"
local file_name=$(basename ${filepath}) local file_name=$(basename ${filepath})
cp ${backup_folder}/${file_name} ${filepath} rm -f ${filepath} || die "Unable to remove existing file ${filepath}, restore failed."
cp ${backup_folder}/${file_name} ${filepath} || die "Unable to copy file ${backup_folder}/${file_name} to ${filepath}, restore failed."
;; ;;
esac esac
} }

View File

@ -1,7 +0,0 @@
#!/bin/bash
echo "Running remote agent self-test..."
echo "Completed remote agent self-test."
exit 0

View File

@ -142,8 +142,12 @@ if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
fi fi
fi fi
# check if dehydrate command is installed
if ! command -v dehydrate &> /dev/null; then
curl -fsSL https://gitea.jde.nz/public/dehydrate/releases/download/latest/install.sh | bash
fi
dehydrate -u
print_status "All dependencies installed successfully!" print_status "All dependencies installed successfully!"

View File

@ -14,6 +14,7 @@ if ! command -v dehydrate &> /dev/null; then
curl -fsSL https://gitea.jde.nz/public/dehydrate/releases/download/latest/install.sh | bash curl -fsSL https://gitea.jde.nz/public/dehydrate/releases/download/latest/install.sh | bash
fi fi
SCRIPT_DIR=$(dirname "$0") mkdir -p "${SCRIPT_DIR}/src/autogen"
dehydrate "${SCRIPT_DIR}/agent" "${SCRIPT_DIR}/src/autogen" dehydrate "${SCRIPT_DIR}/agent-remote" "${SCRIPT_DIR}/src/autogen"
dehydrate "${SCRIPT_DIR}/agent-local" "${SCRIPT_DIR}/src/autogen"

View File

@ -1,442 +0,0 @@
#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[] = "IyEvYmluL2Jhc2gKCiMgVGhpcyBzY3JpcHQgY29udGFpbnMgdGhlIGNvbW1vbiB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cmUoKSB7"\
"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"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
// 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, 4669115953916396805ULL, 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"\
"IDxjb250YWluZXJfbmFtZT4g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ZlX3RyZWUoKSB7CiAgICBsb2NhbCB0b19yZW1vdmU9IiQxIgogICAg"\
"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;
}
}

View File

@ -1,15 +0,0 @@
#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);
}

View File

@ -1,11 +0,0 @@
// 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;
}

View File

@ -102,7 +102,7 @@ namespace dropshell
} }
// Create backups directory locally if it doesn't exist // Create backups directory locally if it doesn't exist
std::string local_backups_dir = gConfig().get_local_backup_path(); std::string local_backups_dir = localpath::backups();
if (local_backups_dir.empty()) if (local_backups_dir.empty())
{ {
error << "Error: Local backups directory not found" << std::endl; error << "Error: Local backups directory not found" << std::endl;

View File

@ -0,0 +1,63 @@
#include "command_registry.hpp"
#include "config.hpp"
#include "utils/utils.hpp"
#include "utils/directories.hpp"
#include "shared_commands.hpp"
#include "version.hpp"
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <sstream>
#include <filesystem>
#include "utils/assert.hpp"
namespace dropshell {
void create_server_autocomplete(const CommandContext& ctx);
int create_server_handler(const CommandContext& ctx);
static std::vector<std::string> create_server_name_list={"create-server"};
// Static registration
struct CreateServerCommandRegister {
CreateServerCommandRegister() {
CommandRegistry::instance().register_command({
create_server_name_list,
create_server_handler,
create_server_autocomplete,
false, // hidden
true, // requires_config
true, // requires_install
1, // min_args (after command)
1, // max_args (after command)
"create-server [SERVER]",
"Create a new server entry on this host.",
// heredoc
R"(
Create a new server entry on this host.
Note you will need to use ds install SERVER to prepare the service for use.
create-server SERVER
)"
});
}
} create_server_command_register;
void create_server_autocomplete(const CommandContext& ctx) {
return; // can't autocomplete as it's a new server!
}
int create_server_handler(const CommandContext& ctx) {
// create a new server entry on this host
if (ctx.args.size() == 0) {
error << "No server name provided" << std::endl;
return 1;
}
bool ok = create_server(ctx.args[0]);
return ok ? 0 : 1;
}
} // namespace dropshell

View File

@ -7,6 +7,8 @@
#include "utils/utils.hpp" #include "utils/utils.hpp"
#include "services.hpp" #include "services.hpp"
#include <fstream>
namespace dropshell namespace dropshell
{ {
@ -65,6 +67,39 @@ namespace dropshell
namespace shared_commands namespace shared_commands
{ {
bool print_readme(const template_info &tinfo, std::string server, std::string service)
{
std::vector<std::string> variants_to_try = {"README.txt", "readme.txt", "ReadMe.txt", "README", "readme", "README.md", "readme.md"};
std::filesystem::path readme_path = tinfo.local_template_path();
for (const auto &variant : variants_to_try)
{
if (std::filesystem::exists(readme_path / variant))
{
readme_path = readme_path / variant;
break;
}
}
if (!std::filesystem::exists(readme_path))
return false;
std::map<std::string, std::string> all_env_vars;
get_all_service_env_vars(server, service, all_env_vars);
all_env_vars["LOCAL_CONFIG_PATH"] = localpath::service(server, service);
all_env_vars["LOCAL_TEMPLATE_PATH"] = tinfo.local_template_path().string();
info << std::endl;
std::ifstream readme_file(readme_path);
std::string line;
while (std::getline(readme_file, line))
{
rawout << substitute_provided_key_value_pairs(line, all_env_vars) << std::endl;
}
return true;
}
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name) bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name)
{ {
if (server_name.empty() || template_name.empty() || service_name.empty()) if (server_name.empty() || template_name.empty() || service_name.empty())
@ -112,10 +147,14 @@ namespace dropshell
recursive_copy(tinfo.local_template_path() / "config", service_dir); recursive_copy(tinfo.local_template_path() / "config", service_dir);
info << "Service " << service_name << " created successfully" << std::endl; info << "Service " << service_name << " created successfully" << std::endl;
info << std::endl;
info << "To complete the installation, please:" << std::endl; if (!print_readme(tinfo, server_name, service_name))
info << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl; {
info << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl; info << std::endl;
info << "To complete the installation, please:" << std::endl;
info << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
info << "2. install the remote service: dropshell install " << server_name << " " << service_name << std::endl;
}
return true; return true;
} }

View File

@ -0,0 +1,63 @@
#include "command_registry.hpp"
#include "config.hpp"
#include "utils/utils.hpp"
#include "utils/directories.hpp"
#include "shared_commands.hpp"
#include "version.hpp"
#include "utils/assert.hpp"
#include "templates.hpp"
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <sstream>
#include <filesystem>
namespace dropshell {
void create_template_autocomplete(const CommandContext& ctx);
int create_template_handler(const CommandContext& ctx);
static std::vector<std::string> create_template_name_list={"create-template"};
// Static registration
struct CreateTemplateCommandRegister {
CreateTemplateCommandRegister() {
CommandRegistry::instance().register_command({
create_template_name_list,
create_template_handler,
create_template_autocomplete,
false, // hidden
true, // requires_config
true, // requires_install
1, // min_args (after command)
1, // max_args (after command)
"create-template TEMPLATE",
"Create a new template.",
// heredoc
R"(
Create a new template.
create-template TEMPLATE
)"
});
}
} create_template_command_register;
void create_template_autocomplete(const CommandContext& ctx) {
return; // can't autocomplete as it's a new server!
}
int create_template_handler(const CommandContext& ctx) {
// create a new server entry on this host
if (ctx.args.size() == 0) {
error << "No template name provided" << std::endl;
return 1;
}
bool ok = gTemplateManager().create_template(ctx.args[0]);
return ok ? 0 : 1;
}
} // namespace dropshell

View File

@ -14,7 +14,7 @@ namespace dropshell
{ {
int nuke_handler(const CommandContext &ctx); int nuke_handler(const CommandContext &ctx);
static std::vector<std::string> nuke_name_list = {"nuke"}; static std::vector<std::string> nuke_name_list = {"destroy","nuke"};
// Static registration // Static registration
struct NukeCommandRegister struct NukeCommandRegister
@ -29,15 +29,15 @@ namespace dropshell
true, // requires_install true, // requires_install
2, // min_args (after command) 2, // min_args (after command)
2, // max_args (after command) 2, // max_args (after command)
"nuke SERVER SERVICE|all", "destroy SERVER SERVICE|all",
"Nuke a service on a server. Destroys everything, both local and remote!", "Destroy a service on a server. Erases everything, both local and remote!",
// heredoc // heredoc
R"( R"(
Nuke a service. Nuke a service.
Examples: Examples:
nuke SERVER SERVICE nuke the given service on the given server. destroy SERVER SERVICE destroy the given service on the given server.
nuke SERVER all nuke all services on the given server. destroy SERVER all destroy all services on the given server.
Note: This command is destructive and will destroy all data and all configuration, Note: This command is destructive and will destroy all data and all configuration,
both on the dropshell host and on the remote server. both on the dropshell host and on the remote server.

View File

@ -45,7 +45,7 @@ struct HelpCommandRegister {
void help_autocomplete(const CommandContext& ctx) { void help_autocomplete(const CommandContext& ctx) {
if (ctx.args.size() == 1) { if (ctx.args.size() == 0) {
// list all commands // list all commands
for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) { for (const auto& cmd : CommandRegistry::instance().list_primary_commands(false)) {
rawout << cmd << std::endl; rawout << cmd << std::endl;
@ -55,6 +55,11 @@ void help_autocomplete(const CommandContext& ctx) {
} }
void show_command(const std::string& cmd) { void show_command(const std::string& cmd) {
// get console width
int width = get_console_width() - 6; // 5 for [INF] + 1 for space
int firstcol = 34;
int secondcol = width - firstcol - 3;
const auto& cmd_info = CommandRegistry::instance().find_command(cmd); const auto& cmd_info = CommandRegistry::instance().find_command(cmd);
if (!cmd_info) if (!cmd_info)
{ {
@ -62,9 +67,22 @@ void show_command(const std::string& cmd) {
return; return;
} }
info << " "; if (cmd_info->help_usage.length() < width-secondcol)
info << left_align(cmd_info->help_usage, 32); {
info << cmd_info->help_description << std::endl; std::string remaining_description = cmd_info->help_description;
info << " " << left_align(cmd_info->help_usage, firstcol) << get_line_wrap(remaining_description, secondcol);
while (!remaining_description.empty())
info << " " << left_align(" ",firstcol) << get_line_wrap(remaining_description, secondcol-1);
}
else
{
info << " " << cmd_info->help_usage << std::endl;
std::string remaining_description = cmd_info->help_description;
info << " " << left_align(" ",firstcol) << get_line_wrap(remaining_description, secondcol);
while (!remaining_description.empty())
info << " " << left_align(" ",firstcol) << get_line_wrap(remaining_description, secondcol-1);
}
} }
extern const std::string VERSION; extern const std::string VERSION;
@ -106,7 +124,7 @@ int help_handler(const CommandContext& ctx) {
if (ctx.args.size() > 0) if (ctx.args.size() > 0)
return show_command_help(ctx.args[0]); return show_command_help(ctx.args[0]);
info << std::endl; std::cout << std::endl;
maketitle("DropShell version " + VERSION); maketitle("DropShell version " + VERSION);
info << std::endl; info << std::endl;
info << "A tool for managing remote servers, by " << AUTHOR << std::endl; info << "A tool for managing remote servers, by " << AUTHOR << std::endl;
@ -120,16 +138,18 @@ int help_handler(const CommandContext& ctx) {
{ {
// show more! // show more!
show_command("list"); show_command("list");
std::cout << std::endl; info << std::endl;
show_command("install"); show_command("install");
show_command("uninstall"); show_command("uninstall");
show_command("nuke"); show_command("nuke");
std::cout << std::endl; info << std::endl;
show_command("start"); show_command("start");
show_command("stop"); show_command("stop");
std::cout << std::endl; info << std::endl;
show_command("ssh"); show_command("ssh");
std::cout << std::endl; info << std::endl;
show_command("create-server");
show_command("create-service");
} }
return 0; return 0;
} }

View File

@ -5,16 +5,19 @@
#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 "autogen/_agent-local.hpp"
#include "autogen/_agent-remote.hpp"
#include "services.hpp" #include "services.hpp"
#include "utils/output.hpp" #include "utils/output.hpp"
#include <fstream>
#include <unistd.h> #include <unistd.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <filesystem> #include <filesystem>
#include "utils/assert.hpp" #include "utils/assert.hpp"
#include "servers.hpp"
namespace dropshell namespace dropshell
{ {
@ -37,14 +40,14 @@ namespace dropshell
0, // min_args (after command) 0, // min_args (after command)
2, // max_args (after command) 2, // max_args (after command)
"install [SERVER] [SERVICE|all]", "install [SERVER] [SERVICE|all]",
"Install/reinstall host, remote servers, or service(s). Safe/non-destructive way to update.", "Install/reinstall host and remote servers, or service(s). Safe 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
servers and their services. servers and their services.
install (re)install dropshell components on this computer. install (re)install dropshell components on this computer, and on all servers.
install SERVER (re)install dropshell agent on the given server. install SERVER (re)install dropshell agent on the particular given server.
install SERVER [SERVICE|all] (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:
@ -53,6 +56,7 @@ namespace dropshell
} }
} install_command_register; } install_command_register;
namespace shared_commands namespace shared_commands
{ {
@ -130,6 +134,8 @@ namespace dropshell
// print health tick // print health tick
info << "Health: " << shared_commands::healthtick(server, service) << std::endl; info << "Health: " << shared_commands::healthtick(server, service) << std::endl;
return true; return true;
} }
@ -159,6 +165,8 @@ namespace dropshell
int update_dropshell() int update_dropshell()
{ {
maketitle("Updating dropshell on this computer...");
// determine path to this executable // determine path to this executable
std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe"); std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe");
std::filesystem::path parent_path = dropshell_path.parent_path(); std::filesystem::path parent_path = dropshell_path.parent_path();
@ -202,8 +210,8 @@ namespace dropshell
if (currentver >= newver) if (currentver >= newver)
{ {
std::cout << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl; info << "Current dropshell version: " << currentver << ", published version: " << newver << std::endl;
std::cout << "No update needed." << std::endl; info << "Release version is not newer, no update needed." << std::endl;
return 0; return 0;
} }
@ -214,71 +222,39 @@ namespace dropshell
rval = system(bash_script_2.c_str()); rval = system(bash_script_2.c_str());
if (rval != 0) if (rval != 0)
{ {
std::cerr << "Failed to install new version of dropshell." << std::endl; error << "Failed to install new version of dropshell." << std::endl;
return -1; return -1;
} }
std::cout << "Successfully updated " << dropshell_path << " to the latest " << arch << " version." << std::endl; info << "Successfully updated " << dropshell_path << " to the latest " << arch << " version." << std::endl;
// execute the new version // execute the new version
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr); execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
std::cerr << "Failed to execute new version of dropshell." << std::endl; error << "Failed to execute new version of dropshell." << std::endl;
return -1; return -1;
} }
int install_local_agent() int install_local_agent()
{ {
std::vector<std::filesystem::path> paths = { maketitle("Installing dropshell agent on this computer...");
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) // clear out old cruft.
if (!std::filesystem::exists(p)) std::filesystem::remove_all(localpath::agent_local());
{ std::filesystem::remove_all(localpath::agent_remote());
std::cout << "Creating directory: " << p << std::endl;
std::filesystem::create_directories(p);
}
// download bb64 for the host architecture. // recreate the directories.
if (!std::filesystem::exists(localpath::agent() + "bb64")) localpath::create_directories();
{
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; // populate the agent-local directory.
recreate_agent::recreate_tree(localpath::files_for_remote_agent()); recreate_agent_local::recreate_tree(localpath::agent_local());
return 0; // run the local agent installer.
} execute_local_command(localpath::agent_local(), "agent-install.sh",{}, nullptr, cMode::Defaults | cMode::NoBB64);
int install_host() // populate the agent-remote directory.
{ info << "Creating local files to copy to remote agents..." << std::endl;
// update dropshell. recreate_agent_remote::recreate_tree(localpath::agent_remote());
// install the local dropshell agent.
int rval = update_dropshell();
if (rval != 0)
return rval;
rval = install_local_agent();
if (rval != 0)
return rval;
std::cout << "Installation complete." << std::endl;
return 0; return 0;
} }
@ -304,26 +280,12 @@ namespace dropshell
// now create the agent. // now create the agent.
// copy across from the local agent files. // copy across from the local agent files.
info << "Copying local agent files to remote server... " << std::flush; info << "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); shared_commands::rsync_tree_to_remote(localpath::agent_remote(), agent_path, server_env, false);
info << "done." << std::endl; info << "done." << std::endl;
// add in bb64. We can't use execute_remote_command() here, as that relies on bb64 which we're installing! // run the agent installer. Can't use BB64 yet, as we're installing it on the remote server.
info << "Installing bb64 on " << server << "..." << std::endl
<< std::flush;
std::string remote_cmd = bool okay = execute_ssh_command(server_env.get_SSH_INFO(), sCommand(agent_path, "agent-install.sh",{}), cMode::Defaults | cMode::NoBB64, nullptr);
"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))
error << "Failed to download bb64 to " << agent_path << " on remote server." << std::endl;
else
debug << "Downloaded bb64 to " << agent_path << " on remote server." << std::endl;
// run the self-test.
bool okay = execute_ssh_command(server_env.get_SSH_INFO(), sCommand(agent_path, "./selftest.sh", {}), cMode::Defaults, nullptr);
if (!okay) if (!okay)
{ {
error << "ERROR: Failed to install remote agent on " << server << std::endl; error << "ERROR: Failed to install remote agent on " << server << std::endl;
@ -334,6 +296,35 @@ namespace dropshell
return 0; return 0;
} }
// ------------------------------------------------------------------------------------------------
// install_host
// ------------------------------------------------------------------------------------------------
int install_host()
{
// update dropshell.
// install the local dropshell agent.
int rval = update_dropshell();
if (rval != 0)
return rval;
rval = install_local_agent();
if (rval != 0)
return rval;
// install the dropshell agent on all servers.
std::vector<ServerInfo> servers = get_configured_servers();
for (const auto &server : servers)
{
rval = install_server(server.name);
if (rval != 0)
return rval;
}
std::cout << "Installation complete." << std::endl;
return 0;
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// install command implementation // install command implementation
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -57,7 +57,7 @@ namespace dropshell
std::vector<shared_commands::cBackupFileName> get_backup_files(const std::string &server, const std::string &match_service = "", const std::string &match_template_name = "") std::vector<shared_commands::cBackupFileName> get_backup_files(const std::string &server, const std::string &match_service = "", const std::string &match_template_name = "")
{ {
std::string local_backups_dir = gConfig().get_local_backup_path(); std::string local_backups_dir = localpath::backups();
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
{ {
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl; error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
@ -127,7 +127,17 @@ namespace dropshell
ASSERT(backup_details.has_value() && backup_details->is_valid(), "Invalid backup file."); ASSERT(backup_details.has_value() && backup_details->is_valid(), "Invalid backup file.");
std::string local_backups_dir = gConfig().get_local_backup_path(); debug << "Backup details: " << std::endl;
debug << " Backup filename: " << backup_details->get_filename() << std::endl;
debug << " Backup template: " << backup_details->get_template_name() << std::endl;
debug << " Backup taken from server: " << backup_details->get_server() << std::endl;
debug << " Backup taken from service: " << backup_details->get_service() << std::endl;
debug << " " << std::endl;
debug << "Restoring to:" << std::endl;
debug << " Server: " << server << std::endl;
debug << " Service: " << service << std::endl;
std::string local_backups_dir = localpath::backups();
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
{ {
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl; error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
@ -143,14 +153,11 @@ namespace dropshell
if (backup_details->get_template_name() != service_info.template_name) if (backup_details->get_template_name() != service_info.template_name)
{ {
error << "Error: Backup template does not match service template. Can't restore." << std::endl; error << "Error: Backup template does not match service template. Can't restore." << std::endl;
debug << "Backup template: " << backup_details->get_template_name() << std::endl; info << "Backup template: " << backup_details->get_template_name() << std::endl;
debug << "Service template: " << service_info.template_name << std::endl; info << "Service template: " << service_info.template_name << std::endl;
debug << "Backup file: " << local_backup_file_path << std::endl;
return 1; return 1;
} }
info << "Restoring " << backup_details->get_datetime() << " backup of " << backup_details->get_service() << " taken from " << backup_details->get_server() << ", onto " << server << "/" << service << std::endl;
info << std::endl;
warning << "*** ALL DATA FOR " << server << "/" << service << " WILL BE OVERWRITTEN! ***" << std::endl; warning << "*** ALL DATA FOR " << server << "/" << service << " WILL BE OVERWRITTEN! ***" << std::endl;
// run the restore script // run the restore script
@ -160,9 +167,9 @@ namespace dropshell
info << "1) Backing up old service... " << std::endl; info << "1) Backing up old service... " << std::endl;
if (!shared_commands::backupdata_service(server, service)) if (!shared_commands::backupdata_service(server, service))
{ {
error << "Error: Backup failed, restore aborted." << std::endl; error << "Backup failed, restore aborted." << std::endl;
error << "You can try using dropshell install " << server << " " << service << " to install the service afresh." << std::endl; info << "You can try using dropshell install " << server << " " << service << " to install the service afresh." << std::endl;
error << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl; info << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
return 1; return 1;
} }
info << "Backup complete." << std::endl; info << "Backup complete." << std::endl;
@ -191,6 +198,8 @@ namespace dropshell
std::string remote_backups_dir = remotepath::backups(server); std::string remote_backups_dir = remotepath::backups(server);
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_details->get_filename(); std::string remote_backup_file_path = remote_backups_dir + "/" + backup_details->get_filename();
debug << "Copying backup file from local to server: " << local_backup_file_path << " -> " << remote_backup_file_path << std::endl;
// Copy backup file from local to server // Copy backup file from local to server
if (!shared_commands::scp_file_to_remote(server_env, local_backup_file_path, remote_backup_file_path, false)) if (!shared_commands::scp_file_to_remote(server_env, local_backup_file_path, remote_backup_file_path, false))
{ {
@ -199,6 +208,11 @@ namespace dropshell
} }
shared_commands::cRemoteTempFolder remote_temp_folder(server_env); shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
debug << "Running restore script on server: " << server << std::endl;
debug << " BACKUP_FILE: " << remote_backup_file_path << std::endl;
debug << " TEMP_DIR: " << remote_temp_folder.path() << std::endl;
server_env.run_remote_template_command(service, "restore", {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}}); server_env.run_remote_template_command(service, "restore", {}, false, {{"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

View File

@ -121,7 +121,7 @@ namespace dropshell
std::string output; 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)}}), 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 | cMode::Silent, cMode::Silent,
&output)) &output))
return status; return status;

View File

@ -46,6 +46,15 @@ bool config::load_config() { // load json config file.
return true; return true;
} }
void _append(std::vector<std::string> & a, const std::vector<std::string> & b) {
if (b.empty())
return;
if (a.empty())
a = b;
else
a.insert(std::end(a), std::begin(b), std::end(b));
}
bool config::save_config(bool create_aux_directories) bool config::save_config(bool create_aux_directories)
{ {
std::string config_path = localfile::dropshell_json(); std::string config_path = localfile::dropshell_json();
@ -61,37 +70,27 @@ bool config::save_config(bool create_aux_directories)
if (!mIsConfigSet) if (!mIsConfigSet)
{ {
std::string homedir = localpath::current_user_home(); std::string homedir = localpath::current_user_home();
std::string dropshell_base = homedir + "/.dropshell"; std::string dropshell_base = homedir + "/.local/dropshell_files";
mConfig["tempfiles"] = dropshell_base + "/tmp";
mConfig["backups"] = dropshell_base + "/backups";
mConfig["template_cache"] = dropshell_base + "/template_cache";
mConfig["template_registry_URLs"] = {
"https://templates.dropshell.app"
};
mConfig["template_local_paths"] = {
dropshell_base + "/local_templates"
};
mConfig["server_definition_paths"] = { mConfig["server_definition_paths"] = {
dropshell_base + "/servers" dropshell_base + "/servers"
}; };
mConfig["template_upload_registry_url"] = "https://templates.dropshell.app"; mConfig["template_local_paths"] = {
mConfig["template_upload_registry_token"] = "SECRETTOKEN"; dropshell_base + "/local_templates"
};
mConfig["template_registry_URLs"] = {
"https://templates.dropshell.app"
};
mConfig["template_upload_token"] = "SECRETTOKEN";
} }
config_file << mConfig.dump(4); config_file << mConfig.dump(4);
config_file.close(); config_file.close();
if (create_aux_directories) { if (create_aux_directories) {
std::vector<std::filesystem::path> paths = { std::vector<std::string> paths;
get_local_template_cache_path(), _append(paths, get_local_template_paths());
get_local_backup_path(), _append(paths, get_local_server_definition_paths());
get_local_tempfiles_path()
};
for (auto & p : get_local_server_definition_paths())
paths.push_back(p);
for (auto & p : paths) for (auto & p : paths)
if (!std::filesystem::exists(p)) if (!std::filesystem::exists(p))
{ {
@ -100,6 +99,11 @@ bool config::save_config(bool create_aux_directories)
} }
} }
debug << "Config paths: " << std::endl;
for (auto [key,value] : mConfig.items()) {
debug << " " << key << ": " << value << std::endl;
}
return true; return true;
} }
@ -110,31 +114,20 @@ bool config::is_config_set() const
bool config::is_agent_installed() bool config::is_agent_installed()
{ {
return std::filesystem::exists(localpath::agent() + "/bb64"); return std::filesystem::exists(localfile::bb64());
}
std::string config::get_local_tempfiles_path() {
return mConfig["tempfiles"];
}
std::string config::get_local_backup_path() {
return mConfig["backups"];
}
std::string config::get_local_template_cache_path() {
return mConfig["template_cache"];
} }
std::vector<std::string> config::get_template_registry_urls() { 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;
for (auto &url : template_registry_urls) { for (auto &url : template_registry_urls) {
urls.push_back(url); if (url.is_string() && !url.empty())
urls.push_back(url);
} }
return urls; return urls;
} }
std::vector<std::string> config::get_template_local_paths() std::vector<std::string> config::get_local_template_paths()
{ {
nlohmann::json template_local_paths = mConfig["template_local_paths"]; nlohmann::json template_local_paths = mConfig["template_local_paths"];
std::vector<std::string> paths; std::vector<std::string> paths;
@ -147,23 +140,40 @@ std::vector<std::string> config::get_template_local_paths()
std::vector<std::string> config::get_local_server_definition_paths() { std::vector<std::string> config::get_local_server_definition_paths() {
nlohmann::json server_definition_paths = mConfig["server_definition_paths"]; nlohmann::json server_definition_paths = mConfig["server_definition_paths"];
std::vector<std::string> paths; std::vector<std::string> paths;
for (auto &path : server_definition_paths) { for (auto &path : server_definition_paths) {
if (path.is_string() && !path.empty()) if (path.is_string() && !path.empty())
paths.push_back(path); paths.push_back(path);
else
warning << "Invalid server definition path: " << path << std::endl;
} }
return paths; return paths;
} }
std::string config::get_template_upload_registry_url() { std::string config::get_server_create_path()
return mConfig["template_upload_registry_url"]; {
std::vector<std::string> paths = get_local_server_definition_paths();
if (paths.empty())
return "";
return paths[0];
} }
std::string config::get_template_upload_registry_token() { std::string config::get_template_create_path()
return mConfig["template_upload_registry_token"]; {
std::vector<std::string> paths = get_local_template_paths();
if (paths.empty())
return "";
return paths[0];
}
std::string config::get_template_upload_url()
{
std::vector<std::string> urls = get_template_registry_urls();
if (urls.empty())
return "";
return urls[0];
}
std::string config::get_template_upload_token() {
return mConfig["template_upload_token"];
} }
} // namespace dropshell } // namespace dropshell

View File

@ -17,15 +17,14 @@ class config {
bool is_config_set() const; bool is_config_set() const;
static bool is_agent_installed(); static bool is_agent_installed();
std::string get_local_tempfiles_path();
std::string get_local_backup_path();
std::string get_local_template_cache_path();
std::vector<std::string> get_template_registry_urls(); std::vector<std::string> get_template_registry_urls();
std::vector<std::string> get_template_local_paths(); std::vector<std::string> get_local_template_paths();
std::vector<std::string> get_local_server_definition_paths(); std::vector<std::string> get_local_server_definition_paths();
std::string get_template_upload_registry_url(); std::string get_server_create_path();
std::string get_template_upload_registry_token(); std::string get_template_create_path();
std::string get_template_upload_url();
std::string get_template_upload_token();
private: private:
nlohmann::json mConfig; nlohmann::json mConfig;

View File

@ -218,8 +218,7 @@ bool server_env_manager::run_remote_template_command_and_capture_output(const st
for (const auto& [key, value] : extra_env_vars) for (const auto& [key, value] : extra_env_vars)
scommand->add_env_var(key, value); scommand->add_env_var(key, value);
cMode mode = cMode::CaptureOutput; return execute_ssh_command(get_SSH_INFO(), scommand.value(), cMode::Defaults, &output);
return execute_ssh_command(get_SSH_INFO(), scommand.value(), mode, &output);
} }

View File

@ -7,6 +7,7 @@
#include "config.hpp" #include "config.hpp"
#include "templates.hpp" #include "templates.hpp"
#include "contrib/transwarp.hpp" #include "contrib/transwarp.hpp"
#include "utils/output.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -76,16 +77,16 @@ bool create_server(const std::string &server_name)
// 1. check if server name already exists // 1. check if server name already exists
std::string server_existing_dir = localpath::server(server_name); std::string server_existing_dir = localpath::server(server_name);
if (!server_existing_dir.empty()) { if (!server_existing_dir.empty()) {
std::cerr << "Error: Server name already exists: " << server_name << std::endl; error << "Error: Server name already exists: " << server_name << std::endl;
std::cerr << "Current server path: " << server_existing_dir << std::endl; info << "Current server path: " << server_existing_dir << std::endl;
return false; return false;
} }
// 2. create a new directory in the user config directory // 2. create a new directory in the user config directory
auto lsdp = gConfig().get_local_server_definition_paths(); auto lsdp = gConfig().get_local_server_definition_paths();
if (lsdp.empty() || lsdp[0].empty()) { if (lsdp.empty() || lsdp[0].empty()) {
std::cerr << "Error: Local server definition path not found" << std::endl; error << "Error: Local server definition path not found" << std::endl;
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl; info << "Run 'dropshell edit' to configure DropShell" << std::endl;
return false; return false;
} }
std::string server_dir = lsdp[0] + "/" + server_name; std::string server_dir = lsdp[0] + "/" + server_name;
@ -93,20 +94,20 @@ bool create_server(const std::string &server_name)
// 3. create a template server.env file in the server directory // 3. create a template server.env file in the server directory
std::string user = getenv("USER"); std::string user = getenv("USER");
std::string server_env_path = server_dir + "/server.env"; std::string server_env_path = server_dir + "/server.json";
std::ofstream server_env_file(server_env_path); std::ofstream server_env_file(server_env_path);
server_env_file << "SSH_HOST=" << server_name << std::endl; server_env_file << "{" << std::endl;
server_env_file << "SSH_USER=" << user << std::endl; server_env_file << " \"SSH_HOST\": \"" << server_name << "\"," << std::endl;
server_env_file << "SSH_PORT=" << 22 << std::endl; server_env_file << " \"SSH_USER\": \"" << user << "\"," << std::endl;
server_env_file << std::endl; server_env_file << " \"SSH_PORT\": " << 22 << "," << std::endl;
server_env_file << "DROPSHELL_DIR=/home/"+user+"/.dropshell" << std::endl; server_env_file << " \"DROPSHELL_DIR\": \"" << "/home/"+user+"/.dropshell\"" << std::endl;
server_env_file << "}" << std::endl;
server_env_file.close(); server_env_file.close();
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) install the server: dropshell install " << server_name << 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;
} }

File diff suppressed because it is too large Load Diff

View File

@ -137,7 +137,7 @@ std::set<std::string> list_backups(const std::string &server_name, const std::st
return backups; return backups;
} }
std::string backups_dir = gConfig().get_local_backup_path(); std::string backups_dir = localpath::backups();
if (backups_dir.empty()) if (backups_dir.empty())
return backups; return backups;

View File

@ -185,7 +185,7 @@
return false; return false;
} }
auto local_template_paths = gConfig().get_template_local_paths(); auto local_template_paths = gConfig().get_local_template_paths();
if (local_template_paths.empty()) { if (local_template_paths.empty()) {
std::cerr << "Error: No local template paths found" << std::endl; std::cerr << "Error: No local template paths found" << std::endl;
std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl; std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
@ -252,7 +252,7 @@
ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty)."); ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty).");
ASSERT(gConfig().is_config_set(), "Config not set."); ASSERT(gConfig().is_config_set(), "Config not set.");
ASSERT(!mLoaded, "Template manager already loaded."); ASSERT(!mLoaded, "Template manager already loaded.");
auto local_template_paths = gConfig().get_template_local_paths(); auto local_template_paths = gConfig().get_local_template_paths();
if (local_template_paths.empty()) if (local_template_paths.empty())
return; return;
for (const auto& path : local_template_paths) for (const auto& path : local_template_paths)

View File

@ -19,11 +19,11 @@ class template_info {
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); 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() const { return mIsSet; }
std::string name() { return mTemplateName; } std::string name() const { return mTemplateName; }
std::string locationID() { return mLocationID; } std::string locationID() const { return mLocationID; }
std::filesystem::path local_template_path() { return mTemplateLocalPath; } std::filesystem::path local_template_path() const { return mTemplateLocalPath; }
bool template_valid() { return mTemplateValid; } bool template_valid() const { return mTemplateValid; }
private: private:
std::string mTemplateName; std::string mTemplateName;
std::string mLocationID; std::string mLocationID;

View File

@ -41,6 +41,16 @@ namespace localfile {
return (servicepath.empty() ? "" : (fs::path(servicepath) / ".template_info.env").string()); return (servicepath.empty() ? "" : (fs::path(servicepath) / ".template_info.env").string());
} }
std::string template_example()
{
return localpath::agent_local() + "/template_example";
}
std::string bb64()
{
return localpath::agent_local() + "/bb64";
}
} // namespace localfile } // namespace localfile
@ -62,16 +72,17 @@ namespace localpath {
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 template_cache_path = gConfig().get_local_template_cache_path(); std::string template_cache_path = localpath::template_cache();
return ((template_cache_path.empty() || service_name.empty()) ? "" : return ((template_cache_path.empty() || service_name.empty()) ? "" :
(template_cache_path+"/remote_versions/"+service_name+".json")); (template_cache_path+"/remote_versions/"+service_name+".json"));
} }
std::string agent(){ std::string agent_local()
return current_user_home() + "/.local/dropshell_agent";
}
std::string files_for_remote_agent()
{ {
return agent() + "/files_for_remote_agent"; return current_user_home()+"/.local/dropshell_agent/agent-local";
}
std::string agent_remote()
{
return current_user_home() + "/.local/dropshell_agent/agent-remote";
} }
std::string current_user_home() std::string current_user_home()
{ {
@ -84,6 +95,50 @@ namespace localpath {
warning << "Couldn't determine user directory" << std::endl; warning << "Couldn't determine user directory" << std::endl;
return std::string(); return std::string();
} }
std::string dropshell_files()
{
return current_user_home() + "/.local/dropshell_files";
return std::string();
}
std::string backups()
{
return dropshell_files() + "/backups";
}
std::string temp_files()
{
return dropshell_files() + "/temp_files";
}
std::string template_cache()
{
return dropshell_files() + "template_cache";
}
bool create_directories()
{
std::vector<std::filesystem::path> paths = {
dropshell_files(),
agent_local(),
agent_remote(),
template_cache(),
backups(),
temp_files()
};
for (auto &p : gConfig().get_local_server_definition_paths())
paths.push_back(p);
for (auto &p : paths)
if (!p.empty() && !std::filesystem::exists(p))
{
info << "Creating directory: " << p << std::endl;
std::filesystem::create_directories(p);
}
return false;
}
} // namespace localpath } // namespace localpath
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------

View File

@ -14,9 +14,29 @@ namespace dropshell {
// ~/.config/dropshell/dropshell.json // ~/.config/dropshell/dropshell.json
// ~/.local/dropshell_agent // ~/.local/dropshell_agent
// |-- bb64 (only used locally, as it's for the local machine's architecture!) // |-- agent-local
// |-- files_for_remote_agent // |-- agent-install.sh
// |-- (other agent files, including _allservicesstatus.sh) // |-- bb64 (only used locally, as it's for the local machine's architecture!)
// |-- template_example
// |-- agent-remote
// |-- (remote agent files, including _allservicesstatus.sh)
// ~/.local/dropshell_files
// |-- backups
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
// |-- temp_files
// |-- template_cache
// |-- templates
// | |-- <template_name>.json
// | |-- <template_name>
// | |-- (...script files...)
// | |-- _default.env
// | |-- config
// | |-- service.env
// | |-- .template_info.env
// | |-- (...other service config files...)
// |-- remote_versions
// | |-- server_name-service_name.json
// server_definition_path // server_definition_path
// |-- <server_name> // |-- <server_name>
@ -27,23 +47,7 @@ namespace dropshell {
// |-- .template_info.env // |-- .template_info.env
// |-- (...other config files for specific server&service...) // |-- (...other config files for specific server&service...)
// backup path
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
// temp files path
// template cache path
// |-- templates
// | |-- <template_name>.json
// | |-- <template_name>
// | |-- (...script files...)
// | |-- _default.env
// | |-- config
// | |-- service.env
// | |-- .template_info.env
// | |-- (...other service config files...)
// |-- remote_versions
// | |-- server_name-service_name.json
namespace localfile { namespace localfile {
// ~/.config/dropshell/dropshell.json // ~/.config/dropshell/dropshell.json
@ -51,6 +55,8 @@ namespace dropshell {
std::string server_json(const std::string &server_name); std::string server_json(const std::string &server_name);
std::string service_env(const std::string &server_name, const std::string &service_name); std::string service_env(const std::string &server_name, const std::string &service_name);
std::string template_info_env(const std::string &server_name, const std::string &service_name); std::string template_info_env(const std::string &server_name, const std::string &service_name);
std::string template_example();
std::string bb64();
} // namespace localfile } // namespace localfile
namespace localpath { namespace localpath {
@ -59,9 +65,16 @@ 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_local();
std::string files_for_remote_agent(); std::string agent_remote();
std::string current_user_home(); std::string current_user_home();
std::string dropshell_files();
std::string backups();
std::string temp_files();
std::string template_cache();
bool create_directories();
} // namespace local } // namespace local

View File

@ -22,7 +22,6 @@ 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 // execute_local_command_interactive
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
@ -30,7 +29,7 @@ namespace dropshell
{ {
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(localpath::agent()); // Get the command string std::string full_command = command.construct_cmd(localfile::bb64()); // Get the command string
pid_t pid = fork(); pid_t pid = fork();
@ -62,7 +61,7 @@ namespace dropshell
class fancypinter class fancypinter
{ {
public: public:
fancypinter(sColour startColour) : startColour_(startColour), currentColour_(startColour) {} fancypinter(sColour startColour) : startColour_(startColour), currentColour_(startColour) {}
void print_chunk(std::string chunk) void print_chunk(std::string chunk)
@ -85,15 +84,19 @@ namespace dropshell
currentColour_ = startColour_; currentColour_ = startColour_;
} }
colourstream(currentColour_) << chunk; colourstream(currentColour_) << chunk;
newline_ = (chunk[chunk.size()-1] == '\n'); newline_ = (chunk[chunk.size() - 1] == '\n');
} }
void print(const std::string& buffer) { void print(const std::string &buffer)
{
size_t start = 0; size_t start = 0;
while (start < buffer.size()) { while (start < buffer.size())
{
size_t newline_pos = buffer.find('\n', start); size_t newline_pos = buffer.find('\n', start);
if (newline_pos == std::string::npos) { if (newline_pos == std::string::npos)
if (start < buffer.size()) { {
if (start < buffer.size())
{
print_chunk(buffer.substr(start)); print_chunk(buffer.substr(start));
} }
break; break;
@ -103,32 +106,36 @@ namespace dropshell
} }
} }
private: private:
bool newline_ = true; bool newline_ = true;
sColour startColour_; sColour startColour_;
sColour currentColour_; sColour currentColour_;
}; };
bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode) bool execute_local_command(std::string directory_to_run_in, std::string command_to_run, const std::map<std::string, std::string> &env_vars, std::string *output, cMode mode)
{ {
sCommand command(directory_to_run_in, command_to_run, env_vars); sCommand command(directory_to_run_in, command_to_run, env_vars);
if (hasFlag(mode, cMode::Interactive)) if (hasFlag(mode, cMode::Interactive))
{ {
ASSERT(!hasFlag(mode, cMode::CaptureOutput), "Interactive mode and capture output mode cannot be used together");
ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together"); ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together");
return execute_local_command_interactive(command); return execute_local_command_interactive(command);
} }
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(localpath::agent()) + (hasFlag(mode, cMode::CaptureOutput) ? " 2>&1" : ""); // capture both stdout and stderr std::string full_cmd;
if (!hasFlag(mode, cMode::NoBB64))
full_cmd = command.construct_cmd(localfile::bb64());
else
full_cmd = command.construct_cmd("");
if (output != nullptr)
full_cmd += " 2>&1"; // capture both stdout and stderr
FILE *pipe = popen(full_cmd.c_str(), "r"); FILE *pipe = popen(full_cmd.c_str(), "r");
if (!pipe) if (!pipe)
@ -140,7 +147,7 @@ namespace dropshell
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{ {
if (output != nullptr) if (output != nullptr)
(*output) += buffer; (*output) += buffer;
if (!silent) if (!silent)
fancyprint.print(buffer); fancyprint.print(buffer);
@ -157,30 +164,27 @@ namespace dropshell
if (remote_command.get_command_to_run().empty()) if (remote_command.get_command_to_run().empty())
return false; return false;
ASSERT(!(hasFlag(mode, cMode::CaptureOutput) && output == nullptr), "Capture output mode must be used with an output string");
std::stringstream ssh_cmd; std::stringstream ssh_cmd;
ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "") ssh_cmd << "ssh -p " << ssh_info.port << " " << (hasFlag(mode, cMode::Interactive) ? "-tt " : "")
<< ssh_info.user << "@" << ssh_info.host; << ssh_info.user << "@" << ssh_info.host;
std::string remote_agent_path = remotepath::agent(ssh_info.server_ID); std::string remote_bb64_path;
if (!hasFlag(mode, cMode::NoBB64))
remote_bb64_path = remotepath::agent(ssh_info.server_ID) + "/bb64";
bool rval = execute_local_command( bool rval = execute_local_command(
"", // directory to run in "", // local directory to run in
ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path), // local command to run ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path), // local command to run
{}, // environment variables {}, // environment variables
output, // output string output, // output string
mode // mode mode // mode
); );
if (!rval && !hasFlag(mode, cMode::Silent)) if (!rval && !hasFlag(mode, cMode::Silent))
{ {
std::cerr << std::endl error << "Error: Failed to execute ssh command:" << std::endl;
<< std::endl; debug << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path) << std::endl;
std::cerr << "Error: Failed to execute ssh command:" << std::endl;
std::cerr << "\033[90m" << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_agent_path) << "\033[0m" << std::endl;
std::cerr << std::endl
<< std::endl;
} }
return rval; return rval;
} }
@ -188,19 +192,19 @@ namespace dropshell
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
// makesafecmd // makesafecmd
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
std::string sCommand::makesafecmd(std::string agent_path, const std::string &command) const std::string sCommand::makesafecmd(std::string bb64path, const std::string &command) const
{ {
if (command.empty()) if (command.empty())
return ""; return "";
std::string encoded = base64_encode(dequote(trim(command))); std::string encoded = base64_encode(dequote(trim(command)));
std::string commandstr = agent_path + "/bb64 " + encoded; std::string commandstr = bb64path + " " + encoded;
return commandstr; return commandstr;
} }
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
// construct_cmd // construct_cmd
// ---------------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------------
std::string sCommand::construct_cmd(std::string agent_path) const std::string sCommand::construct_cmd(std::string bb64path) const
{ {
if (mCmd.empty()) if (mCmd.empty())
return ""; return "";
@ -208,17 +212,27 @@ namespace dropshell
// need to construct to change directory and set environment variables // need to construct to change directory and set environment variables
std::string cmdstr; std::string cmdstr;
if (!mDir.empty()) if (!bb64path.empty())
cmdstr += "cd " + quote(mDir) + " && "; {
if (!mDir.empty())
cmdstr += "cd " + quote(mDir) + " && ";
if (!mVars.empty()) if (!mVars.empty())
for (const auto &env_var : mVars) for (const auto &env_var : mVars)
cmdstr += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " "; cmdstr += env_var.first + "=" + quote(dequote(trim(env_var.second))) + " ";
cmdstr += mCmd; cmdstr += mCmd;
if (!agent_path.empty()) cmdstr = makesafecmd(bb64path, cmdstr);
cmdstr = makesafecmd(agent_path, cmdstr); }
else
{ // raw! bootstrapping only.
ASSERT(mVars.empty(), "Bootstrapping command must not have environment variables");
if (!mDir.empty())
cmdstr += mDir + "/" + mCmd;
else
cmdstr += mCmd;
}
return cmdstr; return cmdstr;
} }

View File

@ -13,7 +13,7 @@ enum class cMode {
Defaults = 0, Defaults = 0,
Interactive = 1, Interactive = 1,
Silent = 2, Silent = 2,
CaptureOutput = 4 NoBB64 = 4
}; };
inline cMode operator&(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & static_cast<int>(rhs));} inline cMode operator&(cMode lhs, cMode rhs) {return static_cast<cMode>(static_cast<int>(lhs) & static_cast<int>(rhs));}
@ -52,10 +52,10 @@ class sCommand {
bool empty() const { return mCmd.empty(); } bool empty() const { return mCmd.empty(); }
std::string construct_cmd(std::string agent_path) const; std::string construct_cmd(std::string bb64path) const;
private: private:
std::string makesafecmd(std::string agent_path, const std::string& command) const; std::string makesafecmd(std::string bb64path, const std::string& command) const;
private: private:
std::string mDir; std::string mDir;

View File

@ -42,7 +42,7 @@ namespace dropshell
std::lock_guard<std::mutex> lock(output_mutex); std::lock_guard<std::mutex> lock(output_mutex);
if (c == EOF) if (c == EOF)
return !EOF; return !EOF;
if (at_line_start_ && c != '\n') if (at_line_start_) // && c != '\n')
{ {
dest_ << GREY << tag_ << RESET << ' ' << colour_; dest_ << GREY << tag_ << RESET << ' ' << colour_;
at_line_start_ = false; at_line_start_ = false;

View File

@ -7,6 +7,8 @@
#include <filesystem> #include <filesystem>
#include <regex> #include <regex>
#include <random> #include <random>
#include <sys/ioctl.h>
#include <unistd.h>
namespace dropshell { namespace dropshell {
@ -286,29 +288,6 @@ std::vector<std::string> split(const std::string& str, const std::string& delimi
return tokens; return tokens;
} }
std::string replace_with_environment_variables_like_bash(std::string str) {
// Combined regex pattern for both ${var} and $var formats
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
std::string result = str;
std::smatch match;
while (std::regex_search(result, match, var_pattern)) {
// match[1] will contain capture from ${var} format
// match[2] will contain capture from $var format
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
// Get value from system environment variables
const char* env_value = std::getenv(var_name.c_str());
std::string value = env_value ? env_value : "";
result = result.replace(match.position(), match.length(), value);
}
// dequote the result
return result;
}
std::string random_alphanumeric_string(int length) std::string random_alphanumeric_string(int length)
{ {
static std::mt19937 generator(std::random_device{}()); static std::mt19937 generator(std::random_device{}());
@ -366,5 +345,91 @@ std::string center_align(const std::string & str, int width) {
} }
std::string replace_with_environment_variables_like_bash(std::string str) {
// Combined regex pattern for both ${var} and $var formats
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
std::string result = str;
std::smatch match;
while (std::regex_search(result, match, var_pattern)) {
// match[1] will contain capture from ${var} format
// match[2] will contain capture from $var format
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
// Get value from system environment variables
const char* env_value = std::getenv(var_name.c_str());
std::string value = env_value ? env_value : "";
result = result.replace(match.position(), match.length(), value);
}
// dequote the result
return result;
}
std::string substitute_provided_key_value_pairs(std::string str, const std::map<std::string, std::string> &env_vars)
{
// Combined regex pattern for both ${var} and $var formats
std::regex var_pattern("\\$(?:\\{([^}]+)\\}|([a-zA-Z0-9_]+))");
std::string result = str;
std::smatch match;
while (std::regex_search(result, match, var_pattern)) {
// match[1] will contain capture from ${var} format
// match[2] will contain capture from $var format
std::string var_name = match[1].matched ? match[1].str() : match[2].str();
// Get value from environment variables map
auto it = env_vars.find(var_name);
std::string value = (it != env_vars.end()) ? it->second : "";
result = result.replace(match.position(), match.length(), value);
}
return result;
}
int get_console_width()
{
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
return w.ws_col;
}
// Fallback to a reasonable default if we can't get the width
return 80;
}
std::string remove_return(std::string str)
{
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
return str;
}
std::string get_line_wrap(std::string &src, int maxchars)
{
if (src.empty())
return "";
if (src.length() <= maxchars)
{
std::string out = src;
src.erase();
return remove_return(out) + '\n';
}
// find last whitespace up to but not more than maxchars
size_t grab_to=maxchars;
size_t lastreturn = src.rfind('\n', maxchars);
size_t lastspace = src.rfind(' ', maxchars);
if (lastreturn != std::string::npos)
grab_to = lastreturn;
else if (lastspace != std::string::npos)
grab_to = lastspace;
std::string out = src.substr(0, grab_to);
src = src.substr(grab_to + 1);
return remove_return(out) + '\n';
}
} // namespace dropshell } // namespace dropshell

View File

@ -2,6 +2,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
#include "output.hpp" #include "output.hpp"
@ -41,8 +42,6 @@ void ensure_directories_exist(std::vector<std::string> directories);
std::vector<int> search(const std::string &pat, const std::string &txt); std::vector<int> search(const std::string &pat, const std::string &txt);
int count_substring(const std::string &substring, const std::string &text); int count_substring(const std::string &substring, const std::string &text);
std::string replace_with_environment_variables_like_bash(std::string str);
std::string random_alphanumeric_string(int length); std::string random_alphanumeric_string(int length);
int die(const std::string & msg); int die(const std::string & msg);
@ -53,4 +52,11 @@ std::string left_align(const std::string & str, int width);
std::string right_align(const std::string & str, int width); std::string right_align(const std::string & str, int width);
std::string center_align(const std::string & str, int width); std::string center_align(const std::string & str, int width);
std::string replace_with_environment_variables_like_bash(std::string str);
std::string substitute_provided_key_value_pairs(std::string str, const std::map<std::string, std::string> & env_vars);
int get_console_width();
std::string get_line_wrap(std::string & src, int maxchars);
} // namespace dropshell } // namespace dropshell