migrate 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/python3
  2. import json
  3. import os
  4. import sys
  5. def migrate_volume(volume, suffix=''):
  6. return {
  7. 'type': 'hostPath',
  8. 'hostPathConfig': {
  9. 'hostPath': volume['hostPath']+suffix
  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. 'service', 'updateStrategy', 'certificate', 'enableResourceLimits', 'cpuLimit',
  20. 'memLimit', 'dnsConfig', 'environmentVariables', 'nextcloud', 'cronjob', 'nginx',
  21. 'nginxConfig', 'postgresAppVolumeMounts', 'extraAppVolumeMounts', 'appVolumeMounts',
  22. 'useServiceNameForHost',
  23. ]
  24. values.update({
  25. # Migrate Network
  26. 'ncNetwork': {
  27. 'webPort': values['service']['nodePort'],
  28. 'certificateID': values['certificate'],
  29. 'nginx': {
  30. 'proxyTimeouts': values.get('nginxConfig', {}).get('proxy_timeouts', 60),
  31. 'useDifferentAccessPort': values.get('nginxConfig', {}).get('useDifferentAccessPort', False),
  32. 'externalAccessPort': values.get('nginxConfig', {}).get('externalAccessPort', 443)
  33. } if values['certificate'] else {}
  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. 'ncConfig': {
  53. 'additionalEnvs': values.get('environmentVariables', []),
  54. 'adminUser': values['nextcloud']['username'],
  55. 'adminPassword': values['nextcloud']['password'],
  56. 'host': values['nextcloud'].get('host', ''),
  57. 'dataDir': values['nextcloud']['datadir'],
  58. 'commands': (['ffmpeg'] if values['nextcloud']['install_ffmpeg'] else []) + (['smbclient'] if values['nextcloud']['install_smbclient'] else []),
  59. 'maxUploadLimit': values['nextcloud']['max_upload_size'],
  60. 'maxExecutionTime': values['nextcloud']['max_execution_time'],
  61. 'phpMemoryLimit': values['nextcloud']['php_memory_limit'],
  62. 'opCacheMemoryConsumption': values['nextcloud']['opcache_memory_consumption'],
  63. 'cron': {
  64. 'enabled': values['cronjob']['enabled'],
  65. 'schedule': values['cronjob']['schedule'] if values['cronjob']['enabled'] else '*/15 * * * *',
  66. }
  67. },
  68. # Migrate Storage
  69. 'ncStorage': {
  70. 'isDataInTheSameVolume': True,
  71. 'pgData': migrate_volume(values['postgresAppVolumeMounts']['postgres-data']),
  72. 'pgBackup': migrate_volume(values['postgresAppVolumeMounts']['postgres-backup']),
  73. 'data': migrate_volume(values['appVolumeMounts']['nextcloud-data']),
  74. 'html': migrate_volume(values['appVolumeMounts']['nextcloud-data']),
  75. 'additionalStorages': [
  76. {
  77. 'type': 'hostPath',
  78. 'hostPathConfig': {'hostPath': e['hostPath']},
  79. 'mountPath': e['mountPath'],
  80. }
  81. for e in values.get('extraAppVolumeMounts', [])
  82. ],
  83. },
  84. })
  85. for k in delete_keys:
  86. values.pop(k, None)
  87. return values
  88. def migrate(values):
  89. if 'isDataInTheSameVolume' in values.keys():
  90. values['ncStorage']['isDataInTheSameVolume'] = values.pop('isDataInTheSameVolume', True)
  91. return values
  92. # If this missing, we have already migrated
  93. if not 'appVolumeMounts' in values.keys():
  94. if 'certificateID' in values['ncNetwork']:
  95. if not values['ncNetwork']['certificateID']:
  96. values['ncNetwork']['nginx'] = {}
  97. # If 'shouldFixMigration' missing, we should fix migration and then add the key
  98. if not 'migrationFixed' in values['ncStorage'].keys():
  99. values['ncStorage']['isDataInTheSameVolume'] = True
  100. values['ncStorage']['migrationFixed'] = True
  101. return values
  102. return migrate_common_lib(values)
  103. if __name__ == '__main__':
  104. if len(sys.argv) != 2:
  105. exit(1)
  106. if os.path.exists(sys.argv[1]):
  107. with open(sys.argv[1], 'r') as f:
  108. print(json.dumps(migrate(json.loads(f.read()))))