@@ -109,15 +109,16 @@ def _can_initialise_stanza(self) -> bool:
109109 # Don't allow stanza initialisation if this unit hasn't started the database
110110 # yet and either hasn't joined the peer relation yet or hasn't configured TLS
111111 # yet while other unit already has TLS enabled.
112- if not self .charm ._patroni .member_started and (
113- (len (self .charm ._peers .data .keys ()) == 2 )
114- or (
115- "tls" not in self .charm .unit_peer_data
116- and any ("tls" in unit_data for _ , unit_data in self .charm ._peers .data .items ())
112+ return not (
113+ not self .charm ._patroni .member_started
114+ and (
115+ (len (self .charm ._peers .data .keys ()) == 2 )
116+ or (
117+ "tls" not in self .charm .unit_peer_data
118+ and any ("tls" in unit_data for _ , unit_data in self .charm ._peers .data .items ())
119+ )
117120 )
118- ):
119- return False
120- return True
121+ )
121122
122123 def _can_unit_perform_backup (self ) -> Tuple [bool , Optional [str ]]:
123124 """Validates whether this unit can perform a backup."""
@@ -182,18 +183,17 @@ def can_use_s3_repository(self) -> Tuple[bool, Optional[str]]:
182183 ])
183184 if error != "" :
184185 raise Exception (error )
185- system_identifier_from_instance = [
186+ system_identifier_from_instance = next (
186187 line
187188 for line in system_identifier_from_instance .splitlines ()
188189 if "Database system identifier" in line
189- ][ 0 ] .split (" " )[- 1 ]
190+ ) .split (" " )[- 1 ]
190191 system_identifier_from_stanza = str (stanza .get ("db" )[0 ]["system-id" ])
191192 if system_identifier_from_instance != system_identifier_from_stanza :
192193 logger .debug (
193194 f"can_use_s3_repository: incompatible system identifier s3={ system_identifier_from_stanza } , local={ system_identifier_from_instance } "
194195 )
195196 return False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
196-
197197 return True , None
198198
199199 def _construct_endpoint (self , s3_parameters : Dict ) -> str :
@@ -278,7 +278,7 @@ def _change_connectivity_to_database(self, connectivity: bool) -> None:
278278 self .charm .update_config (is_creating_backup = True )
279279
280280 def _execute_command (
281- self , command : List [str ], timeout : float = None , stream : bool = False
281+ self , command : List [str ], timeout : Optional [ float ] = None , stream : bool = False
282282 ) -> Tuple [Optional [str ], Optional [str ]]:
283283 """Execute a command in the workload container."""
284284 try :
@@ -338,17 +338,7 @@ def _format_backup_list(self, backup_list) -> str:
338338 path ,
339339 ) in backup_list :
340340 backups .append (
341- "{:<20s} | {:<19s} | {:<8s} | {:<20s} | {:<23s} | {:<20s} | {:<20s} | {:<8s} | {:s}" .format (
342- backup_id ,
343- backup_action ,
344- backup_status ,
345- reference ,
346- lsn_start_stop ,
347- start ,
348- stop ,
349- backup_timeline ,
350- path ,
351- )
341+ f"{ backup_id :<20s} | { backup_action :<19s} | { backup_status :<8s} | { reference :<20s} | { lsn_start_stop :<23s} | { start :<20s} | { stop :<20s} | { backup_timeline :<8s} | { path :s} "
352342 )
353343 return "\n " .join (backups )
354344
@@ -395,7 +385,7 @@ def _generate_backup_list_output(self) -> str:
395385 backup_path ,
396386 ))
397387
398- for timeline , (timeline_stanza , timeline_id ) in self ._list_timelines ().items ():
388+ for timeline , (_ , timeline_id ) in self ._list_timelines ().items ():
399389 backup_list .append ((
400390 timeline ,
401391 "restore" ,
@@ -648,7 +638,7 @@ def _is_primary_pgbackrest_service_running(self) -> bool:
648638 try :
649639 primary = self .charm ._patroni .get_primary ()
650640 except (RetryError , ConnectionError ) as e :
651- logger .error (f"failed to get primary with error { str ( e ) } " )
641+ logger .error (f"failed to get primary with error { e !s } " )
652642 return False
653643
654644 if primary is None :
@@ -666,7 +656,7 @@ def _is_primary_pgbackrest_service_running(self) -> bool:
666656 ])
667657 except ExecError as e :
668658 logger .warning (
669- f"Failed to contact pgBackRest TLS server on { primary_endpoint } with error { str ( e ) } "
659+ f"Failed to contact pgBackRest TLS server on { primary_endpoint } with error { e !s } "
670660 )
671661 return False
672662
@@ -798,7 +788,7 @@ def _on_create_backup_action(self, event) -> None: # noqa: C901
798788Model Name: { self .model .name }
799789Application Name: { self .model .app .name }
800790Unit Name: { self .charm .unit .name }
801- Juju Version: { str ( juju_version ) }
791+ Juju Version: { juju_version !s }
802792"""
803793 if not self ._upload_content_to_s3 (
804794 metadata ,
@@ -862,7 +852,7 @@ def _on_create_backup_action(self, event) -> None: # noqa: C901
862852 f"backup/{ self .stanza_name } /{ backup_id } /backup.log" ,
863853 s3_parameters ,
864854 )
865- error_message = f"Failed to backup PostgreSQL with error: { str ( e ) } "
855+ error_message = f"Failed to backup PostgreSQL with error: { e !s } "
866856 logger .error (f"Backup failed: { error_message } " )
867857 event .fail (error_message )
868858 else :
@@ -924,7 +914,7 @@ def _on_list_backups_action(self, event) -> None:
924914 event .set_results ({"backups" : formatted_list })
925915 except ExecError as e :
926916 logger .exception (e )
927- event .fail (f"Failed to list PostgreSQL backups with error: { str ( e ) } " )
917+ event .fail (f"Failed to list PostgreSQL backups with error: { e !s } " )
928918
929919 def _on_restore_action (self , event ): # noqa: C901
930920 """Request that pgBackRest restores a backup."""
@@ -943,10 +933,8 @@ def _on_restore_action(self, event): # noqa: C901
943933 logger .info ("Validating provided backup-id and restore-to-time" )
944934 backups = self ._list_backups (show_failed = False )
945935 timelines = self ._list_timelines ()
946- is_backup_id_real = backup_id and backup_id in backups .keys ()
947- is_backup_id_timeline = (
948- backup_id and not is_backup_id_real and backup_id in timelines .keys ()
949- )
936+ is_backup_id_real = backup_id and backup_id in backups
937+ is_backup_id_timeline = backup_id and not is_backup_id_real and backup_id in timelines
950938 if backup_id and not is_backup_id_real and not is_backup_id_timeline :
951939 error_message = f"Invalid backup-id: { backup_id } "
952940 logger .error (f"Restore failed: { error_message } " )
@@ -1001,7 +989,7 @@ def _on_restore_action(self, event): # noqa: C901
1001989 try :
1002990 self .container .stop (self .charm ._postgresql_service )
1003991 except ChangeError as e :
1004- error_message = f"Failed to stop database service with error: { str ( e ) } "
992+ error_message = f"Failed to stop database service with error: { e !s } "
1005993 logger .error (f"Restore failed: { error_message } " )
1006994 event .fail (error_message )
1007995 return
@@ -1025,9 +1013,7 @@ def _on_restore_action(self, event): # noqa: C901
10251013 except ApiError as e :
10261014 # If previous PITR restore was unsuccessful, there are no such endpoints.
10271015 if "restore-to-time" not in self .charm .app_peer_data :
1028- error_message = (
1029- f"Failed to remove previous cluster information with error: { str (e )} "
1030- )
1016+ error_message = f"Failed to remove previous cluster information with error: { e !s} "
10311017 logger .error (f"Restore failed: { error_message } " )
10321018 event .fail (error_message )
10331019 self ._restart_database ()
@@ -1037,7 +1023,7 @@ def _on_restore_action(self, event): # noqa: C901
10371023 try :
10381024 self ._empty_data_files ()
10391025 except ExecError as e :
1040- error_message = f"Failed to remove contents of the data directory with error: { str ( e ) } "
1026+ error_message = f"Failed to remove contents of the data directory with error: { e !s } "
10411027 logger .error (f"Restore failed: { error_message } " )
10421028 event .fail (error_message )
10431029 self ._restart_database ()
@@ -1188,7 +1174,7 @@ def _render_pgbackrest_conf_file(self) -> bool:
11881174 )
11891175
11901176 # Open the template pgbackrest.conf file.
1191- with open ("templates/pgbackrest.conf.j2" , "r" ) as file :
1177+ with open ("templates/pgbackrest.conf.j2" ) as file :
11921178 template = Template (file .read ())
11931179 # Render the template file with the correct values.
11941180 rendered = template .render (
@@ -1217,13 +1203,14 @@ def _render_pgbackrest_conf_file(self) -> bool:
12171203 )
12181204
12191205 # Render the logrotate configuration file.
1220- with open ("templates/pgbackrest.logrotate.j2" , "r" ) as file :
1206+ with open ("templates/pgbackrest.logrotate.j2" ) as file :
12211207 template = Template (file .read ())
12221208 self .container .push (PGBACKREST_LOGROTATE_FILE , template .render ())
1223- self .container .push (
1224- "/home/postgres/rotate_logs.py" ,
1225- open ("src/rotate_logs.py" , "r" ).read (),
1226- )
1209+ with open ("src/rotate_logs.py" ) as f :
1210+ self .container .push (
1211+ "/home/postgres/rotate_logs.py" ,
1212+ f .read (),
1213+ )
12271214 self .container .start (self .charm .rotate_logs_service )
12281215
12291216 return True
0 commit comments