backups: Fix issue with repository not being initialized

Signed-off-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Joseph Nuthalapati 2019-05-17 18:56:05 +05:30
parent 3a8fbdd799
commit 56babf5cb4
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35

View File

@ -157,6 +157,7 @@ class AddRepositoryForm(forms.Form):
dir_path = dir_path.replace('~', f'/home/{username}') dir_path = dir_path.replace('~', f'/home/{username}')
password = credentials['ssh_password'] password = credentials['ssh_password']
ssh_client = paramiko.SSHClient() ssh_client = paramiko.SSHClient()
# TODO Prompt to accept fingerprint of the server
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try: try:
ssh_client.connect(hostname, username=username, password=password) ssh_client.connect(hostname, username=username, password=password)
@ -164,23 +165,27 @@ class AddRepositoryForm(forms.Form):
msg = _('Accessing the remote repository failed. Details: %(err)s') msg = _('Accessing the remote repository failed. Details: %(err)s')
raise forms.ValidationError(msg, params={'err': str(err)}) raise forms.ValidationError(msg, params={'err': str(err)})
else: else:
with ssh_client.open_sftp() as sftp_client: sftp_client = ssh_client.open_sftp()
try: try:
dir_contents = sftp_client.listdir(dir_path) dir_contents = sftp_client.listdir(dir_path)
except FileNotFoundError: except FileNotFoundError:
logger.info( logger.info(
_(f"Directory {dir_path} doesn't exist. Creating ...")) _(f"Directory {dir_path} doesn't exist. Creating ..."))
sftp_client.mkdir(dir_path) sftp_client.mkdir(dir_path)
else: self.repository = SshBorgRepository(path=path,
if dir_contents: credentials=credentials)
try: else:
self.repository = SshBorgRepository( if dir_contents:
path=path, credentials=credentials) try:
self.repository.get_info() self.repository = SshBorgRepository(
except BorgRepositoryDoesNotExistError: path=path, credentials=credentials)
msg = _(f'Directory {path.split(":")[-1]} is ' self.repository.get_info()
'neither empty nor is an existing ' except BorgRepositoryDoesNotExistError:
'backups repository.') msg = _(f'Directory {path.split(":")[-1]} is '
raise forms.ValidationError(msg) 'neither empty nor is an existing '
'backups repository.')
raise forms.ValidationError(msg)
finally:
sftp_client.close()
finally: finally:
ssh_client.close() ssh_client.close()