Coverage for src/edwh_restic_plugin/repositories/swift.py: 26%

39 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-10 20:53 +0100

1import os 

2 

3from invoke import Context 

4 

5from . import Repository, register 

6 

7 

8@register( 

9 short_name="os", 

10 aliases=("swift",), 

11 priority=1, # high prio 

12) 

13class SwiftRepository(Repository): 

14 def __init__(self): 

15 super().__init__() 

16 self.restic_password = None 

17 self.password = None 

18 self.container_name = None 

19 self.name = None 

20 

21 def setup(self): 

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

23 self.check_env( 

24 "OS_AUTH_URL", 

25 default="https://identity.stack.cloudvps.com/v2.0", 

26 comment="Auth URL for this openstack environment", 

27 ) 

28 self.check_env( 

29 "OS_TENANT_ID", 

30 default=None, 

31 comment='Tenant name, comes from the openrc file, or from the auth info, looks like "f8d15....269"', 

32 ) 

33 self.check_env( 

34 "OS_TENANT_NAME", 

35 default="BST000425 productie-backups", 

36 comment="Project name within openstack, for example 'BST000425 production backups'", 

37 ) 

38 self.check_env( 

39 "OS_REGION_NAME", 

40 default="NL", 

41 comment="NL is supported, others are unknown.", 

42 ) 

43 self.check_env( 

44 "OS_USERNAME", 

45 default="backup@edwh.nl", 

46 comment="Username is the openstack username", 

47 ) 

48 self.check_env( 

49 "OS_PASSWORD", 

50 default=None, 

51 comment="Password belonging to the openstack user", 

52 ) 

53 self.check_env( 

54 "OS_CONTAINERNAME", 

55 default="backups", 

56 comment="Objectstore container name, should be created automatically if it doesn't exist.", 

57 ) 

58 self.check_env( 

59 "OS_NAME", 

60 default=None, 

61 comment="Repository name within the bucket", 

62 ) 

63 self.check_env( 

64 "OS_RESTIC_PASSWORD", 

65 default=None, 

66 comment="Password of the repository within the container", 

67 ) 

68 

69 # check_env( 

70 # DOTENV, 

71 # "OS_STORAGE_URL", 

72 # default=None, 

73 # comment="voer hier de juiste URL in.", 

74 # ) 

75 # check_env( 

76 # DOTENV, 

77 # "OS_AUTH_TOKEN", 

78 # default=None, 

79 # comment="gvoer hier de juiste TOKEN in.", 

80 # ) 

81 

82 def prepare_for_restic(self, _: Context): 

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

84 env = self.env_config 

85 

86 self.name = env["OS_NAME"] 

87 self.container_name = env["OS_CONTAINERNAME"] 

88 os.environ["OS_USERNAME"] = env["OS_USERNAME"] 

89 os.environ["OS_AUTH_URL"] = env["OS_AUTH_URL"] 

90 os.environ["OS_TENANT_ID"] = env["OS_TENANT_ID"] 

91 os.environ["OS_TENANT_NAME"] = env["OS_TENANT_NAME"] 

92 os.environ["OS_REGION_NAME"] = env["OS_REGION_NAME"] 

93 # os.environ["OS_STORAGE_URL"] = self.keyid = env["OS_STORAGE_URL"] 

94 # os.environ["OS_AUTH_TOKEN"] = self.key = env["OS_AUTH_TOKEN"] 

95 os.environ["OS_PASSWORD"] = self.password = env["OS_PASSWORD"] 

96 os.environ["RESTIC_PASSWORD"] = self.restic_password = env["OS_RESTIC_PASSWORD"] 

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

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

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

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

101 

102 @property 

103 def uri(self): 

104 """ 

105 :return: the swift uri with self.containername and self.name 

106 """ 

107 return f"swift:{self.container_name}:/{self.name}"