Skip to content

Commit

Permalink
do not run apt update or simulate apt dist-upgrade
Browse files Browse the repository at this point in the history
This is causing all sorts of problems. The first of which is that
we're hitting our poor mirrors every time the script is ran, which, in
the Debian package configuration, is *every 15 minutes* (!!).

The second is that this locks the cache and makes this script
needlessly stumble upon a possible regression in APT from Debian
bookworm and Ubuntu 22.06:

https://bugs.launchpad.net/ubuntu/+source/apt/+bug/2003851

That still has to be confirmed: it's possible that `apt update` can
hang for a long time, but that shouldn't concern us if we delegate
this work out of band.

I also do not believe actually performing the `dist-upgrade`
calculations is necessary to compute the pending upgrades at all. I've
done work with python-apt for other projects and haven't found that to
be required: the cache has the necessary information about pending
upgrades.

Closes: prometheus-community#179

Signed-off-by: Antoine Beaupré <[email protected]>
  • Loading branch information
anarcat committed Oct 14, 2023
1 parent 34dd42e commit fddbd16
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions apt_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
# Description: Expose metrics from apt. This is inspired by and
# intended to be a replacement for the original apt.sh.
#
# This script does *not* update the apt cache, deliberately. You need
# something else to run `apt update` regularly for the metrics to be
# up to date. This can be done un numerous ways, but the canonical way
# is to use the normal `APT::Periodic::Update-Package-Lists`
# setting.
#
# This, for example, will enable a nightly job that runs `apt update`:
#
# echo 'APT::Periodic::Update-Package-Lists "1";' > /etc/apt/apt.conf.d/99_auto_apt_update.conf
#
# See /usr/lib/apt/apt.systemd.daily for details.
#
# Dependencies: python3-apt, python3-prometheus-client
#
# Authors: Kyle Fazzari <[email protected]>
# Daniel Swarbrick <[email protected]>

import apt
import collections
import contextlib
import os
from prometheus_client import CollectorRegistry, Gauge, generate_latest

Expand Down Expand Up @@ -83,15 +94,6 @@ def _write_reboot_required(registry):
def _main():
cache = apt.cache.Cache()

# First of all, attempt to update the index. If we don't have permission
# to do so (or it fails for some reason), it's not the end of the world,
# we'll operate on the old index.
with contextlib.suppress(apt.cache.LockFailedException, apt.cache.FetchFailedException):
cache.update()

cache.open()
cache.upgrade(True)

registry = CollectorRegistry()
_write_pending_upgrades(registry, cache)
_write_held_upgrades(registry, cache)
Expand Down

0 comments on commit fddbd16

Please sign in to comment.