-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbackups.py
1455 lines (1273 loc) · 59.7 KB
/
backups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
"""Backups implementation."""
import json
import logging
import os
import pwd
import re
import shutil
import tempfile
import time
from datetime import datetime, timezone
from io import BytesIO
from pathlib import Path
from subprocess import TimeoutExpired, run
import boto3 as boto3
import botocore
from botocore.exceptions import ClientError
from charms.data_platform_libs.v0.s3 import (
CredentialsChangedEvent,
S3Requirer,
)
from charms.operator_libs_linux.v2 import snap
from jinja2 import Template
from ops.charm import ActionEvent, HookEvent
from ops.framework import Object
from ops.jujuversion import JujuVersion
from ops.model import ActiveStatus, MaintenanceStatus
from tenacity import RetryError, Retrying, stop_after_attempt, wait_fixed
from constants import (
BACKUP_ID_FORMAT,
BACKUP_TYPE_OVERRIDES,
BACKUP_USER,
PATRONI_CONF_PATH,
PGBACKREST_BACKUP_ID_FORMAT,
PGBACKREST_CONF_PATH,
PGBACKREST_CONFIGURATION_FILE,
PGBACKREST_EXECUTABLE,
PGBACKREST_LOGROTATE_FILE,
PGBACKREST_LOGS_PATH,
POSTGRESQL_DATA_PATH,
)
logger = logging.getLogger(__name__)
ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE = "the S3 repository has backups from another cluster"
FAILED_TO_ACCESS_CREATE_BUCKET_ERROR_MESSAGE = (
"failed to access/create the bucket, check your S3 settings"
)
FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE = "failed to initialize stanza, check your S3 settings"
CANNOT_RESTORE_PITR = "cannot restore PITR, juju debug-log for details"
S3_BLOCK_MESSAGES = [
ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE,
FAILED_TO_ACCESS_CREATE_BUCKET_ERROR_MESSAGE,
FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE,
]
class ListBackupsError(Exception):
"""Raised when pgBackRest fails to list backups."""
class PostgreSQLBackups(Object):
"""In this class, we manage PostgreSQL backups."""
def __init__(self, charm, relation_name: str):
"""Manager of PostgreSQL backups."""
super().__init__(charm, "backup")
self.charm = charm
self.relation_name = relation_name
# s3 relation handles the config options for s3 backups
self.s3_client = S3Requirer(self.charm, self.relation_name)
self.framework.observe(
self.s3_client.on.credentials_changed, self._on_s3_credential_changed
)
# When the leader unit is being removed, s3_client.on.credentials_gone is performed on it (and only on it).
# After a new leader is elected, the S3 connection must be reinitialized.
self.framework.observe(self.charm.on.leader_elected, self._on_s3_credential_changed)
self.framework.observe(self.s3_client.on.credentials_gone, self._on_s3_credential_gone)
self.framework.observe(self.charm.on.create_backup_action, self._on_create_backup_action)
self.framework.observe(self.charm.on.list_backups_action, self._on_list_backups_action)
self.framework.observe(self.charm.on.restore_action, self._on_restore_action)
@property
def stanza_name(self) -> str:
"""Stanza name, composed by model and cluster name."""
return f"{self.model.name}.{self.charm.cluster_name}"
@property
def _tls_ca_chain_filename(self) -> str:
"""Returns the path to the TLS CA chain file."""
s3_parameters, _ = self._retrieve_s3_parameters()
if s3_parameters.get("tls-ca-chain") is not None:
return f"{self.charm._storage_path}/pgbackrest-tls-ca-chain.crt"
return ""
def _are_backup_settings_ok(self) -> tuple[bool, str | None]:
"""Validates whether backup settings are OK."""
if self.model.get_relation(self.relation_name) is None:
return (
False,
"Relation with s3-integrator charm missing, cannot create/restore backup.",
)
_, missing_parameters = self._retrieve_s3_parameters()
if missing_parameters:
return False, f"Missing S3 parameters: {missing_parameters}"
return True, None
@property
def _can_initialise_stanza(self) -> bool:
"""Validates whether this unit can initialise a stanza."""
# Don't allow stanza initialisation if this unit hasn't started the database
# yet and either hasn't joined the peer relation yet or hasn't configured TLS
# yet while other unit already has TLS enabled.
return not (
not self.charm._patroni.member_started
and (
(len(self.charm._peers.data.keys()) == 2)
or (
"tls" not in self.charm.unit_peer_data
and any("tls" in unit_data for _, unit_data in self.charm._peers.data.items())
)
)
)
def _can_unit_perform_backup(self) -> tuple[bool, str | None]:
"""Validates whether this unit can perform a backup."""
if self.charm.is_blocked:
return False, "Unit is in a blocking state"
tls_enabled = "tls" in self.charm.unit_peer_data
# Check if this unit is the primary (if it was not possible to retrieve that information,
# then show that the unit cannot perform a backup, because possibly the database is offline).
try:
is_primary = self.charm.is_primary
except RetryError:
return False, "Unit cannot perform backups as the database seems to be offline"
# Only enable backups on primary if there are replicas but TLS is not enabled.
if is_primary and self.charm.app.planned_units() > 1 and tls_enabled:
return False, "Unit cannot perform backups as it is the cluster primary"
# Can create backups on replicas only if TLS is enabled (it's needed to enable
# pgBackRest to communicate with the primary to request that missing WAL files
# are pushed to the S3 repo before the backup action is triggered).
if not is_primary and not tls_enabled:
return False, "Unit cannot perform backups as TLS is not enabled"
if not self.charm._patroni.member_started:
return False, "Unit cannot perform backups as it's not in running state"
if "stanza" not in self.charm.app_peer_data:
return False, "Stanza was not initialised"
return self._are_backup_settings_ok()
def can_use_s3_repository(self) -> tuple[bool, str | None]:
"""Returns whether the charm was configured to use another cluster repository."""
# Check model uuid
s3_parameters, _ = self._retrieve_s3_parameters()
s3_model_uuid = self._read_content_from_s3(
"model-uuid.txt",
s3_parameters,
)
if s3_model_uuid and s3_model_uuid.strip() != self.model.uuid:
logger.debug(
f"can_use_s3_repository: incompatible model-uuid s3={s3_model_uuid.strip()}, local={self.model.uuid}"
)
return False, ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
try:
return_code, stdout, stderr = self._execute_command(
[PGBACKREST_EXECUTABLE, PGBACKREST_CONFIGURATION_FILE, "info", "--output=json"],
timeout=30,
)
except TimeoutExpired as e:
# Raise an error if the connection timeouts, so the user has the possibility to
# fix network issues and call juju resolve to re-trigger the hook that calls
# this method.
logger.error(f"error: {e!s} - please fix the error and call juju resolve on this unit")
raise TimeoutError from e
else:
if return_code != 0:
logger.error(stderr)
return False, FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE
for stanza in json.loads(stdout):
if stanza.get("name") != self.stanza_name:
logger.debug(
f"can_use_s3_repository: incompatible stanza name s3={stanza.get('name', '')}, local={self.stanza_name}"
)
return False, ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
return_code, system_identifier_from_instance, error = self._execute_command([
f"/snap/charmed-postgresql/current/usr/lib/postgresql/{self.charm._patroni.get_postgresql_version().split('.')[0]}/bin/pg_controldata",
POSTGRESQL_DATA_PATH,
])
if return_code != 0:
raise Exception(error)
system_identifier_from_instance = next(
line
for line in system_identifier_from_instance.splitlines()
if "Database system identifier" in line
).split(" ")[-1]
system_identifier_from_stanza = str(stanza.get("db")[0]["system-id"])
if system_identifier_from_instance != system_identifier_from_stanza:
logger.debug(
f"can_use_s3_repository: incompatible system identifier s3={system_identifier_from_stanza}, local={system_identifier_from_instance}"
)
return False, ANOTHER_CLUSTER_REPOSITORY_ERROR_MESSAGE
return True, None
def _change_connectivity_to_database(self, connectivity: bool) -> None:
"""Enable or disable the connectivity to the database."""
self.charm.unit_peer_data.update({"connectivity": "on" if connectivity else "off"})
self.charm.update_config()
def _construct_endpoint(self, s3_parameters: dict) -> str:
"""Construct the S3 service endpoint using the region.
This is needed when the provided endpoint is from AWS, and it doesn't contain the region.
"""
# Use the provided endpoint if a region is not needed.
endpoint = s3_parameters["endpoint"]
# Load endpoints data.
loader = botocore.loaders.create_loader()
data = loader.load_data("endpoints")
# Construct the endpoint using the region.
resolver = botocore.regions.EndpointResolver(data)
endpoint_data = resolver.construct_endpoint("s3", s3_parameters["region"])
# Use the built endpoint if it is an AWS endpoint.
if endpoint_data and endpoint.endswith(endpoint_data["dnsSuffix"]):
endpoint = f"{endpoint.split('://')[0]}://{endpoint_data['hostname']}"
return endpoint
def _create_bucket_if_not_exists(self) -> None:
s3_parameters, missing_parameters = self._retrieve_s3_parameters()
if missing_parameters:
return
bucket_name = s3_parameters["bucket"]
region = s3_parameters.get("region")
session = boto3.session.Session(
aws_access_key_id=s3_parameters["access-key"],
aws_secret_access_key=s3_parameters["secret-key"],
region_name=s3_parameters["region"],
)
try:
s3 = session.resource(
"s3",
endpoint_url=self._construct_endpoint(s3_parameters),
verify=(self._tls_ca_chain_filename or None),
)
except ValueError as e:
logger.exception("Failed to create a session '%s' in region=%s.", bucket_name, region)
raise e
bucket = s3.Bucket(bucket_name)
try:
bucket.meta.client.head_bucket(Bucket=bucket_name)
logger.info("Bucket %s exists.", bucket_name)
exists = True
except botocore.exceptions.ConnectTimeoutError as e:
# Re-raise the error if the connection timeouts, so the user has the possibility to
# fix network issues and call juju resolve to re-trigger the hook that calls
# this method.
logger.error(f"error: {e!s} - please fix the error and call juju resolve on this unit")
raise e
except ClientError:
logger.warning("Bucket %s doesn't exist or you don't have access to it.", bucket_name)
exists = False
if not exists:
try:
bucket.create(CreateBucketConfiguration={"LocationConstraint": region})
bucket.wait_until_exists()
logger.info("Created bucket '%s' in region=%s", bucket_name, region)
except ClientError as error:
logger.exception(
"Couldn't create bucket named '%s' in region=%s.", bucket_name, region
)
raise error
def _empty_data_files(self) -> bool:
"""Empty the PostgreSQL data directory in preparation of backup restore."""
try:
path = Path(POSTGRESQL_DATA_PATH)
if path.exists() and path.is_dir():
shutil.rmtree(path)
except OSError as e:
logger.warning(f"Failed to remove contents of the data directory with error: {e!s}")
return False
return True
def _execute_command(
self,
command: list[str],
command_input: bytes | None = None,
timeout: int | None = None,
) -> tuple[int, str, str]:
"""Execute a command in the workload container."""
def demote():
pw_record = pwd.getpwnam("snap_daemon")
def result():
os.setgid(pw_record.pw_gid)
os.setuid(pw_record.pw_uid)
return result
# Input is generated by the charm
process = run( # noqa: S603
command,
input=command_input,
capture_output=True,
preexec_fn=demote(),
timeout=timeout,
)
return process.returncode, process.stdout.decode(), process.stderr.decode()
def _format_backup_list(self, backup_list) -> str:
"""Formats provided list of backups as a table."""
s3_parameters, _ = self._retrieve_s3_parameters()
backups = [
"Storage bucket name: {:s}".format(s3_parameters["bucket"]),
"Backups base path: {:s}/backup/\n".format(s3_parameters["path"]),
"{:<20s} | {:<19s} | {:<8s} | {:<20s} | {:<23s} | {:<20s} | {:<20s} | {:<8s} | {:s}".format(
"backup-id",
"action",
"status",
"reference-backup-id",
"LSN start/stop",
"start-time",
"finish-time",
"timeline",
"backup-path",
),
]
backups.append("-" * len(backups[2]))
for (
backup_id,
backup_action,
backup_status,
reference,
lsn_start_stop,
start,
stop,
backup_timeline,
path,
) in backup_list:
backups.append(
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}"
)
return "\n".join(backups)
def _generate_backup_list_output(self) -> str:
"""Generates a list of backups in a formatted table.
List contains successful and failed backups in order of ascending time.
"""
backup_list = []
return_code, output, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
"info",
"--output=json",
])
if return_code != 0:
raise ListBackupsError(f"Failed to list backups with error: {stderr}")
backups = json.loads(output)[0]["backup"]
for backup in backups:
backup_id, backup_type = self._parse_backup_id(backup["label"])
backup_action = f"{backup_type} backup"
backup_reference = "None"
if backup["reference"]:
backup_reference, _ = self._parse_backup_id(backup["reference"][-1])
lsn_start_stop = f"{backup['lsn']['start']} / {backup['lsn']['stop']}"
time_start, time_stop = (
datetime.strftime(
datetime.fromtimestamp(stamp, timezone.utc), "%Y-%m-%dT%H:%M:%SZ"
)
for stamp in backup["timestamp"].values()
)
backup_timeline = (
backup["archive"]["start"][:8].lstrip("0")
if backup["archive"] and backup["archive"]["start"]
else ""
)
backup_path = f"/{self.stanza_name}/{backup['label']}"
error = backup["error"]
backup_status = "finished"
if error:
backup_status = f"failed: {error}"
backup_list.append((
backup_id,
backup_action,
backup_status,
backup_reference,
lsn_start_stop,
time_start,
time_stop,
backup_timeline,
backup_path,
))
for timeline, (_, timeline_id) in self._list_timelines().items():
backup_list.append((
timeline,
"restore",
"finished",
"None",
"n/a",
timeline,
"n/a",
timeline_id,
"n/a",
))
backup_list.sort(key=lambda x: x[0])
return self._format_backup_list(backup_list)
def _list_backups(self, show_failed: bool, parse=True) -> dict[str, tuple[str, str]]:
"""Retrieve the list of backups.
Args:
show_failed: whether to also return the failed backups.
parse: whether to convert backup labels to their IDs or not.
Returns:
a dict of previously created backups: id => (stanza, timeline) or an empty dict if there is no backups in
the S3 bucket.
"""
return_code, output, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
"info",
"--output=json",
])
if return_code != 0:
raise ListBackupsError(f"Failed to list backups with error: {stderr}")
repository_info = next(iter(json.loads(output)), None)
# If there are no backups, returns an empty dict.
if repository_info is None:
return dict[str, tuple[str, str]]()
backups = repository_info["backup"]
stanza_name = repository_info["name"]
return dict[str, tuple[str, str]]({
self._parse_backup_id(backup["label"])[0] if parse else backup["label"]: (
stanza_name,
backup["archive"]["start"][:8].lstrip("0")
if backup["archive"] and backup["archive"]["start"]
else "",
)
for backup in backups
if show_failed or not backup["error"]
})
def _list_timelines(self) -> dict[str, tuple[str, str]]:
"""Lists the timelines from the pgBackRest stanza.
Returns:
a dict of timelines: id => (stanza, timeline) or an empty dict if there is no timelines in the S3 bucket.
"""
return_code, output, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
"repo-ls",
"--recurse",
"--output=json",
])
if return_code != 0:
raise ListBackupsError(f"Failed to list repository with error: {stderr}")
repository = json.loads(output).items()
if repository is None:
return dict[str, tuple[str, str]]()
return dict[str, tuple[str, str]]({
datetime.strftime(
datetime.fromtimestamp(timeline_object["time"], timezone.utc),
BACKUP_ID_FORMAT,
): (
timeline.split("/")[1],
timeline.split("/")[-1].split(".")[0].lstrip("0"),
)
for timeline, timeline_object in repository
if timeline.endswith(".history") and not timeline.endswith("backup.history")
})
def _get_nearest_timeline(self, timestamp: str) -> tuple[str, str] | None:
"""Finds the nearest timeline or backup prior to the specified timeline.
Returns:
(stanza, timeline) of the nearest timeline or backup. None, if there are no matches.
"""
timelines = self._list_backups(show_failed=False) | self._list_timelines()
filtered_timelines = [
(timeline_key, timeline_object)
for timeline_key, timeline_object in timelines.items()
if datetime.strptime(timeline_key, BACKUP_ID_FORMAT)
<= self._parse_psql_timestamp(timestamp)
]
return max(filtered_timelines)[1] if len(filtered_timelines) > 0 else None
def _is_psql_timestamp(self, timestamp: str) -> bool:
if not re.match(
r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d{1,6})?([-+](?:\d{2}|\d{4}|\d{2}:\d{2}))?$",
timestamp,
):
return False
try:
self._parse_psql_timestamp(timestamp)
return True
except ValueError:
return False
def _parse_psql_timestamp(self, timestamp: str) -> datetime:
"""Intended to use with data only after _is_psql_timestamp check."""
# With the python >= 3.11 only the datetime.fromisoformat will be sufficient without any regexes. Therefore,
# it will not be required for the _is_psql_timestamp check that ensures intended regex execution.
t = re.sub(r"([-+]\d{2})$", r"\1:00", timestamp)
t = re.sub(r"([-+]\d{2})(\d{2})$", r"\1:\2", t)
t = re.sub(r"\.(\d+)", lambda x: f".{x[1]:06}", t)
dt = datetime.fromisoformat(t)
# Convert to the timezone-naive
if dt.tzinfo is not None and dt.tzinfo is not timezone.utc:
dt = dt.astimezone(tz=timezone.utc)
return dt.replace(tzinfo=None)
def _parse_backup_id(self, label) -> tuple[str, str]:
"""Parse backup ID as a timestamp and its type."""
if label[-1] == "F":
timestamp = label
backup_type = "full"
elif label[-1] == "D":
timestamp = label.split("_")[1]
backup_type = "differential"
elif label[-1] == "I":
timestamp = label.split("_")[1]
backup_type = "incremental"
else:
raise ValueError("Unknown label format for backup ID: %s", label)
return (
datetime.strftime(
datetime.strptime(timestamp[:-1], PGBACKREST_BACKUP_ID_FORMAT),
BACKUP_ID_FORMAT,
),
backup_type,
)
def _initialise_stanza(self, event: HookEvent) -> bool:
"""Initialize the stanza.
A stanza is the configuration for a PostgreSQL database cluster that defines where it is located, how it will
be backed up, archiving options, etc. (more info in
https://pgbackrest.org/user-guide.html#quickstart/configure-stanza).
Returns:
whether stanza initialization were successful.
"""
# Enable stanza initialisation if the backup settings were fixed after being invalid
# or pointing to a repository where there are backups from another cluster.
if self.charm.is_blocked and self.charm.unit.status.message not in S3_BLOCK_MESSAGES:
logger.warning("couldn't initialize stanza due to a blocked status, deferring event")
event.defer()
return False
self.charm.unit.status = MaintenanceStatus("initialising stanza")
# Create the stanza.
try:
# If the tls is enabled, it requires all the units in the cluster to run the pgBackRest service to
# successfully complete validation, and upon receiving the same parent event other units should start it.
# Therefore, the first retry may fail due to the delay of these other units to start this service. 60s given
# for that or else the s3 initialization sequence will fail.
for attempt in Retrying(stop=stop_after_attempt(6), wait=wait_fixed(10), reraise=True):
with attempt:
return_code, _, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
f"--stanza={self.stanza_name}",
"stanza-create",
])
if return_code == 49:
# Raise an error if the connection timeouts, so the user has the possibility to
# fix network issues and call juju resolve to re-trigger the hook that calls
# this method.
logger.error(
f"error: {stderr} - please fix the error and call juju resolve on this unit"
)
raise TimeoutError
if return_code != 0:
raise Exception(stderr)
except TimeoutError as e:
raise e
except Exception as e:
# If the check command doesn't succeed, remove the stanza name
# and rollback the configuration.
logger.exception(e)
self._s3_initialization_set_failure(FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE)
return False
self.start_stop_pgbackrest_service()
# Rest of the successful s3 initialization sequence such as s3-initialization-start and s3-initialization-done
# are left to the check_stanza func.
if self.charm.unit.is_leader():
self.charm.app_peer_data.update({
"stanza": self.stanza_name,
})
else:
self.charm.unit_peer_data.update({
"stanza": self.stanza_name,
})
return True
def check_stanza(self) -> bool:
"""Runs the pgbackrest stanza validation.
Returns:
whether stanza validation was successful.
"""
# Update the configuration to use pgBackRest as the archiving mechanism.
self.charm.update_config()
self.charm.unit.status = MaintenanceStatus("checking stanza")
try:
# If the tls is enabled, it requires all the units in the cluster to run the pgBackRest service to
# successfully complete validation, and upon receiving the same parent event other units should start it.
# Therefore, the first retry may fail due to the delay of these other units to start this service. 60s given
# for that or else the s3 initialization sequence will fail.
for attempt in Retrying(stop=stop_after_attempt(6), wait=wait_fixed(10), reraise=True):
with attempt:
if self.charm._patroni.member_started:
self.charm._patroni.reload_patroni_configuration()
return_code, _, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
f"--stanza={self.stanza_name}",
"check",
])
if return_code != 0:
raise Exception(stderr)
self.charm._set_primary_status_message()
except Exception as e:
# If the check command doesn't succeed, remove the stanza name
# and rollback the configuration.
logger.exception(e)
self._s3_initialization_set_failure(FAILED_TO_INITIALIZE_STANZA_ERROR_MESSAGE)
self.charm.update_config()
return False
if self.charm.unit.is_leader():
self.charm.app_peer_data.update({
"s3-initialization-start": "",
})
else:
self.charm.unit_peer_data.update({"s3-initialization-done": "True"})
return True
def coordinate_stanza_fields(self) -> None:
"""Coordinate the stanza name between the primary and the leader units."""
if (
not self.charm.unit.is_leader()
or "s3-initialization-start" not in self.charm.app_peer_data
):
return
for _unit, unit_data in self.charm._peers.data.items():
if "s3-initialization-done" not in unit_data:
continue
self.charm.app_peer_data.update({
"stanza": unit_data.get("stanza", ""),
"s3-initialization-block-message": unit_data.get(
"s3-initialization-block-message", ""
),
"s3-initialization-start": "",
"s3-initialization-done": "True",
})
self.charm.update_config()
if self.charm._patroni.member_started:
self.charm._patroni.reload_patroni_configuration()
break
@property
def _is_primary_pgbackrest_service_running(self) -> bool:
if not self.charm.primary_endpoint:
logger.warning("Failed to contact pgBackRest TLS server: no primary endpoint")
return False
return_code, _, stderr = self._execute_command([
PGBACKREST_EXECUTABLE,
"server-ping",
"--io-timeout=10",
self.charm.primary_endpoint,
])
if return_code != 0:
logger.warning(
f"Failed to contact pgBackRest TLS server on {self.charm.primary_endpoint} with error {stderr}"
)
return return_code == 0
def _on_s3_credential_changed(self, event: CredentialsChangedEvent):
"""Call the stanza initialization when the credentials or the connection info change."""
if "cluster_initialised" not in self.charm.app_peer_data:
logger.debug("Cannot set pgBackRest configurations, PostgreSQL has not yet started.")
event.defer()
return
# Prevents config change in bad state, so DB peer relations change event will not cause patroni related errors.
if self.charm.unit.status.message == CANNOT_RESTORE_PITR:
logger.info("Cannot change S3 configuration in bad PITR restore status")
event.defer()
return
# Prevents S3 change in the middle of restoring backup and patroni / pgbackrest errors caused by that.
if (
"restoring-backup" in self.charm.app_peer_data
or "restore-to-time" in self.charm.app_peer_data
):
logger.info("Cannot change S3 configuration during restore")
event.defer()
return
if not self._render_pgbackrest_conf_file():
logger.debug("Cannot set pgBackRest configurations, missing configurations.")
return
if not self._can_initialise_stanza:
logger.debug("Cannot initialise stanza yet.")
event.defer()
return
# Start the pgBackRest service for the check_stanza to be successful. It's required to run on all the units if the tls is enabled.
self.start_stop_pgbackrest_service()
if self.charm.unit.is_leader():
self.charm.app_peer_data.update({
"s3-initialization-block-message": "",
"s3-initialization-start": time.asctime(time.gmtime()),
"stanza": "",
"s3-initialization-done": "",
})
if not self.charm.is_primary:
self.charm._set_primary_status_message()
if self.charm.is_primary and "s3-initialization-done" not in self.charm.unit_peer_data:
self._on_s3_credential_changed_primary(event)
if self.charm.is_standby_leader:
logger.info(
"S3 credentials will not be connected on standby cluster until it becomes primary"
)
def _on_s3_credential_changed_primary(self, event: HookEvent) -> bool:
"""Stanza must be cleared before calling this function."""
self.charm.update_config()
if self.charm._patroni.member_started:
self.charm._patroni.reload_patroni_configuration()
try:
self._create_bucket_if_not_exists()
except (ClientError, ValueError):
self._s3_initialization_set_failure(FAILED_TO_ACCESS_CREATE_BUCKET_ERROR_MESSAGE)
return False
can_use_s3_repository, validation_message = self.can_use_s3_repository()
if not can_use_s3_repository:
self._s3_initialization_set_failure(validation_message)
return False
if not self._initialise_stanza(event):
return False
if not self.check_stanza():
return False
s3_parameters, _ = self._retrieve_s3_parameters()
self._upload_content_to_s3(
self.model.uuid,
"model-uuid.txt",
s3_parameters,
)
return True
def _on_s3_credential_gone(self, _) -> None:
if self.charm.unit.is_leader():
self.charm.app_peer_data.update({
"stanza": "",
"s3-initialization-start": "",
"s3-initialization-done": "",
"s3-initialization-block-message": "",
})
self.charm.unit_peer_data.update({
"stanza": "",
"s3-initialization-start": "",
"s3-initialization-done": "",
"s3-initialization-block-message": "",
})
if self.charm.is_blocked and self.charm.unit.status.message in S3_BLOCK_MESSAGES:
self.charm._set_primary_status_message()
def _on_create_backup_action(self, event) -> None:
"""Request that pgBackRest creates a backup."""
backup_type = event.params.get("type", "full")
if backup_type not in BACKUP_TYPE_OVERRIDES:
error_message = f"Invalid backup type: {backup_type}. Possible values: {', '.join(BACKUP_TYPE_OVERRIDES.keys())}."
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
return
if (
backup_type in ["differential", "incremental"]
and len(self._list_backups(show_failed=False)) == 0
):
error_message = (
f"Invalid backup type: {backup_type}. No previous full backup to reference."
)
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
return
logger.info(f"A {backup_type} backup has been requested on unit")
can_unit_perform_backup, validation_message = self._can_unit_perform_backup()
if not can_unit_perform_backup:
logger.error(f"Backup failed: {validation_message}")
event.fail(validation_message)
return
# Retrieve the S3 Parameters to use when uploading the backup logs to S3.
s3_parameters, _ = self._retrieve_s3_parameters()
# Test uploading metadata to S3 to test credentials before backup.
datetime_backup_requested = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
juju_version = JujuVersion.from_environ()
metadata = f"""Date Backup Requested: {datetime_backup_requested}
Model Name: {self.model.name}
Application Name: {self.model.app.name}
Unit Name: {self.charm.unit.name}
Juju Version: {juju_version!s}
"""
if not self._upload_content_to_s3(
metadata,
f"backup/{self.stanza_name}/latest",
s3_parameters,
):
error_message = "Failed to upload metadata to provided S3"
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
return
if not self.charm.is_primary:
# Create a rule to mark the cluster as in a creating backup state and update
# the Patroni configuration.
self._change_connectivity_to_database(connectivity=False)
self.charm.unit.status = MaintenanceStatus("creating backup")
# Set flag due to missing in progress backups on JSON output
# (reference: https://github.com/pgbackrest/pgbackrest/issues/2007)
self.charm.update_config(is_creating_backup=True)
self._run_backup(event, s3_parameters, datetime_backup_requested, backup_type)
if not self.charm.is_primary:
# Remove the rule that marks the cluster as in a creating backup state
# and update the Patroni configuration.
self._change_connectivity_to_database(connectivity=True)
self.charm.update_config(is_creating_backup=False)
self.charm.unit.status = ActiveStatus()
def _run_backup(
self,
event: ActionEvent,
s3_parameters: dict,
datetime_backup_requested: str,
backup_type: str,
) -> None:
command = [
PGBACKREST_EXECUTABLE,
PGBACKREST_CONFIGURATION_FILE,
f"--stanza={self.stanza_name}",
"--log-level-console=debug",
f"--type={BACKUP_TYPE_OVERRIDES[backup_type]}",
"backup",
]
if self.charm.is_primary:
# Force the backup to run in the primary if it's not possible to run it
# on the replicas (that happens when TLS is not enabled).
command.append("--no-backup-standby")
return_code, stdout, stderr = self._execute_command(command)
if return_code != 0:
logger.error(stderr)
# Recover the backup id from the logs.
backup_label_stdout_line = re.findall(
r"(new backup label = )([0-9]{8}[-][0-9]{6}[F])$", stdout, re.MULTILINE
)
if len(backup_label_stdout_line) > 0:
backup_id = backup_label_stdout_line[0][1]
else:
# Generate a backup id from the current date and time if the backup failed before
# generating the backup label (our backup id).
backup_id = self._generate_fake_backup_id(backup_type)
# Upload the logs to S3.
logs = f"""Stdout:
{stdout}
Stderr:
{stderr}
"""
self._upload_content_to_s3(
logs,
f"backup/{self.stanza_name}/{backup_id}/backup.log",
s3_parameters,
)
error_message = f"Failed to backup PostgreSQL with error: {stderr}"
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
else:
try:
backup_id = list(self._list_backups(show_failed=True).keys())[-1]
except ListBackupsError as e:
logger.exception(e)
error_message = "Failed to retrieve backup id"
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
return
# Upload the logs to S3 and fail the action if it doesn't succeed.
logs = f"""Stdout:
{stdout}
Stderr:
{stderr}
"""
if not self._upload_content_to_s3(
logs,
f"backup/{self.stanza_name}/{backup_id}/backup.log",
s3_parameters,
):
error_message = "Error uploading logs to S3"
logger.error(f"Backup failed: {error_message}")
event.fail(error_message)
else:
logger.info(f"Backup succeeded: with backup-id {datetime_backup_requested}")
event.set_results({"backup-status": "backup created"})
def _on_list_backups_action(self, event) -> None:
"""List the previously created backups."""
are_backup_settings_ok, validation_message = self._are_backup_settings_ok()
if not are_backup_settings_ok:
logger.warning(validation_message)
event.fail(validation_message)
return
try:
formatted_list = self._generate_backup_list_output()
event.set_results({"backups": formatted_list})
except ListBackupsError as e:
logger.exception(e)
event.fail(f"Failed to list PostgreSQL backups with error: {e!s}")
def _on_restore_action(self, event): # noqa: C901
"""Request that pgBackRest restores a backup."""
if not self._pre_restore_checks(event):
return
backup_id = event.params.get("backup-id")
restore_to_time = event.params.get("restore-to-time")