From e727fc518ff8881671f7484b5fd969c3bd672009 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 5 May 2025 20:11:36 +1200 Subject: [PATCH] Pipe through temp folder to scripts. --- src/server_env_manager.hpp | 1 + src/service_runner.cpp | 57 +++++++++++++------ src/service_runner.hpp | 11 ++++ src/utils/directories.cpp | 6 ++ src/utils/directories.hpp | 5 +- src/utils/utils.cpp | 14 +++++ src/utils/utils.hpp | 2 + .../dropshell-agent/shared/_autocommands.sh | 24 ++++++++ templates/test_template.sh | 10 ---- 9 files changed, 103 insertions(+), 27 deletions(-) diff --git a/src/server_env_manager.hpp b/src/server_env_manager.hpp index 8e1ac20..e4f4bf8 100644 --- a/src/server_env_manager.hpp +++ b/src/server_env_manager.hpp @@ -64,6 +64,7 @@ class server_env_manager { std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); } std::string get_DROPSHELL_DIR() const { return get_variable("DROPSHELL_DIR"); } bool is_valid() const { return mValid; } + std::string get_server_name() const { return mServerName; } // helper functions public: diff --git a/src/service_runner.cpp b/src/service_runner.cpp index 16b687b..9acb65e 100644 --- a/src/service_runner.cpp +++ b/src/service_runner.cpp @@ -556,8 +556,10 @@ bool service_runner::restore(std::string backup_file, bool silent) std::cerr << "Failed to copy backup file from server" << std::endl; return false; } - mServerEnv.run_remote_template_command(mService, "restore", {remote_backup_file_path}, silent); - } + + cRemoteTempFolder remote_temp_folder(mServerEnv); + mServerEnv.run_remote_template_command(mService, "restore", {remote_backup_file_path, remote_temp_folder.path()}, silent); + } // dtor of remote_temp_folder will clean up the temp folder on the server // healthcheck the service std::cout << "3) Healthchecking service..." << std::endl; @@ -652,20 +654,22 @@ bool service_runner::backup(bool silent) { // 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)); - // Run backup script - if (!mServerEnv.run_remote_template_command(mService, command, {remote_backup_file_path}, silent)) { - std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl; - return false; - } + { // Run backup script + cRemoteTempFolder remote_temp_folder(mServerEnv); + if (!mServerEnv.run_remote_template_command(mService, command, {remote_backup_file_path, remote_temp_folder.path()}, silent)) { + std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl; + return false; + } - // Copy backup file from server to local - std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + - mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + - quote(remote_backup_file_path) + " " + quote(local_backup_file_path) + (silent ? " > /dev/null 2>&1" : ""); - if (!mServerEnv.execute_local_command(scp_cmd)) { - std::cerr << "Failed to copy backup file from server" << std::endl; - return false; - } + // Copy backup file from server to local + std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + + quote(remote_backup_file_path) + " " + quote(local_backup_file_path) + (silent ? " > /dev/null 2>&1" : ""); + if (!mServerEnv.execute_local_command(scp_cmd)) { + std::cerr << "Failed to copy backup file from server" << std::endl; + return false; + } + } // dtor of remote_temp_folder will clean up the temp folder on the server if (!silent) { std::cout << "Backup created successfully. Restore with:"< #include #include +#include + namespace dropshell { void maketitle(const std::string& title) { @@ -292,6 +294,18 @@ std::string replace_with_environment_variables_like_bash(std::string str) { return result; } +std::string random_alphanumeric_string(int length) +{ + static std::mt19937 generator(std::random_device{}()); + static const std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + std::uniform_int_distribution<> distribution(0, chars.size() - 1); + std::string random_string; + for (int i = 0; i < length; ++i) { + random_string += chars[distribution(generator)]; + } + + return random_string; +} std::string requote(std::string str) { return quote(trim(dequote(trim(str)))); diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index ab8ce7e..91c5869 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -38,4 +38,6 @@ int count_substring(const std::string &substring, const std::string &text); std::string replace_with_environment_variables_like_bash(std::string str); +std::string random_alphanumeric_string(int length); + } // namespace dropshell \ No newline at end of file diff --git a/templates/dropshell-agent/shared/_autocommands.sh b/templates/dropshell-agent/shared/_autocommands.sh index c181ccb..bb2e36f 100644 --- a/templates/dropshell-agent/shared/_autocommands.sh +++ b/templates/dropshell-agent/shared/_autocommands.sh @@ -1,6 +1,27 @@ #!/bin/bash +_autocommandrun_volume() { + command="$1" + value="$2" + backup_file="$3" + temp_path="$4" +} + +_autocommandrun_path() { + command="$1" + value="$2" + backup_file="$3" + temp_path="$4" +} + +_autocommandrun_file() { + command="$1" + value="$2" + backup_file="$3" + temp_path="$4" +} + _autocommandrun() { command="$1" key="$2" @@ -13,12 +34,15 @@ _autocommandrun() { case "$key" in volume) echo "Volume: $value" + _autocommandrun_volume "$command" "$value" "$backup_file" "$temp_path" ;; path) echo "Path: $value" + _autocommandrun_path "$command" "$value" "$backup_file" "$temp_path" ;; file) echo "File: $value" + _autocommandrun_file "$command" "$value" "$backup_file" "$temp_path" ;; *) _die "Unknown key $key passed to auto${command}. We only support volume, path and file." diff --git a/templates/test_template.sh b/templates/test_template.sh index afd38d1..38c0e7c 100755 --- a/templates/test_template.sh +++ b/templates/test_template.sh @@ -27,16 +27,6 @@ fi title "Checking template $TEMPLATE" - -HASH1=$(ds hash "$SCRIPT_DIR/$TEMPLATE") -HASH2=$(ds hash "/opt/dropshell/templates/$TEMPLATE") - -if [ "$HASH1" != "$HASH2" ]; then - echo "Template $TEMPLATE is out of date" - echo "Need to run build.sh, and install locally." - exit 1 -fi - SERVICE_NAME="test-$TEMPLATE" title "Creating service"