repository.py

changeset 74
4f56142e7497
parent 64
6cfe6a89e810
child 75
2a44b9649212
equal deleted inserted replaced
73:4f0e9cf8f230 74:4f56142e7497
1 # 1 #
2 # Repository abstraction for queuing 2 # Repository abstraction for queuing
3 # 3 #
4 4
5 import weakref 5 import weakref
6 import keyring
6 import borgend 7 import borgend
8 import config
7 from scheduler import QueueThread, QueuedEvent 9 from scheduler import QueueThread, QueuedEvent
8 10
9 logger=borgend.logger.getChild(__name__) 11 logger=borgend.logger.getChild(__name__)
10 12
11 class FIFOEvent(QueuedEvent): 13 class FIFOEvent(QueuedEvent):
70 # Let _fifo_thread proceed to next action 72 # Let _fifo_thread proceed to next action
71 self._cond.notify() 73 self._cond.notify()
72 74
73 return ev._goodtogo 75 return ev._goodtogo
74 76
77 repositories=weakref.WeakValueDictionary()
78
75 class Repository(FIFO): 79 class Repository(FIFO):
76 def __init__(self, name): 80 def __decode_config(self, cfg):
77 super().__init__(name = 'RepositoryThread %s' % name) 81 loc0='Repository %d' % self.identifier
78 self.repository_name=name 82
83 self.repository_name=config.check_string(cfg, 'name', 'Name', loc0)
84
85 logger.debug("Configuring repository '%s'" % self.repository_name)
86
87 loc = 'Repository "%s"'
88
89 self.logger=logger.getChild(self.repository_name)
90
91 self.location=config.check_string(cfg, 'location',
92 'Target repository location', loc)
93
94 self.borg_parameters=config.BorgParameters.from_config(cfg, loc)
95
96 self.__keychain_account=config.check_string(cfg, 'keychain_account',
97 'Keychain account', loc,
98 default='')
99
100 self.__passphrase=None
101
102 if config.settings['extract_passphrases_at_startup']:
103 try:
104 self.extract_passphrase()
105 except Exception:
106 pass
107
108 def __init__(self, identifier, cfg):
109 self.identifier=identifier
110 self.__decode_config(cfg)
111 super().__init__(name = 'RepositoryThread %s' % self.repository_name)
112 repositories[self.repository_name]=self
113
114 def __extract_passphrase(self):
115 acc=self.__keychain_account
116 if not self.__passphrase:
117 if acc and acc!='':
118 self.logger.debug('Requesting passphrase')
119 try:
120 pw=keyring.get_password("borg-backup", acc)
121 except Exception as err:
122 self.logger.error('Failed to retrieve passphrase')
123 raise err
124 else:
125 self.logger.debug('Received passphrase')
126 self.__passphrase=pw
127 else:
128 self.__passphrase=None
129 return self.__passphrase
130
131 def launch_borg_instance(self, inst):
132 passphrase=self.__extract_passphrase()
133 inst.launch(passphrase=passphrase)
134
135 def find_repository(name):
136 if name in repositories:
137 return repositories[name]
138 else:
139 return None
79 140
80 141
81 # TODO: Should use weak references but they give KeyError
82 repositories=weakref.WeakValueDictionary()
83
84 def get_controller(name):
85 if name in repositories:
86 repo = repositories[name]
87 else:
88 repo = Repository(name)
89 repo.start()
90 repositories[name] = repo
91 return repo
92

mercurial