Coverage for src/edwh_restic_plugin/repositories/s3.py: 42%
24 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-28 16:28 +0100
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-28 16:28 +0100
1import os
3from edwh.helpers import generate_password
4from invoke import Context
6from . import Repository, register
9@register()
10class S3Repository(Repository):
11 # todo: currently tested on Oracle s3 compat and other non-Amazon S3 compatible services, so check with actual S3!
13 def setup(self) -> None:
14 self.check_env(
15 "S3_NAME",
16 default=None,
17 comment="Bucket to store your backups",
18 )
20 self.check_env(
21 "S3_PASSWORD",
22 default=generate_password(silent=True),
23 comment="Create a Restic password, keep it safe.",
24 )
26 self.check_env("S3_URL", default=None, comment="Full S3 storage URL")
28 self.check_env(
29 "S3_ACCESS_KEY_ID", default=None, comment="Specifies an AWS access key associated with an IAM account."
30 )
31 self.check_env(
32 "S3_SECRET_ACCESS_KEY", default=None, comment="Specifies the secret key associated with the access key."
33 )
35 def prepare_for_restic(self, _: Context) -> None:
36 env = self.env_config
37 os.environ["RESTIC_PASSWORD"] = env["S3_PASSWORD"]
38 os.environ["RESTIC_REPOSITORY"] = self.uri
40 os.environ["AWS_ACCESS_KEY_ID"] = env["S3_ACCESS_KEY_ID"]
41 os.environ["AWS_SECRET_ACCESS_KEY"] = env["S3_SECRET_ACCESS_KEY"]
43 @property
44 def uri(self) -> str:
45 bucket = self.env_config["S3_NAME"]
46 base = self.env_config["S3_URL"]
47 # make sure prefix and suffix are there but only once:
48 base = base.removeprefix("s3:").removesuffix(f"/{bucket}").strip("/")
49 return f"s3:{base}/{bucket}"