def enable_as_master(self, service, master_config, for_failover=False): # For a server to be a master in postgres, we need to enable # replication user in pg_hba and ensure that WAL logging is # the appropriate level (use the same settings as backups) self._get_or_create_replication_user() hba_entry = "host replication replicator 0.0.0.0/0 md5 \n" tmp_hba = '/tmp/pg_hba' operating_system.copy(self.pgsql_hba_config, tmp_hba, force=True, as_root=True) operating_system.chmod(tmp_hba, FileMode.SET_ALL_RWX(), as_root=True) with open(tmp_hba, 'a+') as hba_file: hba_file.write(hba_entry) operating_system.copy(tmp_hba, self.pgsql_hba_config, force=True, as_root=True) operating_system.chmod(self.pgsql_hba_config, FileMode.SET_USR_RWX(), as_root=True) operating_system.remove(tmp_hba, as_root=True) pgutil.psql("SELECT pg_reload_conf()")
def enable_as_master(self, service, master_config, for_failover=False): """For a server to be a master in postgres, we need to enable the replication user in pg_hba and ensure that WAL logging is at the appropriate level (use the same settings as backups) """ LOG.debug("Enabling as master, with cfg: %s ", master_config) self._get_or_create_replication_user(service) hba_entry = "host replication replicator 0.0.0.0/0 md5 \n" tmp_hba = '/tmp/pg_hba' operating_system.copy(service.pgsql_hba_config, tmp_hba, force=True, as_root=True) operating_system.chmod(tmp_hba, FileMode.SET_ALL_RWX(), as_root=True) with open(tmp_hba, 'a+') as hba_file: hba_file.write(hba_entry) operating_system.copy(tmp_hba, service.pgsql_hba_config, force=True, as_root=True) operating_system.chmod(service.pgsql_hba_config, FileMode.SET_USR_RWX(), as_root=True) operating_system.remove(tmp_hba, as_root=True) service.reload_configuration()
def enable_as_master(self, service, master_config): """Primary postgredql settings. For a server to be a master in postgres, we need to enable the replication user in pg_hba.conf """ self._get_or_create_replication_user(service) hba_entry = f"host replication {REPL_USER} 0.0.0.0/0 md5\n" tmp_hba = '/tmp/pg_hba' operating_system.copy(pg_service.HBA_CONFIG_FILE, tmp_hba, force=True, as_root=True) operating_system.chmod(tmp_hba, FileMode.SET_ALL_RWX(), as_root=True) with open(tmp_hba, 'a+') as hba_file: hba_file.write(hba_entry) operating_system.copy(tmp_hba, pg_service.HBA_CONFIG_FILE, force=True, as_root=True) operating_system.chown(pg_service.HBA_CONFIG_FILE, user=CONF.database_service_uid, group=CONF.database_service_uid, as_root=True) operating_system.chmod(pg_service.HBA_CONFIG_FILE, FileMode.SET_USR_RWX(), as_root=True) operating_system.remove(tmp_hba, as_root=True) LOG.debug(f"{pg_service.HBA_CONFIG_FILE} changed") service.restart()
def _create_replication_user(self, pwfile): """Create the replication user. Unfortunately, to be able to run pg_rewind, we need SUPERUSER, not just REPLICATION privilege """ pw = utils.generate_random_password() operating_system.write_file(pwfile, pw, as_root=True) operating_system.chown(pwfile, user=self.PGSQL_OWNER, group=self.PGSQL_OWNER, as_root=True) operating_system.chmod(pwfile, FileMode.SET_USR_RWX(), as_root=True) pgutil.psql("CREATE USER %s SUPERUSER ENCRYPTED " "password '%s';" % (REPL_USER, pw)) return pw
def _create_replication_user(self, service, admin, pwfile): """Create the replication user. Unfortunately, to be able to run pg_rewind, we need SUPERUSER, not just REPLICATION privilege """ pw = utils.generate_random_password() operating_system.write_file(pwfile, pw, as_root=True) operating_system.chown(pwfile, user=service.pgsql_owner, group=service.pgsql_owner, as_root=True) operating_system.chmod(pwfile, FileMode.SET_USR_RWX(), as_root=True) repl_user = models.PostgreSQLUser(name=REPL_USER, password=pw) admin._create_user(context=None, user=repl_user) admin.alter_user(None, repl_user, True, 'REPLICATION', 'SUPERUSER', 'LOGIN') return pw
def _create_replication_user(self, service, adm_mgr, pwfile): """Create the replication user and password file. Unfortunately, to be able to run pg_rewind, we need SUPERUSER, not just REPLICATION privilege """ pw = utils.generate_random_password() operating_system.write_file(pwfile, pw, as_root=True) operating_system.chown(pwfile, user=CONF.database_service_uid, group=CONF.database_service_uid, as_root=True) operating_system.chmod(pwfile, FileMode.SET_USR_RWX(), as_root=True) LOG.debug(f"File {pwfile} created") LOG.debug(f"Creating replication user {REPL_USER}") repl_user = models.PostgreSQLUser(name=REPL_USER, password=pw) adm_mgr.create_user(repl_user, None, *('REPLICATION', 'SUPERUSER', 'LOGIN')) return pw
def _incremental_restore(self, location, checksum): metadata = self.storage.load_metadata(location, checksum) if 'parent_location' in metadata: LOG.info(_("Found parent at %s"), metadata['parent_location']) parent_location = metadata['parent_location'] parent_checksum = metadata['parent_checksum'] self._incremental_restore(parent_location, parent_checksum) cmd = self._incremental_restore_cmd(incr=True) self.content_length += self._unpack(location, checksum, cmd) else: # For the parent base backup, revert to the default restore cmd LOG.info(_("Recursed back to full backup.")) super(PgBaseBackupIncremental, self).pre_restore() cmd = self._incremental_restore_cmd(incr=False) self.content_length += self._unpack(location, checksum, cmd) operating_system.chmod(self.app.pgsql_data_dir, FileMode.SET_USR_RWX(), as_root=True, recursive=True, force=True)
def post_restore(self): operating_system.chmod(self.app.pgsql_data_dir, FileMode.SET_USR_RWX(), as_root=True, recursive=True, force=True)