dropshell/src/service_runner.hpp
2025-04-23 21:50:04 +12:00

79 lines
3.4 KiB
C++

// server_service.hpp
//
// manage a service on a server
//
#include <string>
#include <vector>
#include <memory>
#include "server_env.hpp"
#include "services.hpp"
namespace dropshell {
class service_runner {
public:
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 that service directory/template (from the templates directory for the specified server, using templates.hpp to identify the path)
// 4. copying the {service_name}.env file to the service directory (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();
// 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
// do a lot of checks, such as:
// checking that we can ssh to the server.
// checking whether the service directory exists on the server.
// checking that the command exists in the service directory.
// checking that the command is a valid .sh file.
// 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
bool is_healthy();
// get the ports of the service
// 1. run _ports.sh on the server
// 2. return the output of the _ports.sh script, handling a port per line or comma separated.
std::vector<int> get_ports();
std::string healthtick();
private:
std::string m_server_name;
ServiceInfo m_service_info;
std::unique_ptr<server_env> m_server_env;
std::string mRemote_service_path;
std::string mRemote_service_config_path;
std::string mRemote_service_template_path;
std::string mRemote_service_env_file;
// Helper methods
std::string construct_ssh_cmd() const;
bool check_remote_dir_exists(const std::string& ssh_cmd, const std::string& dir_path) 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;
bool execute_local_command(const std::string& command, const std::string& error_msg) const;
void maketitle(const std::string& title) const;
};
} // namespace dropshell