Skip to content

Commit

Permalink
chore: Add support for native installation of repos
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftydinar authored Dec 22, 2024
1 parent fd4ad28 commit cd91b05
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
3 changes: 3 additions & 0 deletions modules/dnf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ You can also add repository files directly into your git repository if URLs are
repos:
- my-repository.repo # copies in .repo file from files/dnf/my-repository.repo to /etc/yum.repos.d/
```
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:` a hybrid command `dnf remove <packages> --install <packages>` is used, which should allow you to switch required packages for other ones.
Expand Down
55 changes: 33 additions & 22 deletions modules/dnf/dnf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,42 @@
# Tell build process to exit if there are any errors.
set -euo pipefail

# Fail the build if dnf5 isn't installed
if ! rpm -q dnf5 &>/dev/null; then
echo "ERROR: Main dependency 'dnf5' is not installed. Install 'dnf5' before using this module to solve this error."
exit 1
fi

# Fail the build if dnf5 plugins aren't installed
if ! rpm -q dnf5-plugins &>/dev/null; then
echo "ERROR: Dependency 'dnf5-plugins' is not installed. It is needed for cleanly adding COPR repositories.
echo " Install 'dnf5-plugins' before using this module to solve this error."
exit 1
fi
# Pull in repos
get_json_array REPOS 'try .["repos"][]' "$1"
if [[ ${#REPOS[@]} -gt 0 ]]; then
echo "Adding repositories"
for REPO in "${REPOS[@]}"; do
REPO="${REPO//%OS_VERSION%/${OS_VERSION}}"
REPO="${REPO//[$'\t\r\n ']}"

# If it's the COPR repo, then download the repo normally
# If it's not, then download the repo with URL in it's filename, to avoid duplicate repo name issue
if [[ "${REPO}" =~ ^https?:\/\/.* ]] && [[ "${REPO}" == "https://copr.fedorainfracloud.org/coprs/"* ]]; then
echo "Downloading repo file ${REPO}"
curl -fLs --create-dirs -O "${REPO}" --output-dir "/etc/yum.repos.d/"
echo "Downloaded repo file ${REPO}"
elif [[ "${REPO}" =~ ^https?:\/\/.* ]] && [[ "${REPO}" != "https://copr.fedorainfracloud.org/coprs/"* ]]; then
CLEAN_REPO_NAME=$(echo "${REPO}" | sed -E 's|^https?://([^?]+)(\?.*)?$|\1|')
CLEAN_REPO_NAME="${CLEAN_REPO_NAME//\//.}"

echo "Downloading repo file ${REPO}"
curl -fLs --create-dirs "${REPO}" -o "/etc/yum.repos.d/${CLEAN_REPO_NAME}"
echo "Downloaded repo file ${REPO}"
elif [[ ! "${REPO}" =~ ^https?:\/\/.* ]] && [[ "${REPO}" == *".repo" ]] && [[ -f "${CONFIG_DIRECTORY}/rpm-ostree/${REPO}" ]]; then
cp "${CONFIG_DIRECTORY}/rpm-ostree/${REPO}" "/etc/yum.repos.d/${REPO##*/}"
fi
done
echo "Adding repositories"
# Substitute %OS_VERSION% & remove newlines/whitespaces from all repo entries
for i in "${!REPOS[@]}"; do
repo="${REPOS[$i]}"
repo="${repo//%OS_VERSION%/${OS_VERSION}}"
REPOS[$i]="${repo//[$'\t\r\n ']}"
done
# dnf config-manager & dnf copr don't support adding multiple repositories at once, hence why for/done loop is used
for repo in "${REPOS[@]}"; do
if [[ "${repo}" =~ ^https?:\/\/.* ]]; then
echo "Adding repository URL: '${repo}'"
dnf config-manager addrepo --from-repofile="${repo}"
elif [[ "${repo}" == *".repo" ]] && [[ -f "${CONFIG_DIRECTORY}/dnf/${repo}" ]]; then
echo "Adding repository file: '${repo}'"
dnf config-manager addrepo --from-repofile="${repo}"
elif [[ "${repo}" == "copr: "* ]]; then
echo "Adding COPR repository: '${repo#copr: }'"
dnf copr enable "${repo#copr: }"
fi
done
fi
get_json_array KEYS 'try .["keys"][]' "$1"
Expand Down
2 changes: 1 addition & 1 deletion modules/dnf/module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ shortdesc: The dnf module offers pseudo-declarative package and repository manag
example: |
type: dnf
repos:
- https://copr.fedorainfracloud.org/coprs/atim/starship/repo/fedora-%OS_VERSION%/atim-starship-fedora-%OS_VERSION%.repo # when including COPR repos, use the %OS_VERSION% magic string
- copr: atim/starship
- https://brave-browser-rpm-release.s3.brave.com/brave-browser.repo
keys:
- https://brave-browser-rpm-release.s3.brave.com/brave-core.asc
Expand Down

0 comments on commit cd91b05

Please sign in to comment.