migrate 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 'enableResourceLimits', 'memLimit', 'cpuLimit', 'dnsConfig',
  20. 'web_port', 'environmentVariables', 'timezone', 'password',
  21. 'extraAppVolumeMounts', 'appVolumeMounts', 'dhcp', 'dhcp_start',
  22. 'dhcp_end', 'dhcp_gateway', 'ownerUID', 'ownerGID',
  23. ]
  24. values.update({
  25. # Migrate Network
  26. 'piholeNetwork': {
  27. 'webPort': values['web_port'],
  28. 'dhcp': {
  29. 'enabled': values['dhcp'],
  30. 'start': values.get('dhcp_start', ''),
  31. 'end': values.get('dhcp_end', ''),
  32. 'gateway': values.get('dhcp_gateway', ''),
  33. }
  34. },
  35. # Migrate Resources
  36. 'resources': {
  37. 'limits': {
  38. 'cpu': values.get('cpuLimit', '4000m'),
  39. 'memory': values.get('memLimit', '8Gi'),
  40. }
  41. },
  42. # Migrate DNS
  43. 'podOptions': {
  44. 'dnsConfig': {
  45. 'options': [
  46. {'name': opt['name'], 'value': opt['value']}
  47. for opt in values.get('dnsConfig', {}).get('options', [])
  48. ]
  49. }
  50. },
  51. # Migrate Config
  52. 'TZ': values['timezone'],
  53. 'piholeConfig': {
  54. 'webPassword': values['password'],
  55. 'additionalEnvs': values.get('environmentVariables', []),
  56. },
  57. # Migrate Storage
  58. 'piholeStorage': {
  59. 'config': migrate_volume(values['appVolumeMounts']['config']),
  60. 'dnsmasq': migrate_volume(values['appVolumeMounts']['dnsmasq']),
  61. 'additionalStorages': [
  62. {
  63. 'type': 'hostPath',
  64. 'hostPathConfig': {'hostPath': e['hostPath']},
  65. 'mountPath': e['mountPath'],
  66. 'readOnly': e.get('readOnly', False),
  67. }
  68. for e in values.get('extraAppVolumeMounts', [])
  69. ],
  70. },
  71. })
  72. for k in delete_keys:
  73. values.pop(k, None)
  74. return values
  75. def migrate(values):
  76. # If this missing, we have already migrated
  77. if not 'appVolumeMounts' in values.keys():
  78. # Handle typo for users that already gone through the migration
  79. if 'cache' in values['piholeStorage'].keys():
  80. values['piholeStorage']['dnsmasq'] = values['piholeStorage'].pop('cache')
  81. return values
  82. return migrate_common_lib(values)
  83. if __name__ == '__main__':
  84. if len(sys.argv) != 2:
  85. exit(1)
  86. if os.path.exists(sys.argv[1]):
  87. with open(sys.argv[1], 'r') as f:
  88. print(json.dumps(migrate(json.loads(f.read()))))