Exemple #1
0
    def download_install(self, version, load_config=None, sync=False):
        if isstring(version):
            version = PanOSVersion(version)
        # Get list of software if needed
        if not self.versions:
            self.check()
        # Get versions as StrictVersion objects
        available_versions = map(PanOSVersion, self.versions.keys())
        target_version = PanOSVersion(str(version))
        current_version = PanOSVersion(self.pandevice.version)

        if str(target_version) not in available_versions:
            raise err.PanDeviceError("Error upgrading to unknown version: %s" %
                                     target_version)

        # Check if already on the target version
        if current_version == target_version:
            raise err.PanDeviceError(
                "Requested upgrade to version %s which is already running on device %s"
                % (target_version, self.pandevice.id))

        # Download the software upgrade
        if not self.versions[str(target_version)]["downloaded"]:
            self.download(target_version, sync=True)
        # Install the software upgrade
        result = self.install(target_version,
                              load_config=load_config,
                              sync=sync)
        return result
Exemple #2
0
 def _next_major_version(self, version):
     if isstring(version):
         version = PanOSVersion(version)
     next_version = PanOSVersion(str(version.major + 1) + ".0.0")
     # Account for lack of PAN-OS 7.0.0
     if next_version == "7.0.0":
         next_version = PanOSVersion("7.0.1")
     return next_version
Exemple #3
0
def test_comp(panos1, panos2):
    x = PanOSVersion(panos1)
    y = PanOSVersion(panos2)
    assert y > x
    assert y >= x
    assert x < y
    assert x <= y
    assert x != y
    assert (x == y) == False
Exemple #4
0
def test_gen_eq(panos1, panos2):
    x = PanOSVersion(panos1)
    y = PanOSVersion(panos2)
    assert x == y
    assert (x != y) == False
    assert x >= y
    assert x <= y
    assert (x > y) == False
    assert (x < y) == False
Exemple #5
0
def main():
    helper = get_connection(
        with_classic_provider_spec=True,
        argument_spec=dict(
            version=dict(type='str', required=True),
            sync_to_peer=dict(type='bool', default=False),
            download=dict(type='bool', default=True),
            install=dict(type='bool', default=True),
            restart=dict(type='bool', default=False),
            timeout=dict(type='int', default=1200)
        )
    )

    module = AnsibleModule(
        argument_spec=helper.argument_spec,
        required_one_of=helper.required_one_of,
        supports_check_mode=True
    )

    # Verify libs are present, get parent object.
    device = helper.get_pandevice_parent(module)

    # Module params.
    version = module.params['version']
    sync_to_peer = module.params['sync_to_peer']
    download = module.params['download']
    install = module.params['install']
    restart = module.params['restart']
    timeout = module.params['timeout']

    changed = False

    try:
        device.timeout = timeout
        device.software.check()

        if PanOSVersion(version) != PanOSVersion(device.version):

            if not module.check_mode:
                if download:
                    device.software.download(version, sync_to_peer=sync_to_peer, sync=True)

                if install:
                    device.software.install(version, sync=True)

                if restart:
                    device.restart()

            changed = True

    except PanDeviceError as e:
        module.fail_json(msg=e.message)

    module.exit_json(changed=changed, version=version)
Exemple #6
0
    def _next_minor_version(self, version):
        from panos.firewall import Firewall

        if isstring(version):
            next_version = PanOSVersion(version)
        if version.minor == 1:
            next_version = PanOSVersion(str(version.major + 1) + ".0.0")
        # There is no PAN-OS 5.1 for firewalls, so next minor release from 5.0.x is 6.0.0.
        elif (version.major == 5 and version.minor == 0
              and issubclass(type(self.pandevice), Firewall)):
            next_version = PanOSVersion("6.0.0")
        else:
            next_version = PanOSVersion(str(version.major) + ".1.0")
        # Account for lack of PAN-OS 7.0.0
        if next_version == "7.0.0":
            next_version = PanOSVersion("7.0.1")
        return next_version
