Skip to content

OS Version

netutils.os_version

Functions for working with OS Versions.

get_upgrade_path(current_version, target_version, firmware_list)

Utility to return the upgrade path from the current to target firmware version.

Returns:

Type Description
List[str]

List of firmware versions to upgrade from current to target.

Parameters:

Name Type Description Default
current_version str

Current firmware version.

required
target_version str

Target firmware version.

required
firmware_list List[str]

List of firmware versions to use as the upgrade path.

required

Raises:

Type Description
ValueError

If target version is older than current version.

ValueError

If target version equals current version.

Examples:

>>> from netutils.os_version import get_upgrade_path
>>> get_upgrade_path("9.1.6", "10.1.9", ["9.1.10", "9.1.15-h1", "10.0.0", "10.1.9"])
['9.1.10', '9.1.15-h1', '10.0.0', '10.1.9']
>>> from netutils.constants import UPGRADE_PATHS
>>> get_upgrade_path("9.1.6", "10.1.9", UPGRADE_PATHS["PANOS_OFFICIAL_V1"])
['9.1.15-h1', '10.0.0', '10.0.12', '10.1.0', '10.1.9']
Source code in netutils/os_version.py
def get_upgrade_path(
    current_version: str,
    target_version: str,
    firmware_list: t.List[str],
) -> t.List[str]:
    """Utility to return the upgrade path from the current to target firmware version.

    Returns:
        List of firmware versions to upgrade from current to target.

    Args:
        current_version: Current firmware version.
        target_version: Target firmware version.
        firmware_list: List of firmware versions to use as the upgrade path.

    Raises:
        ValueError: If target version is older than current version.
        ValueError: If target version equals current version.

    Examples:
        >>> from netutils.os_version import get_upgrade_path
        >>> get_upgrade_path("9.1.6", "10.1.9", ["9.1.10", "9.1.15-h1", "10.0.0", "10.1.9"])
        ['9.1.10', '9.1.15-h1', '10.0.0', '10.1.9']
        >>> from netutils.constants import UPGRADE_PATHS
        >>> get_upgrade_path("9.1.6", "10.1.9", UPGRADE_PATHS["PANOS_OFFICIAL_V1"])
        ['9.1.15-h1', '10.0.0', '10.0.12', '10.1.0', '10.1.9']
    """
    if LooseVersion(current_version) > LooseVersion(target_version):
        raise ValueError("Target version must be newer than current version.")

    if LooseVersion(current_version) == LooseVersion(target_version):
        raise ValueError("Target version equals current version. No upgrade necessary.")

    upgrade_path = [
        version
        for version in firmware_list
        if LooseVersion(version) > LooseVersion(current_version)
        and LooseVersion(version) <= LooseVersion(target_version)
    ]

    if target_version not in upgrade_path:
        upgrade_path.append(target_version)

    return upgrade_path