Coverage for src/edwh_restic_plugin/repositories/b2.py: 29%

1 import os

2

3 from invoke import Context

4

5 from . import Repository, register

6

7

8@register(priority=2)

9 class B2Repository(Repository):

10 def __init__(self):

11 super().__init__()

12 self.key = None

13 self.keyid = None

14 self.bucket_name = None

15 self.password = None

16 self.name = None

17

18 def setup(self):

19 """Ensure the required settings are defined in the .env file."""

20 self.check_env(

21 "B2_NAME",

22 default=None,

23 comment="Repository name is mandatory (directory)",

24 )

25 self.check_env(

26 "B2_PASSWORD",

27 default=None,

28 comment="Create a password, keep it safe.",

29 )

30 self.check_env(

31 "B2_BUCKETNAME",

32 default=None,

33 comment="Use the correct bucketname (directory above repo name",

34 )

35 self.check_env(

36 "B2_ACCOUNT_ID",

37 default=None,

38 comment="enter the correct KEY ID here.",

39 )

40 self.check_env(

41 "B2_ACCOUNT_KEY",

42 default=None,

43 comment="enter the correct KEY here.",

44 )

45

46 def prepare_for_restic(self, _: Context):

47 """read variables out of .env file"""

48 env = self.env_config

49

50 self.name = env["B2_NAME"]

51 self.password = env["B2_PASSWORD"]

52 self.bucket_name = env["B2_BUCKETNAME"]

53 self.keyid = env["B2_ACCOUNT_ID"]

54 self.key = env["B2_ACCOUNT_KEY"]

55 os.environ["B2_ACCOUNT_ID"] = self.keyid

56 os.environ["B2_ACCOUNT_KEY"] = self.key

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

58 os.environ["RESTIC_HOST"] = self.hostarg

59 os.environ["RESTIC_PASSWORD"] = self.password

60 os.environ["HOST"] = self.hostarg

61 os.environ["URI"] = self.uri

62

63 @property

64 def uri(self):

65 """

66 :return: uri of b2 with self.bucketname and self.name

67 """

68 return f"b2:{self.bucket_name}:{self.name}"