migrate 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python3
  2. import json
  3. import os
  4. import sys
  5. def migrate_volume(volume):
  6. return {
  7. 'type': 'hostPath',
  8. 'hostPathConfig': {
  9. 'hostPath': volume['hostPath']
  10. },
  11. } if volume.get('hostPathEnabled', False) else {
  12. 'type': 'ixVolume',
  13. 'ixVolumeConfig': {
  14. 'datasetName': volume['datasetName'],
  15. },
  16. }
  17. def migrate_common_lib(values):
  18. delete_keys = [
  19. 'hostNetwork', 'plexServiceTCP', 'updateStrategy', 'timezone', 'claimToken',
  20. 'environmentVariables', 'gpuConfiguration', 'enableResourceLimits', 'cpuLimit',
  21. 'memLimit', 'dnsConfig', 'extraAppVolumeMounts', 'appVolumeMounts', 'enablePlexPass'
  22. ]
  23. values.update({
  24. # Migrate Network
  25. 'plexNetwork': {
  26. 'webPort': values['plexServiceTCP']['port'],
  27. 'hostNetwork': values['hostNetwork'],
  28. },
  29. # Migrate Resources
  30. 'resources': {
  31. 'limits': {
  32. 'cpu': values.get('cpuLimit', '4000m'),
  33. 'memory': values.get('memLimit', '8Gi'),
  34. }
  35. },
  36. # Migrate DNS
  37. 'podOptions': {
  38. 'dnsConfig': {
  39. 'options': [
  40. {'name': opt['name'], 'value': opt['value']}
  41. for opt in values.get('dnsConfig', {}).get('options', [])
  42. ]
  43. }
  44. },
  45. # Migrate ID
  46. 'plexID': {
  47. # We didn't have exposed this on UI the default
  48. # set by the container is 1000, so we will use that
  49. # if the environmentVariables contains PLEX_UID/PLEX_GID use that
  50. 'user': next((e['value'] for e in values.get('environmentVariables', []) if e['name'] == 'PLEX_UID'), 1000),
  51. 'group': next((e['value'] for e in values.get('environmentVariables', []) if e['name'] == 'PLEX_GID'), 1000),
  52. },
  53. # Migrate Config
  54. 'TZ': values['timezone'],
  55. 'plexConfig': {
  56. 'imageSelector': 'plexPassImage' if values['enablePlexPass'] else 'image',
  57. 'claimToken': values['claimToken'],
  58. # Filter out the PLEX_UID and PLEX_GID
  59. 'additionalEnvs': [e for e in values.get('environmentVariables', []) if e['name'] not in ['PLEX_UID', 'PLEX_GID'] ],
  60. },
  61. 'plexGPU': values.get('gpuConfiguration', {}),
  62. # Migrate Storage
  63. 'plexStorage': {
  64. 'config': migrate_volume(values['appVolumeMounts']['config']),
  65. 'data': migrate_volume(values['appVolumeMounts']['data']),
  66. 'transcode': migrate_volume(values['appVolumeMounts']['transcode']),
  67. 'additionalStorages': [
  68. {
  69. 'type': 'hostPath',
  70. 'hostPathConfig': {'hostPath': e['hostPath']},
  71. 'mountPath': e['mountPath'],
  72. 'readOnly': e['readOnly'],
  73. }
  74. for e in values.get('extraAppVolumeMounts', [])
  75. ],
  76. },
  77. })
  78. for k in delete_keys:
  79. values.pop(k, None)
  80. return values
  81. def migrate(values):
  82. # If this missing, we have already migrated
  83. if not 'appVolumeMounts' in values.keys():
  84. return values
  85. return migrate_common_lib(values)
  86. if __name__ == '__main__':
  87. if len(sys.argv) != 2:
  88. exit(1)
  89. if os.path.exists(sys.argv[1]):
  90. with open(sys.argv[1], 'r') as f:
  91. print(json.dumps(migrate(json.loads(f.read()))))