migrate 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 'user': 1000,
  50. 'group': 1000,
  51. },
  52. # Migrate Config
  53. 'TZ': values['timezone'],
  54. 'plexConfig': {
  55. 'imageSelector': 'plexPassImage' if values['enablePlexPass'] else 'image',
  56. 'claimToken': values['claimToken'],
  57. 'additionalEnvs': values.get('environmentVariables', []),
  58. },
  59. 'plexGPU': values.get('gpuConfiguration', {}),
  60. # Migrate Storage
  61. 'plexStorage': {
  62. 'config': migrate_volume(values['appVolumeMounts']['config']),
  63. 'data': migrate_volume(values['appVolumeMounts']['data']),
  64. 'transcode': migrate_volume(values['appVolumeMounts']['transcode']),
  65. 'additionalStorages': [
  66. {
  67. 'type': 'hostPath',
  68. 'hostPathConfig': {'hostPath': e['hostPath']},
  69. 'mountPath': e['mountPath'],
  70. 'readOnly': e['readOnly'],
  71. }
  72. for e in values.get('extraAppVolumeMounts', [])
  73. ],
  74. },
  75. })
  76. for k in delete_keys:
  77. values.pop(k, None)
  78. return values
  79. def migrate(values):
  80. # If this missing, we have already migrated
  81. if not 'appVolumeMounts' in values.keys():
  82. return values
  83. return migrate_common_lib(values)
  84. if __name__ == '__main__':
  85. if len(sys.argv) != 2:
  86. exit(1)
  87. if os.path.exists(sys.argv[1]):
  88. with open(sys.argv[1], 'r') as f:
  89. print(json.dumps(migrate(json.loads(f.read()))))