Exemple #7
0
    def _direct_upgrade_possible(self, current_version, target_version):
        """Check if current version can directly upgrade to target version

        :returns True if a direct upgrade is possible, False if not

        """
        if isstring(current_version):
            current_version = PanOSVersion(current_version)
        if isstring(target_version):
            target_version = PanOSVersion(target_version)

        # Upgrade the patch version
        # eg. 6.0.2 -> 6.0.3
        if (current_version.major == target_version.major
                and current_version.minor == target_version.minor):
            return True

        # Upgrade the minor version
        # eg. 6.0.2 -> 6.1.0
        if (current_version.major == target_version.major
                and current_version.minor == 0 and target_version.minor == 1
                and target_version.patch == 0):
            return True

        # Upgrade the major version
        # eg. 6.1.2 -> 7.0.0
        if (current_version.major + 1 == target_version.major
                and current_version.minor == 1 and target_version.minor == 0
                and target_version.patch == 0):
            return True

        # Upgrading a firewall from PAN-OS 5.0.x to 6.0.x
        # This is a special case because there is no PAN-OS 5.1.x
        from panos.firewall import Firewall

        if (current_version.major == 5 and current_version.minor == 0
                and target_version == "6.0.0"
                and issubclass(type(self.pandevice), Firewall)):
            return True

        return False
Exemple #8
0
 def download_install_reboot(self, version, load_config=None, sync=False):
     if isstring(version):
         version = PanOSVersion(version)
     self.download_install(version, load_config, sync=True)
     # Reboot the device
     self._logger.info(
         "Device %s is rebooting after upgrading to version  %s. This will take a while."
         % (self.pandevice.id, version))
     self.pandevice.restart()
     if sync:
         new_version = self.pandevice.syncreboot()
         if version != new_version:
             raise err.PanDeviceError(
                 "Attempt to upgrade to version %s failed."
                 "Device %s is on version %s after reboot." %
                 (version, self.pandevice.id, new_version))
         self.pandevice.version = new_version
         return new_version
     else:
         return None
Exemple #9
0
 def _next_patch_version(self, version):
     if isstring(version):
         version = PanOSVersion(version)
     next_version = PanOSVersion(
         str(version.major) + str(version.minor) + str(version.patch + 1))
     return next_version
Exemple #10
0
    def upgrade_to_version(self, target_version, dryrun=False):
        """Upgrade to the target version, completely all intermediate upgrades

        For example, if firewall is running version 6.0.5 and target version is 7.0.2,
        then this method will proceed through the following steps:

         - Upgrade to 6.1.0 and reboot
         - Upgrade to 7.0.0 and reboot
         - Upgrade to 7.0.1 and reboot

         This method does not support HA pairs.
         """
        # Get list of software if needed
        if not self.versions:
            self.check()

        # For a dry run, need to record the starting version
        starting_version = self.pandevice.version

        # Get versions as StrictVersion objects
        available_versions = map(PanOSVersion, self.versions.keys())
        current_version = PanOSVersion(self.pandevice.version)
        latest_version = max(available_versions)
        next_minor_version = self._next_minor_version(current_version)

        # Check that this is an upgrade, not a downgrade
        if current_version > target_version:
            raise err.PanDeviceError(
                "Device %s upgrade failed: Can't upgrade from %s to %s." %
                (self.pandevice.id, self.pandevice.version, target_version))

        # Determine the next version to upgrade to
        if target_version == "latest":
            next_version = min(latest_version, next_minor_version)
        elif latest_version < target_version:
            next_version = next_minor_version
        elif not self._direct_upgrade_possible(current_version,
                                               target_version):
            next_version = next_minor_version
        else:
            next_version = PanOSVersion(str(target_version))

        if next_version not in available_versions and not dryrun:
            self._logger.info(
                "Device %s upgrading to %s, currently on %s. Checking for newer versions."
                % (self.pandevice.id, target_version, self.pandevice.version))
            self.check()
            available_versions = map(PanOSVersion, self.versions.keys())
            latest_version = max(available_versions)

        # Check if done upgrading
        if current_version == target_version:
            self._logger.info("Device %s is running target version: %s" %
                              (self.pandevice.id, target_version))
            return True
        elif target_version == "latest" and current_version == latest_version:
            self._logger.info("Device %s is running latest version: %s" %
                              (self.pandevice.id, latest_version))
            if dryrun:
                self._logger.info(
                    "NOTE: dryrun with 'latest' does not show all upgrades,")
                self._logger.info(
                    "as new versions are learned through the upgrade process,")
                self._logger.info(
                    "so results may be different than dryrun output when using 'latest'."
                )
            return True

        # Ensure the content pack is upgraded to the latest
        self.pandevice.content.download_and_install_latest(sync=True)

        # Upgrade to the next version
        self._logger.info("Device %s will be upgraded to version: %s" %
                          (self.pandevice.id, next_version))
        if dryrun:
            self.pandevice.version = str(next_version)
        else:
            self.download_install_reboot(next_version, sync=True)
            self.check()
        result = self.upgrade_to_version(target_version, dryrun=dryrun)
        if result and dryrun:
            self.pandevice.version = starting_version
        return result
