Skip to content

Commit

Permalink
chore: Add support for replacing packages
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftydinar authored Dec 22, 2024
1 parent 7d4aff9 commit a4e9ba4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions modules/dnf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Specific COPR repositories can also be specified in `copr: user/project` format

If you use a repo that requires adding custom keys (eg. Brave Browser), you can import the keys by declaring the key URLs under `keys:`. The magic string acts the same as it does in `repos`.

Then the module installs the packages declared under `install:` using `dnf install`, it removes the packages declared under `remove:` using `dnf remove`. If there are packages declared under both `install:` and `remove:` then removal is performed 1st & install 2nd.
Then the module installs the packages declared under `install:` using `dnf -y install`, it removes the packages declared under `remove:` using `dnf -y remove`. If there are packages declared under both `install:` and `remove:` then removal is performed 1st & install 2nd.

Installing RPM packages directly from a `http(s)` url that points to the RPM file is also supported, you can just put the URLs under `install:` and they'll be installed along with the other packages. The magic string `%OS_VERSION%` is substituted with the current VERSION_ID (major Fedora version) like with the `repos:` property.

Expand All @@ -23,7 +23,7 @@ If an RPM is not available in a repository or as an URL, you can also install it
install:
- weird-package.rpm # tries to install files/dnf/weird-package.rpm
```
The module can also replace base RPM packages with packages from COPR repo. Under `replace:`, the module finds every pair of keys `- from-repo:` and `packages:`. (Multiple pairs are supported.) The module downloads the COPR repository file declared by `- from-repo:` into `/etc/yum.repos.d/`, and from that repository replaces packages declared under `packages:` using the command `dnf replace`. The COPR repository file is then deleted. The magic string `%OS_VERSION%` is substituted with the current VERSION_ID (major Fedora version) as already said above. At the moment, only COPR repo is supported.
The module can also replace base RPM packages with packages from any repo. Under `replace:`, the module finds every pair of keys `- from-repo:` and `packages:`. (Multiple pairs are supported.) The module uses `- from-repo:` key to gather the repo for package replacement, then it replaces packages declared under `packages:` using the command `dnf -y distro-sync --refresh --repo "${repo}" "${packages}"`. The magic string `%OS_VERSION%` is substituted with the current VERSION_ID (major Fedora version) as already said above. You need to assure that you provided the repo in `repos:` before using replacement functionality. To gather the repo ID that you need to input, you can use `dnf repo list` command.

:::note
[Removed packages are still present in the underlying ostree repository](https://coreos.github.io/rpm-ostree/administrator-handbook/#removing-a-base-package), what `remove` does is kind of like hiding them from the system, it doesn't free up storage space.
Expand Down
25 changes: 7 additions & 18 deletions modules/dnf/dnf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ if [[ ${#REPOS[@]} -gt 0 ]]; then
done
fi

# Install RPM keys if they are provided
get_json_array KEYS 'try .["keys"][]' "${1}"
if [[ ${#KEYS[@]} -gt 0 ]]; then
echo "Adding keys"
Expand Down Expand Up @@ -141,46 +142,34 @@ fi

get_json_array REPLACE 'try .["replace"][]' "$1"

# Override-replace RPM packages
# Replace RPM packages from any repository
if [[ ${#REPLACE[@]} -gt 0 ]]; then
for REPLACEMENT in "${REPLACE[@]}"; do

# Get repository
REPO=$(echo "${REPLACEMENT}" | jq -r 'try .["from-repo"]')
REPO="${REPO//%OS_VERSION%/${OS_VERSION}}"
REPO="${REPO//[$'\t\r\n ']}"

# Ensure repository is provided
if [[ "${REPO}" == "null" ]]; then
echo "Error: Key 'from-repo' was declared, but repository URL was not provided."
exit 1
fi

# Get info from repository URL
MAINTAINER=$(awk -F'/' '{print $5}' <<< "${REPO}")
REPO_NAME=$(awk -F'/' '{print $6}' <<< "${REPO}")
FILE_NAME=$(awk -F'/' '{print $9}' <<< "${REPO}" | sed 's/\?.*//') # Remove params after '?'

# Get packages to replace
get_json_array PACKAGES 'try .["packages"][]' "${REPLACEMENT}"
REPLACE_STR="$(echo "${PACKAGES[*]}" | tr -d '\n')"

# Ensure packages are provided
if [[ ${#PACKAGES[@]} == 0 ]]; then
echo "Error: No packages were provided for repository '${REPO_NAME}'."
echo "Error: No packages were provided for repository '${REPO}'."
exit 1
fi

echo "Replacing packages from COPR repository: '${REPO_NAME}' owned by '${MAINTAINER}'"
echo "Replacing packages from repository: '${REPO}'"
echo "Replacing: ${REPLACE_STR}"

REPO_URL="${REPO//[$'\t\r\n ']}"

echo "Downloading repo file ${REPO_URL}"
curl -fLs --create-dirs -O "${REPO_URL}" --output-dir "/etc/yum.repos.d/"
echo "Downloaded repo file ${REPO_URL}"

rpm-ostree override replace --experimental --from "repo=copr:copr.fedorainfracloud.org:${MAINTAINER}:${REPO_NAME}" ${REPLACE_STR}
rm "/etc/yum.repos.d/${FILE_NAME}"
dnf -y distro-sync --refresh --repo "${REPO}" "${PACKEGES[@]}"

done
fi
fi
2 changes: 1 addition & 1 deletion modules/dnf/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ example: |
- firefox
- firefox-langpacks
replace:
- from-repo: https://copr.fedorainfracloud.org/coprs/trixieua/mutter-patched/repo/fedora-%OS_VERSION%/trixieua-mutter-patched-fedora-%OS_VERSION%.repo
- from-repo: copr:copr.fedorainfracloud.org:trixieua:mutter-patched
packages:
- mutter
- mutter-common
Expand Down

0 comments on commit a4e9ba4

Please sign in to comment.