dropshell/src/service_runner.hpp
Your Name 8fc3384c03
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 18s
Tidy
2025-05-12 20:11:08 +12:00

103 lines
3.6 KiB
C++

// server_service.hpp
//
// manage a service on a server
//
#ifndef SERVICE_RUNNER_HPP
#define SERVICE_RUNNER_HPP
#include <string>
#include <vector>
#include <memory>
#include "server_env_manager.hpp"
#include "services.hpp"
#include "utils/utils.hpp"
#include "utils/hash.hpp"
namespace dropshell {
typedef enum HealthStatus {
HEALTHY,
UNHEALTHY,
NOTINSTALLED,
ERROR,
UNKNOWN
} HealthStatus;
typedef struct ServiceStatus {
HealthStatus health;
std::vector<int> ports;
} ServiceStatus;
class service_runner {
public:
service_runner(const std::string& server_name, const std::string& service_name);
bool isValid() const { return mValid; }
// 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, std::vector<std::string> additional_args={}, std::map<std::string, std::string> env_vars={});
// check health of service. Silent.
// 1. run status.sh on the server
// 2. return the output of the status.sh script
HealthStatus is_healthy();
std::string healthtick();
std::string healthmark();
public:
// backup and restore
bool backup(bool silent=false);
bool restore(std::string backup_file, bool silent=false);
// nuke the service
bool nuke(bool silent=false); // nukes all data for this service on the remote server
bool fullnuke(); // nuke all data for this service on the remote server, and then nukes all the local service definitionfiles
// launch an interactive ssh session on a server or service
// replaces the current dropshell process with the ssh process
bool interactive_ssh_service();
bool scp_file_to_remote(const std::string& local_path, const std::string& remote_path, bool silent=false);
bool scp_file_from_remote(const std::string& remote_path, const std::string& local_path, bool silent=false);
public:
// utility functions
static std::string get_latest_backup_file(const std::string& server, const std::string& service);
static bool interactive_ssh(const std::string & server_name, const std::string & command);
static std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
private:
std::string mServer;
server_env_manager mServerEnv;
LocalServiceInfo mServiceInfo;
std::string mService;
bool mValid;
// Helper methods
public:
};
class cRemoteTempFolder {
public:
cRemoteTempFolder(const server_env_manager & server_env); // create a temp folder on the remote server
~cRemoteTempFolder(); // delete the temp folder on the remote server
std::string path() const; // get the path to the temp folder on the remote server
private:
std::string mPath;
const server_env_manager & mServerEnv;
};
} // namespace dropshell
#endif // SERVICE_RUNNER_HPP