Broken
This commit is contained in:
parent
3fa5d5076a
commit
3c96da9e18
@ -81,6 +81,54 @@ bool server_service::init(const std::string& server_name, const std::string& ser
|
|||||||
return true;
|
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() {
|
bool server_service::install() {
|
||||||
if (!m_server_env) {
|
if (!m_server_env) {
|
||||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||||
@ -95,34 +143,27 @@ bool server_service::install() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct SSH command
|
std::string script_dir = get_script_dir();
|
||||||
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() << " ";
|
|
||||||
|
|
||||||
// Create service directory
|
// Create service directory
|
||||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
std::string mkdir_cmd = "'mkdir -p " + script_dir + "'";
|
||||||
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + service_dir + "/template'";
|
if (!execute_ssh_command(mkdir_cmd, "Failed to create service directory")) {
|
||||||
if (system(mkdir_cmd.c_str()) != 0) {
|
|
||||||
std::cerr << "Error: Failed to create service directory" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if rsync is installed on remote host
|
// Check if rsync is installed on remote host
|
||||||
std::string check_rsync_cmd = ssh_cmd.str() + "'which rsync > /dev/null 2>&1'";
|
std::string check_rsync_cmd = "'which rsync > /dev/null 2>&1'";
|
||||||
if (system(check_rsync_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(check_rsync_cmd, "rsync is not installed on the remote host")) {
|
||||||
std::cerr << "Error: rsync is not installed on the remote host" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy template files, preserving the directory structure and file permissions
|
// Copy template files
|
||||||
std::cout << "Copying template files from " << info.path << " to " << service_dir << "/template/" << std::endl;
|
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() + "' " +
|
std::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " +
|
||||||
info.path + "/ " +
|
info.path + "/ " +
|
||||||
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
||||||
service_dir + "/template/";
|
script_dir + "/";
|
||||||
if (system(rsync_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(rsync_cmd, "Failed to copy template files")) {
|
||||||
std::cerr << "Error: Failed to copy template files" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,21 +177,15 @@ bool server_service::install() {
|
|||||||
std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " +
|
std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " +
|
||||||
service_env.string() + " " +
|
service_env.string() + " " +
|
||||||
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
||||||
service_dir + "/" + m_service_name + ".env";
|
get_env_path();
|
||||||
if (system(scp_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(scp_cmd, "Failed to copy service environment file")) {
|
||||||
std::cerr << "Error: Failed to copy service environment file" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run install script
|
// Run install script
|
||||||
std::string install_cmd = ssh_cmd.str() + "'cd " + service_dir + "/template && /bin/bash _install.sh " +
|
std::string install_cmd = "'cd " + script_dir +
|
||||||
service_dir + "/" + m_service_name + ".env'";
|
" && /bin/bash _install.sh " + get_env_path() + "'";
|
||||||
if (system(install_cmd.c_str()) != 0) {
|
return execute_ssh_command(install_cmd, "Failed to run install script");
|
||||||
std::cerr << "Error: Failed to run install script" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool server_service::run_command(const std::string& command) {
|
bool server_service::run_command(const std::string& command) {
|
||||||
@ -159,46 +194,29 @@ bool server_service::run_command(const std::string& command) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if service directory exists
|
std::string ssh_cmd = construct_ssh_cmd();
|
||||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
std::string script_dir = get_script_dir();
|
||||||
std::stringstream ssh_cmd;
|
std::string script_path = script_dir + "/" + command + ".sh";
|
||||||
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 script_path_and_command = script_dir + "/" + command + ".sh";
|
|
||||||
std::string env_path = service_dir + "/" + m_service_name + ".env";
|
|
||||||
|
|
||||||
// Check if service directory exists
|
// Check if service directory exists
|
||||||
std::string check_dir_cmd = ssh_cmd.str() + "'test -d " + service_dir + "'";
|
if (!check_service_dir_exists(ssh_cmd)) {
|
||||||
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 false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if command script exists
|
// Check if command script exists
|
||||||
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path_and_command + "'";
|
if (!check_remote_file_exists(ssh_cmd, script_path)) {
|
||||||
if (system(check_script_cmd.c_str()) != 0) {
|
|
||||||
std::cerr << "Error: Command script '" << command << ".sh' not found" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if env file exists
|
// Check if env file exists
|
||||||
std::string check_env_cmd = ssh_cmd.str() + "'test -f " + env_path + "'";
|
if (!check_remote_file_exists(ssh_cmd, get_env_path())) {
|
||||||
if (system(check_env_cmd.c_str()) != 0) {
|
|
||||||
std::cerr << "Error: Service environment file not found on server" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the command
|
// Run the command
|
||||||
std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir +
|
std::string run_cmd = "'cd " + script_dir +
|
||||||
" && /bin/bash " + script_path_and_command + " "+ env_path + "'";
|
" && /bin/bash " + script_path + " " + get_env_path() + "'";
|
||||||
if (system(run_cmd.c_str()) != 0) {
|
return execute_ssh_command(run_cmd, "Command returned error code: " + script_path);
|
||||||
std::cerr << "Command returned error code: " << script_path_and_command << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool server_service::backup() {
|
bool server_service::backup() {
|
||||||
@ -207,42 +225,18 @@ bool server_service::backup() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if service directory exists
|
std::string ssh_cmd = construct_ssh_cmd();
|
||||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
std::string script_dir = get_script_dir();
|
||||||
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 script_path = script_dir + "/backup.sh";
|
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
|
// Check if basic installed stuff is in place.
|
||||||
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path + "'";
|
if (!check_service_dir_exists(ssh_cmd) || !check_remote_file_exists(ssh_cmd, script_path) || !check_remote_file_exists(ssh_cmd, get_env_path()))
|
||||||
if (system(check_script_cmd.c_str()) != 0) {
|
|
||||||
std::cerr << "Error: Backup script not found" << std::endl;
|
|
||||||
return false;
|
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
|
// Create backups directory on server if it doesn't exist
|
||||||
std::string server_backups_dir = m_server_env->get_DROPSHELL_DIR() + "/backups";
|
std::string server_backups_dir = m_server_env->get_DROPSHELL_DIR() + "/backups";
|
||||||
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + server_backups_dir + "'";
|
std::string mkdir_cmd = "'mkdir -p " + server_backups_dir + "'";
|
||||||
if (system(mkdir_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(mkdir_cmd, "Failed to create backups directory on server")) {
|
||||||
std::cerr << "Error: Failed to create backups directory on server" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,10 +263,9 @@ bool server_service::backup() {
|
|||||||
std::string local_backup_path = (local_backups_dir / backup_filename).string();
|
std::string local_backup_path = (local_backups_dir / backup_filename).string();
|
||||||
|
|
||||||
// Run backup script
|
// Run backup script
|
||||||
std::string backup_cmd = ssh_cmd.str() + "'cd " + script_dir +
|
std::string backup_cmd = "'cd " + script_dir +
|
||||||
" && /bin/bash _backup.sh " + env_path + " " + server_backup_path + "'";
|
" && /bin/bash _backup.sh " + get_env_path() + " " + server_backup_path + "'";
|
||||||
if (system(backup_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(backup_cmd, "Backup script failed")) {
|
||||||
std::cerr << "Error: Backup script failed" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,8 +273,7 @@ bool server_service::backup() {
|
|||||||
std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " +
|
std::string scp_cmd = "scp -P " + m_server_env->get_SSH_PORT() + " " +
|
||||||
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
||||||
server_backup_path + " " + local_backup_path;
|
server_backup_path + " " + local_backup_path;
|
||||||
if (system(scp_cmd.c_str()) != 0) {
|
if (!execute_ssh_command(scp_cmd, "Failed to copy backup file from server")) {
|
||||||
std::cerr << "Error: Failed to copy backup file from server" << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,17 +292,8 @@ bool server_service::is_healthy()
|
|||||||
std::string script_dir = service_dir + "/template";
|
std::string script_dir = service_dir + "/template";
|
||||||
std::string env_path = service_dir + "/" + m_service_name + ".env";
|
std::string env_path = service_dir + "/" + m_service_name + ".env";
|
||||||
|
|
||||||
std::stringstream ssh_cmd;
|
// Run status script, does not display output.
|
||||||
ssh_cmd << "ssh -p " << m_server_env->get_SSH_PORT() << " "
|
return execute_ssh_command("'cd " + script_dir + " && /bin/bash status.sh " + env_path + " > /dev/null 2>&1'","");
|
||||||
<< 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -54,6 +54,15 @@ class server_service {
|
|||||||
std::string m_server_name;
|
std::string m_server_name;
|
||||||
std::string m_service_name;
|
std::string m_service_name;
|
||||||
std::unique_ptr<server_env> m_server_env;
|
std::unique_ptr<server_env> 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
|
} // namespace dropshell
|
||||||
|
Loading…
x
Reference in New Issue
Block a user