Pipe through temp folder to scripts.
This commit is contained in:
parent
7c9a45edf5
commit
e727fc518f
@ -64,6 +64,7 @@ class server_env_manager {
|
|||||||
std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); }
|
std::string get_SSH_PORT() const { return get_variable("SSH_PORT"); }
|
||||||
std::string get_DROPSHELL_DIR() const { return get_variable("DROPSHELL_DIR"); }
|
std::string get_DROPSHELL_DIR() const { return get_variable("DROPSHELL_DIR"); }
|
||||||
bool is_valid() const { return mValid; }
|
bool is_valid() const { return mValid; }
|
||||||
|
std::string get_server_name() const { return mServerName; }
|
||||||
|
|
||||||
// helper functions
|
// helper functions
|
||||||
public:
|
public:
|
||||||
|
@ -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;
|
std::cerr << "Failed to copy backup file from server" << std::endl;
|
||||||
return false;
|
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
|
// healthcheck the service
|
||||||
std::cout << "3) Healthchecking service..." << std::endl;
|
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 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));
|
ASSERT(3 == count_substring(magic_string, local_backup_file_path));
|
||||||
|
|
||||||
// Run backup script
|
{ // Run backup script
|
||||||
if (!mServerEnv.run_remote_template_command(mService, command, {remote_backup_file_path}, silent)) {
|
cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||||
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
if (!mServerEnv.run_remote_template_command(mService, command, {remote_backup_file_path, remote_temp_folder.path()}, silent)) {
|
||||||
return false;
|
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Copy backup file from server to local
|
// Copy backup file from server to local
|
||||||
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " +
|
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " +
|
||||||
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" +
|
mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" +
|
||||||
quote(remote_backup_file_path) + " " + quote(local_backup_file_path) + (silent ? " > /dev/null 2>&1" : "");
|
quote(remote_backup_file_path) + " " + quote(local_backup_file_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||||
if (!mServerEnv.execute_local_command(scp_cmd)) {
|
if (!mServerEnv.execute_local_command(scp_cmd)) {
|
||||||
std::cerr << "Failed to copy backup file from server" << std::endl;
|
std::cerr << "Failed to copy backup file from server" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
std::cout << "Backup created successfully. Restore with:"<<std::endl;
|
std::cout << "Backup created successfully. Restore with:"<<std::endl;
|
||||||
@ -674,6 +678,27 @@ bool service_runner::backup(bool silent) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cRemoteTempFolder::cRemoteTempFolder(const server_env_manager &server_env) : mServerEnv(server_env)
|
||||||
|
{
|
||||||
|
std::string p = remotepath::temp_files(server_env.get_server_name()) + "/" + random_alphanumeric_string(10);
|
||||||
|
std::string mkdir_cmd = "mkdir -p " + quote(p);
|
||||||
|
if (!server_env.execute_ssh_command(mkdir_cmd))
|
||||||
|
std::cerr << "Failed to create temp directory on server" << std::endl;
|
||||||
|
else
|
||||||
|
mPath = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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
|
} // namespace dropshell
|
@ -102,6 +102,17 @@ class service_runner {
|
|||||||
public:
|
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)
|
// other utility routines (not specific to service_runner)
|
||||||
void interactive_ssh(const std::string & server_name, const std::string & command);
|
void interactive_ssh(const std::string & server_name, const std::string & command);
|
||||||
|
@ -136,6 +136,12 @@ namespace remotepath {
|
|||||||
return (dsp.empty() ? "" : (dsp + "/backups"));
|
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_env(const std::string &server_name, const std::string &service_name)
|
||||||
{
|
{
|
||||||
std::string service_path = service_config(server_name, service_name);
|
std::string service_path = service_config(server_name, service_name);
|
||||||
|
@ -62,6 +62,7 @@ namespace dropshell {
|
|||||||
// remote paths
|
// remote paths
|
||||||
// DROPSHELL_DIR
|
// DROPSHELL_DIR
|
||||||
// |-- backups
|
// |-- backups
|
||||||
|
// |-- temp_files
|
||||||
// |-- services
|
// |-- services
|
||||||
// |-- service name
|
// |-- service name
|
||||||
// |-- config
|
// |-- config
|
||||||
@ -83,6 +84,7 @@ namespace dropshell {
|
|||||||
std::string service_config(const std::string &server_name, const std::string &service_name);
|
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 service_template(const std::string &server_name, const std::string &service_name);
|
||||||
std::string backups(const std::string &server_name);
|
std::string backups(const std::string &server_name);
|
||||||
|
std::string temp_files(const std::string &server_name);
|
||||||
} // namespace remotepath
|
} // namespace remotepath
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------
|
||||||
@ -91,4 +93,5 @@ namespace dropshell {
|
|||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
||||||
|
|
||||||
|
|
||||||
#endif // DIRECTORIES_HPP
|
#endif // DIRECTORIES_HPP
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
void maketitle(const std::string& title) {
|
void maketitle(const std::string& title) {
|
||||||
@ -292,6 +294,18 @@ std::string replace_with_environment_variables_like_bash(std::string str) {
|
|||||||
return result;
|
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) {
|
std::string requote(std::string str) {
|
||||||
return quote(trim(dequote(trim(str))));
|
return quote(trim(dequote(trim(str))));
|
||||||
|
@ -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 replace_with_environment_variables_like_bash(std::string str);
|
||||||
|
|
||||||
|
std::string random_alphanumeric_string(int length);
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -1,6 +1,27 @@
|
|||||||
#!/bin/bash
|
#!/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() {
|
_autocommandrun() {
|
||||||
command="$1"
|
command="$1"
|
||||||
key="$2"
|
key="$2"
|
||||||
@ -13,12 +34,15 @@ _autocommandrun() {
|
|||||||
case "$key" in
|
case "$key" in
|
||||||
volume)
|
volume)
|
||||||
echo "Volume: $value"
|
echo "Volume: $value"
|
||||||
|
_autocommandrun_volume "$command" "$value" "$backup_file" "$temp_path"
|
||||||
;;
|
;;
|
||||||
path)
|
path)
|
||||||
echo "Path: $value"
|
echo "Path: $value"
|
||||||
|
_autocommandrun_path "$command" "$value" "$backup_file" "$temp_path"
|
||||||
;;
|
;;
|
||||||
file)
|
file)
|
||||||
echo "File: $value"
|
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."
|
_die "Unknown key $key passed to auto${command}. We only support volume, path and file."
|
||||||
|
@ -27,16 +27,6 @@ fi
|
|||||||
|
|
||||||
title "Checking template $TEMPLATE"
|
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"
|
SERVICE_NAME="test-$TEMPLATE"
|
||||||
|
|
||||||
title "Creating service"
|
title "Creating service"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user