upgrade_strategy 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python3
  2. import json
  3. import re
  4. import sys
  5. from catalog_update.upgrade_strategy import semantic_versioning
  6. RE_STABLE_VERSION_BASE = r'\d+\.\d+\.\d+'
  7. ENUMS = {
  8. 'node18Image': {
  9. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-18'),
  10. 'STRIP_TEXT': '-18'
  11. },
  12. 'node18MinimalImage': {
  13. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-18-minimal'),
  14. 'STRIP_TEXT': '-18-minimal'
  15. },
  16. 'node16Image': {
  17. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-16'),
  18. 'STRIP_TEXT': '-16'
  19. },
  20. 'node16MinimalImage': {
  21. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-16-minimal'),
  22. 'STRIP_TEXT': '-16-minimal'
  23. },
  24. 'node14Image': {
  25. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-14'),
  26. 'STRIP_TEXT': '-14'
  27. },
  28. 'node14MinimalImage': {
  29. 'RE_STABLE_VERSION': re.compile(rf'{RE_STABLE_VERSION_BASE}-14-minimal'),
  30. 'STRIP_TEXT': '-14-minimal'
  31. }
  32. }
  33. def newer_mapping(image_tags):
  34. output = {
  35. "tags": {},
  36. "app_version": ""
  37. }
  38. for key in image_tags.keys():
  39. STRIP_TEXT = ENUMS[key].get('STRIP_TEXT', None) if key in ENUMS else None
  40. RE_STABLE_VERSION = ENUMS[key].get('RE_STABLE_VERSION', None) if key in ENUMS else None
  41. if (STRIP_TEXT is None) or (RE_STABLE_VERSION is None):
  42. continue
  43. tags = {t.strip(STRIP_TEXT): t for t in image_tags[key] if RE_STABLE_VERSION.fullmatch(t)}
  44. version = semantic_versioning(list(tags))
  45. if not version:
  46. continue
  47. # 16 is the "default" (Also tied to latest tag)
  48. if key == 'node16Image':
  49. output['app_version'] = version
  50. output['tags'][key] = tags[version]
  51. return output
  52. if __name__ == '__main__':
  53. try:
  54. versions_json = json.loads(sys.stdin.read())
  55. except ValueError:
  56. raise ValueError('Invalid json specified')
  57. print(json.dumps(newer_mapping(versions_json)))