Added first draft of recovery logic
This commit is contained in:
parent
96b94f5c89
commit
b98976a016
@ -51,27 +51,54 @@ class ImageEndpoint:
|
|||||||
# get stored SHA512 hash
|
# get stored SHA512 hash
|
||||||
stored_hash = metadata.get('sha512', '')
|
stored_hash = metadata.get('sha512', '')
|
||||||
logger.debug('Sorted SHA512: {}'.format(stored_hash))
|
logger.debug('Sorted SHA512: {}'.format(stored_hash))
|
||||||
service_image_bytes = None
|
# recovery code
|
||||||
|
recovery_has_image = {}
|
||||||
|
recovery_hash_matches = {}
|
||||||
|
valid_image_bytes = None
|
||||||
|
# check image existence and correctness for each service + retrieve valid image if possible
|
||||||
for service in ImageEndpoint.storageServiceList:
|
for service in ImageEndpoint.storageServiceList:
|
||||||
logger.debug('Checking hash for ' + service.name)
|
logger.debug('Checking recovery for service ' + service.name)
|
||||||
# get image bytes from Dropbox
|
print('Checking recovery for service ' + service.name)
|
||||||
service_image_bytes = service.read_file(metadata['filename'])
|
service_image_bytes = service.read_file(metadata['filename'])
|
||||||
if service_image_bytes is None:
|
if service_image_bytes is not None:
|
||||||
return JsonResponse({'Result': 'Error - could not find image in ' + service.name, 'id': identifier},
|
recovery_has_image[service.name] = True
|
||||||
status=404, safe=False)
|
actual_service_hash = create_sha512(service_image_bytes)
|
||||||
|
if stored_hash == actual_service_hash:
|
||||||
|
recovery_hash_matches[service.name] = True
|
||||||
|
if valid_image_bytes is None:
|
||||||
|
valid_image_bytes = service_image_bytes
|
||||||
|
else:
|
||||||
|
recovery_hash_matches[service.name] = False
|
||||||
|
else:
|
||||||
|
recovery_has_image[service.name] = False
|
||||||
|
recovery_hash_matches[service.name] = False
|
||||||
|
|
||||||
# calc dropbox image hash
|
# TODO: after talking with tobias about updating => replace with older version if hash is wrong
|
||||||
actual_service_hash = create_sha512(service_image_bytes)
|
|
||||||
logger.debug('Actual '+service.name+' SHA512: {}'.format(actual_service_hash))
|
# check if any service has the image saved
|
||||||
if stored_hash != actual_service_hash:
|
if not ImageEndpoint.combine_boolean_dict_or(recovery_has_image):
|
||||||
return JsonResponse('Stored hash in service "'+service.name+'"does not match generated one! '
|
logger.debug('None of the storage services has the requested image saved')
|
||||||
'stored: {} actual: {}'.format(stored_hash, actual_service_hash),
|
MongoDBService.deleteSingle(identifier)
|
||||||
status=400, safe=False)
|
return JsonResponse({'Result': 'Error - image is not available on any storage service and was deleted',
|
||||||
|
'id': identifier}, status=404, safe=False)
|
||||||
|
|
||||||
|
# check if any service has a valid version of the image saved
|
||||||
|
if not ImageEndpoint.combine_boolean_dict_or(recovery_hash_matches) and valid_image_bytes is None:
|
||||||
|
logger.debug('None of the storage services has a valid image saved')
|
||||||
|
MongoDBService.deleteSingle(identifier)
|
||||||
|
return JsonResponse({'Result': 'Error - image is not available on any storage service and was deleted',
|
||||||
|
'id': identifier}, status=404, safe=False)
|
||||||
|
|
||||||
|
# at this point we know at least one valid image is available
|
||||||
|
for service in ImageEndpoint.storageServiceList:
|
||||||
|
if not recovery_has_image[service.name] or not recovery_hash_matches[service.name]:
|
||||||
|
if not service.create_file(metadata['filename'], valid_image_bytes):
|
||||||
|
logger.error('Error duplicating file in service ' + service.name)
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
'id': identifier,
|
'id': identifier,
|
||||||
'metadata': metadata,
|
'metadata': metadata,
|
||||||
'image_data': WrapperService.wrap_file(service_image_bytes)
|
'image_data': WrapperService.wrap_file(valid_image_bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
return JsonResponse(payload, safe=False)
|
return JsonResponse(payload, safe=False)
|
||||||
@ -175,6 +202,20 @@ class ImageEndpoint:
|
|||||||
return JsonResponse({'id': identifier, 'filename': filename},
|
return JsonResponse({'id': identifier, 'filename': filename},
|
||||||
safe=False)
|
safe=False)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def combine_boolean_dict_and(d: dict) -> bool:
|
||||||
|
result = True
|
||||||
|
for entry in d:
|
||||||
|
result = (result and d[entry])
|
||||||
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def combine_boolean_dict_or(d: dict) -> bool:
|
||||||
|
result = False
|
||||||
|
for entry in d:
|
||||||
|
result = (result or d[entry])
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class HealthEndpoint:
|
class HealthEndpoint:
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user