Customisable backup directory.
This commit is contained in:
parent
3c99ad1503
commit
fa7236b4f5
@ -6,6 +6,7 @@
|
|||||||
#include <boost/property_tree/ini_parser.hpp>
|
#include <boost/property_tree/ini_parser.hpp>
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
#include "utils/envmanager.hpp"
|
#include "utils/envmanager.hpp"
|
||||||
|
#include "utils/utils.hpp"
|
||||||
|
|
||||||
namespace fs = boost::filesystem;
|
namespace fs = boost::filesystem;
|
||||||
namespace pt = boost::property_tree;
|
namespace pt = boost::property_tree;
|
||||||
@ -18,6 +19,7 @@ config *get_global_config() {
|
|||||||
return gConfig;
|
return gConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
config::config() {
|
config::config() {
|
||||||
}
|
}
|
||||||
config::~config() {
|
config::~config() {
|
||||||
@ -32,12 +34,18 @@ bool config::load_config() {
|
|||||||
if (!config_env.load())
|
if (!config_env.load())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
std::string mDirectories = config_env.get_variable_substituted("local.config.directories");
|
std::string directories = config_env.get_variable_substituted("local.config.directories");
|
||||||
if (mDirectories.empty())
|
if (directories.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Split the directories string into a vector of strings
|
// Split the directories string into a vector of strings
|
||||||
mLocalConfigPaths = string2multi(mDirectories);
|
mLocalConfigPaths = string2multi(directories);
|
||||||
|
|
||||||
|
mLocalBackupPath = config_env.get_variable_substituted("local.backup.directory");
|
||||||
|
|
||||||
|
// legacy config file conversion
|
||||||
|
if (mLocalBackupPath.empty() && mLocalConfigPaths.size()>0)
|
||||||
|
mLocalBackupPath = mLocalConfigPaths[0] + "/backups";
|
||||||
|
|
||||||
//std::cout << "Local config path: " << mLocalConfigPath << std::endl;
|
//std::cout << "Local config path: " << mLocalConfigPath << std::endl;
|
||||||
return true;
|
return true;
|
||||||
@ -56,12 +64,13 @@ void config::save_config()
|
|||||||
envmanager config_env(config_path);
|
envmanager config_env(config_path);
|
||||||
|
|
||||||
config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths));
|
config_env.set_variable("local.config.directories", multi2string(mLocalConfigPaths));
|
||||||
|
config_env.set_variable("local.backup.path", mLocalBackupPath);
|
||||||
config_env.save();
|
config_env.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool config::is_config_set() const
|
bool config::is_config_set() const
|
||||||
{
|
{
|
||||||
return !mLocalConfigPaths.empty();
|
return !mLocalConfigPaths.empty() && !mLocalBackupPath.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -91,9 +100,17 @@ bool config::add_local_config_directory(const std::string &path)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mLocalBackupPath.empty())
|
||||||
|
mLocalBackupPath = path_str + "/backups";
|
||||||
|
|
||||||
std::cerr << "Warning: The local config directory is already registered: " << path_str << std::endl;
|
std::cerr << "Warning: The local config directory is already registered: " << path_str << std::endl;
|
||||||
std::cerr << "No changes made to the DropShell configuration." << std::endl;
|
std::cerr << "No changes made to the DropShell configuration." << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string &config::get_local_backup_path() const
|
||||||
|
{
|
||||||
|
return mLocalBackupPath;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -17,8 +17,11 @@ class config {
|
|||||||
const std::vector<std::string> & get_local_config_directories() const;
|
const std::vector<std::string> & get_local_config_directories() const;
|
||||||
bool add_local_config_directory(const std::string& path);
|
bool add_local_config_directory(const std::string& path);
|
||||||
|
|
||||||
|
const std::string & get_local_backup_path() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::string> mLocalConfigPaths;
|
std::vector<std::string> mLocalConfigPaths;
|
||||||
|
std::string mLocalBackupPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
10
src/main.cpp
10
src/main.cpp
@ -29,7 +29,7 @@ void print_help() {
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl;
|
std::cout << "Service commands: (if no service is specified, all services for the server are affected)" << std::endl;
|
||||||
std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Non-destructive." << std::endl;
|
std::cout << " install SERVER [SERVICE] Install/reinstall/update service(s). Non-destructive." << std::endl;
|
||||||
std::cout << " edit SERVER [SERVICE] Edit the configuration of the server/service." << std::endl;
|
std::cout << " edit [SERVER] [SERVICE] Edit the configuration of dropshell/server/service." << std::endl;
|
||||||
std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s)." << std::endl;
|
std::cout << " COMMAND SERVER [SERVICE] Run a command on service(s)." << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
std::cout << "Standard commands: install, uninstall, backup, restore, start, stop" << std::endl;
|
std::cout << "Standard commands: install, uninstall, backup, restore, start, stop" << std::endl;
|
||||||
@ -237,11 +237,9 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
if (cmd == "edit" && argc < 4) {
|
if (cmd == "edit" && argc < 4) {
|
||||||
if (argc < 3)
|
if (argc < 3)
|
||||||
{
|
dropshell::edit_file(dropshell::get_local_dropshell_config_path(), "Please ensure any directories you have introduced in the config file exist.");
|
||||||
std::cerr << "Error: edit requires a server name and optionally service name" << std::endl;
|
else
|
||||||
return 1;
|
dropshell::edit_server(argv[2]);
|
||||||
}
|
|
||||||
dropshell::edit_server(argv[2]);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,7 +325,7 @@ bool service_runner::backup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create backups directory locally if it doesn't exist
|
// Create backups directory locally if it doesn't exist
|
||||||
std::string local_backups_dir = get_local_config_backups_path(0);
|
std::string local_backups_dir = get_local_backup_path();
|
||||||
if (local_backups_dir.empty()) {
|
if (local_backups_dir.empty()) {
|
||||||
std::cerr << "Error: Local backups directory not found - is DropShell initialised?" << std::endl;
|
std::cerr << "Error: Local backups directory not found - is DropShell initialised?" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
@ -38,12 +38,25 @@ int getNumConfigDirectories()
|
|||||||
std::string get_local_config_path(int index)
|
std::string get_local_config_path(int index)
|
||||||
{
|
{
|
||||||
config *cfg = get_global_config();
|
config *cfg = get_global_config();
|
||||||
|
if (!cfg)
|
||||||
|
return std::string();
|
||||||
|
|
||||||
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
|
std::vector<std::string> local_config_directories = cfg->get_local_config_directories();
|
||||||
if (index < 0 || index >= local_config_directories.size())
|
if (index < 0 || index >= local_config_directories.size())
|
||||||
return std::string();
|
return std::string();
|
||||||
return local_config_directories[index];
|
return local_config_directories[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string get_local_backup_path()
|
||||||
|
{
|
||||||
|
config *cfg = get_global_config();
|
||||||
|
if (!cfg)
|
||||||
|
return std::string();
|
||||||
|
|
||||||
|
return cfg->get_local_backup_path();
|
||||||
|
}
|
||||||
|
|
||||||
std::string get_local_config_templates_path(int index)
|
std::string get_local_config_templates_path(int index)
|
||||||
{
|
{
|
||||||
std::string config_path = get_local_config_path(index);
|
std::string config_path = get_local_config_path(index);
|
||||||
|
@ -9,6 +9,8 @@ namespace dropshell {
|
|||||||
std::string get_local_dropshell_config_path();
|
std::string get_local_dropshell_config_path();
|
||||||
std::string get_local_system_templates_path();
|
std::string get_local_system_templates_path();
|
||||||
|
|
||||||
|
std::string get_local_backup_path();
|
||||||
|
|
||||||
int getNumConfigDirectories();
|
int getNumConfigDirectories();
|
||||||
std::string get_local_config_path(int index);
|
std::string get_local_config_path(int index);
|
||||||
std::string get_local_config_templates_path(int index);
|
std::string get_local_config_templates_path(int index);
|
||||||
|
@ -36,7 +36,7 @@ bool envmanager::load() {
|
|||||||
std::string value = line.substr(pos + 1);
|
std::string value = line.substr(pos + 1);
|
||||||
|
|
||||||
// trim whitespace from the key and value
|
// trim whitespace from the key and value
|
||||||
m_variables[trim(key)] = trim(value);
|
m_variables[dequote(trim(key))] = dequote(trim(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
@ -50,13 +50,13 @@ void envmanager::save() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& pair : m_variables) {
|
for (const auto& pair : m_variables) {
|
||||||
file << pair.first << "=" << pair.second << std::endl;
|
file << pair.first << "=" << quote(pair.second) << std::endl;
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string envmanager::get_variable(std::string key) const {
|
std::string envmanager::get_variable(std::string key) const {
|
||||||
key = trim(key);
|
key = dequote(trim(key));
|
||||||
|
|
||||||
// Use case-insensitive comparison to find the key
|
// Use case-insensitive comparison to find the key
|
||||||
for (const auto& pair : m_variables) {
|
for (const auto& pair : m_variables) {
|
||||||
@ -91,7 +91,7 @@ void envmanager::add_variables(std::map<std::string, std::string> variables) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void envmanager::set_variable(std::string key, std::string value) {
|
void envmanager::set_variable(std::string key, std::string value) {
|
||||||
m_variables[trim(key)] = trim(value);
|
m_variables[dequote(trim(key))] = dequote(trim(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void envmanager::clear_variables() {
|
void envmanager::clear_variables() {
|
||||||
@ -115,7 +115,8 @@ std::string envmanager::expand_patterns(std::string str) const {
|
|||||||
|
|
||||||
result = result.replace(match.position(), match.length(), value);
|
result = result.replace(match.position(), match.length(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dequote the result
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user