This commit is contained in:
parent
9063edb45f
commit
270d6ef792
@ -102,7 +102,7 @@ namespace dropshell
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create backups directory locally if it doesn't exist
|
// Create backups directory locally if it doesn't exist
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty())
|
if (local_backups_dir.empty())
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found" << std::endl;
|
error << "Error: Local backups directory not found" << std::endl;
|
||||||
|
@ -238,20 +238,7 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
maketitle("Installing dropshell agent on this computer...");
|
maketitle("Installing dropshell agent on this computer...");
|
||||||
|
|
||||||
std::vector<std::filesystem::path> paths = {
|
localpath::create_directories();
|
||||||
gConfig().get_local_template_cache_path(),
|
|
||||||
gConfig().get_local_backup_path(),
|
|
||||||
gConfig().get_local_tempfiles_path(),
|
|
||||||
localpath::agent()};
|
|
||||||
for (auto &p : gConfig().get_local_server_definition_paths())
|
|
||||||
paths.push_back(p);
|
|
||||||
|
|
||||||
for (auto &p : paths)
|
|
||||||
if (!std::filesystem::exists(p))
|
|
||||||
{
|
|
||||||
info << "Creating directory: " << p << std::endl;
|
|
||||||
std::filesystem::create_directories(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the agent-local directory.
|
// create the agent-local directory.
|
||||||
recreate_agent_local::recreate_tree(localpath::agent());
|
recreate_agent_local::recreate_tree(localpath::agent());
|
||||||
|
@ -57,7 +57,7 @@ namespace dropshell
|
|||||||
|
|
||||||
std::vector<shared_commands::cBackupFileName> get_backup_files(const std::string &server, const std::string &match_service = "", const std::string &match_template_name = "")
|
std::vector<shared_commands::cBackupFileName> get_backup_files(const std::string &server, const std::string &match_service = "", const std::string &match_template_name = "")
|
||||||
{
|
{
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
@ -137,7 +137,7 @@ namespace dropshell
|
|||||||
debug << " Server: " << server << std::endl;
|
debug << " Server: " << server << std::endl;
|
||||||
debug << " Service: " << service << std::endl;
|
debug << " Service: " << service << std::endl;
|
||||||
|
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
|
@ -46,6 +46,15 @@ bool config::load_config() { // load json config file.
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _append(std::vector<std::string> & a, const std::vector<std::string> & b) {
|
||||||
|
if (b.empty())
|
||||||
|
return;
|
||||||
|
if (a.empty())
|
||||||
|
a = b;
|
||||||
|
else
|
||||||
|
a.insert(std::end(a), std::begin(b), std::end(b));
|
||||||
|
}
|
||||||
|
|
||||||
bool config::save_config(bool create_aux_directories)
|
bool config::save_config(bool create_aux_directories)
|
||||||
{
|
{
|
||||||
std::string config_path = localfile::dropshell_json();
|
std::string config_path = localfile::dropshell_json();
|
||||||
@ -61,37 +70,27 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
if (!mIsConfigSet)
|
if (!mIsConfigSet)
|
||||||
{
|
{
|
||||||
std::string homedir = localpath::current_user_home();
|
std::string homedir = localpath::current_user_home();
|
||||||
std::string dropshell_base = homedir + "/.dropshell";
|
std::string dropshell_base = homedir + "/.local/dropshell_files";
|
||||||
mConfig["tempfiles"] = dropshell_base + "/tmp";
|
|
||||||
mConfig["backups"] = dropshell_base + "/backups";
|
|
||||||
|
|
||||||
mConfig["template_cache"] = dropshell_base + "/template_cache";
|
mConfig["server_definition_paths"] = {
|
||||||
mConfig["template_registry_URLs"] = {
|
dropshell_base + "/servers"
|
||||||
"https://templates.dropshell.app"
|
|
||||||
};
|
};
|
||||||
mConfig["template_local_paths"] = {
|
mConfig["template_local_paths"] = {
|
||||||
dropshell_base + "/local_templates"
|
dropshell_base + "/local_templates"
|
||||||
};
|
};
|
||||||
|
mConfig["template_registry_URLs"] = {
|
||||||
mConfig["server_definition_paths"] = {
|
"https://templates.dropshell.app"
|
||||||
dropshell_base + "/servers"
|
};
|
||||||
};
|
mConfig["template_upload_token"] = "SECRETTOKEN";
|
||||||
mConfig["template_upload_registry_url"] = "https://templates.dropshell.app";
|
|
||||||
mConfig["template_upload_registry_token"] = "SECRETTOKEN";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
config_file << mConfig.dump(4);
|
config_file << mConfig.dump(4);
|
||||||
config_file.close();
|
config_file.close();
|
||||||
|
|
||||||
if (create_aux_directories) {
|
if (create_aux_directories) {
|
||||||
std::vector<std::filesystem::path> paths = {
|
std::vector<std::string> paths;
|
||||||
get_local_template_cache_path(),
|
_append(paths, get_local_template_paths());
|
||||||
get_local_backup_path(),
|
_append(paths, get_local_server_definition_paths());
|
||||||
get_local_tempfiles_path()
|
|
||||||
};
|
|
||||||
for (auto & p : get_local_server_definition_paths())
|
|
||||||
paths.push_back(p);
|
|
||||||
|
|
||||||
for (auto & p : paths)
|
for (auto & p : paths)
|
||||||
if (!std::filesystem::exists(p))
|
if (!std::filesystem::exists(p))
|
||||||
{
|
{
|
||||||
@ -99,6 +98,11 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
std::filesystem::create_directories(p);
|
std::filesystem::create_directories(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug << "Config paths: " << std::endl;
|
||||||
|
for (auto [key,value] : mConfig.items()) {
|
||||||
|
debug << " " << key << ": " << value << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -113,28 +117,17 @@ bool config::is_agent_installed()
|
|||||||
return std::filesystem::exists(localpath::agent() + "/bb64");
|
return std::filesystem::exists(localpath::agent() + "/bb64");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_local_tempfiles_path() {
|
|
||||||
return mConfig["tempfiles"];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string config::get_local_backup_path() {
|
|
||||||
return mConfig["backups"];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string config::get_local_template_cache_path() {
|
|
||||||
return mConfig["template_cache"];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> config::get_template_registry_urls() {
|
std::vector<std::string> config::get_template_registry_urls() {
|
||||||
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
||||||
std::vector<std::string> urls;
|
std::vector<std::string> urls;
|
||||||
for (auto &url : template_registry_urls) {
|
for (auto &url : template_registry_urls) {
|
||||||
urls.push_back(url);
|
if (url.is_string() && !url.empty())
|
||||||
|
urls.push_back(url);
|
||||||
}
|
}
|
||||||
return urls;
|
return urls;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> config::get_template_local_paths()
|
std::vector<std::string> config::get_local_template_paths()
|
||||||
{
|
{
|
||||||
nlohmann::json template_local_paths = mConfig["template_local_paths"];
|
nlohmann::json template_local_paths = mConfig["template_local_paths"];
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
@ -147,23 +140,40 @@ std::vector<std::string> config::get_template_local_paths()
|
|||||||
|
|
||||||
std::vector<std::string> config::get_local_server_definition_paths() {
|
std::vector<std::string> config::get_local_server_definition_paths() {
|
||||||
nlohmann::json server_definition_paths = mConfig["server_definition_paths"];
|
nlohmann::json server_definition_paths = mConfig["server_definition_paths"];
|
||||||
|
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
for (auto &path : server_definition_paths) {
|
for (auto &path : server_definition_paths) {
|
||||||
if (path.is_string() && !path.empty())
|
if (path.is_string() && !path.empty())
|
||||||
paths.push_back(path);
|
paths.push_back(path);
|
||||||
else
|
|
||||||
warning << "Invalid server definition path: " << path << std::endl;
|
|
||||||
}
|
}
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_template_upload_registry_url() {
|
std::string config::get_server_create_path()
|
||||||
return mConfig["template_upload_registry_url"];
|
{
|
||||||
|
std::vector<std::string> paths = get_local_server_definition_paths();
|
||||||
|
if (paths.empty())
|
||||||
|
return "";
|
||||||
|
return paths[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_template_upload_registry_token() {
|
std::string config::get_template_create_path()
|
||||||
return mConfig["template_upload_registry_token"];
|
{
|
||||||
|
std::vector<std::string> paths = get_local_template_paths();
|
||||||
|
if (paths.empty())
|
||||||
|
return "";
|
||||||
|
return paths[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string config::get_template_upload_url()
|
||||||
|
{
|
||||||
|
std::vector<std::string> urls = get_template_registry_urls();
|
||||||
|
if (urls.empty())
|
||||||
|
return "";
|
||||||
|
return urls[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string config::get_template_upload_token() {
|
||||||
|
return mConfig["template_upload_token"];
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -17,15 +17,14 @@ class config {
|
|||||||
bool is_config_set() const;
|
bool is_config_set() const;
|
||||||
static bool is_agent_installed();
|
static bool is_agent_installed();
|
||||||
|
|
||||||
std::string get_local_tempfiles_path();
|
|
||||||
std::string get_local_backup_path();
|
|
||||||
std::string get_local_template_cache_path();
|
|
||||||
std::vector<std::string> get_template_registry_urls();
|
std::vector<std::string> get_template_registry_urls();
|
||||||
std::vector<std::string> get_template_local_paths();
|
std::vector<std::string> get_local_template_paths();
|
||||||
std::vector<std::string> get_local_server_definition_paths();
|
std::vector<std::string> get_local_server_definition_paths();
|
||||||
|
|
||||||
std::string get_template_upload_registry_url();
|
std::string get_server_create_path();
|
||||||
std::string get_template_upload_registry_token();
|
std::string get_template_create_path();
|
||||||
|
std::string get_template_upload_url();
|
||||||
|
std::string get_template_upload_token();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
nlohmann::json mConfig;
|
nlohmann::json mConfig;
|
||||||
|
@ -1,529 +1,529 @@
|
|||||||
|
|
||||||
#include <iostream>
|
// #include <iostream>
|
||||||
#include <fstream>
|
// #include <fstream>
|
||||||
#include <sstream>
|
// #include <sstream>
|
||||||
#include <cstdlib>
|
// #include <cstdlib>
|
||||||
#include <chrono>
|
// #include <chrono>
|
||||||
#include <iomanip>
|
// #include <iomanip>
|
||||||
#include <filesystem>
|
// #include <filesystem>
|
||||||
#include <unistd.h>
|
// #include <unistd.h>
|
||||||
#include "utils/assert.hpp"
|
// #include "utils/assert.hpp"
|
||||||
|
|
||||||
#include "config.hpp"
|
// #include "config.hpp"
|
||||||
#include "server_env_manager.hpp"
|
// #include "server_env_manager.hpp"
|
||||||
#include "templates.hpp"
|
// #include "templates.hpp"
|
||||||
#include "services.hpp"
|
// #include "services.hpp"
|
||||||
#include "utils/directories.hpp"
|
// #include "utils/directories.hpp"
|
||||||
#include "utils/utils.hpp"
|
// #include "utils/utils.hpp"
|
||||||
#include "command_registry.hpp"
|
// #include "command_registry.hpp"
|
||||||
#include "shared_commands.hpp"
|
// #include "shared_commands.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace dropshell {
|
// namespace dropshell {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class service_runner {
|
// class service_runner {
|
||||||
public:
|
// public:
|
||||||
service_runner(const std::string& server_name, const std::string& service_name);
|
// service_runner(const std::string& server_name, const std::string& service_name);
|
||||||
bool isValid() const { return mValid; }
|
// bool isValid() const { return mValid; }
|
||||||
|
|
||||||
// run a command over ssh, using the credentials from server.env (via server_env.hpp)
|
// // run a command over ssh, using the credentials from server.env (via server_env.hpp)
|
||||||
// first check that the command corresponds to a valid .sh file in the service directory
|
// // first check that the command corresponds to a valid .sh file in the service directory
|
||||||
// then run the command, passing the {service_name}.env file as an argument
|
// // then run the command, passing the {service_name}.env file as an argument
|
||||||
// do a lot of checks, such as:
|
// // do a lot of checks, such as:
|
||||||
// checking that we can ssh to the server.
|
// // checking that we can ssh to the server.
|
||||||
// checking whether the service directory exists on the server.
|
// // checking whether the service directory exists on the server.
|
||||||
// checking that the command exists in the service directory.
|
// // checking that the command exists in the service directory.
|
||||||
// checking that the command is a valid .sh file.
|
// // checking that the command is a valid .sh file.
|
||||||
// checking that the {service_name}.env file exists in the service directory.
|
// // checking that the {service_name}.env file exists in the service directory.
|
||||||
bool run_command(const std::string& command, std::vector<std::string> additional_args={}, std::map<std::string, std::string> env_vars={});
|
// bool run_command(const std::string& command, std::vector<std::string> additional_args={}, std::map<std::string, std::string> env_vars={});
|
||||||
|
|
||||||
// check health of service. Silent.
|
// // check health of service. Silent.
|
||||||
// 1. run status.sh on the server
|
// // 1. run status.sh on the server
|
||||||
// 2. return the output of the status.sh script
|
// // 2. return the output of the status.sh script
|
||||||
|
|
||||||
//HealthStatus is_healthy();
|
// //HealthStatus is_healthy();
|
||||||
|
|
||||||
// std::string healthtick();
|
// // std::string healthtick();
|
||||||
// std::string healthmark();
|
// // std::string healthmark();
|
||||||
|
|
||||||
public:
|
// public:
|
||||||
// backup and restore
|
// // backup and restore
|
||||||
bool backup(bool silent=false);
|
// bool backup(bool silent=false);
|
||||||
bool restore(std::string backup_file, bool silent=false);
|
// bool restore(std::string backup_file, bool silent=false);
|
||||||
|
|
||||||
// nuke the service
|
// // nuke the service
|
||||||
bool nuke(bool silent=false); // nukes all data for this service on the remote server
|
// bool nuke(bool silent=false); // nukes all data for this service on the remote server
|
||||||
bool fullnuke(); // nuke all data for this service on the remote server, and then nukes all the local service definitionfiles
|
// bool fullnuke(); // nuke all data for this service on the remote server, and then nukes all the local service definitionfiles
|
||||||
|
|
||||||
// launch an interactive ssh session on a server or service
|
// // launch an interactive ssh session on a server or service
|
||||||
// replaces the current dropshell process with the ssh process
|
// // replaces the current dropshell process with the ssh process
|
||||||
bool interactive_ssh_service();
|
// bool interactive_ssh_service();
|
||||||
|
|
||||||
bool scp_file_to_remote(const std::string& local_path, const std::string& remote_path, bool silent=false);
|
// bool scp_file_to_remote(const std::string& local_path, const std::string& remote_path, bool silent=false);
|
||||||
bool scp_file_from_remote(const std::string& remote_path, const std::string& local_path, bool silent=false);
|
// bool scp_file_from_remote(const std::string& remote_path, const std::string& local_path, bool silent=false);
|
||||||
public:
|
// public:
|
||||||
// utility functions
|
// // utility functions
|
||||||
static std::string get_latest_backup_file(const std::string& server, const std::string& service);
|
// static std::string get_latest_backup_file(const std::string& server, const std::string& service);
|
||||||
static bool interactive_ssh(const std::string & server_name, const std::string & command);
|
// static bool interactive_ssh(const std::string & server_name, const std::string & command);
|
||||||
// static std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
|
// // static std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
|
||||||
|
|
||||||
private:
|
// private:
|
||||||
std::string mServer;
|
// std::string mServer;
|
||||||
server_env_manager mServerEnv;
|
// server_env_manager mServerEnv;
|
||||||
LocalServiceInfo mServiceInfo;
|
// LocalServiceInfo mServiceInfo;
|
||||||
std::string mService;
|
// std::string mService;
|
||||||
bool mValid;
|
// bool mValid;
|
||||||
|
|
||||||
// Helper methods
|
// // Helper methods
|
||||||
public:
|
// public:
|
||||||
};
|
// };
|
||||||
|
|
||||||
|
|
||||||
} // namespace dropshell
|
// } // namespace dropshell
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
// namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace dropshell {
|
// namespace dropshell {
|
||||||
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
// {
|
||||||
if (server_name.empty() || service_name.empty())
|
// if (server_name.empty() || service_name.empty())
|
||||||
return;
|
// return;
|
||||||
|
|
||||||
// Initialize server environment
|
// // Initialize server environment
|
||||||
if (!mServerEnv.is_valid())
|
// if (!mServerEnv.is_valid())
|
||||||
return;
|
// return;
|
||||||
|
|
||||||
mServiceInfo = get_service_info(server_name, service_name);
|
// mServiceInfo = get_service_info(server_name, service_name);
|
||||||
if (mServiceInfo.service_name.empty())
|
// if (mServiceInfo.service_name.empty())
|
||||||
return;
|
// return;
|
||||||
|
|
||||||
mService = mServiceInfo.service_name;
|
// mService = mServiceInfo.service_name;
|
||||||
|
|
||||||
mValid = !mServiceInfo.local_template_path.empty();
|
// mValid = !mServiceInfo.local_template_path.empty();
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool service_runner::nuke(bool silent)
|
// bool service_runner::nuke(bool silent)
|
||||||
{
|
// {
|
||||||
maketitle("Nuking " + mService + " (" + mServiceInfo.template_name + ") on " + mServer);
|
// maketitle("Nuking " + mService + " (" + mServiceInfo.template_name + ") on " + mServer);
|
||||||
|
|
||||||
if (!mServerEnv.is_valid()) return false; // should never hit this.
|
// if (!mServerEnv.is_valid()) return false; // should never hit this.
|
||||||
|
|
||||||
std::string remote_service_path = remotepath::service(mServer, mService);
|
// std::string remote_service_path = remotepath::service(mServer, mService);
|
||||||
|
|
||||||
info << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
// info << "Service " << mService << " successfully nuked from " << mServer << std::endl;
|
||||||
|
|
||||||
if (!silent) {
|
// if (!silent) {
|
||||||
info << "There's nothing left on the remote server." << std::endl;
|
// info << "There's nothing left on the remote server." << std::endl;
|
||||||
info << "You can remove the local files with:" << std::endl;
|
// info << "You can remove the local files with:" << std::endl;
|
||||||
info << " rm -rf " << localpath::service(mServer,mService) << std::endl;
|
// info << " rm -rf " << localpath::service(mServer,mService) << std::endl;
|
||||||
}
|
// }
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool service_runner::fullnuke()
|
// bool service_runner::fullnuke()
|
||||||
{
|
// {
|
||||||
if (!nuke(true))
|
// if (!nuke(true))
|
||||||
{
|
// {
|
||||||
warning << "Nuke script failed, aborting." << 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)) {
|
||||||
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)) {
|
||||||
error << "Failed to remove service directory" << std::endl;
|
// error << "Failed to remove service directory" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// // ------------------------------------------------------------------------------------------------
|
||||||
// Run a command on the service.
|
// // Run a command on the service.
|
||||||
// ------------------------------------------------------------------------------------------------
|
// // ------------------------------------------------------------------------------------------------
|
||||||
bool service_runner::run_command(const std::string& command, std::vector<std::string> additional_args, std::map<std::string, std::string> env_vars) {
|
// bool service_runner::run_command(const std::string& command, std::vector<std::string> additional_args, std::map<std::string, std::string> env_vars) {
|
||||||
if (!mServerEnv.is_valid()) {
|
// if (!mServerEnv.is_valid()) {
|
||||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
// std::cerr << "Error: Server service not initialized" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name);
|
// template_info tinfo = gTemplateManager().get_template_info(mServiceInfo.template_name);
|
||||||
if (!tinfo.is_set()) {
|
// if (!tinfo.is_set()) {
|
||||||
std::cerr << "Error: Template '" << mServiceInfo.template_name << "' not found" << std::endl;
|
// std::cerr << "Error: Template '" << mServiceInfo.template_name << "' not found" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (command == "fullnuke")
|
// if (command == "fullnuke")
|
||||||
return fullnuke();
|
// return fullnuke();
|
||||||
|
|
||||||
if (command == "nuke")
|
// if (command == "nuke")
|
||||||
{
|
// {
|
||||||
std::cout << "Nuking " << mService << " (" << mServiceInfo.template_name << ") on " << mServer << std::endl;
|
// std::cout << "Nuking " << mService << " (" << mServiceInfo.template_name << ") on " << mServer << std::endl;
|
||||||
return nuke();
|
// return nuke();
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (!gTemplateManager().template_command_exists(mServiceInfo.template_name, command)) {
|
// if (!gTemplateManager().template_command_exists(mServiceInfo.template_name, command)) {
|
||||||
std::cout << "No command script for " << mServiceInfo.template_name << " : " << command << std::endl;
|
// std::cout << "No command script for " << mServiceInfo.template_name << " : " << command << std::endl;
|
||||||
return true; // nothing to run.
|
// return true; // nothing to run.
|
||||||
}
|
// }
|
||||||
|
|
||||||
// install doesn't require anything on the server yet.
|
// // install doesn't require anything on the server yet.
|
||||||
// if (command == "install")
|
// // if (command == "install")
|
||||||
// return install_service(mServer, mService, false);
|
// // return install_service(mServer, mService, false);
|
||||||
|
|
||||||
std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh";
|
// std::string script_path = remotepath::service_template(mServer, mService) + "/" + command + ".sh";
|
||||||
|
|
||||||
// Check if service directory exists
|
// // Check if service directory exists
|
||||||
if (!mServerEnv.check_remote_dir_exists(remotepath::service(mServer, mService))) {
|
// if (!mServerEnv.check_remote_dir_exists(remotepath::service(mServer, mService))) {
|
||||||
std::cerr << "Error: Service is not installed: " << mService << std::endl;
|
// std::cerr << "Error: Service is not installed: " << mService << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Check if command script exists
|
// // Check if command script exists
|
||||||
if (!mServerEnv.check_remote_file_exists(script_path)) {
|
// if (!mServerEnv.check_remote_file_exists(script_path)) {
|
||||||
std::cerr << "Error: Remote command script not found: " << script_path << std::endl;
|
// std::cerr << "Error: Remote command script not found: " << script_path << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Check if env file exists
|
// // Check if env file exists
|
||||||
if (!mServerEnv.check_remote_file_exists(remotefile::service_env(mServer, mService))) {
|
// if (!mServerEnv.check_remote_file_exists(remotefile::service_env(mServer, mService))) {
|
||||||
std::cerr << "Error: Service config file not found: " << remotefile::service_env(mServer, mService) << std::endl;
|
// std::cerr << "Error: Service config file not found: " << remotefile::service_env(mServer, mService) << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// if (command == "uninstall")
|
// // if (command == "uninstall")
|
||||||
// return uninstall();
|
// // return uninstall();
|
||||||
|
|
||||||
if (command == "ssh") {
|
// if (command == "ssh") {
|
||||||
interactive_ssh_service();
|
// interactive_ssh_service();
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
if (command == "restore") {
|
// if (command == "restore") {
|
||||||
if (additional_args.size() < 1) {
|
// if (additional_args.size() < 1) {
|
||||||
std::cerr << "Error: restore requires a backup file:" << std::endl;
|
// std::cerr << "Error: restore requires a backup file:" << std::endl;
|
||||||
std::cerr << "dropshell restore <server> <service> <backup-file>" << std::endl;
|
// std::cerr << "dropshell restore <server> <service> <backup-file>" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
return restore(additional_args[0], false);
|
// return restore(additional_args[0], false);
|
||||||
}
|
// }
|
||||||
if (command == "backup") {
|
// if (command == "backup") {
|
||||||
return backup(false);
|
// return backup(false);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Run the generic command
|
// // Run the generic command
|
||||||
std::vector<std::string> args; // not passed through yet.
|
// std::vector<std::string> args; // not passed through yet.
|
||||||
return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars);
|
// return mServerEnv.run_remote_template_command(mService, command, args, false, env_vars);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) {
|
// bool service_runner::interactive_ssh(const std::string & server_name, const std::string & command) {
|
||||||
std::string serverpath = localpath::server(server_name);
|
// std::string serverpath = localpath::server(server_name);
|
||||||
if (serverpath.empty()) {
|
// if (serverpath.empty()) {
|
||||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
// std::cerr << "Error: Server not found: " << server_name << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
server_env_manager env(server_name);
|
// server_env_manager env(server_name);
|
||||||
if (!env.is_valid()) {
|
// if (!env.is_valid()) {
|
||||||
std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
|
// std::cerr << "Error: Invalid server environment file: " << server_name << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
sCommand scommand("", "bash",{});
|
// sCommand scommand("", "bash",{});
|
||||||
return execute_ssh_command(env.get_SSH_INFO(), scommand, cMode::Interactive);
|
// return execute_ssh_command(env.get_SSH_INFO(), scommand, cMode::Interactive);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool service_runner::interactive_ssh_service()
|
// bool service_runner::interactive_ssh_service()
|
||||||
{
|
// {
|
||||||
std::set<std::string> used_commands = get_used_commands(mServer, mService);
|
// std::set<std::string> used_commands = get_used_commands(mServer, mService);
|
||||||
if (used_commands.find("ssh") == used_commands.end()) {
|
// if (used_commands.find("ssh") == used_commands.end()) {
|
||||||
std::cerr << "Error: "<< mService <<" does not support ssh" << std::endl;
|
// std::cerr << "Error: "<< mService <<" does not support ssh" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::vector<std::string> args; // not passed through yet.
|
// std::vector<std::string> args; // not passed through yet.
|
||||||
return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {});
|
// return mServerEnv.run_remote_template_command(mService, "ssh", args, false, {});
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent)
|
// bool service_runner::scp_file_to_remote(const std::string &local_path, const std::string &remote_path, bool silent)
|
||||||
{
|
// {
|
||||||
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
// std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + quote(local_path) + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + (silent ? " > /dev/null 2>&1" : "");
|
||||||
return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
// return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent)
|
// bool service_runner::scp_file_from_remote(const std::string &remote_path, const std::string &local_path, bool silent)
|
||||||
{
|
// {
|
||||||
std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.get_SSH_HOST() + ":" + quote(remote_path) + " " + quote(local_path) + (silent ? " > /dev/null 2>&1" : "");
|
// std::string scp_cmd = "scp -P " + mServerEnv.get_SSH_PORT() + " " + mServerEnv.get_SSH_USER() + "@" + mServerEnv.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));
|
// return execute_local_command("", scp_cmd, {}, nullptr, (silent ? cMode::Silent : cMode::Defaults));
|
||||||
}
|
// }
|
||||||
|
|
||||||
bool service_runner::restore(std::string backup_file, bool silent)
|
// bool service_runner::restore(std::string backup_file, bool silent)
|
||||||
{
|
// {
|
||||||
if (backup_file.empty()) {
|
// if (backup_file.empty()) {
|
||||||
std::cerr << "Error: not enough arguments. dropshell restore <server> <service> <backup-file>" << std::endl;
|
// std::cerr << "Error: not enough arguments. dropshell restore <server> <service> <backup-file>" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
// std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||||
|
|
||||||
if (backup_file == "latest") {
|
// if (backup_file == "latest") {
|
||||||
// get the latest backup file from the server
|
// // get the latest backup file from the server
|
||||||
backup_file = get_latest_backup_file(mServer, mService);
|
// backup_file = get_latest_backup_file(mServer, mService);
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string();
|
// std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_file).string();
|
||||||
|
|
||||||
if (! std::filesystem::exists(local_backup_file_path)) {
|
// if (! std::filesystem::exists(local_backup_file_path)) {
|
||||||
std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
// std::cerr << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// 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, "-_-");
|
// 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;
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::string backup_server_name = parts[0];
|
// std::string backup_server_name = parts[0];
|
||||||
std::string backup_template_name = parts[1];
|
// std::string backup_template_name = parts[1];
|
||||||
std::string backup_service_name = parts[2];
|
// std::string backup_service_name = parts[2];
|
||||||
std::string backup_datetime = parts[3];
|
// std::string backup_datetime = parts[3];
|
||||||
|
|
||||||
if (backup_template_name != mServiceInfo.template_name) {
|
// if (backup_template_name != mServiceInfo.template_name) {
|
||||||
std::cerr << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
// std::cerr << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::string nicedate = std::string(backup_datetime).substr(0, 10);
|
// std::string nicedate = std::string(backup_datetime).substr(0, 10);
|
||||||
|
|
||||||
std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "<<backup_server_name<<", onto "<<mServer<<"/"<<mService<<std::endl;
|
// std::cout << "Restoring " << nicedate << " backup of " << backup_template_name << " taken from "<<backup_server_name<<", onto "<<mServer<<"/"<<mService<<std::endl;
|
||||||
std::cout << std::endl;
|
// std::cout << std::endl;
|
||||||
std::cout << "*** ALL DATA FOR "<<mServer<<"/"<<mService<<" WILL BE OVERWRITTEN! ***"<<std::endl;
|
// std::cout << "*** ALL DATA FOR "<<mServer<<"/"<<mService<<" WILL BE OVERWRITTEN! ***"<<std::endl;
|
||||||
|
|
||||||
// run the restore script
|
// // run the restore script
|
||||||
std::cout << "OK, here goes..." << std::endl;
|
// std::cout << "OK, here goes..." << std::endl;
|
||||||
|
|
||||||
{ // backup existing service
|
// { // backup existing service
|
||||||
maketitle("1) Backing up old service... ");
|
// maketitle("1) Backing up old service... ");
|
||||||
if (!backup(true)) // silent=true
|
// if (!backup(true)) // silent=true
|
||||||
{
|
// {
|
||||||
std::cerr << std::endl;
|
// std::cerr << std::endl;
|
||||||
std::cerr << "Error: Backup failed, restore aborted." << std::endl;
|
// std::cerr << "Error: Backup failed, restore aborted." << std::endl;
|
||||||
std::cerr << "You can try using dropshell install "<<mServer<<" "<<mService<<" to install the service afresh." << std::endl;
|
// std::cerr << "You can try using dropshell install "<<mServer<<" "<<mService<<" to install the service afresh." << std::endl;
|
||||||
std::cerr << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
|
// std::cerr << "Otherwise, stop the service, create and initialise a new one, then restore to that." << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
std::cout << "Backup complete." << std::endl;
|
// std::cout << "Backup complete." << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
{ // uninstall service, then nuke it.
|
// { // uninstall service, then nuke it.
|
||||||
maketitle("2) Uninstalling old service...");
|
// maketitle("2) Uninstalling old service...");
|
||||||
// if (!uninstall(true))
|
// // if (!uninstall(true))
|
||||||
// return false;
|
// // return false;
|
||||||
|
|
||||||
maketitle("3) Nuking old service...");
|
// maketitle("3) Nuking old service...");
|
||||||
// if (!nuke(true))
|
// // if (!nuke(true))
|
||||||
// return false;
|
// // return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
{ // restore service from backup
|
// { // restore service from backup
|
||||||
maketitle("4) Restoring service data from backup...");
|
// maketitle("4) Restoring service data from backup...");
|
||||||
std::string remote_backups_dir = remotepath::backups(mServer);
|
// std::string remote_backups_dir = remotepath::backups(mServer);
|
||||||
std::string remote_backup_file_path = remote_backups_dir + "/" + backup_file;
|
// std::string remote_backup_file_path = remote_backups_dir + "/" + backup_file;
|
||||||
|
|
||||||
// Copy backup file from local to server
|
// // Copy backup file from local to server
|
||||||
if (!scp_file_to_remote(local_backup_file_path, remote_backup_file_path, silent)) {
|
// if (!scp_file_to_remote(local_backup_file_path, remote_backup_file_path, silent)) {
|
||||||
std::cerr << "Failed to copy backup file from local to server" << std::endl;
|
// std::cerr << "Failed to copy backup file from local to server" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
shared_commands::cRemoteTempFolder remote_temp_folder(mServerEnv);
|
// shared_commands::cRemoteTempFolder remote_temp_folder(mServerEnv);
|
||||||
mServerEnv.run_remote_template_command(mService, "restore", {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
// mServerEnv.run_remote_template_command(mService, "restore", {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}});
|
||||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
// } // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
|
|
||||||
// { // installing fresh service
|
// // { // installing fresh service
|
||||||
// maketitle("5) Non-destructive install of fresh service...");
|
// // maketitle("5) Non-destructive install of fresh service...");
|
||||||
// if (!install_service(mServer, mService, true))
|
// // if (!install_service(mServer, mService, true))
|
||||||
// return false;
|
// // return false;
|
||||||
// }
|
// // }
|
||||||
|
|
||||||
bool healthy = false;
|
// bool healthy = false;
|
||||||
{// healthcheck the service
|
// {// healthcheck the service
|
||||||
maketitle("6) Healthchecking service...");
|
// maketitle("6) Healthchecking service...");
|
||||||
std::string green_tick = "\033[32m✓\033[0m";
|
// std::string green_tick = "\033[32m✓\033[0m";
|
||||||
std::string red_cross = "\033[31m✗\033[0m";
|
// std::string red_cross = "\033[31m✗\033[0m";
|
||||||
healthy= (mServerEnv.run_remote_template_command(mService, "status", {}, silent, {}));
|
// healthy= (mServerEnv.run_remote_template_command(mService, "status", {}, silent, {}));
|
||||||
if (!silent)
|
// if (!silent)
|
||||||
std::cout << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
// std::cout << (healthy ? green_tick : red_cross) << " Service is " << (healthy ? "healthy" : "NOT healthy") << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return healthy;
|
// return healthy;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
// 3. store it in the server's DROPSHELL_DIR/backups folder
|
// // 3. store it in the server's DROPSHELL_DIR/backups folder
|
||||||
// 4. copy it to the local user_dir/backups folder
|
// // 4. copy it to the local user_dir/backups folder
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// // ------------------------------------------------------------------------------------------------
|
||||||
// Backup the service.
|
// // Backup the service.
|
||||||
// ------------------------------------------------------------------------------------------------
|
// // ------------------------------------------------------------------------------------------------
|
||||||
bool service_runner::backup(bool silent) {
|
// bool service_runner::backup(bool silent) {
|
||||||
auto service_info = get_service_info(mServer, mService);
|
// auto service_info = get_service_info(mServer, mService);
|
||||||
if (service_info.local_service_path.empty()) {
|
// if (service_info.local_service_path.empty()) {
|
||||||
std::cerr << "Error: Service not found" << std::endl;
|
// std::cerr << "Error: Service not found" << std::endl;
|
||||||
return 1;
|
// return 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
const std::string command = "backup";
|
// const std::string command = "backup";
|
||||||
|
|
||||||
if (!gTemplateManager().template_command_exists(service_info.template_name, command)) {
|
// if (!gTemplateManager().template_command_exists(service_info.template_name, command)) {
|
||||||
std::cout << "No backup script for " << service_info.template_name << std::endl;
|
// std::cout << "No backup script for " << service_info.template_name << std::endl;
|
||||||
return true; // nothing to back up.
|
// return true; // nothing to back up.
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Check if basic installed stuff is in place.
|
// // Check if basic installed stuff is in place.
|
||||||
std::string remote_service_template_path = remotepath::service_template(mServer, mService);
|
// std::string remote_service_template_path = remotepath::service_template(mServer, mService);
|
||||||
std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
// std::string remote_command_script_file = remote_service_template_path + "/" + command + ".sh";
|
||||||
std::string remote_service_config_path = remotepath::service_config(mServer, mService);
|
// std::string remote_service_config_path = remotepath::service_config(mServer, mService);
|
||||||
if (!mServerEnv.check_remote_items_exist({
|
// if (!mServerEnv.check_remote_items_exist({
|
||||||
remotepath::service(mServer, mService),
|
// remotepath::service(mServer, mService),
|
||||||
remote_command_script_file,
|
// remote_command_script_file,
|
||||||
remotefile::service_env(mServer, mService)})
|
// remotefile::service_env(mServer, mService)})
|
||||||
)
|
// )
|
||||||
{
|
// {
|
||||||
std::cerr << "Error: Required service directories not found on remote server" << std::endl;
|
// std::cerr << "Error: Required service directories not found on remote server" << std::endl;
|
||||||
std::cerr << "Is the service installed?" << std::endl;
|
// std::cerr << "Is the service installed?" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Create backups directory on server if it doesn't exist
|
// // Create backups directory on server if it doesn't exist
|
||||||
std::string remote_backups_dir = remotepath::backups(mServer);
|
// std::string remote_backups_dir = remotepath::backups(mServer);
|
||||||
if (!silent) std::cout << "Remote backups directory on "<< mServer <<": " << remote_backups_dir << std::endl;
|
// if (!silent) std::cout << "Remote backups directory on "<< mServer <<": " << remote_backups_dir << std::endl;
|
||||||
std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
// std::string mkdir_cmd = "mkdir -p " + quote(remote_backups_dir);
|
||||||
if (!execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand("",mkdir_cmd, {}), cMode::Silent)) {
|
// if (!execute_ssh_command(mServerEnv.get_SSH_INFO(), sCommand("",mkdir_cmd, {}), cMode::Silent)) {
|
||||||
std::cerr << "Failed to create backups directory on server" << std::endl;
|
// std::cerr << "Failed to create backups directory on server" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Create backups directory locally if it doesn't exist
|
// // Create backups directory locally if it doesn't exist
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
// std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||||
if (local_backups_dir.empty()) {
|
// if (local_backups_dir.empty()) {
|
||||||
std::cerr << "Error: Local backups directory not found" << std::endl;
|
// std::cerr << "Error: Local backups directory not found" << std::endl;
|
||||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
// std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
if (!std::filesystem::exists(local_backups_dir))
|
// if (!std::filesystem::exists(local_backups_dir))
|
||||||
std::filesystem::create_directories(local_backups_dir);
|
// std::filesystem::create_directories(local_backups_dir);
|
||||||
|
|
||||||
// Get current datetime for backup filename
|
// // Get current datetime for backup filename
|
||||||
auto now = std::chrono::system_clock::now();
|
// auto now = std::chrono::system_clock::now();
|
||||||
auto time = std::chrono::system_clock::to_time_t(now);
|
// auto time = std::chrono::system_clock::to_time_t(now);
|
||||||
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");
|
||||||
|
|
||||||
// Construct backup filename
|
// // Construct backup filename
|
||||||
shared_commands::cBackupFileName backup_filename_construction(mServer, mService, service_info.template_name);
|
// shared_commands::cBackupFileName backup_filename_construction(mServer, mService, service_info.template_name);
|
||||||
if (!backup_filename_construction.is_valid()) {
|
// if (!backup_filename_construction.is_valid()) {
|
||||||
std::cerr << "Invalid backup filename" << std::endl;
|
// std::cerr << "Invalid backup filename" << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
std::string backup_filename = backup_filename_construction.get_filename();
|
// 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("-_-", 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);
|
||||||
if (!mServerEnv.run_remote_template_command(mService, command, {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) {
|
// if (!mServerEnv.run_remote_template_command(mService, command, {}, silent, {{"BACKUP_FILE", remote_backup_file_path}, {"TEMP_DIR", remote_temp_folder.path()}})) {
|
||||||
std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
// std::cerr << "Backup script failed on remote server: " << remote_backup_file_path << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Copy backup file from server to local
|
// // Copy backup file from server to local
|
||||||
if (!scp_file_from_remote(remote_backup_file_path, local_backup_file_path, silent)) {
|
// if (!scp_file_from_remote(remote_backup_file_path, local_backup_file_path, 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;
|
||||||
}
|
// }
|
||||||
} // dtor of remote_temp_folder will clean up the temp folder on the server
|
// } // dtor of remote_temp_folder will clean up the temp folder on the server
|
||||||
|
|
||||||
if (!silent) {
|
// if (!silent) {
|
||||||
std::cout << "Backup created successfully. Restore with:"<<std::endl;
|
// std::cout << "Backup created successfully. Restore with:"<<std::endl;
|
||||||
std::cout << " dropshell restore " << mServer << " " << mService << " " << backup_filename << std::endl;
|
// std::cout << " dropshell restore " << mServer << " " << mService << " " << backup_filename << std::endl;
|
||||||
}
|
// }
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Helper function to get the latest backup file for a given server and service
|
// // Helper function to get the latest backup file for a given server and service
|
||||||
std::string service_runner::get_latest_backup_file(const std::string& server, const std::string& service) {
|
// std::string service_runner::get_latest_backup_file(const std::string& server, const std::string& service) {
|
||||||
std::string local_backups_dir = gConfig().get_local_backup_path();
|
// std::string local_backups_dir = gConfig().get_local_backup_path();
|
||||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) {
|
// if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir)) {
|
||||||
std::cerr << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
// std::cerr << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
return "";
|
// return "";
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Get the template name for this service
|
// // Get the template name for this service
|
||||||
LocalServiceInfo info = get_service_info(server, service);
|
// LocalServiceInfo info = get_service_info(server, service);
|
||||||
if (info.template_name.empty()) {
|
// if (info.template_name.empty()) {
|
||||||
std::cerr << "Error: Could not determine template name for service: " << service << std::endl;
|
// std::cerr << "Error: Could not determine template name for service: " << service << std::endl;
|
||||||
return "";
|
// return "";
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Build the expected prefix for backup files
|
// // Build the expected prefix for backup files
|
||||||
std::string prefix = server + "-_-" + info.template_name + "-_-" + service + "-_-";
|
// std::string prefix = server + "-_-" + info.template_name + "-_-" + service + "-_-";
|
||||||
std::string latest_file;
|
// std::string latest_file;
|
||||||
std::string latest_datetime;
|
// std::string latest_datetime;
|
||||||
|
|
||||||
std::cout << "Looking for backup files in " << local_backups_dir << std::endl;
|
// std::cout << "Looking for backup files in " << local_backups_dir << std::endl;
|
||||||
|
|
||||||
for (const auto& entry : std::filesystem::directory_iterator(local_backups_dir)) {
|
// for (const auto& entry : std::filesystem::directory_iterator(local_backups_dir)) {
|
||||||
if (!entry.is_regular_file()) continue;
|
// if (!entry.is_regular_file()) continue;
|
||||||
std::string filename = entry.path().filename().string();
|
// std::string filename = entry.path().filename().string();
|
||||||
if (filename.rfind(prefix, 0) == 0) { // starts with prefix
|
// if (filename.rfind(prefix, 0) == 0) { // starts with prefix
|
||||||
// Extract the datetime part
|
// // Extract the datetime part
|
||||||
size_t dt_start = prefix.size();
|
// size_t dt_start = prefix.size();
|
||||||
size_t dt_end = filename.find(".tgz", dt_start);
|
// size_t dt_end = filename.find(".tgz", dt_start);
|
||||||
if (dt_end == std::string::npos) continue;
|
// if (dt_end == std::string::npos) continue;
|
||||||
std::string datetime = filename.substr(dt_start, dt_end - dt_start);
|
// std::string datetime = filename.substr(dt_start, dt_end - dt_start);
|
||||||
std::cout << "Found backup file: " << filename << " with datetime: " << datetime << std::endl;
|
// std::cout << "Found backup file: " << filename << " with datetime: " << datetime << std::endl;
|
||||||
if (datetime > latest_datetime) {
|
// if (datetime > latest_datetime) {
|
||||||
latest_datetime = datetime;
|
// latest_datetime = datetime;
|
||||||
latest_file = filename;
|
// latest_file = filename;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (latest_file.empty()) {
|
// if (latest_file.empty()) {
|
||||||
std::cerr << "Error: No backup files found for " << server << ", " << service << std::endl;
|
// std::cerr << "Error: No backup files found for " << server << ", " << service << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
std::cout << "Latest backup file: " << latest_file << std::endl;
|
// std::cout << "Latest backup file: " << latest_file << std::endl;
|
||||||
return latest_file;
|
// return latest_file;
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace dropshell
|
// } // namespace dropshell
|
@ -137,7 +137,7 @@ std::set<std::string> list_backups(const std::string &server_name, const std::st
|
|||||||
return backups;
|
return backups;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string backups_dir = gConfig().get_local_backup_path();
|
std::string backups_dir = localpath::backups();
|
||||||
if (backups_dir.empty())
|
if (backups_dir.empty())
|
||||||
return backups;
|
return backups;
|
||||||
|
|
||||||
|
@ -185,7 +185,7 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto local_template_paths = gConfig().get_template_local_paths();
|
auto local_template_paths = gConfig().get_local_template_paths();
|
||||||
if (local_template_paths.empty()) {
|
if (local_template_paths.empty()) {
|
||||||
std::cerr << "Error: No local template paths found" << std::endl;
|
std::cerr << "Error: No local template paths found" << std::endl;
|
||||||
std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
|
std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
|
||||||
@ -252,7 +252,7 @@
|
|||||||
ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty).");
|
ASSERT(mSources.empty(), "Template manager already loaded (sources are not empty).");
|
||||||
ASSERT(gConfig().is_config_set(), "Config not set.");
|
ASSERT(gConfig().is_config_set(), "Config not set.");
|
||||||
ASSERT(!mLoaded, "Template manager already loaded.");
|
ASSERT(!mLoaded, "Template manager already loaded.");
|
||||||
auto local_template_paths = gConfig().get_template_local_paths();
|
auto local_template_paths = gConfig().get_local_template_paths();
|
||||||
if (local_template_paths.empty())
|
if (local_template_paths.empty())
|
||||||
return;
|
return;
|
||||||
for (const auto& path : local_template_paths)
|
for (const auto& path : local_template_paths)
|
||||||
|
@ -62,7 +62,7 @@ namespace localpath {
|
|||||||
|
|
||||||
std::string remote_versions(const std::string &server_name, const std::string &service_name)
|
std::string remote_versions(const std::string &server_name, const std::string &service_name)
|
||||||
{
|
{
|
||||||
std::string template_cache_path = gConfig().get_local_template_cache_path();
|
std::string template_cache_path = localpath::template_cache();
|
||||||
return ((template_cache_path.empty() || service_name.empty()) ? "" :
|
return ((template_cache_path.empty() || service_name.empty()) ? "" :
|
||||||
(template_cache_path+"/remote_versions/"+service_name+".json"));
|
(template_cache_path+"/remote_versions/"+service_name+".json"));
|
||||||
}
|
}
|
||||||
@ -84,6 +84,48 @@ namespace localpath {
|
|||||||
warning << "Couldn't determine user directory" << std::endl;
|
warning << "Couldn't determine user directory" << std::endl;
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string dropshell_files()
|
||||||
|
{
|
||||||
|
return current_user_home() + "/.local/dropshell_files";
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string backups()
|
||||||
|
{
|
||||||
|
return dropshell_files() + "/backups";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string temp_files()
|
||||||
|
{
|
||||||
|
return dropshell_files() + "/temp_files";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string template_cache()
|
||||||
|
{
|
||||||
|
return dropshell_files() + "template_cache";
|
||||||
|
}
|
||||||
|
|
||||||
|
bool create_directories()
|
||||||
|
{
|
||||||
|
std::vector<std::filesystem::path> paths = {
|
||||||
|
dropshell_files(),
|
||||||
|
template_cache(),
|
||||||
|
backups(),
|
||||||
|
temp_files(),
|
||||||
|
agent()};
|
||||||
|
for (auto &p : gConfig().get_local_server_definition_paths())
|
||||||
|
paths.push_back(p);
|
||||||
|
|
||||||
|
for (auto &p : paths)
|
||||||
|
if (!std::filesystem::exists(p))
|
||||||
|
{
|
||||||
|
info << "Creating directory: " << p << std::endl;
|
||||||
|
std::filesystem::create_directories(p);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace localpath
|
} // namespace localpath
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------
|
||||||
|
@ -18,6 +18,23 @@ namespace dropshell {
|
|||||||
// |-- files_for_remote_agent
|
// |-- files_for_remote_agent
|
||||||
// |-- (other agent files, including _allservicesstatus.sh)
|
// |-- (other agent files, including _allservicesstatus.sh)
|
||||||
|
|
||||||
|
// ~/.local/dropshell_files
|
||||||
|
// |-- backups
|
||||||
|
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
|
||||||
|
// |-- temp_files
|
||||||
|
// |-- template_cache
|
||||||
|
// |-- templates
|
||||||
|
// | |-- <template_name>.json
|
||||||
|
// | |-- <template_name>
|
||||||
|
// | |-- (...script files...)
|
||||||
|
// | |-- _default.env
|
||||||
|
// | |-- config
|
||||||
|
// | |-- service.env
|
||||||
|
// | |-- .template_info.env
|
||||||
|
// | |-- (...other service config files...)
|
||||||
|
// |-- remote_versions
|
||||||
|
// | |-- server_name-service_name.json
|
||||||
|
|
||||||
// server_definition_path
|
// server_definition_path
|
||||||
// |-- <server_name>
|
// |-- <server_name>
|
||||||
// |-- server.json
|
// |-- server.json
|
||||||
@ -27,23 +44,7 @@ namespace dropshell {
|
|||||||
// |-- .template_info.env
|
// |-- .template_info.env
|
||||||
// |-- (...other config files for specific server&service...)
|
// |-- (...other config files for specific server&service...)
|
||||||
|
|
||||||
// backup path
|
|
||||||
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
|
|
||||||
|
|
||||||
// temp files path
|
|
||||||
|
|
||||||
// template cache path
|
|
||||||
// |-- templates
|
|
||||||
// | |-- <template_name>.json
|
|
||||||
// | |-- <template_name>
|
|
||||||
// | |-- (...script files...)
|
|
||||||
// | |-- _default.env
|
|
||||||
// | |-- config
|
|
||||||
// | |-- service.env
|
|
||||||
// | |-- .template_info.env
|
|
||||||
// | |-- (...other service config files...)
|
|
||||||
// |-- remote_versions
|
|
||||||
// | |-- server_name-service_name.json
|
|
||||||
|
|
||||||
namespace localfile {
|
namespace localfile {
|
||||||
// ~/.config/dropshell/dropshell.json
|
// ~/.config/dropshell/dropshell.json
|
||||||
@ -62,6 +63,14 @@ namespace dropshell {
|
|||||||
std::string agent();
|
std::string agent();
|
||||||
std::string files_for_remote_agent();
|
std::string files_for_remote_agent();
|
||||||
std::string current_user_home();
|
std::string current_user_home();
|
||||||
|
|
||||||
|
std::string dropshell_files();
|
||||||
|
std::string backups();
|
||||||
|
std::string temp_files();
|
||||||
|
std::string template_cache();
|
||||||
|
|
||||||
|
|
||||||
|
bool create_directories();
|
||||||
} // namespace local
|
} // namespace local
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user