diff --git a/src/server_service.cpp b/src/server_service.cpp index a3bf2d6..7cf1623 100644 --- a/src/server_service.cpp +++ b/src/server_service.cpp @@ -81,6 +81,54 @@ bool server_service::init(const std::string& server_name, const std::string& ser return true; } +// Helper method implementations +std::string server_service::construct_ssh_cmd() const { + std::stringstream ssh_cmd; + ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " " + << m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " "; + return ssh_cmd.str(); +} + +std::string server_service::get_service_dir() const { + return m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name; +} + +std::string server_service::get_env_path() const { + return get_service_dir() + "/" + m_service_name + ".env"; +} + +std::string server_service::get_script_dir() const { + return get_service_dir() + "/template"; +} + +bool server_service::check_service_dir_exists(const std::string& ssh_cmd) const { + std::string check_dir_cmd = ssh_cmd + "'test -d " + get_service_dir() + "'"; + if (system(check_dir_cmd.c_str()) != 0) { + std::cerr << "Error: Service directory not found on server - has it been installed?" << std::endl; + return false; + } + return true; +} + +bool server_service::check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const { + std::string check_cmd = ssh_cmd + "'test -f " + file_path + "'"; + if (system(check_cmd.c_str()) != 0) { + std::cerr << "Error: File not found: " << file_path << std::endl; + return false; + } + return true; +} + +bool server_service::execute_ssh_command(const std::string& command, const std::string& error_msg) const { + std::string full_cmd = construct_ssh_cmd() + command; + + bool okay = (system(full_cmd.c_str()) == 0); + + if (!okay && !error_msg.empty()) + std::cerr << "Error: " << error_msg << std::endl; + return okay; +} + bool server_service::install() { if (!m_server_env) { std::cerr << "Error: Server service not initialized" << std::endl; @@ -95,34 +143,27 @@ bool server_service::install() { return false; } - // Construct SSH command - std::stringstream ssh_cmd; - ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " " - << m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " "; + std::string script_dir = get_script_dir(); // Create service directory - std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name; - std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + service_dir + "/template'"; - if (system(mkdir_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to create service directory" << std::endl; + std::string mkdir_cmd = "'mkdir -p " + script_dir + "'"; + 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 = ssh_cmd.str() + "'which rsync > /dev/null 2>&1'"; - if (system(check_rsync_cmd.c_str()) != 0) { - std::cerr << "Error: rsync is not installed on the remote host" << std::endl; + 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")) { return false; } - // Copy template files, preserving the directory structure and file permissions - std::cout << "Copying template files from " << info.path << " to " << service_dir << "/template/" << std::endl; + // Copy template files + std::cout << "Copying template files from " << info.path << " to " << script_dir << "/" << std::endl; std::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " + info.path + "/ " + m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + - service_dir + "/template/"; - if (system(rsync_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to copy template files" << std::endl; + script_dir + "/"; + if (!execute_ssh_command(rsync_cmd, "Failed to copy template files")) { return false; } @@ -136,21 +177,15 @@ bool server_service::install() { std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " + service_env.string() + " " + m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + - service_dir + "/" + m_service_name + ".env"; - if (system(scp_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to copy service environment file" << std::endl; + get_env_path(); + if (!execute_ssh_command(scp_cmd, "Failed to copy service environment file")) { return false; } // Run install script - std::string install_cmd = ssh_cmd.str() + "'cd " + service_dir + "/template && /bin/bash _install.sh " + - service_dir + "/" + m_service_name + ".env'"; - if (system(install_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to run install script" << std::endl; - return false; - } - - return true; + std::string install_cmd = "'cd " + script_dir + + " && /bin/bash _install.sh " + get_env_path() + "'"; + return execute_ssh_command(install_cmd, "Failed to run install script"); } bool server_service::run_command(const std::string& command) { @@ -159,46 +194,29 @@ bool server_service::run_command(const std::string& command) { return false; } - // Check if service directory exists - std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name; - std::stringstream ssh_cmd; - ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " " - << m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " "; + std::string ssh_cmd = construct_ssh_cmd(); + std::string script_dir = get_script_dir(); + std::string script_path = script_dir + "/" + command + ".sh"; - std::string script_dir = service_dir + "/template"; - std::string script_path_and_command = script_dir + "/" + command + ".sh"; - std::string env_path = service_dir + "/" + m_service_name + ".env"; - // Check if service directory exists - std::string check_dir_cmd = ssh_cmd.str() + "'test -d " + service_dir + "'"; - if (system(check_dir_cmd.c_str()) != 0) { - std::cerr << "Error: Service directory not found on server - has it been installed?" << std::endl; + if (!check_service_dir_exists(ssh_cmd)) { return false; } // Check if command script exists - std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path_and_command + "'"; - if (system(check_script_cmd.c_str()) != 0) { - std::cerr << "Error: Command script '" << command << ".sh' not found" << std::endl; + if (!check_remote_file_exists(ssh_cmd, script_path)) { return false; } // Check if env file exists - std::string check_env_cmd = ssh_cmd.str() + "'test -f " + env_path + "'"; - if (system(check_env_cmd.c_str()) != 0) { - std::cerr << "Error: Service environment file not found on server" << std::endl; + if (!check_remote_file_exists(ssh_cmd, get_env_path())) { return false; } // Run the command - std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir + - " && /bin/bash " + script_path_and_command + " "+ env_path + "'"; - if (system(run_cmd.c_str()) != 0) { - std::cerr << "Command returned error code: " << script_path_and_command << std::endl; - return false; - } - - return true; + std::string run_cmd = "'cd " + script_dir + + " && /bin/bash " + script_path + " " + get_env_path() + "'"; + return execute_ssh_command(run_cmd, "Command returned error code: " + script_path); } bool server_service::backup() { @@ -207,42 +225,18 @@ bool server_service::backup() { return false; } - // Check if service directory exists - std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name; - std::stringstream ssh_cmd; - ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " " - << m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " "; - - std::string script_dir = service_dir + "/template"; + std::string ssh_cmd = construct_ssh_cmd(); + std::string script_dir = get_script_dir(); std::string script_path = script_dir + "/backup.sh"; - std::string env_path = service_dir + "/" + m_service_name + ".env"; - - // Check if service directory exists - std::string check_dir_cmd = ssh_cmd.str() + "'test -d " + service_dir + "'"; - if (system(check_dir_cmd.c_str()) != 0) { - std::cerr << "Error: Service directory not found on server - has it been installed?" << std::endl; - return false; - } - // Check if backup script exists - std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path + "'"; - if (system(check_script_cmd.c_str()) != 0) { - std::cerr << "Error: Backup script not found" << std::endl; + // Check if basic installed stuff is in place. + if (!check_service_dir_exists(ssh_cmd) || !check_remote_file_exists(ssh_cmd, script_path) || !check_remote_file_exists(ssh_cmd, get_env_path())) return false; - } - - // Check if env file exists - std::string check_env_cmd = ssh_cmd.str() + "'test -f " + env_path + "'"; - if (system(check_env_cmd.c_str()) != 0) { - std::cerr << "Error: Service environment file not found on server" << std::endl; - return false; - } // Create backups directory on server if it doesn't exist std::string server_backups_dir = m_server_env->get_DROPSHELL_DIR() + "/backups"; - std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + server_backups_dir + "'"; - if (system(mkdir_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to create backups directory on server" << std::endl; + std::string mkdir_cmd = "'mkdir -p " + server_backups_dir + "'"; + if (!execute_ssh_command(mkdir_cmd, "Failed to create backups directory on server")) { return false; } @@ -269,10 +263,9 @@ bool server_service::backup() { std::string local_backup_path = (local_backups_dir / backup_filename).string(); // Run backup script - std::string backup_cmd = ssh_cmd.str() + "'cd " + script_dir + - " && /bin/bash _backup.sh " + env_path + " " + server_backup_path + "'"; - if (system(backup_cmd.c_str()) != 0) { - std::cerr << "Error: Backup script failed" << std::endl; + std::string backup_cmd = "'cd " + script_dir + + " && /bin/bash _backup.sh " + get_env_path() + " " + server_backup_path + "'"; + if (!execute_ssh_command(backup_cmd, "Backup script failed")) { return false; } @@ -280,8 +273,7 @@ bool server_service::backup() { std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " + m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" + server_backup_path + " " + local_backup_path; - if (system(scp_cmd.c_str()) != 0) { - std::cerr << "Error: Failed to copy backup file from server" << std::endl; + if (!execute_ssh_command(scp_cmd, "Failed to copy backup file from server")) { return false; } @@ -300,17 +292,8 @@ bool server_service::is_healthy() std::string script_dir = service_dir + "/template"; std::string env_path = service_dir + "/" + m_service_name + ".env"; - std::stringstream ssh_cmd; - ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " " - << m_server_env->get_SSH_USER() << "@" << m_server_env->get_SSH_HOST() << " "; - - // Run status script, does not display output. - std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir + - " && /bin/bash status.sh " + env_path + " > /dev/null 2>&1'"; - if (system(run_cmd.c_str()) != 0) { - return false; - } - return true; + // Run status script, does not display output. + return execute_ssh_command("'cd " + script_dir + " && /bin/bash status.sh " + env_path + " > /dev/null 2>&1'",""); } } // namespace dropshell \ No newline at end of file diff --git a/src/server_service.hpp b/src/server_service.hpp index 62a27a1..31f2db0 100644 --- a/src/server_service.hpp +++ b/src/server_service.hpp @@ -54,6 +54,15 @@ class server_service { std::string m_server_name; std::string m_service_name; std::unique_ptr m_server_env; + + // Helper methods + std::string construct_ssh_cmd() const; + std::string get_service_dir() const; + std::string get_env_path() const; + std::string get_script_dir() const; + bool check_service_dir_exists(const std::string& ssh_cmd) const; + bool check_remote_file_exists(const std::string& ssh_cmd, const std::string& file_path) const; + bool execute_ssh_command(const std::string& command, const std::string& error_msg) const; }; } // namespace dropshell