125 lines
4.8 KiB
C++
125 lines
4.8 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/assert.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={});
|
|
|
|
// 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();
|
|
|
|
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 and restore
|
|
bool backup(bool silent=false);
|
|
bool restore(std::string backup_file, bool silent=false);
|
|
|
|
// nuke the service
|
|
bool nuke(); // 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
|
|
void interactive_ssh_service();
|
|
|
|
// edit the service configuration file
|
|
void edit_service_config();
|
|
|
|
|
|
public:
|
|
// utility functions
|
|
static std::string get_latest_backup_file(const std::string& server, const std::string& service);
|
|
static void interactive_ssh(const std::string & server_name, const std::string & command);
|
|
static void edit_server(const std::string & server_name);
|
|
static bool edit_file(const std::string & file_path);
|
|
static std::map<std::string, ServiceStatus> get_all_services_status(std::string server_name);
|
|
static std::string HealthStatus2String(HealthStatus status);
|
|
|
|
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
|