Coverage for src/edwh_restic_plugin/repositories/sftp.py: 30%

30 statements  

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

1import os 

2 

3from . import Repository, register 

4 

5 

6@register("sftp", priority=3) 

7class SFTPRepository(Repository): 

8 def __init__(self): 

9 super().__init__() 

10 self.hostname = None 

11 self.password = None 

12 self.name = None 

13 

14 def setup(self): 

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

16 self.check_env( 

17 "SFTP_NAME", 

18 default=None, 

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

20 ) 

21 self.check_env( 

22 "SFTP_PASSWORD", 

23 default=None, 

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

25 ) 

26 self.check_env( 

27 "SFTP_HOSTNAME", 

28 default=None, 

29 comment="Use the correnct hostname (directory above the repo name)", 

30 ) # 

31 

32 def prepare_for_restic(self, c): 

33 """read out of .env file""" 

34 env = self.env_config 

35 

36 self.name = env["SFTP_NAME"] 

37 self.password = env["SFTP_PASSWORD"] 

38 self.hostname = env["SFTP_HOSTNAME"] 

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

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

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

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

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

44 ran = c.run(f'ssh {self.hostname} "exit"', warn=True, hide=True) 

45 if not ran.ok: 

46 print( 

47 """ 

48 SSH config file not (properly) configured, configure according to the following format: 

49 Host romy 

50 HostName romy.edwh.nl 

51 User ubuntu 

52 IdentityFile ~/romy.key 

53 To save a new host in the ssh config file, go to ~/.ssh and edit the config file there. 

54 For more information, read the ssh_config manual (man ssh_config) 

55 """ 

56 ) 

57 exit(1) 

58 

59 @property 

60 def uri(self): 

61 """ 

62 :return: sftp uri with self.hostname and self.name 

63 """ 

64 return f"sftp:{self.hostname}:{self.name}"