migrate 2.3 KB

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