Exemple #11
0
def main():
    helper = get_connection(with_classic_provider_spec=True,
                            argument_spec=dict(version=dict(type='str',
                                                            required=True),
                                               sync_to_peer=dict(
                                                   type='bool', default=False),
                                               download=dict(type='bool',
                                                             default=True),
                                               install=dict(type='bool',
                                                            default=True),
                                               restart=dict(type='bool',
                                                            default=False),
                                               timeout=dict(type='int',
                                                            default=1200)))

    module = AnsibleModule(argument_spec=helper.argument_spec,
                           required_one_of=helper.required_one_of,
                           supports_check_mode=True)

    # Verify libs are present, get parent object.
    device = helper.get_pandevice_parent(module)

    # Module params.
    version = module.params['version']
    sync_to_peer = module.params['sync_to_peer']
    download = module.params['download']
    install = module.params['install']
    restart = module.params['restart']
    timeout = module.params['timeout']

    changed = False

    try:
        device.timeout = timeout
        device.software.check()

        if PanOSVersion(version) != PanOSVersion(device.version):

            changed = True

            if not module.check_mode:
                if download:
                    cmd_string = 'request system software info'

                    try:
                        response = device.op(cmd=cmd_string)
                        downloaded = response.findtext(
                            './result/sw-updates/versions/entry/version[.="{0}"]/../'
                            'downloaded'.format(version))

                        if downloaded != 'yes':
                            device.software.download(version,
                                                     sync_to_peer=sync_to_peer,
                                                     sync=True)
                        else:
                            changed = False

                    except PanDeviceError as e:
                        module.fail_json(
                            msg='Failed "{0}": {1}'.format(cmd_string, e))

                if install:
                    device.software.install(version, sync=True)

                if restart:
                    device.restart()

    except PanDeviceError as e:
        module.fail_json(msg=e.message)

    module.exit_json(changed=changed, version=version)
def main():
    helper = get_connection(
        with_classic_provider_spec=True,
        argument_spec=dict(
            version=dict(type="str", required=True),
            sync_to_peer=dict(type="bool", default=False),
            download=dict(type="bool", default=True),
            install=dict(type="bool", default=True),
            restart=dict(type="bool", default=False),
            timeout=dict(type="int", default=1200),
        ),
    )

    module = AnsibleModule(
        argument_spec=helper.argument_spec,
        required_one_of=helper.required_one_of,
        supports_check_mode=True,
    )

    # Verify libs are present, get parent object.
    device = helper.get_pandevice_parent(module)

    # Module params.
    target = PanOSVersion(module.params["version"])
    sync_to_peer = module.params["sync_to_peer"]
    download = module.params["download"]
    install = module.params["install"]
    restart = module.params["restart"]
    timeout = module.params["timeout"]

    current = PanOSVersion(device.version)

    changed = False

    try:
        device.timeout = timeout
        device.software.check()

        if target != current:

            if not is_valid_upgrade(current, target):
                module.fail_json(
                    msg="Upgrade is invalid: {0} -> {1}".format(current, target)
                )

            # Download new base version if needed.
            if download and (
                (current.major != target.major) or (current.minor != target.minor)
            ):
                base = PanOSVersion("{0}.{1}.0".format(target.major, target.minor))

                if needs_download(device, base) and not module.check_mode:
                    device.software.download(base, sync_to_peer, sync=True)
                    changed = True

            if download:
                if needs_download(device, target) and not module.check_mode:
                    device.software.download(
                        target, sync_to_peer=sync_to_peer, sync=True
                    )
                    changed = True

            if install:
                if not module.check_mode:
                    device.software.install(target, sync=True)
                changed = True

            if restart:
                if not module.check_mode:
                    device.restart()
                changed = True

    except PanDeviceError as e:
        module.fail_json(msg=e.message)

    module.exit_json(changed=changed, version=str(target))
Exemple #13
0
def test_eq(panos1, panos2, expected):
    x = PanOSVersion(panos1)
    y = PanOSVersion(panos2)
    assert (x == y) == expected
Exemple #14
0
def test_gt(panos1, panos2, expected):
    x = PanOSVersion(panos1)
    y = PanOSVersion(panos2)
    assert (x > y) == expected