migrate 2.5 KB

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