SEBA-570 compute backend_filename for restores
Change-Id: Ifee9fee823a5dc37c92632664099c288f90ce640
diff --git a/xos/coreapi/backupsetwatcher.py b/xos/coreapi/backupsetwatcher.py
index 8b16639..0778e48 100644
--- a/xos/coreapi/backupsetwatcher.py
+++ b/xos/coreapi/backupsetwatcher.py
@@ -81,6 +81,12 @@
self.backup_request_dir = "/var/run/xos/backup/requests"
self.backup_response_dir = "/var/run/xos/backup/responses"
self.backup_file_dir = "/var/run/xos/backup/local"
+
+ # For people to copy in a file, it helps to ensure the directory exists.
+ if not os.path.exists(self.backup_file_dir):
+ os.makedirs(self.backup_file_dir)
+
+ # Process any responses that were generated before the coreapi process started.
self.process_response_dir()
def init_request_dir(self):
@@ -256,41 +262,41 @@
update_fields=["enacted", "backend_code", "backend_status"])
continue
+ if backupop.file:
+ # file was specified, check that the uri is allowed
+ if (not backupop.file.uri) or (not backupop.file.uri.startswith(ALLOWED_URI)):
+ backupop.backend_code = 2
+ backupop.backend_status = "Only backup_uri that starts with %s is supported" % ALLOWED_URI
+ backupop.save(allow_modify_feedback=True,
+ update_fields=["backend_code", "backend_status"])
+ continue
+
+ # Generate a backend_filename from uri by stripping off file://
+ # This will leave one leading slash on the URL.
+ backupop.file.backend_filename = backupop.file.uri[7:]
+ backupop.file.save(allow_modify_feedback=True,
+ update_fields=["backend_filename"])
+
# Restores must always specify a file
- if backupop.operation == "restore" and (not backupop.file):
+ if (backupop.operation == "restore") and (not backupop.file):
backupop.backend_code = 2
backupop.backend_status = "Must specify file for restore operation"
backupop.save(allow_modify_feedback=True,
update_fields=["backend_code", "backend_status"])
continue
- if backupop.operation == "create":
- if (backupop.file):
- # file was specified, check that the uri is allowed
- if (not backupop.file.uri) or (not backupop.file.uri.startswith(ALLOWED_URI)):
- backupop.backend_code = 2
- backupop.backend_status = "Only backup_uri that starts with %s is supported" % ALLOWED_URI
- backupop.save(allow_modify_feedback=True,
- update_fields=["backend_code", "backend_status"])
- continue
- else:
- # Generate a backend_filename from uri by stripping off file://
- # This will leave one leading slash on the URL.
- backupop.file.backend_filename = backupop.file.uri[7:]
- backupop.file.save(allow_modify_feedback=True,
- update_fields=["backend_filename"])
- else:
- # Create operation doesn't specify a file, so autogenerate one.
- log.info("Adding autogenerated file to BackupOp", backupop=backupop)
- current_datetime = datetime.datetime.today().strftime('%Y-%m-%d-%H-%M-%S')
- name = "autogenerated-file-%s" % current_datetime
- backend_filename = os.path.join(self.backup_file_dir, name)
- backupop_file = BackupFile(name=name,
- uri="file://" + backend_filename,
- backend_filename=backend_filename)
- backupop_file.save(allow_modify_feedback=True)
- backupop.file = backupop_file
- backupop.save(update_fields=["file"])
+ # Create operation doesn't require a file, so autogenerate one as necessary.
+ if (backupop.operation == "create") and (not backupop.file):
+ log.info("Adding autogenerated file to BackupOp", backupop=backupop)
+ current_datetime = datetime.datetime.today().strftime('%Y-%m-%d-%H-%M-%S')
+ name = "autogenerated-file-%s" % current_datetime
+ backend_filename = os.path.join(self.backup_file_dir, name)
+ backupop_file = BackupFile(name=name,
+ uri="file://" + backend_filename,
+ backend_filename=backend_filename)
+ backupop_file.save(allow_modify_feedback=True)
+ backupop.file = backupop_file
+ backupop.save(update_fields=["file"])
# There can only be one request at a time. Ensure the directory is empty.
self.init_request_dir()
diff --git a/xos/coreapi/test_backupsetwatcher.py b/xos/coreapi/test_backupsetwatcher.py
index 014384d..d95a2ee 100644
--- a/xos/coreapi/test_backupsetwatcher.py
+++ b/xos/coreapi/test_backupsetwatcher.py
@@ -238,9 +238,8 @@
def test_run_once_create(self):
file = Mock(id=7,
- uri="file:///var/run/xos/backup/local/",
- checksum="1234",
- backend_filename="/mybackup")
+ uri="file:///var/run/xos/backup/local/mybackup",
+ checksum="1234")
file.name = "mybackup",
request = Mock(id=3,
@@ -260,15 +259,15 @@
self.assertEqual(request, saved_op)
self.assertEqual(saved_op.status, "inprogress")
+ self.assertEqual(saved_op.file.backend_filename, "/var/run/xos/backup/local/mybackup")
self.assertTrue(self.watcher.exiting)
delayed_shutdown.assert_called()
def test_run_once_restore(self):
file = Mock(id=7,
- uri="file:///var/run/xos/backup/local/",
- checksum="1234",
- backend_filename="/mybackup")
+ uri="file:///var/run/xos/backup/local/mybackup",
+ checksum="1234")
file.name = "mybackup",
request = Mock(id=3,
@@ -288,15 +287,15 @@
self.assertEqual(request, saved_op)
self.assertEqual(saved_op.status, "inprogress")
+ self.assertEqual(saved_op.file.backend_filename, "/var/run/xos/backup/local/mybackup")
self.assertTrue(self.watcher.exiting)
delayed_shutdown.assert_called()
def test_run_once_not_xos(self):
file = Mock(id=7,
- uri="file:///var/run/xos/backup/local/",
- checksum="1234",
- backend_filename="/mybackup")
+ uri="file:///var/run/xos/backup/local/mybackup",
+ checksum="1234")
file.name = "mybackup",
request = Mock(id=3,