Big Refactor
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
#include "utils/utils.hpp"
|
||||
#include "services.hpp"
|
||||
#include "contrib/base64.hpp"
|
||||
#include "templates.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@ -76,10 +77,7 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se
|
||||
std::string script_path = remote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(get_local_service_env_path(mServer_name,service_name));
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
env_vars["CONFIG_PATH"] = remote_service_config_path;
|
||||
get_all_service_env_vars(service_name, env_vars);
|
||||
|
||||
std::string argstr = quote(remote_service_config_path);
|
||||
for (const auto& arg : args) {
|
||||
@ -91,6 +89,40 @@ std::string server_env::construct_standard_command_run_cmd(const std::string &se
|
||||
return run_cmd;
|
||||
}
|
||||
|
||||
void server_env::get_all_service_env_vars(const std::string &service_name, std::map<std::string, std::string> & all_env_vars) const
|
||||
{
|
||||
all_env_vars.clear();
|
||||
|
||||
// add in some handy variables.
|
||||
all_env_vars["CONFIG_PATH"] = get_remote_service_config_path(mServer_name,service_name);
|
||||
all_env_vars["SERVER"] = mServer_name;
|
||||
all_env_vars["SERVICE"] = service_name;
|
||||
|
||||
|
||||
{ // load service.env from the service on this machine.
|
||||
std::map<std::string, std::string> env_vars;
|
||||
envmanager env_manager(get_local_service_env_path(mServer_name,service_name));
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
|
||||
{ // load _default.env from the template on this machine.
|
||||
std::map<std::string, std::string> env_vars;
|
||||
ServiceInfo service_info = get_service_info(mServer_name, service_name);
|
||||
std::string defaultenvpath = service_info.local_template_default_env_path;
|
||||
if (std::filesystem::exists(defaultenvpath)) {
|
||||
envmanager env_manager(defaultenvpath);
|
||||
env_manager.load();
|
||||
env_manager.get_all_variables(env_vars);
|
||||
all_env_vars.merge(env_vars);
|
||||
}
|
||||
else
|
||||
std::cerr << "Warning: _default.env not found in template: " << defaultenvpath << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool server_env::check_remote_dir_exists(const std::string &dir_path) const
|
||||
{
|
||||
sCommand scommand("test -d " + quote(dir_path));
|
||||
@ -125,9 +157,6 @@ bool server_env::check_remote_items_exist(const std::vector<std::string> &file_p
|
||||
bool server_env::execute_ssh_command(const sCommand& command) const {
|
||||
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
|
||||
bool okay = execute_local_command(full_cmd);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
@ -135,9 +164,6 @@ bool server_env::execute_ssh_command_and_capture_output(const sCommand& command,
|
||||
{
|
||||
std::string full_cmd = construct_ssh_cmd() + " " + quote(command.construct_safecmd());
|
||||
bool okay = execute_local_command_and_capture_output(full_cmd, output);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
@ -145,17 +171,11 @@ bool server_env::run_remote_template_command(const std::string &service_name, co
|
||||
{
|
||||
std::string full_cmd = construct_standard_command_run_cmd(service_name, command, args, silent);
|
||||
bool okay = execute_ssh_command(full_cmd);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on remote server: " << full_cmd << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
bool server_env::execute_local_command(const sCommand& command) {
|
||||
bool okay = (system(command.construct_safecmd().c_str()) == 0);
|
||||
if (!okay) {
|
||||
std::cerr << "Error: Failed to execute command on local machine: " << command.construct_safecmd() << std::endl;
|
||||
}
|
||||
return okay;
|
||||
}
|
||||
|
||||
|
@ -73,6 +73,9 @@ class server_env {
|
||||
std::string construct_ssh_cmd() const;
|
||||
std::string construct_standard_command_run_cmd(const std::string& service_name, const std::string& command, std::vector<std::string> args, bool silent) const;
|
||||
|
||||
private:
|
||||
void get_all_service_env_vars(const std::string& service_name, std::map<std::string, std::string> & all_env_vars) const;
|
||||
|
||||
private:
|
||||
std::string mServer_name;
|
||||
std::map<std::string, std::string> variables;
|
||||
|
@ -95,6 +95,7 @@ ServiceInfo get_service_info(const std::string &server_name, const std::string &
|
||||
|
||||
// find the template path
|
||||
service.local_template_path = tinfo.local_template_path;
|
||||
service.local_template_default_env_path = tinfo.local_template_default_env_path;
|
||||
|
||||
return service;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ namespace dropshell {
|
||||
std::string template_name;
|
||||
std::string local_service_path;
|
||||
std::string local_template_path;
|
||||
std::string local_template_default_env_path;
|
||||
};
|
||||
|
||||
std::vector<ServiceInfo> get_server_services_info(const std::string& server_name);
|
||||
|
@ -27,6 +27,7 @@ bool get_templates(std::vector<template_info>& templates) {
|
||||
template_info info;
|
||||
info.template_name = entry.path().filename().string();
|
||||
info.local_template_path = entry.path().string();
|
||||
info.local_template_default_env_path = entry.path() / "_default.env";
|
||||
|
||||
// Check if template with same name already exists
|
||||
bool duplicate = false;
|
||||
|
@ -8,6 +8,7 @@ class template_info {
|
||||
public:
|
||||
std::string template_name;
|
||||
std::string local_template_path;
|
||||
std::string local_template_default_env_path;
|
||||
};
|
||||
|
||||
// templates are stored in two locations:
|
||||
|
Reference in New Issue
Block a user