This commit is contained in:
Your Name 2025-04-21 15:06:39 +12:00
parent a65fbe0bc5
commit b495554424
3 changed files with 241 additions and 233 deletions

View File

@ -41,9 +41,69 @@ std::vector<std::string> get_server_services(const std::string& server_name) {
return services; return services;
} }
server_service::server_service() : m_server_name(""), m_service_name(""), m_server_env(nullptr) {} class server_service {
private:
std::string m_server_name;
std::string m_service_name;
std::unique_ptr<server_env> m_server_env;
bool server_service::init(const std::string& server_name, const std::string& service_name) { bool check_initialized() const {
if (!m_server_env) {
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
return true;
}
std::string construct_ssh_command() 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 get_service_dir() const {
return m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
}
std::string get_script_dir() const {
return get_service_dir() + "/template";
}
std::string get_env_path() const {
return get_service_dir() + "/" + m_service_name + ".env";
}
bool 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 check_env_file_exists(const std::string& ssh_cmd) const {
std::string check_env_cmd = ssh_cmd + "'test -f " + 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 true;
}
bool run_remote_command(const std::string& ssh_cmd, const std::string& command, const std::string& error_msg) const {
if (system(command.c_str()) != 0) {
std::cerr << "Error: " << error_msg << std::endl;
return false;
}
return true;
}
public:
server_service() : m_server_name(""), m_service_name(""), m_server_env(nullptr) {}
bool init(const std::string& server_name, const std::string& service_name) {
std::string user_dir; std::string user_dir;
if (!get_user_directory(user_dir)) { if (!get_user_directory(user_dir)) {
std::cerr << "Error: User directory not set" << std::endl; std::cerr << "Error: User directory not set" << std::endl;
@ -81,11 +141,8 @@ bool server_service::init(const std::string& server_name, const std::string& ser
return true; return true;
} }
bool server_service::install() { bool install() {
if (!m_server_env) { if (!check_initialized()) return false;
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
// Check if template exists // Check if template exists
template_manager tm; template_manager tm;
@ -95,34 +152,28 @@ bool server_service::install() {
return false; return false;
} }
// Construct SSH command std::string ssh_cmd = construct_ssh_command();
std::stringstream ssh_cmd; std::string service_dir = get_service_dir();
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 = ssh_cmd + "'mkdir -p " + service_dir + "/template'";
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + service_dir + "/template'"; if (!run_remote_command(ssh_cmd, 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 = ssh_cmd + "'which rsync > /dev/null 2>&1'";
if (system(check_rsync_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, 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 " << service_dir << "/template/" << 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/"; service_dir + "/template/";
if (system(rsync_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, rsync_cmd, "Failed to copy template files")) {
std::cerr << "Error: Failed to copy template files" << std::endl;
return false; return false;
} }
@ -137,112 +188,68 @@ bool server_service::install() {
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"; service_dir + "/" + m_service_name + ".env";
if (system(scp_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, 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 = ssh_cmd + "'cd " + service_dir + "/template && /bin/bash _install.sh " +
service_dir + "/" + m_service_name + ".env'"; service_dir + "/" + m_service_name + ".env'";
if (system(install_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, install_cmd, "Failed to run install script")) {
std::cerr << "Error: Failed to run install script" << std::endl;
return false; return false;
} }
return true; return true;
} }
bool server_service::run_command(const std::string& command) { bool run_command(const std::string& command) {
if (!m_server_env) { if (!check_initialized()) return false;
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
// Check if service directory exists std::string ssh_cmd = construct_ssh_command();
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_and_command = script_dir + "/" + command + ".sh"; 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 if (!check_service_dir_exists(ssh_cmd)) return false;
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 command script exists // Check if command script exists
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path_and_command + "'"; std::string check_script_cmd = ssh_cmd + "'test -f " + script_path_and_command + "'";
if (system(check_script_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, check_script_cmd, "Command script '" + command + ".sh' not found")) {
std::cerr << "Error: Command script '" << command << ".sh' not found" << std::endl;
return false; return false;
} }
// Check if env file exists if (!check_env_file_exists(ssh_cmd)) return false;
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;
}
// Run the command // Run the command
std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir + std::string run_cmd = ssh_cmd + "'cd " + script_dir +
" && /bin/bash " + script_path_and_command + " "+ env_path + "'"; " && /bin/bash " + script_path_and_command + " " + get_env_path() + "'";
if (system(run_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, run_cmd, "Command returned error code: " + script_path_and_command)) {
std::cerr << "Command returned error code: " << script_path_and_command << std::endl;
return false; return false;
} }
return true; return true;
} }
bool server_service::backup() { bool backup() {
if (!m_server_env) { if (!check_initialized()) return false;
std::cerr << "Error: Server service not initialized" << std::endl;
return false;
}
// Check if service directory exists std::string ssh_cmd = construct_ssh_command();
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 + "/_backup.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"; if (!check_service_dir_exists(ssh_cmd)) return false;
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 backup script exists
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path + "'"; std::string check_script_cmd = ssh_cmd + "'test -f " + script_path + "'";
if (system(check_script_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, check_script_cmd, "Backup script not found")) {
std::cerr << "Error: Backup script not found" << std::endl;
return false; return false;
} }
// Check if env file exists if (!check_env_file_exists(ssh_cmd)) return false;
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 = ssh_cmd + "'mkdir -p " + server_backups_dir + "'";
if (system(mkdir_cmd.c_str()) != 0) { if (!run_remote_command(ssh_cmd, 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;
} }
@ -266,13 +273,12 @@ bool server_service::backup() {
// Construct backup filename // Construct backup filename
std::string backup_filename = m_server_name + "-" + m_service_name + "-" + datetime.str() + ".tgz"; std::string backup_filename = m_server_name + "-" + m_service_name + "-" + datetime.str() + ".tgz";
std::string server_backup_path = server_backups_dir + "/" + backup_filename; std::string server_backup_path = server_backups_dir + "/" + backup_filename;
std::string local_backup_path = (local_backups_dir / backup_filename).string(); std::string local_backup_path = (fs::path(user_dir) / "backups" / backup_filename).string();
// Run backup script // Run backup script
std::string backup_cmd = ssh_cmd.str() + "'cd " + script_dir + std::string backup_cmd = ssh_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 (!run_remote_command(ssh_cmd, backup_cmd, "Backup script failed")) {
std::cerr << "Error: Backup script failed" << std::endl;
return false; return false;
} }
@ -288,5 +294,6 @@ bool server_service::backup() {
std::cout << "Backup created successfully: " << local_backup_path << std::endl; std::cout << "Backup created successfully: " << local_backup_path << std::endl;
return true; return true;
} }
};
} // namespace dropshell } // namespace dropshell

View File

@ -32,8 +32,9 @@ if ! docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"; then
fi fi
echo "Successfully pulled the docker image from the registry" echo "Successfully pulled the docker image from the registry"
# start the container # remove and restart, as the env may have changed.
_stop_container $CONTAINER_NAME _stop_container $CONTAINER_NAME
_remove_container $CONTAINER_NAME
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}" create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
echo "Installation complete" echo "Installation complete"