def _createMongoClientForReplicaSet( self, cluster_object: V1MongoClusterConfiguration) -> MongoClient: """ Creates a new MongoClient instance for a replica set. :return: The mongo client. """ logging.info("Creating MongoClient for replicaset %s.", cluster_object.metadata.name) client = MongoClient( MongoResources.getMemberHostnames(cluster_object), connectTimeoutMS=120000, serverSelectionTimeoutMS=120000, replicaSet=cluster_object.metadata.name, username='******', password=cluster_object.spec.users.admin_password, authSource='admin', event_listeners=[ CommandLogger(), ServerLogger(), TopologyListener( cluster_object, replica_set_ready_callback=self._onReplicaSetReady), HeartbeatListener( cluster_object, all_hosts_ready_callback=self._onAllHostsReady) ]) logging.info("Created mongoclient connected to %s.", client.address) return client
def restore(self, cluster_object: V1MongoClusterConfiguration, backup_file: str) -> bool: """ Attempts to restore the latest backup in the specified location to the given cluster. Creates a new backup for the given cluster saving it in the NFS storage. :param cluster_object: The cluster object from the YAML file. :param backup_file: The filename of the backup we want to restore. """ hostnames = MongoResources.getMemberHostnames(cluster_object) logging.info("Restoring backup file %s to cluster %s @ ns/%s.", backup_file, cluster_object.metadata.name, cluster_object.metadata.namespace) # Wait for the replica set to become ready for _ in range(self.RESTORE_RETRIES): try: logging.info("Running mongorestore --host %s --gzip --archive=%s", ",".join(hostnames), backup_file) restore_output = check_output(["/opt/rh/rh-mongodb36/root/usr/bin/mongorestore", "--authenticationDatabase=admin", "-u", "admin", "-p", cluster_object.spec.users.admin_password, "--host", ",".join(hostnames), "--gzip", "--archive=" + backup_file]) logging.info("Restore output: %s", restore_output) try: os.remove(backup_file) except OSError as err: logging.error("Unable to remove '%s': %s", backup_file, err.strerror) return True except CalledProcessError as err: logging.error("Could not restore '%s', attempt %d. Return code: %s stderr: '%s' stdout: '%s'", backup_file, _, err.returncode, err.stderr, err.stdout) sleep(self.RESTORE_WAIT) raise TimeoutError("Could not restore '{}' after {} retries!".format(backup_file, self.RESTORE_RETRIES))
def _createMongoClientForReplicaSet( self, cluster_object: V1MongoClusterConfiguration) -> MongoClient: """ Creates a new MongoClient instance for a replica set. :return: The mongo client. """ return MongoClient( MongoResources.getMemberHostnames(cluster_object), connectTimeoutMS=120000, serverSelectionTimeoutMS=120000, replicaSet=cluster_object.metadata.name, event_listeners=[ CommandLogger(), ServerLogger(), TopologyListener( cluster_object, replica_set_ready_callback=self._onReplicaSetReady), HeartbeatListener( cluster_object, all_hosts_ready_callback=self._onAllHostsReady) ])