@@ -109,15 +109,16 @@ def _can_initialise_stanza(self) -> bool:
109
109
# Don't allow stanza initialisation if this unit hasn't started the database
110
110
# yet and either hasn't joined the peer relation yet or hasn't configured TLS
111
111
# 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
+ )
117
120
)
118
- ):
119
- return False
120
- return True
121
+ )
121
122
122
123
def _can_unit_perform_backup (self ) -> Tuple [bool , Optional [str ]]:
123
124
"""Validates whether this unit can perform a backup."""
@@ -182,18 +183,17 @@ def can_use_s3_repository(self) -> Tuple[bool, Optional[str]]:
182
183
])
183
184
if error != "" :
184
185
raise Exception (error )
185
- system_identifier_from_instance = [
186
+ system_identifier_from_instance = next (
186
187
line
187
188
for line in system_identifier_from_instance .splitlines ()
188
189
if "Database system identifier" in line
189
- ][ 0 ] .split (" " )[- 1 ]
190
+ ) .split (" " )[- 1 ]
190
191
system_identifier_from_stanza = str (stanza .get ("db" )[0 ]["system-id" ])
191
192
if system_identifier_from_instance != system_identifier_from_stanza :
192
193
logger .debug (
193
194
f"can_use_s3_repository: incompatible system identifier s3={ system_identifier_from_stanza } , local={ system_identifier_from_instance } "
194
195
)
195
196
return False , ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
196
-
197
197
return True , None
198
198
199
199
def _construct_endpoint (self , s3_parameters : Dict ) -> str :
@@ -278,7 +278,7 @@ def _change_connectivity_to_database(self, connectivity: bool) -> None:
278
278
self .charm .update_config (is_creating_backup = True )
279
279
280
280
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
282
282
) -> Tuple [Optional [str ], Optional [str ]]:
283
283
"""Execute a command in the workload container."""
284
284
try :
@@ -338,17 +338,7 @@ def _format_backup_list(self, backup_list) -> str:
338
338
path ,
339
339
) in backup_list :
340
340
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} "
352
342
)
353
343
return "\n " .join (backups )
354
344
@@ -395,7 +385,7 @@ def _generate_backup_list_output(self) -> str:
395
385
backup_path ,
396
386
))
397
387
398
- for timeline , (timeline_stanza , timeline_id ) in self ._list_timelines ().items ():
388
+ for timeline , (_ , timeline_id ) in self ._list_timelines ().items ():
399
389
backup_list .append ((
400
390
timeline ,
401
391
"restore" ,
@@ -648,7 +638,7 @@ def _is_primary_pgbackrest_service_running(self) -> bool:
648
638
try :
649
639
primary = self .charm ._patroni .get_primary ()
650
640
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 } " )
652
642
return False
653
643
654
644
if primary is None :
@@ -666,7 +656,7 @@ def _is_primary_pgbackrest_service_running(self) -> bool:
666
656
])
667
657
except ExecError as e :
668
658
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 } "
670
660
)
671
661
return False
672
662
@@ -798,7 +788,7 @@ def _on_create_backup_action(self, event) -> None: # noqa: C901
798
788
Model Name: { self .model .name }
799
789
Application Name: { self .model .app .name }
800
790
Unit Name: { self .charm .unit .name }
801
- Juju Version: { str ( juju_version ) }
791
+ Juju Version: { juju_version !s }
802
792
"""
803
793
if not self ._upload_content_to_s3 (
804
794
metadata ,
@@ -862,7 +852,7 @@ def _on_create_backup_action(self, event) -> None: # noqa: C901
862
852
f"backup/{ self .stanza_name } /{ backup_id } /backup.log" ,
863
853
s3_parameters ,
864
854
)
865
- error_message = f"Failed to backup PostgreSQL with error: { str ( e ) } "
855
+ error_message = f"Failed to backup PostgreSQL with error: { e !s } "
866
856
logger .error (f"Backup failed: { error_message } " )
867
857
event .fail (error_message )
868
858
else :
@@ -924,7 +914,7 @@ def _on_list_backups_action(self, event) -> None:
924
914
event .set_results ({"backups" : formatted_list })
925
915
except ExecError as e :
926
916
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 } " )
928
918
929
919
def _on_restore_action (self , event ): # noqa: C901
930
920
"""Request that pgBackRest restores a backup."""
@@ -943,10 +933,8 @@ def _on_restore_action(self, event): # noqa: C901
943
933
logger .info ("Validating provided backup-id and restore-to-time" )
944
934
backups = self ._list_backups (show_failed = False )
945
935
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
950
938
if backup_id and not is_backup_id_real and not is_backup_id_timeline :
951
939
error_message = f"Invalid backup-id: { backup_id } "
952
940
logger .error (f"Restore failed: { error_message } " )
@@ -1001,7 +989,7 @@ def _on_restore_action(self, event): # noqa: C901
1001
989
try :
1002
990
self .container .stop (self .charm ._postgresql_service )
1003
991
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 } "
1005
993
logger .error (f"Restore failed: { error_message } " )
1006
994
event .fail (error_message )
1007
995
return
@@ -1025,9 +1013,7 @@ def _on_restore_action(self, event): # noqa: C901
1025
1013
except ApiError as e :
1026
1014
# If previous PITR restore was unsuccessful, there are no such endpoints.
1027
1015
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} "
1031
1017
logger .error (f"Restore failed: { error_message } " )
1032
1018
event .fail (error_message )
1033
1019
self ._restart_database ()
@@ -1037,7 +1023,7 @@ def _on_restore_action(self, event): # noqa: C901
1037
1023
try :
1038
1024
self ._empty_data_files ()
1039
1025
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 } "
1041
1027
logger .error (f"Restore failed: { error_message } " )
1042
1028
event .fail (error_message )
1043
1029
self ._restart_database ()
@@ -1188,7 +1174,7 @@ def _render_pgbackrest_conf_file(self) -> bool:
1188
1174
)
1189
1175
1190
1176
# 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 :
1192
1178
template = Template (file .read ())
1193
1179
# Render the template file with the correct values.
1194
1180
rendered = template .render (
@@ -1217,13 +1203,14 @@ def _render_pgbackrest_conf_file(self) -> bool:
1217
1203
)
1218
1204
1219
1205
# Render the logrotate configuration file.
1220
- with open ("templates/pgbackrest.logrotate.j2" , "r" ) as file :
1206
+ with open ("templates/pgbackrest.logrotate.j2" ) as file :
1221
1207
template = Template (file .read ())
1222
1208
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
+ )
1227
1214
self .container .start (self .charm .rotate_logs_service )
1228
1215
1229
1216
return True
0 commit comments