def _assert_node_version()

in cli/src/pcluster/api/util.py [0:0]


def _assert_node_version():
    try:
        # A nosec comment is appended to the following line in order to disable the B607 and B603 checks.
        # It is a false positive since the PATH search is wanted and the input of the check_output is static.
        # [B607:start_process_with_partial_path] Is suppressed because location of executable is retrieved from env
        #   PATH
        # [B603:subprocess_without_shell_equals_true] Is suppressed because input of check_output is not coming from
        #   untrusted source
        node_version_string = subprocess.check_output(  # nosec B607 B603
            ["node", "--version"], stderr=subprocess.STDOUT, shell=False, encoding="utf-8"
        )
        LOGGER.debug("Found Node.js version (%s)", node_version_string)
    except Exception:
        LOGGER.debug("Unable to determine current Node.js version from node")
        try:
            # A nosec comment is appended to the following line in order to disable the B607 and B603 checks.
            # It is a false positive since the PATH search is wanted and the input of the check_output is static.
            # [B607:start_process_with_partial_path] Is suppressed because location of executable is retrieved from env
            #   PATH
            # [B603:subprocess_without_shell_equals_true] Is suppressed because input of check_output is not coming from
            #   untrusted source
            node_version_string = subprocess.check_output(  # nosec B607 B603
                ["nvm", "current"], stderr=subprocess.STDOUT, shell=False, encoding="utf-8"
            )
            LOGGER.debug("Found Node.js version '%s' in use", node_version_string)
        except Exception:
            message = "Unable to check Node.js version"
            LOGGER.critical(message)
            raise Exception(message)
        # `nvm current` will return `none` if no versions of Node.js are currently installed.
        if node_version_string == "none":
            message = (
                "Node.js does not appear to be installed. Please use the Node Version Manager (nvm) to install a"
                " version of Node.js compatible with this platform."
            )
        else:
            message = (
                f"Unable to invoke Node.js for the installed version {node_version_string}. This version may not be"
                " compatible with this platform. Please use the Node Version Manager (nvm) to install and use a"
                " compatible version of Node.js compatible with this platform."
            )
        LOGGER.critical(message)
        raise Exception(message)

    node_version = packaging.version.parse(node_version_string)

    if node_version < packaging.version.parse(NODEJS_MIN_VERSION):
        message = (
            f"AWS CDK library used by ParallelCluster requires Node.js version >= {NODEJS_MIN_VERSION},"
            " see installation instructions here: https://docs.aws.amazon.com/parallelcluster/latest/ug/install-v3.html"
        )
        LOGGER.critical(message)
        raise Exception(message)
    if (
        packaging.version.parse(NODEJS_INCOMPATIBLE_VERSION_RANGE[0])
        <= node_version
        <= packaging.version.parse(NODEJS_INCOMPATIBLE_VERSION_RANGE[1])
    ):
        message = (
            f"AWS CDK library used by ParallelCluster requires Node.js to not be in the range"
            f" {NODEJS_INCOMPATIBLE_VERSION_RANGE}, but installed Node.js version {node_version_string}"
            f" is within this range, see https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html"
        )
        LOGGER.critical(message)
        raise Exception(message)