Пример #1
0
    def test_smart_update(self):
        """
        Test that our smart ``apt-get update`` wrapper works.

        Currently this test simply ensures coverage of the happy path.
        Ideally it will evolve to test the handled edge cases as well.
        """
        if os.getuid() != 0:
            return self.skipTest("root privileges required to opt in")
        updater = AptMirrorUpdater()
        # Remove all existing package lists.
        updater.clear_package_lists()
        # Verify that package lists aren't available.
        assert not have_package_lists()
        # Run `apt-get update' to download the package lists.
        updater.smart_update()
        # Verify that package lists are again available.
        assert have_package_lists()
Пример #2
0
    def test_smart_update(self):
        """
        Test that our smart ``apt-get update`` wrapper works.

        Currently this test simply ensures coverage of the happy path.
        Ideally it will evolve to test the handled edge cases as well.
        """
        if os.getuid() != 0:
            return self.skipTest("root privileges required to opt in")
        updater = AptMirrorUpdater()
        # Remove all existing package lists.
        updater.clear_package_lists()
        # Verify that package lists aren't available.
        assert not have_package_lists()
        # Run `apt-get update' to download the package lists.
        updater.smart_update()
        # Verify that package lists are again available.
        assert have_package_lists()
Пример #3
0
def upgrade_remote_system(context, force_reboot=False):
    """
    Perform standard system maintenance tasks on a remote Debian or Ubuntu system.

    :param context: An execution context created by :mod:`executor.contexts`.

    This function performs the following system maintenance tasks:

    1. The ``apt-get update`` command is run (using the Python API
       of the :pypi:`apt-mirror-updater` program).
    2. The ``apt-get dist-upgrade`` command is run [1]_.
    3. The ``apt-get clean`` command is run.
    4. If the file ``/var/run/reboot-required`` exists (indicating that a
       reboot is required due to security updates) the remote system is
       rebooted using the Python API of the ``reboot-remote-system`` program,
       to enable automatic unlocking of remote root disk encryption.
    5. Old kernel packages are removed (using the Python API of the
       ``debuntu-kernel-manager`` program). If more than one meta package is
       installed a warning message is logged but no exception is raised.
    6. The ``apt-get autoremove --purge`` command is run to optionally [1]_
       remove any 'auto-removable' system packages.

    .. [1] Because the ``apt-get`` option ``--yes`` is not used, the operator
           will be asked to confirm using an interactive confirmation prompt.
    """
    # Run 'apt-get update' (with compensation if things break).
    updater = AptMirrorUpdater(context=context)
    updater.smart_update()
    # Run 'apt-get dist-upgrade'.
    logger.info("Upgrading system packages on %s ..", context)
    context.execute('apt-get', 'dist-upgrade', sudo=True, tty=True)
    # Run 'apt-get clean'.
    logger.info("Cleaning up downloaded archives on %s ..", context)
    context.execute('apt-get', 'clean', sudo=True)
    # Use debuntu-kernel-manager to detect when a reboot is required by package
    # upgrades or because the system isn't running on the newest kernel yet.
    kernel_manager = KernelPackageManager(
        apt_options=['--yes'],
        context=context,
    )
    if force_reboot:
        reboot_helper(kernel_manager,
                      "Rebooting %s as requested by operator ..")
    elif kernel_manager.reboot_required:
        reboot_helper(
            kernel_manager,
            "Rebooting %s because this is required by package upgrades ..")
    elif not kernel_manager.running_newest_kernel:
        reboot_helper(
            kernel_manager,
            "Rebooting %s because it's not yet running the newest kernel ..")
    # Cleanup old kernel packages after rebooting, when we're
    # most likely (average case) running on the newest kernel.
    try:
        kernel_manager.cleanup_packages()
    except CleanupError as e:
        # Don't error out when multiple meta packages are installed.
        logger.warning(e)
    # Interactively prompt to remove packages that seem to no longer be needed
    # (but never assume this to be correct: the operator needs to confirm).
    logger.info("Removing 'auto-removable' system packages ..")
    context.execute('apt-get', 'autoremove', '--purge', sudo=True, tty=True)
    logger.info("Done!")