Working
This commit is contained in:
parent
a65fbe0bc5
commit
b495554424
@ -41,9 +41,69 @@ std::vector<std::string> get_server_services(const std::string& server_name) {
|
||||
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;
|
||||
if (!get_user_directory(user_dir)) {
|
||||
std::cerr << "Error: User directory not set" << std::endl;
|
||||
@ -79,14 +139,11 @@ bool server_service::init(const std::string& server_name, const std::string& ser
|
||||
m_server_name = server_name;
|
||||
m_service_name = service_name;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_service::install() {
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool install() {
|
||||
if (!check_initialized()) return false;
|
||||
|
||||
// Check if template exists
|
||||
template_manager tm;
|
||||
template_info info;
|
||||
@ -95,34 +152,28 @@ bool server_service::install() {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Construct SSH command
|
||||
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 ssh_cmd = construct_ssh_command();
|
||||
std::string service_dir = get_service_dir();
|
||||
|
||||
// Create service directory
|
||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
||||
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + service_dir + "/template'";
|
||||
if (system(mkdir_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Failed to create service directory" << std::endl;
|
||||
std::string mkdir_cmd = ssh_cmd + "'mkdir -p " + service_dir + "/template'";
|
||||
if (!run_remote_command(ssh_cmd, mkdir_cmd, "Failed to create service directory")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if rsync is installed on remote host
|
||||
std::string check_rsync_cmd = ssh_cmd.str() + "'which rsync > /dev/null 2>&1'";
|
||||
if (system(check_rsync_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: rsync is not installed on the remote host" << std::endl;
|
||||
std::string check_rsync_cmd = ssh_cmd + "'which rsync > /dev/null 2>&1'";
|
||||
if (!run_remote_command(ssh_cmd, check_rsync_cmd, "rsync is not installed on the remote host")) {
|
||||
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::string rsync_cmd = "rsync --delete -zrpc -e 'ssh -p " + m_server_env->get_SSH_PORT() + "' " +
|
||||
info.path + "/ " +
|
||||
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
||||
service_dir + "/template/";
|
||||
if (system(rsync_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Failed to copy template files" << std::endl;
|
||||
if (!run_remote_command(ssh_cmd, rsync_cmd, "Failed to copy template files")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -137,112 +188,68 @@ bool server_service::install() {
|
||||
service_env.string() + " " +
|
||||
m_server_env->get_SSH_USER() + "@" + m_server_env->get_SSH_HOST() + ":" +
|
||||
service_dir + "/" + m_service_name + ".env";
|
||||
if (system(scp_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Failed to copy service environment file" << std::endl;
|
||||
if (!run_remote_command(ssh_cmd, scp_cmd, "Failed to copy service environment file")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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'";
|
||||
if (system(install_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Failed to run install script" << std::endl;
|
||||
if (!run_remote_command(ssh_cmd, install_cmd, "Failed to run install script")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_service::run_command(const std::string& command) {
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if service directory exists
|
||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
||||
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() << " ";
|
||||
bool run_command(const std::string& command) {
|
||||
if (!check_initialized()) return false;
|
||||
|
||||
std::string script_dir = service_dir + "/template";
|
||||
std::string ssh_cmd = construct_ssh_command();
|
||||
std::string script_dir = get_script_dir();
|
||||
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
|
||||
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;
|
||||
}
|
||||
if (!check_service_dir_exists(ssh_cmd)) return false;
|
||||
|
||||
// Check if command script exists
|
||||
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path_and_command + "'";
|
||||
if (system(check_script_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Command script '" << command << ".sh' not found" << std::endl;
|
||||
std::string check_script_cmd = ssh_cmd + "'test -f " + script_path_and_command + "'";
|
||||
if (!run_remote_command(ssh_cmd, check_script_cmd, "Command script '" + command + ".sh' not found")) {
|
||||
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;
|
||||
}
|
||||
if (!check_env_file_exists(ssh_cmd)) return false;
|
||||
|
||||
// Run the command
|
||||
std::string run_cmd = ssh_cmd.str() + "'cd " + script_dir +
|
||||
" && /bin/bash " + script_path_and_command + " "+ env_path + "'";
|
||||
if (system(run_cmd.c_str()) != 0) {
|
||||
std::cerr << "Command returned error code: " << script_path_and_command << std::endl;
|
||||
std::string run_cmd = ssh_cmd + "'cd " + script_dir +
|
||||
" && /bin/bash " + script_path_and_command + " " + get_env_path() + "'";
|
||||
if (!run_remote_command(ssh_cmd, run_cmd, "Command returned error code: " + script_path_and_command)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool server_service::backup() {
|
||||
if (!m_server_env) {
|
||||
std::cerr << "Error: Server service not initialized" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if service directory exists
|
||||
std::string service_dir = m_server_env->get_DROPSHELL_DIR() + "/" + m_service_name;
|
||||
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() << " ";
|
||||
bool backup() {
|
||||
if (!check_initialized()) return false;
|
||||
|
||||
std::string script_dir = service_dir + "/template";
|
||||
std::string script_path = script_dir + "/backup.sh";
|
||||
std::string env_path = service_dir + "/" + m_service_name + ".env";
|
||||
std::string ssh_cmd = construct_ssh_command();
|
||||
std::string script_dir = get_script_dir();
|
||||
std::string script_path = script_dir + "/_backup.sh";
|
||||
|
||||
// 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;
|
||||
}
|
||||
if (!check_service_dir_exists(ssh_cmd)) return false;
|
||||
|
||||
// Check if backup script exists
|
||||
std::string check_script_cmd = ssh_cmd.str() + "'test -f " + script_path + "'";
|
||||
if (system(check_script_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Backup script not found" << std::endl;
|
||||
std::string check_script_cmd = ssh_cmd + "'test -f " + script_path + "'";
|
||||
if (!run_remote_command(ssh_cmd, check_script_cmd, "Backup script not found")) {
|
||||
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;
|
||||
}
|
||||
if (!check_env_file_exists(ssh_cmd)) return false;
|
||||
|
||||
// Create backups directory on server if it doesn't exist
|
||||
std::string server_backups_dir = m_server_env->get_DROPSHELL_DIR() + "/backups";
|
||||
std::string mkdir_cmd = ssh_cmd.str() + "'mkdir -p " + server_backups_dir + "'";
|
||||
if (system(mkdir_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Failed to create backups directory on server" << std::endl;
|
||||
std::string mkdir_cmd = ssh_cmd + "'mkdir -p " + server_backups_dir + "'";
|
||||
if (!run_remote_command(ssh_cmd, mkdir_cmd, "Failed to create backups directory on server")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -266,13 +273,12 @@ bool server_service::backup() {
|
||||
// Construct backup filename
|
||||
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 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
|
||||
std::string backup_cmd = ssh_cmd.str() + "'cd " + script_dir +
|
||||
" && /bin/bash backup.sh " + env_path + " " + server_backup_path + "'";
|
||||
if (system(backup_cmd.c_str()) != 0) {
|
||||
std::cerr << "Error: Backup script failed" << std::endl;
|
||||
std::string backup_cmd = ssh_cmd + "'cd " + script_dir +
|
||||
" && /bin/bash backup.sh " + get_env_path() + " " + server_backup_path + "'";
|
||||
if (!run_remote_command(ssh_cmd, backup_cmd, "Backup script failed")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -287,6 +293,7 @@ bool server_service::backup() {
|
||||
|
||||
std::cout << "Backup created successfully: " << local_backup_path << std::endl;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace dropshell
|
@ -32,8 +32,9 @@ if ! docker pull "$IMAGE_REGISTRY/$IMAGE_REPO:$IMAGE_TAG"; then
|
||||
fi
|
||||
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
|
||||
_remove_container $CONTAINER_NAME
|
||||
create_and_start_container || die "Failed to start container ${CONTAINER_NAME}"
|
||||
|
||||
echo "Installation complete"
|
Loading…
x
Reference in New Issue
Block a user