repository.py

changeset 74
4f56142e7497
parent 64
6cfe6a89e810
child 75
2a44b9649212
--- a/repository.py	Fri Jan 26 10:35:00 2018 +0000
+++ b/repository.py	Fri Jan 26 19:04:04 2018 +0000
@@ -3,7 +3,9 @@
 #
 
 import weakref
+import keyring
 import borgend
+import config
 from scheduler import QueueThread, QueuedEvent
 
 logger=borgend.logger.getChild(__name__)
@@ -72,21 +74,68 @@
 
         return ev._goodtogo
 
-class Repository(FIFO):
-    def __init__(self, name):
-        super().__init__(name = 'RepositoryThread %s' % name)
-        self.repository_name=name
-
-
-# TODO: Should use weak references but they give KeyError
 repositories=weakref.WeakValueDictionary()
 
-def get_controller(name):
+class Repository(FIFO):
+    def __decode_config(self, cfg):
+        loc0='Repository %d' % self.identifier
+
+        self.repository_name=config.check_string(cfg, 'name', 'Name', loc0)
+
+        logger.debug("Configuring repository '%s'" % self.repository_name)
+
+        loc = 'Repository "%s"'
+
+        self.logger=logger.getChild(self.repository_name)
+
+        self.location=config.check_string(cfg, 'location',
+                                         'Target repository location', loc)
+
+        self.borg_parameters=config.BorgParameters.from_config(cfg, loc)
+
+        self.__keychain_account=config.check_string(cfg, 'keychain_account',
+                                                    'Keychain account', loc,
+                                                    default='')
+
+        self.__passphrase=None
+
+        if config.settings['extract_passphrases_at_startup']:
+            try:
+                self.extract_passphrase()
+            except Exception:
+                pass
+
+    def __init__(self, identifier, cfg):
+        self.identifier=identifier
+        self.__decode_config(cfg)
+        super().__init__(name = 'RepositoryThread %s' % self.repository_name)
+        repositories[self.repository_name]=self
+
+    def __extract_passphrase(self):
+        acc=self.__keychain_account
+        if not self.__passphrase:
+            if acc and acc!='':
+                self.logger.debug('Requesting passphrase')
+                try:
+                    pw=keyring.get_password("borg-backup", acc)
+                except Exception as err:
+                    self.logger.error('Failed to retrieve passphrase')
+                    raise err
+                else:
+                    self.logger.debug('Received passphrase')
+                self.__passphrase=pw
+            else:
+                self.__passphrase=None
+        return self.__passphrase
+
+    def launch_borg_instance(self, inst):
+        passphrase=self.__extract_passphrase()
+        inst.launch(passphrase=passphrase)
+
+def find_repository(name):
     if name in repositories:
-        repo = repositories[name]
+        return repositories[name]
     else:
-        repo = Repository(name)
-        repo.start()
-        repositories[name] = repo
-    return repo
+        return None
 
+

mercurial