Pipe through temp folder to scripts.

This commit is contained in:
Your Name 2025-05-05 20:11:36 +12:00
parent 7c9a45edf5
commit e727fc518f
9 changed files with 103 additions and 27 deletions

View File

@ -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:

View File

@ -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:"<<std::endl;
@ -674,6 +678,27 @@ bool service_runner::backup(bool silent) {
return true;
}
cRemoteTempFolder::cRemoteTempFolder(const server_env_manager &server_env) : mServerEnv(server_env)
{
std::string p = remotepath::temp_files(server_env.get_server_name()) + "/" + random_alphanumeric_string(10);
std::string mkdir_cmd = "mkdir -p " + quote(p);
if (!server_env.execute_ssh_command(mkdir_cmd))
std::cerr << "Failed to create temp directory on server" << std::endl;
else
mPath = p;
}
} // namespace dropshell
cRemoteTempFolder::~cRemoteTempFolder()
{
std::string rm_cmd = "rm -rf " + quote(mPath);
mServerEnv.execute_ssh_command(rm_cmd);
}
std::string cRemoteTempFolder::path() const
{
return mPath;
}
} // namespace dropshell

View File

@ -102,6 +102,17 @@ class service_runner {
public:
};
class cRemoteTempFolder {
public:
cRemoteTempFolder(const server_env_manager & server_env); // create a temp folder on the remote server
~cRemoteTempFolder(); // delete the temp folder on the remote server
std::string path() const; // get the path to the temp folder on the remote server
private:
std::string mPath;
const server_env_manager & mServerEnv;
};
// other utility routines (not specific to service_runner)
void interactive_ssh(const std::string & server_name, const std::string & command);

View File

@ -136,6 +136,12 @@ namespace remotepath {
return (dsp.empty() ? "" : (dsp + "/backups"));
}
std::string temp_files(const std::string &server_name)
{
std::string dsp = DROPSHELL_DIR(server_name);
return (dsp.empty() ? "" : (dsp + "/temp_files"));
}
std::string service_env(const std::string &server_name, const std::string &service_name)
{
std::string service_path = service_config(server_name, service_name);

View File

@ -62,6 +62,7 @@ namespace dropshell {
// remote paths
// DROPSHELL_DIR
// |-- backups
// |-- temp_files
// |-- services
// |-- service name
// |-- config
@ -83,12 +84,14 @@ namespace dropshell {
std::string service_config(const std::string &server_name, const std::string &service_name);
std::string service_template(const std::string &server_name, const std::string &service_name);
std::string backups(const std::string &server_name);
std::string temp_files(const std::string &server_name);
} // namespace remotepath
//------------------------------------------------------------------------------------------------
// utility functions
std::string get_parent(const std::string &path);
} // namespace dropshell
#endif // DIRECTORIES_HPP

View File

@ -6,6 +6,8 @@
#include <algorithm>
#include <filesystem>
#include <regex>
#include <random>
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))));

View File

@ -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

View File

@ -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."

View File

@ -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"