tidy
This commit is contained in:
49
src/main.cpp
49
src/main.cpp
@ -128,7 +128,7 @@ int main(int argc, char* argv[]) {
|
||||
commands.insert("init");
|
||||
if (cfg->is_config_set())
|
||||
commands.merge(std::set<std::string>{
|
||||
"servers","templates","install","uninstall","backup"
|
||||
"servers","templates"
|
||||
});
|
||||
|
||||
for (const auto& command : commands) {
|
||||
@ -188,52 +188,7 @@ int main(int argc, char* argv[]) {
|
||||
dropshell::list_templates();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "install" || cmd == "uninstall") {
|
||||
std::string server_name;
|
||||
std::vector<dropshell::ServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
std::cerr << "Error: " << cmd << " command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
for (const auto& service_info : servicelist) {
|
||||
dropshell::service_runner service;
|
||||
if (!service.init(server_name, service_info.service_name)) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
bool success = ((cmd=="install") ? service.install() : service.uninstall());
|
||||
if (!success) {
|
||||
std::cerr << "Error: Failed to " << cmd << " service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == "backup") {
|
||||
std::string server_name;
|
||||
std::vector<dropshell::ServiceInfo> servicelist;
|
||||
if (!parseargs(safearg(argc, argv, 2), safearg(argc, argv, 3), server_name, servicelist)) {
|
||||
std::cerr << "Error: backup command requires server name and optionally service name" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (const auto& service_info : servicelist) {
|
||||
dropshell::service_runner service;
|
||||
if (!service.init(server_name, service_info.service_name)) {
|
||||
std::cerr << "Error: Failed to initialize service" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (!service.backup()) {
|
||||
std::cerr << "Backup failed." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// handle running a command.
|
||||
for (const auto& command : commands) {
|
||||
if (cmd == command) {
|
||||
|
@ -112,29 +112,23 @@ void service_runner::maketitle(const std::string& title) const {
|
||||
|
||||
bool service_runner::install() {
|
||||
maketitle("Installing " + m_service_info.service_name + " (" + m_service_info.template_name + ") on " + m_server_name);
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!m_server_env) return false; // should never hit this.
|
||||
|
||||
// Check if template exists
|
||||
template_info tinfo;
|
||||
if (!get_template_info(m_service_info.template_name, tinfo)) {
|
||||
std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl;
|
||||
if (!get_template_info(m_service_info.template_name, tinfo))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create service directory
|
||||
std::string mkdir_cmd = "'mkdir -p " + mRemote_service_path + "'";
|
||||
if (!execute_ssh_command(mkdir_cmd, "Failed to create service directory")) {
|
||||
if (!execute_ssh_command(mkdir_cmd, "Failed to create service directory"))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if rsync is installed on remote host
|
||||
std::string check_rsync_cmd = "'which rsync > /dev/null 2>&1'";
|
||||
if (!execute_ssh_command(check_rsync_cmd, "rsync is not installed on the remote host")) {
|
||||
if (!execute_ssh_command(check_rsync_cmd, "rsync is not installed on the remote host"))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy template files
|
||||
{
|
||||
@ -178,17 +172,7 @@ bool service_runner::install() {
|
||||
bool service_runner::uninstall() {
|
||||
maketitle("Uninstalling " + m_service_info.service_name + " (" + m_service_info.template_name + ") on " + m_server_name);
|
||||
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. Check if template exists
|
||||
template_info tinfo;
|
||||
if (!get_template_info(m_service_info.template_name, tinfo)) {
|
||||
std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!m_server_env) return false; // should never hit this.
|
||||
|
||||
// 2. Check if service directory exists on server
|
||||
if (!check_remote_dir_exists(mRemote_service_path)) {
|
||||
@ -226,12 +210,20 @@ bool service_runner::run_command(const std::string& command) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
template_info tinfo;
|
||||
if (!get_template_info(m_service_info.template_name, tinfo)) {
|
||||
std::cerr << "Error: Template '" << m_service_info.template_name << "' not found" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!template_command_exists(m_service_info.template_name, command)) {
|
||||
std::cout << "No command script for " << m_service_info.template_name << " : " << command << std::endl;
|
||||
return true; // nothing to run.
|
||||
}
|
||||
|
||||
// install doesn't require anything on the server yet.
|
||||
if (command == "install")
|
||||
return install();
|
||||
|
||||
std::string script_path = mRemote_service_template_path + "/" + command + ".sh";
|
||||
|
||||
// Check if service directory exists
|
||||
@ -243,13 +235,18 @@ bool service_runner::run_command(const std::string& command) {
|
||||
if (!check_remote_file_exists(script_path)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Check if env file exists
|
||||
if (!check_remote_file_exists(mRemote_service_env_file)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Run the command
|
||||
|
||||
if (command == "uninstall")
|
||||
return uninstall();
|
||||
if (command == "backup")
|
||||
return backup();
|
||||
|
||||
// Run the generic command
|
||||
std::string run_cmd = "'cd " + mRemote_service_template_path +
|
||||
" && /bin/bash " + script_path + " " + mRemote_service_env_file + "'";
|
||||
return execute_ssh_command(run_cmd, "Command returned error code: " + script_path);
|
||||
@ -258,10 +255,7 @@ bool service_runner::run_command(const std::string& command) {
|
||||
bool service_runner::backup() {
|
||||
maketitle("Backing up " + m_service_info.service_name + " (" + m_service_info.template_name + ") on " + m_server_name);
|
||||
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if (!m_server_env) return false; // should never hit this.
|
||||
|
||||
std::string command = "_backup";
|
||||
|
||||
|
@ -16,23 +16,6 @@ class service_runner {
|
||||
service_runner();
|
||||
bool init(const std::string& server_name, const std::string& service_name);
|
||||
|
||||
// install the service over ssh, using the credentials from server.env (via server_env.hpp), by:
|
||||
// 1. check if the server_name exists, and the service_name refers to a valid template
|
||||
// 2. check if service_name is valid for the server_name
|
||||
// 3. create the service directory on the server at {DROPSHELL_DIR}/{service_name}
|
||||
// 3. copy the template files into {DROPSHELL_DIR}/{service_name}/template (from the templates directory for the specified server, using templates.hpp to identify the path)
|
||||
// 4. copying the local service directory into {DROPSHELL_DIR}/{service_name}/config (from the server directory for the specified server)
|
||||
// 5. running the install.sh script on the server, passing the {service_name}.env file as an argument
|
||||
bool install();
|
||||
|
||||
// uninstall the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// 1. check if the server_name exists, and the service_name refers to a valid template
|
||||
// 2. check if service_name is valid for the server_name
|
||||
// 3. run the uninstall.sh script on the server, passing the {service_name}.env file as an argument
|
||||
// 4.
|
||||
// 1. run the uninstall.sh script on the server, passing the {service_name}.env file as an argument
|
||||
bool uninstall();
|
||||
|
||||
// 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
|
||||
// then run the command, passing the {service_name}.env file as an argument
|
||||
@ -44,14 +27,6 @@ class service_runner {
|
||||
// checking that the {service_name}.env file exists in the service directory.
|
||||
bool run_command(const std::string& command);
|
||||
|
||||
// backup the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// 1. run backup.sh on the server
|
||||
// 2. create a backup file with format server-service-datetime.tgz
|
||||
// 3. store it in the server's DROPSHELL_DIR/backups folder
|
||||
// 4. copy it to the local user_dir/backups folder
|
||||
bool backup();
|
||||
|
||||
|
||||
// check health of service. Silent.
|
||||
// 1. run status.sh on the server
|
||||
// 2. return the output of the status.sh script
|
||||
@ -71,6 +46,33 @@ class service_runner {
|
||||
|
||||
std::string healthtick();
|
||||
std::string healthmark();
|
||||
|
||||
|
||||
private:
|
||||
// install the service over ssh, using the credentials from server.env (via server_env.hpp), by:
|
||||
// 1. check if the server_name exists, and the service_name refers to a valid template
|
||||
// 2. check if service_name is valid for the server_name
|
||||
// 3. create the service directory on the server at {DROPSHELL_DIR}/{service_name}
|
||||
// 3. copy the template files into {DROPSHELL_DIR}/{service_name}/template (from the templates directory for the specified server, using templates.hpp to identify the path)
|
||||
// 4. copying the local service directory into {DROPSHELL_DIR}/{service_name}/config (from the server directory for the specified server)
|
||||
// 5. running the install.sh script on the server, passing the {service_name}.env file as an argument
|
||||
bool install();
|
||||
|
||||
// uninstall the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// 1. check if the server_name exists, and the service_name refers to a valid template
|
||||
// 2. check if service_name is valid for the server_name
|
||||
// 3. run the uninstall.sh script on the server, passing the {service_name}.env file as an argument
|
||||
// 4. remove the service directory from the server
|
||||
bool uninstall();
|
||||
|
||||
// backup the service over ssh, using the credentials from server.env (via server_env.hpp)
|
||||
// 1. run backup.sh on the server
|
||||
// 2. create a backup file with format server-service-datetime.tgz
|
||||
// 3. store it in the server's DROPSHELL_DIR/backups folder
|
||||
// 4. copy it to the local user_dir/backups folder
|
||||
bool backup();
|
||||
|
||||
|
||||
private:
|
||||
std::string m_server_name;
|
||||
ServiceInfo m_service_info;
|
||||
|
Reference in New Issue
Block a user