@@ -62,6 +62,7 @@ def __init__(
6262 peers_ips : Set [str ],
6363 superuser_password : str ,
6464 replication_password : str ,
65+ tls_enabled : bool ,
6566 ):
6667 """Initialize the Patroni class.
6768
@@ -74,6 +75,7 @@ def __init__(
7475 planned_units: number of units planned for the cluster
7576 superuser_password: password for the operator user
7677 replication_password: password for the user used in the replication
78+ tls_enabled: whether TLS is enabled
7779 """
7880 self .unit_ip = unit_ip
7981 self .storage_path = storage_path
@@ -83,52 +85,16 @@ def __init__(
8385 self .peers_ips = peers_ips
8486 self .superuser_password = superuser_password
8587 self .replication_password = replication_password
88+ self .tls_enabled = tls_enabled
8689 # Variable mapping to requests library verify parameter.
87- self .verify = f"{ self .storage_path } /{ TLS_CA_FILE } " if self ._tls_enabled else True
88-
89- @property
90- def _tls_enabled (self ) -> bool :
91- # return False
92- def demote (user_uid , user_gid ):
93- def result ():
94- os .setgid (user_gid )
95- os .setuid (user_uid )
96-
97- return result
98-
99- pw_record = pwd .getpwnam ("postgres" )
100- user_uid = pw_record .pw_uid
101- user_gid = pw_record .pw_gid
102-
103- try :
104- env = dict (os .environ , PGPASSWORD = self .superuser_password )
105- ssl_query_result = subprocess .check_output (
106- [
107- "patronictl" ,
108- "-c" ,
109- f"{ self .storage_path } /patroni.yml" ,
110- "query" ,
111- self .cluster_name ,
112- "--command" ,
113- "SHOW ssl;" ,
114- "--dbname" ,
115- "postgres" ,
116- "--username" ,
117- USER ,
118- ],
119- env = env ,
120- preexec_fn = demote (user_uid , user_gid ),
121- timeout = 10 ,
122- ).decode ("UTF-8" )
123- # logger.warning(ssl_query_result)
124- return "on" in ssl_query_result
125- except (subprocess .CalledProcessError , subprocess .TimeoutExpired ):
126- return False
90+ # The CA bundle file is used to validate the server certificate when
91+ # TLS is enabled, otherwise True is set because it's the default value.
92+ self .verify = f"{ self .storage_path } /{ TLS_CA_FILE } " if tls_enabled else True
12793
12894 @property
12995 def _patroni_url (self ) -> str :
13096 """Patroni REST API URL."""
131- return f"{ 'https' if self ._tls_enabled else 'http' } ://{ self .unit_ip } :8008"
97+ return f"{ 'https' if self .tls_enabled else 'http' } ://{ self .unit_ip } :8008"
13298
13399 def bootstrap_cluster (self ) -> bool :
134100 """Bootstrap a PostgreSQL cluster using Patroni."""
@@ -395,12 +361,9 @@ def remove_raft_member(self, member_ip: str) -> None:
395361 raise RemoveRaftMemberFailedError ()
396362
397363 @retry (stop = stop_after_attempt (3 ), wait = wait_exponential (multiplier = 1 , min = 2 , max = 10 ))
398- def reload_patroni_configuration (self , restart_postgresql : bool = False ):
364+ def reload_patroni_configuration (self ):
399365 """Reload Patroni configuration after it was changed."""
400- url = self ._patroni_url
401- if restart_postgresql :
402- url .replace ("https" , "http" )
403- requests .post (f"{ url } /reload" , verify = self .verify )
366+ requests .post (f"{ self ._patroni_url } /reload" , verify = self .verify )
404367
405368 @retry (stop = stop_after_attempt (3 ), wait = wait_exponential (multiplier = 1 , min = 2 , max = 10 ))
406369 def restart_postgresql (self ) -> None :
0 commit comments