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 ,
@@ -1316,6 +1318,42 @@ def _restart_services_after_reboot(self):
1316
1318
self ._patroni .start_patroni ()
1317
1319
self .backup .start_stop_pgbackrest_service ()
1318
1320
1321
+ def _restart_metrics_service (self ) -> None :
1322
+ """Restart the monitoring service if the password was rotated."""
1323
+ cache = snap .SnapCache ()
1324
+ postgres_snap = cache [POSTGRESQL_SNAP_NAME ]
1325
+
1326
+ try :
1327
+ snap_password = postgres_snap .get ("exporter.password" )
1328
+ except snap .SnapError :
1329
+ logger .warning ("Early exit: Trying to reset metrics service with no configuration set" )
1330
+ return None
1331
+
1332
+ if snap_password != self .get_secret (APP_SCOPE , MONITORING_PASSWORD_KEY ):
1333
+ self ._setup_exporter ()
1334
+
1335
+ def _restart_ldap_sync_service (self ) -> None :
1336
+ """Restart the LDAP sync service in case any configuration changed."""
1337
+ if not self ._patroni .member_started :
1338
+ logger .debug ("Restart LDAP sync early exit: Patroni has not started yet" )
1339
+ return
1340
+
1341
+ cache = snap .SnapCache ()
1342
+ postgres_snap = cache [POSTGRESQL_SNAP_NAME ]
1343
+ sync_service = postgres_snap .services ["ldap-sync" ]
1344
+
1345
+ if not self .is_primary and sync_service ["active" ]:
1346
+ logger .debug ("Stopping LDAP sync service. It must only run in the primary" )
1347
+ postgres_snap .stop (services = ["ldap-sync" ])
1348
+
1349
+ if self .is_primary and not self .is_ldap_enabled :
1350
+ logger .debug ("Stopping LDAP sync service" )
1351
+ postgres_snap .stop (services = ["ldap-sync" ])
1352
+ return
1353
+
1354
+ if self .is_primary and self .is_ldap_enabled :
1355
+ self ._setup_ldap_sync ()
1356
+
1319
1357
def _setup_exporter (self ) -> None :
1320
1358
"""Set up postgresql_exporter options."""
1321
1359
cache = snap .SnapCache ()
@@ -1339,6 +1377,36 @@ def _setup_exporter(self) -> None:
1339
1377
postgres_snap .restart (services = [MONITORING_SNAP_SERVICE ])
1340
1378
self .unit_peer_data .update ({"exporter-started" : "True" })
1341
1379
1380
+ def _setup_ldap_sync (self ) -> None :
1381
+ """Set up postgresql_ldap_sync options."""
1382
+ cache = snap .SnapCache ()
1383
+ postgres_snap = cache [POSTGRESQL_SNAP_NAME ]
1384
+
1385
+ ldap_params = self .get_ldap_parameters ()
1386
+ ldap_url = urlparse (ldap_params ["ldapurl" ])
1387
+ ldap_host = ldap_url .hostname
1388
+ ldap_port = ldap_url .port
1389
+
1390
+ ldap_base_dn = ldap_params ["ldapbasedn" ]
1391
+ ldap_bind_username = ldap_params ["ldapbinddn" ]
1392
+ ldap_bind_password = ldap_params ["ldapbindpasswd" ]
1393
+
1394
+ postgres_snap .set ({
1395
+ "ldap-sync.ldap_host" : ldap_host ,
1396
+ "ldap-sync.ldap_port" : ldap_port ,
1397
+ "ldap-sync.ldap_base_dn" : ldap_base_dn ,
1398
+ "ldap-sync.ldap_bind_username" : ldap_bind_username ,
1399
+ "ldap-sync.ldap_bind_password" : ldap_bind_password ,
1400
+ "ldap-sync.postgres_host" : "127.0.0.1" ,
1401
+ "ldap-sync.postgres_port" : DATABASE_PORT ,
1402
+ "ldap-sync.postgres_database" : DATABASE_DEFAULT_NAME ,
1403
+ "ldap-sync.postgres_username" : USER ,
1404
+ "ldap-sync.postgres_password" : self ._get_password (),
1405
+ })
1406
+
1407
+ logger .debug ("Starting LDAP sync service" )
1408
+ postgres_snap .restart (services = ["ldap-sync" ])
1409
+
1342
1410
def _start_primary (self , event : StartEvent ) -> None :
1343
1411
"""Bootstrap the cluster."""
1344
1412
# Set some information needed by Patroni to bootstrap the cluster.
@@ -1985,20 +2053,8 @@ def update_config(self, is_creating_backup: bool = False, no_peers: bool = False
1985
2053
})
1986
2054
1987
2055
self ._handle_postgresql_restart_need (enable_tls )
1988
-
1989
- # Restart the monitoring service if the password was rotated
1990
- cache = snap .SnapCache ()
1991
- postgres_snap = cache [POSTGRESQL_SNAP_NAME ]
1992
-
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
- )
1999
- return True
2000
- if snap_password != self .get_secret (APP_SCOPE , MONITORING_PASSWORD_KEY ):
2001
- self ._setup_exporter ()
2056
+ self ._restart_metrics_service ()
2057
+ self ._restart_ldap_sync_service ()
2002
2058
2003
2059
return True
2004
2060
0 commit comments