Skip to content

Commit

Permalink
Add fetch-repo-package-list timer and service
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Oliver <[email protected]>
  • Loading branch information
roliver-rpi authored and tdewey-rpi committed Aug 1, 2024
1 parent 84d1e21 commit a94c61b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fetch-repo-package-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# fetch-repo-package-list

## Usage
### Oneshot
To fetch the latest Raspberry Pi OS package list:
```
systemctl start fetch-repo-package-list@$(systemd-escape https://archive.raspberrypi.org/debian).service
```

The service makes use of systemd's CacheDirectory, and RuntimeDirectory during
execution. The updated package list is outputted to the RuntimeDirectory,
which would typically as follows for the above example:
```
/var/run/fetch-repo-package-list@https\:--archive.raspberrypi.org-debian.service/Packages
```

### Regularly via timer
To fetch package lists every day at a random, but consistent, time between midnight and 6a.m. (local time):
```
systemctl enable --now fetch-repo-package-list@$(systemd-escape https://archive.raspberrypi.org/debian).timer
```
40 changes: 40 additions & 0 deletions fetch-repo-package-list/fetch-repo-package-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

set -e

REPOSITORY="$1"
REPOSITORY="${REPOSITORY:-https://archive.raspberrypi.org/debian}"
RELEASE="${RELEASE:-bookworm}"
ARCH="${ARCH:-arm64}"

# Should be set by systemd
SERVICE_NAME="fetch-repo-package-list@https:--archive.raspberrypi.org-debian.service"
CACHE_DIRECTORY="${CACHE_DIRECTORY:=/var/cache/${SERVICE_NAME}}"
RUNTUME_DIRECTORY="${RUNTIME_DIRECTORY:=/run/${SERVICE_NAME}}"

PGZ="Packages.gz"
PACKAGES_GZ_FILE="${CACHE_DIRECTORY}/${PGZ}"
PACKAGES_GZ_ETAG_FILE="${CACHE_DIRECTORY}/${PGZ}.etag"
PACKAGES_GZ_URL="${REPOSITORY}/dists/${RELEASE}/main/binary-${ARCH}/${PGZ}"

>&2 echo "Downloading latest package information for ${RELEASE}:${ARCH} from ${REPOSITORY}"
curl \
--etag-compare ${PACKAGES_GZ_ETAG_FILE} \
--etag-save ${PACKAGES_GZ_ETAG_FILE} \
${PACKAGES_GZ_URL} \
-o ${PACKAGES_GZ_FILE} \
2> /dev/null

PACKAGES_GZ_ETAG="$(< ${PACKAGES_GZ_ETAG_FILE})"
PACKAGES_GZ_ETAG="${PACKAGES_GZ_ETAG%\"}"
PACKAGES_GZ_ETAG="${PACKAGES_GZ_ETAG#\"}"

# Extract the downloaded file, use the etag as a suffix to create a unique file
PACKAGES_FILE="${CACHE_DIRECTORY}/Packages_${PACKAGES_GZ_ETAG}"
if [ ! -f ${PACKAGES_FILE} ]
then
gunzip --to-stdout ${PACKAGES_GZ_FILE} > ${PACKAGES_FILE}
fi

>&2 echo "Copying Packages to runtime directory"
cp --update "${PACKAGES_FILE}" "${RUNTIME_DIRECTORY}/Packages"
15 changes: 15 additions & 0 deletions fetch-repo-package-list/fetch-repo-package-list.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Unit]
Description=Downloads latest packages from repo
Wants=fetch-repo-package-list@%i.timer

[Service]
Type=oneshot
EnvironmentFile=/etc/rpi-sb-provisioner/config
ExecStart=/usr/local/bin/fetch-repo-package-list "%I"
#ConfigurationDirectory=rpi-sb-provisioner
CacheDirectory=%n
RuntimeDirectory=%n
RuntimeDirectoryPreserve=true

[Install]
WantedBy=multi-user.target
11 changes: 11 additions & 0 deletions fetch-repo-package-list/fetch-repo-package-list.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Regularly run the package downloader

[Timer]
OnCalendar=daily
RandomizedDelaySec=6hours
FixedRandomDelay=true
Persistent=true

[Install]
WantedBy=timers.target
12 changes: 12 additions & 0 deletions nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ depends:
- rpi-eeprom
- rpiboot
- coreutils
- curl
- bash
- gzip

# Recommended packages. (overridable)
# This will expand any env var you set in the field, e.g. ${RECOMMENDS_BLA}
Expand Down Expand Up @@ -207,6 +210,15 @@ contents:
- src: key-writer/keywriter.sh
dst: /usr/local/bin/keywriter.sh

- src: fetch-repo-package-list/fetch-repo-package-list.timer
dst: /usr/local/lib/systemd/system/[email protected]

- src: fetch-repo-package-list/fetch-repo-package-list.service
dst: /usr/local/lib/systemd/system/[email protected]

- src: fetch-repo-package-list/fetch-repo-package-list
dst: /usr/local/bin/fetch-repo-package-list

# This will add all files in some/directory or in subdirectories at the
# same level under the directory /etc. This means the tree structure in
# some/directory will not be replicated.
Expand Down

0 comments on commit a94c61b

Please sign in to comment.