|
16 | 16 | from datetime import datetime
|
17 | 17 | from pathlib import Path
|
18 | 18 | from typing import Literal, get_args
|
| 19 | +from urllib.parse import urlparse |
19 | 20 |
|
20 | 21 | import psycopg2
|
21 | 22 | from charms.data_platform_libs.v0.data_interfaces import DataPeerData, DataPeerUnitData
|
|
73 | 74 | APP_SCOPE,
|
74 | 75 | BACKUP_USER,
|
75 | 76 | DATABASE_DEFAULT_NAME,
|
| 77 | + DATABASE_PORT, |
76 | 78 | METRICS_PORT,
|
77 | 79 | MONITORING_PASSWORD_KEY,
|
78 | 80 | MONITORING_SNAP_SERVICE,
|
|
110 | 112 | from relations.postgresql_provider import PostgreSQLProvider
|
111 | 113 | from rotate_logs import RotateLogs
|
112 | 114 | from upgrade import PostgreSQLUpgrade, get_postgresql_dependencies_model
|
113 |
| -from utils import new_password |
| 115 | +from utils import new_password, snap_refreshed |
114 | 116 |
|
115 | 117 | logger = logging.getLogger(__name__)
|
116 | 118 |
|
@@ -1316,29 +1318,86 @@ def _restart_services_after_reboot(self):
|
1316 | 1318 | self._patroni.start_patroni()
|
1317 | 1319 | self.backup.start_stop_pgbackrest_service()
|
1318 | 1320 |
|
1319 |
| - def _setup_exporter(self) -> None: |
1320 |
| - """Set up postgresql_exporter options.""" |
1321 |
| - cache = snap.SnapCache() |
1322 |
| - postgres_snap = cache[POSTGRESQL_SNAP_NAME] |
| 1321 | + def _restart_metrics_service(self, postgres_snap: snap.Snap) -> None: |
| 1322 | + """Restart the monitoring service if the password was rotated.""" |
| 1323 | + try: |
| 1324 | + snap_password = postgres_snap.get("exporter.password") |
| 1325 | + except snap.SnapError: |
| 1326 | + logger.warning("Early exit: Trying to reset metrics service with no configuration set") |
| 1327 | + return None |
1323 | 1328 |
|
1324 |
| - if postgres_snap.revision != next( |
1325 |
| - filter(lambda snap_package: snap_package[0] == POSTGRESQL_SNAP_NAME, SNAP_PACKAGES) |
1326 |
| - )[1]["revision"].get(platform.machine()): |
1327 |
| - logger.debug( |
1328 |
| - "Early exit _setup_exporter: snap was not refreshed to the right version yet" |
1329 |
| - ) |
| 1329 | + if snap_password != self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY): |
| 1330 | + self._setup_exporter(postgres_snap) |
| 1331 | + |
| 1332 | + def _restart_ldap_sync_service(self, postgres_snap: snap.Snap) -> None: |
| 1333 | + """Restart the LDAP sync service in case any configuration changed.""" |
| 1334 | + if not self._patroni.member_started: |
| 1335 | + logger.debug("Restart LDAP sync early exit: Patroni has not started yet") |
1330 | 1336 | return
|
1331 | 1337 |
|
| 1338 | + sync_service = postgres_snap.services["ldap-sync"] |
| 1339 | + |
| 1340 | + if not self.is_primary and sync_service["active"]: |
| 1341 | + logger.debug("Stopping LDAP sync service. It must only run in the primary") |
| 1342 | + postgres_snap.stop(services=["ldap-sync"]) |
| 1343 | + |
| 1344 | + if self.is_primary and not self.is_ldap_enabled: |
| 1345 | + logger.debug("Stopping LDAP sync service") |
| 1346 | + postgres_snap.stop(services=["ldap-sync"]) |
| 1347 | + return |
| 1348 | + |
| 1349 | + if self.is_primary and self.is_ldap_enabled: |
| 1350 | + self._setup_ldap_sync(postgres_snap) |
| 1351 | + |
| 1352 | + def _setup_exporter(self, postgres_snap: snap.Snap | None = None) -> None: |
| 1353 | + """Set up postgresql_exporter options.""" |
| 1354 | + if postgres_snap is None: |
| 1355 | + cache = snap.SnapCache() |
| 1356 | + postgres_snap = cache[POSTGRESQL_SNAP_NAME] |
| 1357 | + |
1332 | 1358 | postgres_snap.set({
|
1333 | 1359 | "exporter.user": MONITORING_USER,
|
1334 | 1360 | "exporter.password": self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY),
|
1335 | 1361 | })
|
| 1362 | + |
1336 | 1363 | if postgres_snap.services[MONITORING_SNAP_SERVICE]["active"] is False:
|
1337 | 1364 | postgres_snap.start(services=[MONITORING_SNAP_SERVICE], enable=True)
|
1338 | 1365 | else:
|
1339 | 1366 | postgres_snap.restart(services=[MONITORING_SNAP_SERVICE])
|
| 1367 | + |
1340 | 1368 | self.unit_peer_data.update({"exporter-started": "True"})
|
1341 | 1369 |
|
| 1370 | + def _setup_ldap_sync(self, postgres_snap: snap.Snap | None = None) -> None: |
| 1371 | + """Set up postgresql_ldap_sync options.""" |
| 1372 | + if postgres_snap is None: |
| 1373 | + cache = snap.SnapCache() |
| 1374 | + postgres_snap = cache[POSTGRESQL_SNAP_NAME] |
| 1375 | + |
| 1376 | + ldap_params = self.get_ldap_parameters() |
| 1377 | + ldap_url = urlparse(ldap_params["ldapurl"]) |
| 1378 | + ldap_host = ldap_url.hostname |
| 1379 | + ldap_port = ldap_url.port |
| 1380 | + |
| 1381 | + ldap_base_dn = ldap_params["ldapbasedn"] |
| 1382 | + ldap_bind_username = ldap_params["ldapbinddn"] |
| 1383 | + ldap_bind_password = ldap_params["ldapbindpasswd"] |
| 1384 | + |
| 1385 | + postgres_snap.set({ |
| 1386 | + "ldap-sync.ldap_host": ldap_host, |
| 1387 | + "ldap-sync.ldap_port": ldap_port, |
| 1388 | + "ldap-sync.ldap_base_dn": ldap_base_dn, |
| 1389 | + "ldap-sync.ldap_bind_username": ldap_bind_username, |
| 1390 | + "ldap-sync.ldap_bind_password": ldap_bind_password, |
| 1391 | + "ldap-sync.postgres_host": "127.0.0.1", |
| 1392 | + "ldap-sync.postgres_port": DATABASE_PORT, |
| 1393 | + "ldap-sync.postgres_database": DATABASE_DEFAULT_NAME, |
| 1394 | + "ldap-sync.postgres_username": USER, |
| 1395 | + "ldap-sync.postgres_password": self._get_password(), |
| 1396 | + }) |
| 1397 | + |
| 1398 | + logger.debug("Starting LDAP sync service") |
| 1399 | + postgres_snap.restart(services=["ldap-sync"]) |
| 1400 | + |
1342 | 1401 | def _start_primary(self, event: StartEvent) -> None:
|
1343 | 1402 | """Bootstrap the cluster."""
|
1344 | 1403 | # Set some information needed by Patroni to bootstrap the cluster.
|
@@ -1986,19 +2045,15 @@ def update_config(self, is_creating_backup: bool = False, no_peers: bool = False
|
1986 | 2045 |
|
1987 | 2046 | self._handle_postgresql_restart_need(enable_tls)
|
1988 | 2047 |
|
1989 |
| - # Restart the monitoring service if the password was rotated |
1990 | 2048 | cache = snap.SnapCache()
|
1991 | 2049 | postgres_snap = cache[POSTGRESQL_SNAP_NAME]
|
1992 | 2050 |
|
1993 |
| - try: |
1994 |
| - snap_password = postgres_snap.get("exporter.password") |
1995 |
| - except snap.SnapError: |
1996 |
| - logger.warning( |
1997 |
| - "Early exit update_config: Trying to reset metrics service with no configuration set" |
1998 |
| - ) |
| 2051 | + if not snap_refreshed(postgres_snap.revision): |
| 2052 | + logger.debug("Early exit: snap was not refreshed to the right version yet") |
1999 | 2053 | return True
|
2000 |
| - if snap_password != self.get_secret(APP_SCOPE, MONITORING_PASSWORD_KEY): |
2001 |
| - self._setup_exporter() |
| 2054 | + |
| 2055 | + self._restart_metrics_service(postgres_snap) |
| 2056 | + self._restart_ldap_sync_service(postgres_snap) |
2002 | 2057 |
|
2003 | 2058 | return True
|
2004 | 2059 |
|
|
0 commit comments