Compare commits
9 Commits
2025.0518.
...
2025.0519.
Author | SHA1 | Date | |
---|---|---|---|
6c3c35bf89 | |||
548ffea6f9 | |||
434a2bc6da | |||
cf8738aee9 | |||
d80820db15 | |||
fb6974b51a | |||
263edd9b50 | |||
e45afe460b | |||
630a9fd19a |
83
source/agent-local/agent-install.sh
Executable file
83
source/agent-local/agent-install.sh
Executable 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_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_PATH/bb64" -v
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
_die "bb64 did not install correctly."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "bb64 installed successfully."
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
set -a
|
||||||
|
AGENT_PATH="$SCRIPT_DIR"
|
||||||
|
set +a
|
||||||
|
|
||||||
|
_check_required_env_vars "AGENT_PATH"
|
||||||
|
echo "Installing host agent into $AGENT_PATH"
|
||||||
|
|
||||||
|
_check_docker_installed || _die "Docker is required."
|
||||||
|
|
||||||
|
install_bb64
|
||||||
|
|
70
source/agent-remote/agent-install.sh
Executable file
70
source/agent-remote/agent-install.sh
Executable file
@ -0,0 +1,70 @@
|
|||||||
|
#!/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() {
|
||||||
|
# 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_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_PATH/bb64" -v
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
_die "bb64 did not install correctly."
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "bb64 installed successfully."
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
echo "Installing dropshell agent..."
|
||||||
|
|
||||||
|
install_bb64
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
echo "Running remote agent self-test..."
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
echo "Completed remote agent self-test."
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
echo "Completed dropshell agent installation."
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
|
exit 0
|
@ -47,7 +47,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 +61,14 @@ _autocommandrun_path() {
|
|||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
restore)
|
restore)
|
||||||
echo "Restoring path ${path}"
|
if [ ! -f "${backup_folder}/backup.tgz" ]; then
|
||||||
|
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
|
tar -xzvf ${backup_folder}/backup.tgz -C ${path} --strip-components=1
|
||||||
|
fi
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
@ -91,7 +97,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
|
||||||
}
|
}
|
@ -1,7 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
echo "Running remote agent self-test..."
|
|
||||||
|
|
||||||
echo "Completed remote agent self-test."
|
|
||||||
|
|
||||||
exit 0
|
|
@ -14,6 +14,5 @@ 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")
|
dehydrate "${SCRIPT_DIR}/agent-remote" "${SCRIPT_DIR}/src/autogen"
|
||||||
|
dehydrate "${SCRIPT_DIR}/agent-local" "${SCRIPT_DIR}/src/autogen"
|
||||||
dehydrate "${SCRIPT_DIR}/agent" "${SCRIPT_DIR}/src/autogen"
|
|
||||||
|
160
source/src/autogen/_agent-local.cpp
Normal file
160
source/src/autogen/_agent-local.cpp
Normal file
@ -0,0 +1,160 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
THIS FILE IS AUTO-GENERATED BY DEHYDRATE.
|
||||||
|
DO NOT EDIT THIS FILE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "_agent-local.hpp"
|
||||||
|
namespace recreate_agent_local {
|
||||||
|
|
||||||
|
|
||||||
|
// 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: agent-install.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "agent-install.sh";
|
||||||
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKClNDUklQVF9ESVI9JChkaXJuYW1lICIkMCIpCgplY2hvICJJbnN0YWxsaW5n"\
|
||||||
|
"IGRyb3BzaGVsbCBob3N0IGFnZW50IG9uIHRoaXMgY29tcHV0ZXIuLi4iCgoKIyBQcmludHMgYW4g"\
|
||||||
|
"ZXJyb3IgbWVzc2FnZSBpbiByZWQgYW5kIGV4aXRzIHdpdGggc3RhdHVzIGNvZGUgMS4KX2RpZSgp"\
|
||||||
|
"IHsKICAgIGVjaG8gLWUgIkVycm9yOiAkMSIKICAgIGV4aXQgMQp9CgoKIyBDaGVja3MgaWYgbGlz"\
|
||||||
|
"dGVkIGVudmlyb25tZW50IHZhcmlhYmxlcyBhcmUgc2V0OyBjYWxscyBfZGllKCkga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QgZGlkIG5vdCBpbnN0YWxsIGNvcnJlY3RseS4iCiAgICBm"\
|
||||||
|
"aQoKICAgIGVjaG8gImJiNjQgaW5zdGFsbGVkIHN1Y2Nlc3NmdWxseS4iCiAgICByZXR1cm4gMDsK"\
|
||||||
|
"fQoKCiMtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tCgpzZXQgLWEKQUdFTlRfUEFUSD0iJFNDUklQVF9ESVIiCnNl"\
|
||||||
|
"dCArYQoKX2NoZWNrX3JlcXVpcmVkX2Vudl92YXJzICJBR0VOVF9QQVRIIgplY2hvICJJbnN0YWxs"\
|
||||||
|
"aW5nIGhvc3QgYWdlbnQgaW50byAkQUdFTlRfUEFUSCIKCl9jaGVja19kb2NrZXJfaW5zdGFsbGVk"\
|
||||||
|
"IHx8IF9kaWUgIkRvY2tlciBpcyByZXF1aXJlZC4iCgppbnN0YWxsX2JiNjQKCg==";
|
||||||
|
|
||||||
|
// 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, 14766786204011647584ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
return any_written;
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,6 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
namespace recreate_agent {
|
namespace recreate_agent_local {
|
||||||
bool recreate_tree(std::string destination_folder);
|
bool recreate_tree(std::string destination_folder);
|
||||||
}
|
}
|
@ -12,8 +12,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "_agent.hpp"
|
#include "_agent-remote.hpp"
|
||||||
namespace recreate_agent {
|
namespace recreate_agent_remote {
|
||||||
|
|
||||||
|
|
||||||
// Tiny dependency-free FNV-1a 64-bit hash
|
// Tiny dependency-free FNV-1a 64-bit hash
|
||||||
@ -105,22 +105,6 @@ static bool _recreate_file_(const std::filesystem::path& outpath, uint64_t file_
|
|||||||
bool recreate_tree(std::string destination_folder) {
|
bool recreate_tree(std::string destination_folder) {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
bool any_written = false;
|
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
|
// File: datacommands.sh
|
||||||
fs::path outpath = fs::path(destination_folder) / "datacommands.sh";
|
fs::path outpath = fs::path(destination_folder) / "datacommands.sh";
|
||||||
@ -152,81 +136,91 @@ bool recreate_tree(std::string destination_folder) {
|
|||||||
"ICAgbG9jYWwgcG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ICAgYmFja3VwKQogICAgICAgICAgICBlY2hvICJCYWNraW5n"\
|
||||||
"dXAgcGF0aCAke3BhdGh9IgogICAgICAgICAgICBpZiBbIC1kICIke3BhdGh9IiBdOyB0aGVuCiAg"\
|
"IHVwIHBhdGggJHtwYXRofSIKICAgICAgICAgICAgaWYgWyAtZCAiJHtwYXRofSIgXTsgdGhlbgog"\
|
||||||
"ICAgICAgICAgICAgICBkb2NrZXIgcnVuIC0tcm0gLXYgJHtwYXRofTovcGF0aCAtdiAke2JhY2t1"\
|
"ICAgICAgICAgICAgICAgZG9ja2VyIHJ1biAtLXJtIC12ICR7cGF0aH06L3BhdGggLXYgJHtiYWNr"\
|
||||||
"cF9mb2xkZXJ9Oi9iYWNrdXAgZGViaWFuIGJhc2ggLWMgInRhciAtY3p2ZiAvYmFja3VwL2JhY2t1"\
|
"dXBfZm9sZGVyfTovYmFja3VwIGRlYmlhbiBiYXNoIC1jICJ0YXIgLWN6dmYgL2JhY2t1cC9iYWNr"\
|
||||||
"cC50Z3ogLUMgL3BhdGggLiAmJiBjaG93biAtUiAkTVlJRDokTVlHUlAgL2JhY2t1cCIKICAgICAg"\
|
"dXAudGd6IC1DIC9wYXRoIC4gJiYgY2hvd24gLVIgJE1ZSUQ6JE1ZR1JQIC9iYWNrdXAiCiAgICAg"\
|
||||||
"ICAgICAgZWxzZQogICAgICAgICAgICAgICAgZWNobyAiUGF0aCAke3BhdGh9IGRvZXMgbm90IGV4"\
|
"ICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIGVjaG8gIlBhdGggJHtwYXRofSBkb2VzIG5vdCBl"\
|
||||||
"aXN0IC0gbm90aGluZyB0byBiYWNrdXAiCiAgICAgICAgICAgIGZpCiAgICAgICAgICAgIDs7CiAg"\
|
"eGlzdCAtIG5vdGhpbmcgdG8gYmFja3VwIgogICAgICAgICAgICBmaQogICAgICAgICAgICA7Owog"\
|
||||||
"ICAgICAgcmVzdG9yZSkKICAgICAgICAgICAgZWNobyAiUmVzdG9yaW5nIHBhdGggJHtwYXRofSIK"\
|
"ICAgICAgIHJlc3RvcmUpCiAgICAgICAgICAgIGlmIFsgISAtZiAiJHtiYWNrdXBfZm9sZGVyfS9i"\
|
||||||
"ICAgICAgICAgICAgdGFyIC14enZmICR7YmFja3VwX2ZvbGRlcn0vYmFja3VwLnRneiAtQyAke3Bh"\
|
"YWNrdXAudGd6IiBdOyB0aGVuCiAgICAgICAgICAgICAgICBlY2hvICJCYWNrdXAgZmlsZSAke2Jh"\
|
||||||
"dGh9IC0tc3RyaXAtY29tcG9uZW50cz0xCiAgICAgICAgICAgIDs7CiAgICBlc2FjCn0KCl9hdXRv"\
|
"Y2t1cF9mb2xkZXJ9L2JhY2t1cC50Z3ogZG9lcyBub3QgZXhpc3QgLSBub3RoaW5nIHRvIHJlc3Rv"\
|
||||||
"Y29tbWFuZHJ1bl9maWxlKCkgewogICAgbG9jYWwgY29tbWFuZD0iJDEiCiAgICBsb2NhbCBmaWxl"\
|
"cmUiCiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIGVjaG8gIkNsZWFyaW5nIGV4aXN0"\
|
||||||
"cGF0aD0iJDIiCiAgICBsb2NhbCBiYWNrdXBfZm9sZGVyPSIkMyIKCiAgICBjYXNlICIkY29tbWFu"\
|
"aW5nIGRhdGEgaW4gcGF0aCAke3BhdGh9IgogICAgICAgICAgICAgICAgZG9ja2VyIHJ1biAtLXJt"\
|
||||||
"ZCIgaW4KICAgICAgICBjcmVhdGUpCiAgICAgICAgICAgIDs7CiAgICAgICAgbnVrZSkKICAgICAg"\
|
"IC12ICR7cGF0aH06L3BhdGggZGViaWFuIGJhc2ggLWMgInJtIC1yZnYgL3BhdGgveyosLip9Igog"\
|
||||||
"ICAgICAgcm0gLWYgJHtmaWxlcGF0aH0KICAgICAgICAgICAgOzsKICAgICAgICBiYWNrdXApCiAg"\
|
"ICAgICAgICAgICAgICAgZWNobyAiUmVzdG9yaW5nIHBhdGggJHtwYXRofSBmcm9tIGJhY2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F9QQVRIIgogICAg"\
|
||||||
|
"ZWNobyAiX2F1dG9jb21tYW5kcGFyc2UgW2JhY2t1cF0gWyRCQUNLVVBfVEVNUF9QQVRIXSBbJEBd"\
|
||||||
|
"IgogICAgX2F1dG9jb21tYW5kcGFyc2UgYmFja3VwICIkQkFDS1VQX1RFTVBfUEFUSCIgIiRAIgoK"\
|
||||||
|
"ICAgIHRhciB6Y3ZmICIkQkFDS1VQX0ZJTEUiIC1DICIkQkFDS1VQX1RFTVBfUEFUSCIgLgp9Cgpk"\
|
||||||
|
"YXRhcmVzdG9yZSgpIHsKICAgIF9jaGVja19yZXF1aXJlZF9lbnZfdmFycyAiQkFDS1VQX0ZJTEUi"\
|
||||||
|
"ICJURU1QX0RJUiIKICAgIEJBQ0tVUF9URU1QX1BBVEg9IiRURU1QX0RJUi9yZXN0b3JlIgoKICAg"\
|
||||||
|
"IGVjaG8gIl9hdXRvY29tbWFuZHBhcnNlIFtyZXN0b3JlXSBbJEJBQ0tVUF9URU1QX1BBVEhdIFsk"\
|
||||||
|
"QF0iCgogICAgbWtkaXIgLXAgIiRCQUNLVVBfVEVNUF9QQVRIIgogICAgdGFyIHp4dmYgIiRCQUNL"\
|
||||||
|
"VVBfRklMRSIgLUMgIiRCQUNLVVBfVEVNUF9QQVRIIiAtLXN0cmlwLWNvbXBvbmVudHM9MQoKICAg"\
|
||||||
|
"IF9hdXRvY29tbWFuZHBhcnNlIHJlc3RvcmUgIiRCQUNLVVBfVEVNUF9QQVRIIiAiJEAiCn0K";
|
||||||
|
|
||||||
// Decode Base64 data
|
// Decode Base64 data
|
||||||
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
size_t decoded_size = (strlen(filedata_base64) * 3) / 4;
|
||||||
@ -234,7 +228,49 @@ bool recreate_tree(std::string destination_folder) {
|
|||||||
size_t actual_size;
|
size_t actual_size;
|
||||||
base64_decode(filedata_base64, strlen(filedata_base64), decoded_data, &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);
|
bool file_written = _recreate_file_(outpath, 12213965674971673660ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
|
delete[] decoded_data;
|
||||||
|
any_written = any_written || file_written;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// File: agent-install.sh
|
||||||
|
fs::path outpath = fs::path(destination_folder) / "agent-install.sh";
|
||||||
|
static const char filedata_base64[] = "IyEvYmluL2Jhc2gKCiMgVGhpcyBzY3JpcHQgaXMgdXNlZCB0byBpbnN0YWxsIHRoZSBkcm9wc2hl"\
|
||||||
|
"bGwgYWdlbnQgb24gYSByZW1vdGUgc2VydmVyLgoKU0NSSVBUX0RJUj0kKGRpcm5hbWUgIiQwIikK"\
|
||||||
|
"CnNldCAtYQoKQUdFTlRfUEFUSD0iJFNDUklQVF9ESVIiCgpzZXQgK2EKCgppZiBbIC1mICIkU0NS"\
|
||||||
|
"SVBUX0RJUi9jb21tb24uc2giIF07IHRoZW4KICAgIHNvdXJjZSAiJFNDUklQVF9ESVIvY29tbW9u"\
|
||||||
|
"LnNoIgplbHNlCiAgICBlY2hvICJFcnJvcjogY29tbW9uLnNoIG5vdCBmb3VuZCBpbiAkU0NSSVBU"\
|
||||||
|
"X0RJUiIKICAgIGV4aXQgMQpmaQoKX2NoZWNrX3JlcXVpcmVkX2Vudl92YXJzICJBR0VOVF9QQVRI"\
|
||||||
|
"IgoKZnVuY3Rpb24gaW5zdGFsbF9iYjY0KCkgeyAgICAKICAgICMgY2hlY2sgY3VybCBpbnN0YWxs"\
|
||||||
|
"ZWQKICAgIGlmICEgY29tbWFuZCAtdiBjdXJsICY+IC9kZXYvbnVsbDsgdGhlbgogICAgICAgIF9k"\
|
||||||
|
"aWUgIkN1cmwgaXMgbm90IGluc3RhbGxlZC4gQ3VybCBpcyByZXF1aXJlZCBmb3IgYWdlbnQgaW5z"\
|
||||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tLS0tLS0tLS0tLS0tLQoKZWNobyAiUnVubmluZyByZW1vdGUgYWdlbnQgc2VsZi10"\
|
||||||
|
"ZXN0Li4uIgoKCiMtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgplY2hvICJDb21wbGV0ZWQgcmVtb3RlIGFnZW50"\
|
||||||
|
"IHNlbGYtdGVzdC4iCgoKIy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0KCmVjaG8gIkNvbXBsZXRlZCBkcm9wc2hl"\
|
||||||
|
"bGwgYWdlbnQgaW5zdGFsbGF0aW9uLiIKCiMtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t"\
|
||||||
|
"LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCgpleGl0IDAK";
|
||||||
|
|
||||||
|
// 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, 8417440848759774142ULL, std::filesystem::perms(493), decoded_data, actual_size);
|
||||||
delete[] decoded_data;
|
delete[] decoded_data;
|
||||||
any_written = any_written || file_written;
|
any_written = any_written || file_written;
|
||||||
}
|
}
|
15
source/src/autogen/_agent-remote.hpp
Normal file
15
source/src/autogen/_agent-remote.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_remote {
|
||||||
|
bool recreate_tree(std::string destination_folder);
|
||||||
|
}
|
174
source/src/commands/backupdata.cpp
Normal file
174
source/src/commands/backupdata.cpp
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "utils/output.hpp"
|
||||||
|
#include "utils/assert.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
#include "command_registry.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
#include "servers.hpp"
|
||||||
|
#include "server_env_manager.hpp"
|
||||||
|
#include "templates.hpp"
|
||||||
|
#include "utils/directories.hpp"
|
||||||
|
#include "shared_commands.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int backupdata_handler(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> backupdata_name_list = {"backupdata", "bd", "backup", "bup"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct BackupDataCommandRegister
|
||||||
|
{
|
||||||
|
BackupDataCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({backupdata_name_list,
|
||||||
|
backupdata_handler,
|
||||||
|
shared_commands::std_autocomplete_allowall,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
2, // min_args (after command)
|
||||||
|
2, // max_args (after command)
|
||||||
|
"backupdata SERVER SERVICE",
|
||||||
|
"Backup data for a service on a server.",
|
||||||
|
// heredoc
|
||||||
|
R"(
|
||||||
|
backupdata SERVER SERVICE Backup data for a service on a server.
|
||||||
|
backupdata SERVER all Backup data for all services on a server.
|
||||||
|
|
||||||
|
Note: This command will not create any data or configuration.
|
||||||
|
It will simply backup the data on the remote server, saving it to a local file.
|
||||||
|
Restore the data with restore.
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} backupdata_command_register;
|
||||||
|
|
||||||
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
|
bool backupdata_service(const std::string &server, const std::string &service)
|
||||||
|
{
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
error << "Server " << server << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalServiceInfo sinfo = get_service_info(server, service);
|
||||||
|
if (!SIvalid(sinfo))
|
||||||
|
{
|
||||||
|
error << "Service " << service << " is not valid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string command = "backup";
|
||||||
|
|
||||||
|
if (!gTemplateManager().template_command_exists(sinfo.template_name, command))
|
||||||
|
{
|
||||||
|
info << service << " has no data to backup" << std::endl;
|
||||||
|
debug << "(no backup script for " << sinfo.template_name << ")" << std::endl;
|
||||||
|
return true; // nothing to back up.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if basic installed stuff is in place.
|
||||||
|
std::string remote_service_template_path = remotepath::service_template(server, service);
|
||||||
|
std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
||||||
|
std::string remote_service_config_path = remotepath::service_config(server, service);
|
||||||
|
if (!server_env.check_remote_items_exist({remotepath::service(server, service),
|
||||||
|
remote_command_script_file,
|
||||||
|
remotefile::service_env(server, service)}))
|
||||||
|
{
|
||||||
|
error << "Error: Required service directories not found on remote server" << std::endl;
|
||||||
|
info << "Is the service installed?" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create backups directory on server if it doesn't exist
|
||||||
|
std::string remote_backups_dir = remotepath::backups(server);
|
||||||
|
debug << "Remote backups directory on " << server << ": " << remote_backups_dir << std::endl;
|
||||||
|
std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
||||||
|
if (!execute_ssh_command(server_env.get_SSH_INFO(), sCommand("", mkdir_cmd, {}), cMode::Defaults))
|
||||||
|
{
|
||||||
|
error << "Failed to create backups directory on server" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create backups directory locally if it doesn't exist
|
||||||
|
std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||||
|
if (local_backups_dir.empty())
|
||||||
|
{
|
||||||
|
error << "Error: Local backups directory not found" << std::endl;
|
||||||
|
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!std::filesystem::exists(local_backups_dir))
|
||||||
|
std::filesystem::create_directories(local_backups_dir);
|
||||||
|
|
||||||
|
// Get current datetime for backup filename
|
||||||
|
shared_commands::cBackupFileName backup_filename_construction(server, service, sinfo.template_name);
|
||||||
|
if (!backup_filename_construction.is_valid())
|
||||||
|
{
|
||||||
|
error << "Invalid backup filename" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct backup filename
|
||||||
|
std::string backup_filename = backup_filename_construction.get_filename();
|
||||||
|
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_filename;
|
||||||
|
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_filename).string();
|
||||||
|
|
||||||
|
// assert that the backup filename is valid - -_- appears exactly 3 times in local_backup_file_path.
|
||||||
|
ASSERT(3 == count_substring(magic_string(), local_backup_file_path), "Invalid backup filename");
|
||||||
|
|
||||||
|
{ // Run backup script
|
||||||
|
shared_commands::cRemoteTempFolder remote_temp_folder(server_env);
|
||||||
|
if (!server_env.run_remote_template_command(service, command, {}, false, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}}))
|
||||||
|
{
|
||||||
|
error << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy backup file from server to local
|
||||||
|
if (!shared_commands::scp_file_from_remote(server_env, remote_backup_file_path, local_backup_file_path, false))
|
||||||
|
{
|
||||||
|
error << "Failed to copy backup file from server" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
|
info << "Backup created successfully. Restore with:" << std::endl;
|
||||||
|
info << " dropshell restore " << server << " " << service << " " << backup_filename << std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
|
int backupdata_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
ASSERT(ctx.args.size() == 2, "Invalid number of arguments");
|
||||||
|
|
||||||
|
std::string server = safearg(ctx.args, 0);
|
||||||
|
std::string service = safearg(ctx.args, 1);
|
||||||
|
|
||||||
|
if (service == "all")
|
||||||
|
{
|
||||||
|
// backup all services on the server
|
||||||
|
maketitle("Backing up data for all services on " + server);
|
||||||
|
bool okay = true;
|
||||||
|
std::vector<LocalServiceInfo> services = get_server_services_info(server);
|
||||||
|
for (const LocalServiceInfo &si : services)
|
||||||
|
okay &= shared_commands::backupdata_service(server, si.service_name);
|
||||||
|
return okay ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shared_commands::backupdata_service(server, service) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
@ -11,7 +11,6 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
|
|
||||||
int create_service_handler(const CommandContext &ctx);
|
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);
|
void create_service_autocomplete(const CommandContext &ctx);
|
||||||
|
|
||||||
static std::vector<std::string> create_service_name_list = {"create-service"};
|
static std::vector<std::string> create_service_name_list = {"create-service"};
|
||||||
@ -45,7 +44,7 @@ namespace dropshell
|
|||||||
std::string service = safearg(ctx.args, 1);
|
std::string service = safearg(ctx.args, 1);
|
||||||
std::string template_name = safearg(ctx.args, 2);
|
std::string template_name = safearg(ctx.args, 2);
|
||||||
|
|
||||||
return create_service(server, template_name, service, false) ? 0 : 1;
|
return shared_commands::create_service(server, template_name, service) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void create_service_autocomplete(const CommandContext &ctx)
|
void create_service_autocomplete(const CommandContext &ctx)
|
||||||
@ -58,12 +57,15 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
std::set<std::string> templates = gTemplateManager().get_template_list();
|
std::set<std::string> templates = gTemplateManager().get_template_list();
|
||||||
for (const auto &template_name : templates)
|
for (const auto &template_name : templates)
|
||||||
std::cout << template_name << std::endl;
|
rawout << template_name << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name, bool silent)
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
|
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())
|
||||||
return false;
|
return false;
|
||||||
@ -72,44 +74,34 @@ namespace dropshell
|
|||||||
|
|
||||||
if (service_dir.empty())
|
if (service_dir.empty())
|
||||||
{
|
{
|
||||||
if (!silent)
|
error << "Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
||||||
{
|
info << "Please check the server name is correct and try again" << std::endl;
|
||||||
std::cerr << "Error: Couldn't locate server " << server_name << " in any config directory" << std::endl;
|
info << "You can list all servers with 'dropshell servers'" << std::endl;
|
||||||
std::cerr << "Please check the server name is correct and try again" << std::endl;
|
info << "You can create a new server with 'dropshell create-server " << server_name << "'" << 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (std::filesystem::exists(service_dir))
|
if (std::filesystem::exists(service_dir))
|
||||||
{
|
{
|
||||||
if (!silent)
|
error << "Service already exists: " << service_name << std::endl;
|
||||||
{
|
debug << "Current service path: " << service_dir << std::endl;
|
||||||
std::cerr << "Error: Service already exists: " << service_name << std::endl;
|
|
||||||
std::cerr << "Current service path: " << service_dir << std::endl;
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template_info tinfo = gTemplateManager().get_template_info(template_name);
|
template_info tinfo = gTemplateManager().get_template_info(template_name);
|
||||||
if (!tinfo.is_set())
|
if (!tinfo.is_set())
|
||||||
{
|
{
|
||||||
if (!silent)
|
error << "Template '" << template_name << "' not found" << std::endl;
|
||||||
{
|
info << "Please check the template name is correct and try again" << std::endl;
|
||||||
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
|
info << "You can list all templates with 'dropshell templates'" << std::endl;
|
||||||
std::cerr << "Please check the template name is correct and try again" << std::endl;
|
info << "You can create a new template with 'dropshell create-template " << template_name << "'" << 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check template is all good.
|
// check template is all good.
|
||||||
if (!gTemplateManager().test_template(tinfo.local_template_path()))
|
if (!gTemplateManager().test_template(tinfo.local_template_path()))
|
||||||
{
|
{
|
||||||
if (!silent)
|
error << "Template '" << template_name << "' is not valid" << std::endl;
|
||||||
std::cerr << "Error: Template '" << template_name << "' is not valid" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,15 +111,14 @@ namespace dropshell
|
|||||||
// copy the template config files to the service directory
|
// copy the template config files to the service directory
|
||||||
recursive_copy(tinfo.local_template_path() / "config", service_dir);
|
recursive_copy(tinfo.local_template_path() / "config", service_dir);
|
||||||
|
|
||||||
if (!silent)
|
info << "Service " << service_name << " created successfully" << std::endl;
|
||||||
{
|
info << std::endl;
|
||||||
std::cout << "Service " << service_name << " created successfully" << std::endl;
|
info << "To complete the installation, please:" << std::endl;
|
||||||
std::cout << std::endl;
|
info << "1. edit the service config file: dropshell edit " << server_name << " " << service_name << std::endl;
|
||||||
std::cout << "To complete the installation, please:" << std::endl;
|
info << "2. install the remote service: dropshell install " << server_name << " " << service_name << 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -5,7 +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 "autogen/_agent-local.hpp"
|
||||||
|
#include "autogen/_agent-remote.hpp"
|
||||||
#include "services.hpp"
|
#include "services.hpp"
|
||||||
#include "utils/output.hpp"
|
#include "utils/output.hpp"
|
||||||
|
|
||||||
@ -15,6 +16,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include "utils/assert.hpp"
|
#include "utils/assert.hpp"
|
||||||
|
#include "servers.hpp"
|
||||||
|
|
||||||
namespace dropshell
|
namespace dropshell
|
||||||
{
|
{
|
||||||
@ -43,8 +45,8 @@ namespace dropshell
|
|||||||
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,11 +55,13 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
} install_command_register;
|
} install_command_register;
|
||||||
|
|
||||||
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// install service over ssh : SHARED COMMAND
|
// install service over ssh : SHARED COMMAND
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
bool install_service(const std::string &server, const std::string &service, bool silent)
|
bool install_service(const std::string &server, const std::string &service)
|
||||||
{
|
{
|
||||||
LocalServiceInfo service_info = get_service_info(server, service);
|
LocalServiceInfo service_info = get_service_info(server, service);
|
||||||
if (!SIvalid(service_info))
|
if (!SIvalid(service_info))
|
||||||
@ -104,7 +108,7 @@ namespace dropshell
|
|||||||
debug << "Copying: [LOCAL] " << tinfo.local_template_path() << std::endl
|
debug << "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 (!shared_commands::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, false))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
std::cerr << "Failed to copy template files using rsync" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -114,7 +118,7 @@ namespace dropshell
|
|||||||
debug << "Copying: [LOCAL] " << localpath::service(server, service) << std::endl
|
debug << "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 (!shared_commands::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, false))
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
std::cerr << "Failed to copy service files using rsync" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -123,7 +127,7 @@ namespace dropshell
|
|||||||
// Run install script
|
// Run install script
|
||||||
{
|
{
|
||||||
info << "Running " << service_info.template_name << " install script on " << server << "..." << std::endl;
|
info << "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", {}, false, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
// print health tick
|
// print health tick
|
||||||
@ -131,6 +135,7 @@ namespace dropshell
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// update_dropshell
|
// update_dropshell
|
||||||
@ -156,6 +161,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();
|
||||||
@ -199,8 +206,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,20 +218,22 @@ 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()
|
||||||
{
|
{
|
||||||
|
maketitle("Installing dropshell agent on this computer...");
|
||||||
|
|
||||||
std::vector<std::filesystem::path> paths = {
|
std::vector<std::filesystem::path> paths = {
|
||||||
gConfig().get_local_template_cache_path(),
|
gConfig().get_local_template_cache_path(),
|
||||||
gConfig().get_local_backup_path(),
|
gConfig().get_local_backup_path(),
|
||||||
@ -236,46 +245,20 @@ namespace dropshell
|
|||||||
for (auto &p : paths)
|
for (auto &p : paths)
|
||||||
if (!std::filesystem::exists(p))
|
if (!std::filesystem::exists(p))
|
||||||
{
|
{
|
||||||
std::cout << "Creating directory: " << p << std::endl;
|
info << "Creating directory: " << p << std::endl;
|
||||||
std::filesystem::create_directories(p);
|
std::filesystem::create_directories(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// download bb64 for the host architecture.
|
// create the agent-local directory.
|
||||||
if (!std::filesystem::exists(localpath::agent() + "bb64"))
|
recreate_agent_local::recreate_tree(localpath::agent());
|
||||||
{
|
|
||||||
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;
|
// run the local agent installer.
|
||||||
recreate_agent::recreate_tree(localpath::files_for_remote_agent());
|
execute_local_command(localpath::agent(), "agent-install.sh",{}, nullptr, cMode::Defaults | cMode::NoBB64);
|
||||||
|
|
||||||
return 0;
|
// create the agent-remote directory.
|
||||||
}
|
info << "Creating local files to copy to remote agents..." << std::endl;
|
||||||
|
recreate_agent_remote::recreate_tree(localpath::files_for_remote_agent());
|
||||||
|
|
||||||
int install_host()
|
|
||||||
{
|
|
||||||
// update dropshell.
|
|
||||||
// install the local dropshell agent.
|
|
||||||
|
|
||||||
int rval = update_dropshell();
|
|
||||||
if (rval != 0)
|
|
||||||
return rval;
|
|
||||||
|
|
||||||
rval = install_local_agent();
|
|
||||||
if (rval != 0)
|
|
||||||
return rval;
|
|
||||||
|
|
||||||
std::cout << "Installation complete." << std::endl;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,22 +287,9 @@ namespace dropshell
|
|||||||
shared_commands::rsync_tree_to_remote(localpath::files_for_remote_agent(), agent_path, server_env, false);
|
shared_commands::rsync_tree_to_remote(localpath::files_for_remote_agent(), 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;
|
||||||
@ -330,6 +300,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
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
@ -362,7 +361,7 @@ namespace dropshell
|
|||||||
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)
|
||||||
{
|
{
|
||||||
if (!install_service(server, service.service_name, false))
|
if (!shared_commands::install_service(server, service.service_name))
|
||||||
okay = false;
|
okay = false;
|
||||||
}
|
}
|
||||||
return okay ? 0 : 1;
|
return okay ? 0 : 1;
|
||||||
@ -370,7 +369,7 @@ namespace dropshell
|
|||||||
else
|
else
|
||||||
{ // install the specific service.
|
{ // install the specific service.
|
||||||
std::string service = safearg(ctx.args, 1);
|
std::string service = safearg(ctx.args, 1);
|
||||||
return install_service(server, service, false) ? 0 : 1;
|
return shared_commands::install_service(server, service) ? 0 : 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,16 +10,18 @@
|
|||||||
|
|
||||||
#include "utils/assert.hpp"
|
#include "utils/assert.hpp"
|
||||||
|
|
||||||
namespace dropshell {
|
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 = {"nuke"};
|
||||||
|
|
||||||
// Static registration
|
// Static registration
|
||||||
struct NukeCommandRegister {
|
struct NukeCommandRegister
|
||||||
NukeCommandRegister() {
|
{
|
||||||
CommandRegistry::instance().register_command({
|
NukeCommandRegister()
|
||||||
nuke_name_list,
|
{
|
||||||
|
CommandRegistry::instance().register_command({nuke_name_list,
|
||||||
nuke_handler,
|
nuke_handler,
|
||||||
shared_commands::std_autocomplete,
|
shared_commands::std_autocomplete,
|
||||||
false, // hidden
|
false, // hidden
|
||||||
@ -41,12 +43,14 @@ struct NukeCommandRegister {
|
|||||||
both on the dropshell host and on the remote server.
|
both on the dropshell host and on the remote server.
|
||||||
|
|
||||||
Use with caution!
|
Use with caution!
|
||||||
)"
|
)"});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} nuke_command_register;
|
} nuke_command_register;
|
||||||
|
|
||||||
int nuke_one(std::string server, std::string service)
|
namespace shared_commands
|
||||||
|
{
|
||||||
|
|
||||||
|
bool nuke_service(const std::string &server, const std::string &service)
|
||||||
{
|
{
|
||||||
server_env_manager server_env(server);
|
server_env_manager server_env(server);
|
||||||
|
|
||||||
@ -57,7 +61,7 @@ int nuke_one(std::string server, std::string service)
|
|||||||
|
|
||||||
service_info = get_service_info(server, service);
|
service_info = get_service_info(server, service);
|
||||||
if (!SIvalid(service_info))
|
if (!SIvalid(service_info))
|
||||||
error << "Warning: Invalid service: " << service << std::endl;
|
error << "Invalid service: " << service << std::endl;
|
||||||
|
|
||||||
if (server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
if (server_env.check_remote_dir_exists(remotepath::service(server, service)))
|
||||||
{
|
{
|
||||||
@ -107,8 +111,9 @@ int nuke_one(std::string server, std::string service)
|
|||||||
|
|
||||||
info << "Nuked service " << service << " on server " << server << std::endl;
|
info << "Nuked service " << service << " on server " << server << std::endl;
|
||||||
|
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
int nuke_handler(const CommandContext &ctx)
|
int nuke_handler(const CommandContext &ctx)
|
||||||
{
|
{
|
||||||
@ -135,14 +140,14 @@ int nuke_handler(const CommandContext &ctx)
|
|||||||
if (entry.is_directory() && entry.path().filename().string().find(".") != 0)
|
if (entry.is_directory() && entry.path().filename().string().find(".") != 0)
|
||||||
{
|
{
|
||||||
std::string service_name = entry.path().filename().string();
|
std::string service_name = entry.path().filename().string();
|
||||||
rval |= nuke_one(server, service_name);
|
rval |= (shared_commands::nuke_service(server, service_name) ? 0 : 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rval;
|
return rval;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return nuke_one(server, service);
|
return (shared_commands::nuke_service(server, service) ? 0 : 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
265
source/src/commands/restoredata.cpp
Normal file
265
source/src/commands/restoredata.cpp
Normal file
@ -0,0 +1,265 @@
|
|||||||
|
#include <unistd.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "utils/output.hpp"
|
||||||
|
#include "utils/assert.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
#include "command_registry.hpp"
|
||||||
|
#include "config.hpp"
|
||||||
|
#include "services.hpp"
|
||||||
|
#include "servers.hpp"
|
||||||
|
#include "server_env_manager.hpp"
|
||||||
|
#include "templates.hpp"
|
||||||
|
#include "utils/directories.hpp"
|
||||||
|
#include "shared_commands.hpp"
|
||||||
|
|
||||||
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
int restoredata_handler(const CommandContext &ctx);
|
||||||
|
|
||||||
|
void restoredata_autocomplete(const CommandContext &ctx);
|
||||||
|
|
||||||
|
static std::vector<std::string> restoredata_name_list = {"restoredata", "rd", "restore", "rest"};
|
||||||
|
|
||||||
|
// Static registration
|
||||||
|
struct RestoreDataCommandRegister
|
||||||
|
{
|
||||||
|
RestoreDataCommandRegister()
|
||||||
|
{
|
||||||
|
CommandRegistry::instance().register_command({restoredata_name_list,
|
||||||
|
restoredata_handler,
|
||||||
|
restoredata_autocomplete,
|
||||||
|
false, // hidden
|
||||||
|
true, // requires_config
|
||||||
|
true, // requires_install
|
||||||
|
3, // min_args (after command)
|
||||||
|
3, // max_args (after command)
|
||||||
|
"restoredata SERVER SERVICE BACKUP_FILE|latest",
|
||||||
|
"Restore data for a service on a server, overwriting the existing data.",
|
||||||
|
// heredoc
|
||||||
|
R"(
|
||||||
|
restoredata SERVER SERVICE BACKUP_FILE Restore data to a service on a server. Destructive.
|
||||||
|
restoredata SERVER SERVICE latest Restore the latest backup for the given service.
|
||||||
|
|
||||||
|
Note: This command will not create any service configuration, you need
|
||||||
|
to have a valid service installed first.
|
||||||
|
The backup file must be in the local backups directory.
|
||||||
|
|
||||||
|
WARNING: This will permanently overwrite the service's data on the remote server!
|
||||||
|
)"});
|
||||||
|
}
|
||||||
|
} restoredata_command_register;
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
|
{
|
||||||
|
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<shared_commands::cBackupFileName> backups;
|
||||||
|
for (const auto &entry : std::filesystem::directory_iterator(local_backups_dir))
|
||||||
|
{
|
||||||
|
if (!entry.is_regular_file())
|
||||||
|
continue;
|
||||||
|
std::string filename = entry.path().filename().string();
|
||||||
|
shared_commands::cBackupFileName backup_details(filename);
|
||||||
|
if (backup_details.is_valid())
|
||||||
|
if (match_service.empty() || backup_details.get_service() == match_service)
|
||||||
|
if (match_template_name.empty() || backup_details.get_template_name() == match_template_name)
|
||||||
|
backups.push_back(backup_details);
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort backups by datetime
|
||||||
|
std::sort(backups.begin(), backups.end(), [](const shared_commands::cBackupFileName &a, const shared_commands::cBackupFileName &b)
|
||||||
|
{ return a.get_datetime() > b.get_datetime(); });
|
||||||
|
|
||||||
|
return backups;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int restoredata_handler(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
ASSERT(ctx.args.size() == 3, "Invalid number of arguments");
|
||||||
|
std::string server = ctx.args[0];
|
||||||
|
std::string service = ctx.args[1];
|
||||||
|
std::string backup_arg = ctx.args[2];
|
||||||
|
|
||||||
|
server_env_manager server_env(server);
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
error << "Server " << server << " is not valid" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
LocalServiceInfo service_info = get_service_info(server, service);
|
||||||
|
if (!SIvalid(service_info))
|
||||||
|
{
|
||||||
|
error << "Service " << service << " is not valid" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<shared_commands::cBackupFileName> backup_details;
|
||||||
|
if (backup_arg == "latest")
|
||||||
|
{ // special case.
|
||||||
|
std::vector<shared_commands::cBackupFileName> backups = get_backup_files(server, service, service_info.template_name); // this service only (and also match template in case something changed there!).
|
||||||
|
if (backups.empty())
|
||||||
|
{
|
||||||
|
error << "No backups found for " << server << "/" << service << std::endl;
|
||||||
|
debug << "Template also has to match with the service template: " << service_info.template_name << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
backup_details = backups[0];
|
||||||
|
} else {
|
||||||
|
backup_details = shared_commands::cBackupFileName(backup_arg);
|
||||||
|
if (!backup_details->is_valid())
|
||||||
|
{
|
||||||
|
error << "Invalid backup file: " << backup_arg << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT(backup_details.has_value() && backup_details->is_valid(), "Invalid backup file.");
|
||||||
|
|
||||||
|
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 = gConfig().get_local_backup_path();
|
||||||
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
|
{
|
||||||
|
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_details->get_filename()).string();
|
||||||
|
if (!std::filesystem::exists(local_backup_file_path))
|
||||||
|
{
|
||||||
|
error << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (backup_details->get_template_name() != service_info.template_name)
|
||||||
|
{
|
||||||
|
error << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
||||||
|
info << "Backup template: " << backup_details->get_template_name() << std::endl;
|
||||||
|
info << "Service template: " << service_info.template_name << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
warning << "*** ALL DATA FOR " << server << "/" << service << " WILL BE OVERWRITTEN! ***" << std::endl;
|
||||||
|
|
||||||
|
// run the restore script
|
||||||
|
info << "OK, here goes..." << std::endl;
|
||||||
|
|
||||||
|
{ // backup existing service
|
||||||
|
info << "1) Backing up old service... " << std::endl;
|
||||||
|
if (!shared_commands::backupdata_service(server, service))
|
||||||
|
{
|
||||||
|
error << "Backup failed, restore aborted." << std::endl;
|
||||||
|
info << "You can try using dropshell install " << server << " " << service << " to install the service afresh." << std::endl;
|
||||||
|
info << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
info << "Backup complete." << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // nuke the old service
|
||||||
|
info << "2) Nuking old service..." << std::endl;
|
||||||
|
if (!shared_commands::nuke_service(server, service))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create the new service
|
||||||
|
info << "3) Creating new service..." << std::endl;
|
||||||
|
if (!shared_commands::create_service(server, service_info.template_name, service))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // installing fresh service
|
||||||
|
info << "4) Install of fresh service..." << std::endl;
|
||||||
|
if (!shared_commands::install_service(server, service))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // restore service from backup
|
||||||
|
info << "5) Restoring service data from backup..." << std::endl;
|
||||||
|
std::string remote_backups_dir = remotepath::backups(server);
|
||||||
|
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
|
||||||
|
if (!shared_commands::scp_file_to_remote(server_env, local_backup_file_path, remote_backup_file_path, false))
|
||||||
|
{
|
||||||
|
error << "Failed to copy backup file from local to server" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
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()}});
|
||||||
|
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{ // healthcheck the service
|
||||||
|
info << "5) Healthchecking service..." << std::endl;
|
||||||
|
std::string green_tick = "\033[32m✓\033[0m";
|
||||||
|
std::string red_cross = "\033[31m✗\033[0m";
|
||||||
|
bool healthy = (server_env.run_remote_template_command(service, "status", {}, false, {}));
|
||||||
|
info << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void restoredata_autocomplete(const CommandContext &ctx)
|
||||||
|
{
|
||||||
|
shared_commands::std_autocomplete(ctx);
|
||||||
|
if (ctx.args.size() == 2) // next arg is the backup file
|
||||||
|
{
|
||||||
|
std::string server = ctx.args[0];
|
||||||
|
std::string service = ctx.args[1];
|
||||||
|
|
||||||
|
LocalServiceInfo service_info = get_service_info(server, service);
|
||||||
|
if (!SIvalid(service_info))
|
||||||
|
{
|
||||||
|
error << "Service " << service << " is not valid" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string template_name = service_info.template_name;
|
||||||
|
|
||||||
|
std::vector<shared_commands::cBackupFileName> backups = get_backup_files(server, "", template_name); // any service, but must match template.
|
||||||
|
|
||||||
|
// print most recent backup for each {host,service} pair
|
||||||
|
std::map<std::string, std::string> unique_backups;
|
||||||
|
for (const auto &backup : backups)
|
||||||
|
{
|
||||||
|
std::string key = backup.get_server() + "-" + backup.get_service();
|
||||||
|
if (unique_backups.find(key) == unique_backups.end())
|
||||||
|
unique_backups[key] = backup.get_filename();
|
||||||
|
}
|
||||||
|
for (const auto &[key, value] : unique_backups)
|
||||||
|
rawout << value << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace dropshell
|
@ -120,10 +120,8 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string output;
|
std::string output;
|
||||||
if (!execute_ssh_command(env.get_SSH_INFO(), sCommand(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)}}),
|
||||||
"./_allservicesstatus.sh",
|
cMode::Silent,
|
||||||
{{"HOST_NAME", server_name}, {"SERVER", server_name}, {"AGENT_PATH", remotepath::agent(server_name)}}),
|
|
||||||
cMode::CaptureOutput | cMode::Silent,
|
|
||||||
&output))
|
&output))
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
@ -167,8 +165,6 @@ namespace dropshell
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// healthtick : SHARED COMMAND
|
// healthtick : SHARED COMMAND
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
@ -207,7 +203,6 @@ namespace dropshell
|
|||||||
return ":error:";
|
return ":error:";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// is_healthy : SHARED COMMAND
|
// is_healthy : SHARED COMMAND
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
@ -237,7 +232,6 @@ namespace dropshell
|
|||||||
return HealthStatus::HEALTHY;
|
return HealthStatus::HEALTHY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// healthmark : SHARED COMMAND
|
// healthmark : SHARED COMMAND
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
@ -247,6 +241,91 @@ namespace dropshell
|
|||||||
return HealthStatus2String(status);
|
return HealthStatus2String(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// cBackupFileName : SHARED CLASS
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
cBackupFileName::cBackupFileName(const std::string &server, const std::string &service, const std::string &template_name)
|
||||||
|
{
|
||||||
|
mServer = server;
|
||||||
|
mService = service;
|
||||||
|
mTemplateName = template_name;
|
||||||
|
// Get current datetime for backup filename
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto time = std::chrono::system_clock::to_time_t(now);
|
||||||
|
std::stringstream datetime;
|
||||||
|
datetime << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
|
||||||
|
mDatetime = datetime.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
cBackupFileName::cBackupFileName(const std::string &filename)
|
||||||
|
{
|
||||||
|
// Parse the filename according to the format:
|
||||||
|
// server + magic_string() + template_name + magic_string() + service + magic_string() + datetime + ".tgz"
|
||||||
|
std::string name = filename;
|
||||||
|
if (name.size() > 4 && name.substr(name.size() - 4) == ".tgz")
|
||||||
|
name = name.substr(0, name.size() - 4);
|
||||||
|
std::string sep = magic_string();
|
||||||
|
size_t first = name.find(sep);
|
||||||
|
size_t second = name.find(sep, first + sep.size());
|
||||||
|
size_t third = name.find(sep, second + sep.size());
|
||||||
|
if (first == std::string::npos || second == std::string::npos || third == std::string::npos)
|
||||||
|
{
|
||||||
|
mServer = mService = mTemplateName = mDatetime = "";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mServer = name.substr(0, first);
|
||||||
|
mTemplateName = name.substr(first + sep.size(), second - (first + sep.size()));
|
||||||
|
mService = name.substr(second + sep.size(), third - (second + sep.size()));
|
||||||
|
mDatetime = name.substr(third + sep.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string cBackupFileName::get_filename() const
|
||||||
|
{
|
||||||
|
return mServer + magic_string() + mTemplateName + magic_string() + mService + magic_string() + mDatetime + ".tgz";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string cBackupFileName::get_server() const { return mServer; }
|
||||||
|
std::string cBackupFileName::get_service() const { return mService; }
|
||||||
|
std::string cBackupFileName::get_template_name() const { return mTemplateName; }
|
||||||
|
std::string cBackupFileName::get_datetime() const { return mDatetime; }
|
||||||
|
|
||||||
|
bool cBackupFileName::is_valid() const
|
||||||
|
{
|
||||||
|
// All fields must be non-empty, and none may contain the magic string
|
||||||
|
return !mServer.empty() && !mService.empty() && !mTemplateName.empty() && !mDatetime.empty() &&
|
||||||
|
!has_magic_string(mServer) && !has_magic_string(mService) && !has_magic_string(mTemplateName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// scp_file_to_remote : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
bool scp_file_to_remote(const server_env_manager &server_env, const std::string &local_path, const std::string &remote_path, bool silent)
|
||||||
|
{
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
error << "Invalid server environment" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ASSERT(!remote_path.empty() && !local_path.empty(), "Remote or local path not specified. Can't scp.");
|
||||||
|
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + quote(local_path) + " " + server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||||
|
return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
// scp_file_from_remote : SHARED COMMAND
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
bool scp_file_from_remote(const server_env_manager &server_env, const std::string &remote_path, const std::string &local_path, bool silent)
|
||||||
|
{
|
||||||
|
if (!server_env.is_valid())
|
||||||
|
{
|
||||||
|
error << "Invalid server environment" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
ASSERT(!remote_path.empty() && !local_path.empty(), "Remote or local path not specified. Can't scp.");
|
||||||
|
std::string scp_cmd = "scp -P " + server_env.get_SSH_PORT() + " " + server_env.get_SSH_USER() + "@" + server_env.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||||
|
return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace shared_commands
|
} // namespace shared_commands
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -57,6 +57,44 @@ namespace dropshell
|
|||||||
void std_autocomplete(const CommandContext &ctx);
|
void std_autocomplete(const CommandContext &ctx);
|
||||||
void std_autocomplete_allowall(const CommandContext &ctx);
|
void std_autocomplete_allowall(const CommandContext &ctx);
|
||||||
|
|
||||||
|
class cBackupFileName
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
cBackupFileName(const std::string &server, const std::string &service, const std::string &template_name);
|
||||||
|
cBackupFileName(const std::string &filename);
|
||||||
|
|
||||||
|
std::string get_filename() const;
|
||||||
|
std::string get_server() const;
|
||||||
|
std::string get_service() const;
|
||||||
|
std::string get_template_name() const;
|
||||||
|
std::string get_datetime() const;
|
||||||
|
|
||||||
|
bool is_valid() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string mServer;
|
||||||
|
std::string mService;
|
||||||
|
std::string mTemplateName;
|
||||||
|
std::string mDatetime;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool scp_file_to_remote(const server_env_manager &server_env, const std::string &local_path, const std::string &remote_path, bool silent);
|
||||||
|
bool scp_file_from_remote(const server_env_manager &server_env, const std::string &remote_path, const std::string &local_path, bool silent);
|
||||||
|
|
||||||
|
// defined in backupdata.cpp, used by restoredata.cpp.
|
||||||
|
bool backupdata_service(const std::string& server, const std::string& service);
|
||||||
|
|
||||||
|
// defined in uninstall.cpp
|
||||||
|
bool uninstall_service(const std::string &server, const std::string &service);
|
||||||
|
|
||||||
|
// defined in nuke.cpp
|
||||||
|
bool nuke_service(const std::string &server, const std::string &service);
|
||||||
|
|
||||||
|
// defined in install.cpp
|
||||||
|
bool install_service(const std::string &server, const std::string &service);
|
||||||
|
|
||||||
|
// defined in create-service.cpp
|
||||||
|
bool create_service(const std::string &server_name, const std::string &template_name, const std::string &service_name);
|
||||||
|
|
||||||
} // namespace shared_commands
|
} // namespace shared_commands
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
||||||
|
@ -41,9 +41,9 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
} uninstall_command_register;
|
} uninstall_command_register;
|
||||||
|
|
||||||
bool uninstall_service(const std::string &server, const std::string &service, bool silent = false)
|
namespace shared_commands {
|
||||||
|
bool uninstall_service(const std::string &server, const std::string &service)
|
||||||
{
|
{
|
||||||
if (!silent)
|
|
||||||
maketitle("Uninstalling " + service + " on " + server);
|
maketitle("Uninstalling " + service + " on " + server);
|
||||||
|
|
||||||
server_env_manager server_env(server);
|
server_env_manager server_env(server);
|
||||||
@ -62,24 +62,22 @@ 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 (!server_env.run_remote_template_command(service, "uninstall", {}, silent, {}))
|
if (!server_env.run_remote_template_command(service, "uninstall", {}, false, {}))
|
||||||
if (!silent)
|
|
||||||
warning << "Uninstall script failed, but continuing with directory removal" << std::endl;
|
warning << "Uninstall script failed, but continuing with directory removal" << std::endl;
|
||||||
|
|
||||||
// 4. Remove the service directory from the server, running in a docker container as root.
|
// 4. Remove the service directory from the server, running in a docker container as root.
|
||||||
if (server_env.remove_remote_dir(remotepath::service(server, service), silent))
|
if (server_env.remove_remote_dir(remotepath::service(server, service), false))
|
||||||
{
|
{
|
||||||
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
ASSERT(!server_env.check_remote_dir_exists(remotepath::service(server, service)), "Service directory still found on server after uninstall");
|
||||||
if (!silent)
|
|
||||||
info << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
info << "Removed remote service directory " << remotepath::service(server, service) << std::endl;
|
||||||
}
|
}
|
||||||
else if (!silent)
|
else
|
||||||
warning << "Failed to remove remote service directory" << std::endl;
|
warning << "Failed to remove remote service directory" << std::endl;
|
||||||
|
|
||||||
if (!silent)
|
|
||||||
info << "Completed service " << service << " uninstall on " << server << std::endl;
|
info << "Completed service " << service << " uninstall on " << server << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
} // namespace shared_commands
|
||||||
|
|
||||||
int uninstall_handler(const CommandContext &ctx)
|
int uninstall_handler(const CommandContext &ctx)
|
||||||
{
|
{
|
||||||
@ -98,14 +96,14 @@ namespace dropshell
|
|||||||
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)
|
||||||
{
|
{
|
||||||
if (!uninstall_service(server, service.service_name))
|
if (!shared_commands::uninstall_service(server, service.service_name))
|
||||||
okay = false;
|
okay = false;
|
||||||
}
|
}
|
||||||
return okay ? 0 : 1;
|
return okay ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string service = safearg(ctx.args, 1);
|
std::string service = safearg(ctx.args, 1);
|
||||||
return uninstall_service(server, service) ? 0 : 1;
|
return shared_commands::uninstall_service(server, service) ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -6,6 +6,9 @@
|
|||||||
#include "utils/json.hpp"
|
#include "utils/json.hpp"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include "utils/execute.hpp"
|
#include "utils/execute.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
|
|
||||||
@ -150,7 +153,7 @@ std::vector<std::string> config::get_local_server_definition_paths() {
|
|||||||
if (path.is_string() && !path.empty())
|
if (path.is_string() && !path.empty())
|
||||||
paths.push_back(path);
|
paths.push_back(path);
|
||||||
else
|
else
|
||||||
std::cerr << "Warning: Invalid server definition path: " << path << std::endl;
|
warning << "Invalid server definition path: " << path << std::endl;
|
||||||
}
|
}
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,7 +91,6 @@ namespace fs = std::filesystem;
|
|||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
static const std::string magic_string = "-_-";
|
|
||||||
|
|
||||||
service_runner::service_runner(const std::string& server_name, const std::string& service_name) :
|
service_runner::service_runner(const std::string& server_name, const std::string& service_name) :
|
||||||
mServerEnv(server_name), mServer(server_name), mService(service_name), mValid(false)
|
mServerEnv(server_name), mServer(server_name), mService(service_name), mValid(false)
|
||||||
@ -122,12 +121,12 @@ bool service_runner::nuke(bool silent)
|
|||||||
|
|
||||||
std::string remote_service_path = remotepath::service(mServer, mService);
|
std::string remote_service_path = remotepath::service(mServer, mService);
|
||||||
|
|
||||||
std::cout << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
info << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
||||||
|
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
std::cout << "There's nothing left on the remote server." << std::endl;
|
info << "There's nothing left on the remote server." << std::endl;
|
||||||
std::cout << "You can remove the local files with:" << std::endl;
|
info << "You can remove the local files with:" << std::endl;
|
||||||
std::cout << " rm -rf " << localpath::service(mServer,mService) << std::endl;
|
info << " rm -rf " << localpath::service(mServer,mService) << std::endl;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -136,19 +135,19 @@ bool service_runner::fullnuke()
|
|||||||
{
|
{
|
||||||
if (!nuke(true))
|
if (!nuke(true))
|
||||||
{
|
{
|
||||||
std::cerr << "Warning: Nuke script failed, aborting fullnuke!" << std::endl;
|
warning << "Nuke script failed, aborting." << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string local_service_path = mServiceInfo.local_service_path;
|
std::string local_service_path = mServiceInfo.local_service_path;
|
||||||
if (local_service_path.empty() || !fs::exists(local_service_path)) {
|
if (local_service_path.empty() || !fs::exists(local_service_path)) {
|
||||||
std::cerr << "Error: Service directory not found: " << local_service_path << std::endl;
|
error << "Service directory not found: " << local_service_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string rm_cmd = "rm -rf " + quote(local_service_path);
|
std::string rm_cmd = "rm -rf " + quote(local_service_path);
|
||||||
if (!execute_local_command("", rm_cmd, {}, nullptr, cMode::Silent)) {
|
if (!execute_local_command("", rm_cmd, {}, nullptr, cMode::Silent)) {
|
||||||
std::cerr << "Failed to remove service directory" << std::endl;
|
error << "Failed to remove service directory" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,7 +297,7 @@ bool service_runner::restore(std::string backup_file, bool silent)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// split the backup filename into parts based on the magic string
|
// split the backup filename into parts based on the magic string
|
||||||
std::vector<std::string> parts = dropshell::split(backup_file, magic_string);
|
std::vector<std::string> parts = dropshell::split(backup_file, "-_-");
|
||||||
if (parts.size() != 4) {
|
if (parts.size() != 4) {
|
||||||
std::cerr << "Error: Backup file format is incompatible, - in one of the names?" << std::endl;
|
std::cerr << "Error: Backup file format is incompatible, - in one of the names?" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
@ -383,12 +382,6 @@ bool service_runner::restore(std::string backup_file, bool silent)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool name_breaks_backups(std::string name)
|
|
||||||
{
|
|
||||||
// if name contains -_-, return true
|
|
||||||
return name.find("-_-") != std::string::npos;
|
|
||||||
}
|
|
||||||
|
|
||||||
// backup the service over ssh, using the credentials from server.env (via server_env.hpp)
|
// backup the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||||
// 1. run backup.sh on the server
|
// 1. run backup.sh on the server
|
||||||
// 2. create a backup file with format server-service-datetime.tgz
|
// 2. create a backup file with format server-service-datetime.tgz
|
||||||
@ -452,17 +445,18 @@ bool service_runner::backup(bool silent) {
|
|||||||
std::stringstream datetime;
|
std::stringstream datetime;
|
||||||
datetime << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
|
datetime << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
|
||||||
|
|
||||||
if (name_breaks_backups(mServer)) {std::cerr << "Error: Server name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
|
||||||
if (name_breaks_backups(mService)) {std::cerr << "Error: Service name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
|
||||||
if (name_breaks_backups(service_info.template_name)) {std::cerr << "Error: Service template name contains invalid character sequence ( -_- ) that would break backup naming scheme" << std::endl; return 1;}
|
|
||||||
|
|
||||||
// Construct backup filename
|
// Construct backup filename
|
||||||
std::string backup_filename = mServer + magic_string + service_info.template_name + magic_string + mService + magic_string + datetime.str() + ".tgz";
|
shared_commands::cBackupFileName backup_filename_construction(mServer, mService, service_info.template_name);
|
||||||
|
if (!backup_filename_construction.is_valid()) {
|
||||||
|
std::cerr << "Invalid backup filename" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::string backup_filename = backup_filename_construction.get_filename();
|
||||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_filename;
|
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_filename;
|
||||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_filename).string();
|
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_filename).string();
|
||||||
|
|
||||||
// assert that the backup filename is valid - -_- appears exactly 3 times in local_backup_file_path.
|
// assert that the backup filename is valid - -_- appears exactly 3 times in local_backup_file_path.
|
||||||
ASSERT(3 == count_substring(magic_string, local_backup_file_path), "Invalid backup filename");
|
ASSERT(3 == count_substring("-_-", local_backup_file_path), "Invalid backup filename");
|
||||||
|
|
||||||
{ // Run backup script
|
{ // Run backup script
|
||||||
shared_commands::cRemoteTempFolder remote_temp_folder(mServerEnv);
|
shared_commands::cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||||
@ -501,7 +495,7 @@ std::string service_runner::get_latest_backup_file(const std::string& server, co
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Build the expected prefix for backup files
|
// Build the expected prefix for backup files
|
||||||
std::string prefix = server + magic_string + info.template_name + magic_string + service + magic_string;
|
std::string prefix = server + "-_-" + info.template_name + "-_-" + service + "-_-";
|
||||||
std::string latest_file;
|
std::string latest_file;
|
||||||
std::string latest_datetime;
|
std::string latest_datetime;
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ std::vector<LocalServiceInfo> get_server_services_info(const std::string& server
|
|||||||
if (!service.local_service_path.empty())
|
if (!service.local_service_path.empty())
|
||||||
services.push_back(service);
|
services.push_back(service);
|
||||||
else
|
else
|
||||||
std::cerr << "Warning: Failed to get service info for " << dirname << " on server " << server_name << std::endl;
|
warning << "Failed to get service info for " << dirname << " on server " << server_name << std::endl;
|
||||||
}
|
}
|
||||||
} // end of for
|
} // end of for
|
||||||
}
|
}
|
||||||
@ -188,7 +188,7 @@ bool get_all_service_env_vars(const std::string &server_name, const std::string
|
|||||||
all_env_vars.merge(env_vars);
|
all_env_vars.merge(env_vars);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout << "Warning: Expected environment file not found: " << file << std::endl;
|
warning << "Expected environment file not found: " << file << std::endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load environment files
|
// Load environment files
|
||||||
@ -198,11 +198,10 @@ bool get_all_service_env_vars(const std::string &server_name, const std::string
|
|||||||
// determine template name.
|
// determine template name.
|
||||||
auto it = all_env_vars.find("TEMPLATE");
|
auto it = all_env_vars.find("TEMPLATE");
|
||||||
if (it == all_env_vars.end()) {
|
if (it == all_env_vars.end()) {
|
||||||
std::cerr << std::endl << std::endl;
|
error << "TEMPLATE variable not defined in service " << service_name << " on server " << server_name << std::endl;
|
||||||
std::cerr << "Error: TEMPLATE variable not defined in service " << service_name << " on server " << server_name << std::endl;
|
info << "The TEMPLATE variable is required to determine the template name." << std::endl;
|
||||||
std::cerr << "The TEMPLATE variable is required to determine the template name." << std::endl;
|
info << "Please check the service.env file and the .template_info.env file in:" << std::endl;
|
||||||
std::cerr << "Please check the service.env file and the .template_info.env file in:" << std::endl;
|
info << " " << localpath::service(server_name, service_name) << std::endl << std::endl;
|
||||||
std::cerr << " " << localpath::service(server_name, service_name) << std::endl << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
template_info tinfo = gTemplateManager().get_template_info(it->second);
|
template_info tinfo = gTemplateManager().get_template_info(it->second);
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
#include "directories.hpp"
|
#include "directories.hpp"
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "server_env_manager.hpp"
|
#include "server_env_manager.hpp"
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
@ -78,7 +81,7 @@ namespace localpath {
|
|||||||
std::filesystem::path homedir_path(homedir);
|
std::filesystem::path homedir_path(homedir);
|
||||||
return fs::canonical(homedir_path).string();
|
return fs::canonical(homedir_path).string();
|
||||||
}
|
}
|
||||||
std::cerr << "Warning: Couldn't determine user directory" << std::endl;
|
warning << "Couldn't determine user directory" << std::endl;
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
} // namespace localpath
|
} // namespace localpath
|
||||||
|
@ -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(localpath::agent()+"/bb64"); // Get the command string
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
|
|
||||||
@ -88,12 +87,16 @@ namespace dropshell
|
|||||||
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;
|
||||||
@ -109,26 +112,30 @@ namespace dropshell
|
|||||||
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(localpath::agent()+"/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)
|
||||||
@ -157,17 +164,18 @@ 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
|
||||||
@ -175,12 +183,8 @@ namespace dropshell
|
|||||||
|
|
||||||
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,6 +212,8 @@ 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 (!bb64path.empty())
|
||||||
|
{
|
||||||
if (!mDir.empty())
|
if (!mDir.empty())
|
||||||
cmdstr += "cd " + quote(mDir) + " && ";
|
cmdstr += "cd " + quote(mDir) + " && ";
|
||||||
|
|
||||||
@ -217,8 +223,16 @@ namespace dropshell
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
@ -10,6 +10,16 @@
|
|||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
|
std::string magic_string() {
|
||||||
|
return "-_-";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool has_magic_string(std::string name)
|
||||||
|
{
|
||||||
|
return name.find(magic_string()) != std::string::npos;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void maketitle(const std::string& title, sColour colour) {
|
void maketitle(const std::string& title, sColour colour) {
|
||||||
colourstream(colour) << std::string(title.length() + 4, '-') << std::endl;
|
colourstream(colour) << std::string(title.length() + 4, '-') << std::endl;
|
||||||
colourstream(colour) << "| " << title << " |" << std::endl;
|
colourstream(colour) << "| " << title << " |" << std::endl;
|
||||||
|
@ -16,6 +16,8 @@ void maketitle(const std::string& title, sColour colour=sColour::INFO);
|
|||||||
|
|
||||||
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);
|
bool replace_line_in_file(const std::string& file_path, const std::string& search_string, const std::string& replacement_line);
|
||||||
|
|
||||||
|
std::string magic_string();
|
||||||
|
bool has_magic_string(std::string name);
|
||||||
|
|
||||||
// utility functions
|
// utility functions
|
||||||
std::string trim(std::string str);
|
std::string trim(std::string str);
|
||||||
|
Reference in New Issue
Block a user