Skip to content

Commit

Permalink
fix and simplify install-rpms
Browse files Browse the repository at this point in the history
The install-rpms accepted two non-optional "options" that ended up being
used the same way. There was some logic in that bash script that checked
and processed those options, and as it's typical of bash code, it had a
bug, where if the script was supplied with only one of the two
"options", the value of non-existent $extra_rpms_file, because it was
enclosed in double quotes, would be treated as an actual object (an
empty string), thus making the following code:
```
for file in "$common_rpms_file" "$extra_rpms_file"; do
(...)
mapfile -t tmp_array < <(awk '!/^#/' "$file")
```
iterate over an empty "$file" blocking awk.

Because the only thing that this script did was to install RPMs
specified by text files, this patch changes it to do exactly that
without any error-prone bash logic.
  • Loading branch information
mkisielewski-arista committed Oct 16, 2024
1 parent 786fab2 commit d7857bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 67 deletions.
12 changes: 6 additions & 6 deletions barney.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ images:
build: |
echo "install rpms"
bash /bootstrap/install-rpms/install-rpms.bash \
--common-rpms-file /bootstrap/install-rpms/rpms-common \
--extra-rpms-file /bootstrap/install-rpms/rpms-build
/bootstrap/install-rpms/rpms-common \
/bootstrap/install-rpms/rpms-build
base-image-devel:
units:
- floor: .%internal/bootstrap-devel
sources: []
build: |
bash /bootstrap/install-rpms/install-rpms.bash \
--common-rpms-file /bootstrap/install-rpms/rpms-common \
--extra-rpms-file /bootstrap/install-rpms/rpms-devel
/bootstrap/install-rpms/rpms-common \
/bootstrap/install-rpms/rpms-devel
go-buildfloor:
description: |
Expand Down Expand Up @@ -309,8 +309,8 @@ images:
sources: []
build: |
bash /bootstrap/install-rpms/install-rpms.bash \
--common-rpms-file /bootstrap/install-rpms/rpms-common \
--extra-rpms-file /bootstrap/install-rpms/rpms-devel
/bootstrap/install-rpms/rpms-common \
/bootstrap/install-rpms/rpms-devel
touch $DESTDIR/etc/resolv.conf
entry:
Expand Down
3 changes: 1 addition & 2 deletions bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ The developer just uses these repos to further install further packages in his b
### Base Image Contents
Once the bootstrap image and repo configuration is ready, we run the `install-rpms/install-rpms.bash` script to build the base image.
The set of rpms to be installed is specified in the text files: `install-rpms/rpms-common`, `install-rpms/rpms-build` and `install-rpms/rpms-devel`.
The `install-rpms` script takes two arguments, `--common-rpms-file rpms-common` and `--extra-rpms-file (rpms-build | rpms-devel)`.

The `install-rpms` script accepts one or more paths as its positional arguments, the files supplied this way should provide a list of packages to be installed.
67 changes: 8 additions & 59 deletions bootstrap/install-rpms/install-rpms.bash
Original file line number Diff line number Diff line change
@@ -1,59 +1,8 @@
#!/bin/bash

set -e
set -x

dnf_install() {
dnf --assumeyes --installroot=/dest --noplugins \
--config=/etc/dnf/dnf.conf \
--setopt=cachedir=/var/cache/dnf \
--setopt=reposdir=/etc/yum.repos.d \
--setopt=varsdir=/etc/dnf \
install "$@"
}

usage() {
echo "Usage: $0 --common-rpms-file FILE --extra-rpms-file FILE"
exit 1
}

# Parse command-line options
while [[ "$#" -gt 0 ]]; do
case $1 in
--common-rpms-file)
common_rpms_file="$2"
shift 2
;;
--extra-rpms-file)
extra_rpms_file="$2"
shift 2
;;
*)
usage
;;
esac
done

if [[ -z "$common_rpms_file" && -z "$extra_rpms_file" ]]; then
echo "Error: At least one of the options must be specified."
usage
fi

rpms=()
for file in "$common_rpms_file" "$extra_rpms_file"; do
if [[ -n "$file" && ! -f "$file" ]]; then
echo "Error: File '$file' does not exist."
exit 1
fi

mapfile -t tmp_array < <(awk '!/^#/' "$file")
rpms+=("${tmp_array[@]}")
done

if [[ ${#rpms[@]} -eq 0 ]]; then
echo "Error: No RPMs specified"
exit 1
fi

dnf_install "${rpms[@]}"

#!/usr/bin/env bash

cat "$@" | xargs dnf --assumeyes --installroot=/dest --noplugins \
--config=/etc/dnf/dnf.conf \
--setopt=cachedir=/var/cache/dnf \
--setopt=reposdir=/etc/yum.repos.d \
--setopt=varsdir=/etc/dnf \
install

0 comments on commit d7857bc

Please sign in to comment.