Coverage for src/edwh_restic_plugin/repositories/r2.py: 48%

1 import os

2

3 from edwh.helpers import generate_password

4 from invoke import Context

5

6 from . import register

7 from .s3 import S3Repository

8

9

10@register()

11 class R2Repository(S3Repository):

12 # https://docs.oracle.com/en-us/iaas/Content/Object/Tasks/s3compatibleapi.htm

13

14 def setup(self) -> None:

15 self.check_env(

16 "R2_NAME",

17 default=None,

18 comment="Bucket to store your backups",

19 )

20

21 self.check_env(

22 "R2_ACCOUNT_ID",

23 default=None,

24 comment="The ID of the account this bucket belongs to. "

25 "Usually seen in the url: https://dash.cloudflare.com/your-id-goes-here/",

26 )

27

28 self.check_env(

29 "R2_PASSWORD",

30 default=generate_password(silent=True),

31 comment="Create a Restic password, keep it safe.",

32 )

33

34 self.check_env("R2_ACCESS_KEY_ID", default=None, comment="(Customer) Access Key")

35 self.check_env("R2_SECRET_ACCESS_KEY", default=None, comment="(Customer) Access Secret")

36

37 def prepare_for_restic(self, _: Context) -> None:

38 env = self.env_config

39 os.environ["RESTIC_PASSWORD"] = env["R2_PASSWORD"]

40 os.environ["RESTIC_REPOSITORY"] = self.uri

41

42 os.environ["AWS_ACCESS_KEY_ID"] = env["R2_ACCESS_KEY_ID"]

43 os.environ["AWS_SECRET_ACCESS_KEY"] = env["R2_SECRET_ACCESS_KEY"]

44

45 @property

46 def uri(self) -> str:

47 env = self.env_config

48 return "s3:{account_id}.r2.cloudflarestorage.com/{bucket}".format(

49 account_id=env.get("R2_ACCOUNT_ID", "?"),

50 bucket=env.get("R2_NAME", "?"),

51 )