migrate 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. libraries[idx] = {
  30. 'type': 'hostPath',
  31. 'mountPath': library['hostPath'],
  32. 'hostPathConfig': {
  33. 'hostPath': library['hostPath'],
  34. }
  35. }
  36. return libraries
  37. def migrate(values):
  38. storage_key = 'immichStorage'
  39. storages = ['uploads', 'library', 'thumbs', 'profile', 'video', 'pgData', 'pgBackup']
  40. for storage in storages:
  41. values[storage_key][storage] = storage_migrate(values[storage_key][storage])
  42. # Migrate additionalLibraries,
  43. # if additionalLibraries does not exist, we have already migrated
  44. if libraries := values[storage_key].get('additionalLibraries', None):
  45. # If additionalLibraries exists, additionalStorages does not exist yet
  46. values[storage_key]['additionalStorages'] = libraries_migrate(libraries)
  47. del values[storage_key]['additionalLibraries']
  48. return values
  49. if __name__ == '__main__':
  50. if len(sys.argv) != 2:
  51. exit(1)
  52. if os.path.exists(sys.argv[1]):
  53. with open(sys.argv[1], 'r') as f:
  54. print(json.dumps(migrate(json.loads(f.read()))))