#!/opt/cycle/jetpack/system/python/bin/python3

""" This is used to find out whether the version of the health checks built into
    the HPC image is current enough.
    
    Use: version_check {path to the file with component versions} {the name of the component} {target version}
"""

import sys
from packaging import version

PATH_TO_COMPONENT_VERSIONS = sys.argv[1]
COMPONENT_NAME = sys.argv[2] + "="


def get_current_azurehpc_version():
    with open(PATH_TO_COMPONENT_VERSIONS, "r") as f:
        components = f.readlines()
        for line in components: 
            if (line.startswith(COMPONENT_NAME)):
                return line[len(COMPONENT_NAME):]
   
 
target_version = sys.argv[3]
try:
    current_version = get_current_azurehpc_version()
    if version.parse(current_version) < version.parse(target_version):
        print("Image does not support CycleCloud health checks (azurehpc-health-checks version >= " + target_version + " is required. Found " + str.strip(current_version) + ")")
        exit(1)
except Exception as ex:
    print("Invalid health checks configuration: " + str(ex))
    exit(1)
    
exit(0)
