migrate 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/python3
  2. import json
  3. import os
  4. import sys
  5. # Used to migrate storage format to include ACLs
  6. def storage_migrate(storage):
  7. delete_keys = []
  8. if storage['type'] == 'hostPath':
  9. # Check if the key exists, if not we have already migrated
  10. if not storage.get('hostPath'):
  11. return storage
  12. storage['hostPathConfig'] = {'hostPath': storage['hostPath']}
  13. delete_keys.append('hostPath')
  14. if storage['type'] == 'ixVolume':
  15. # Check if the key exists, if not we have already migrated
  16. if not storage.get('datasetName'):
  17. return storage
  18. storage['ixVolumeConfig'] = {'datasetName': storage['datasetName']}
  19. delete_keys.append('datasetName')
  20. for key in delete_keys:
  21. del storage[key]
  22. return storage
  23. # Used to migrate libraries to additionalStorages
  24. def libraries_migrate(libraries):
  25. # Additional **Libraries** only had a field for hostPath, because Immich
  26. # had a requirement for both hostPath and mountPath to be the same,
  27. # now its no longer the case, so we can merge it with additionalStorages
  28. for idx, library in enumerate(libraries):
  29. if not library.get('hostPath'):
  30. raise Exception(f'Library {idx} is malformed')
  31. libraries[idx] = {
  32. 'type': 'hostPath',
  33. 'mountPath': library['hostPath'],
  34. 'hostPathConfig': {
  35. 'hostPath': library['hostPath'],
  36. }
  37. }
  38. return libraries
  39. def migrate(values):
  40. storage_key = 'immichStorage'
  41. storages = ['uploads', 'library', 'thumbs', 'profile', 'video', 'pgData', 'pgBackup']
  42. for storage in storages:
  43. check_val = values.get(storage_key, {}).get(storage, {})
  44. if not isinstance(check_val, dict) or not check_val:
  45. raise Exception(f'Storage section {storage} is malformed')
  46. values[storage_key][storage] = storage_migrate(check_val)
  47. # Migrate additionalLibraries,
  48. # if additionalLibraries does not exist, we have already migrated
  49. if libraries := values.get(storage_key, {}).get('additionalLibraries', None):
  50. # If additionalLibraries exists, additionalStorages does not exist yet
  51. values[storage_key]['additionalStorages'] = libraries_migrate(libraries)
  52. del values[storage_key]['additionalLibraries']
  53. return values
  54. if __name__ == '__main__':
  55. if len(sys.argv) != 2:
  56. exit(1)
  57. if os.path.exists(sys.argv[1]):
  58. with open(sys.argv[1], 'r') as f:
  59. print(json.dumps(migrate(json.loads(f.read()))))