|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +role_collection_dir="${ROLE_COLLECTION_DIR:-fedora/linux_system_roles}" |
| 6 | +ostree_dir="${OSTREE_DIR:-"$(dirname "$(realpath "$0")")"}" |
| 7 | + |
| 8 | +if [ -z "${4:-}" ] || [ "${1:-}" = help ] || [ "${1:-}" = -h ]; then |
| 9 | + cat <<EOF |
| 10 | +Usage: $0 packages [runtime|testing] DISTRO-MAJOR[.MINOR] [json|yaml|raw|toml] |
| 11 | +The script will use the packages and roles files in $ostree_dir to |
| 12 | +construct the list of packages needed to build the ostree image. The script |
| 13 | +will output the list of packages in the given format |
| 14 | +- json is a JSON list like ["pkg1","pkg2",....,"pkgN"] |
| 15 | +- yaml is the YAML list format |
| 16 | +- raw is the list of packages, one per line |
| 17 | +- toml is a list of [[packages]] elements as in https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/composing_installing_and_managing_rhel_for_edge_images/index#creating-an-image-builder-blueprint-for-a-rhel-for-edge-image-using-the-command-line-interface_composing-a-rhel-for-edge-image-using-image-builder-command-line |
| 18 | +The DISTRO-MAJOR.MINOR is the same format used by Ansible for distribution e.g. CentOS-8, RedHat-8.9, etc. |
| 19 | +EOF |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | +category="$1" |
| 23 | +pkgtype="$2" |
| 24 | +distro_ver="$3" |
| 25 | +format="$4" |
| 26 | +pkgtypes=("$pkgtype") |
| 27 | +if [ "$pkgtype" = testing ]; then |
| 28 | + pkgtypes+=(runtime) |
| 29 | +fi |
| 30 | + |
| 31 | +get_rolepath() { |
| 32 | + local ostree_dir role rolesdir roles_parent_dir |
| 33 | + ostree_dir="$1" |
| 34 | + role="$2" |
| 35 | + roles_parent_dir="$(dirname "$(dirname "$ostree_dir")")" |
| 36 | + rolesdir="$roles_parent_dir/$role/.ostree" |
| 37 | + # assumes collection format |
| 38 | + if [ -d "$rolesdir" ]; then |
| 39 | + echo "$rolesdir" |
| 40 | + return 0 |
| 41 | + fi |
| 42 | + # assumes legacy role format like linux-system-roles.$role/ |
| 43 | + for rolesdir in "$roles_parent_dir"/*-system-roles."$role"/.ostree; do |
| 44 | + if [ -d "$rolesdir" ]; then |
| 45 | + echo "$rolesdir" |
| 46 | + return 0 |
| 47 | + fi |
| 48 | + done |
| 49 | + # look elsewhere |
| 50 | + if [ -n "${ANSIBLE_COLLECTIONS_PATHS:-}" ]; then |
| 51 | + for pth in ${ANSIBLE_COLLECTIONS_PATHS//:/ }; do |
| 52 | + rolesdir="$pth/ansible_collections/$role_collection_dir/roles/$role/.ostree" |
| 53 | + if [ -d "$rolesdir" ]; then |
| 54 | + echo "$rolesdir" |
| 55 | + return 0 |
| 56 | + fi |
| 57 | + done |
| 58 | + fi |
| 59 | + return 1 |
| 60 | +} |
| 61 | + |
| 62 | +get_packages() { |
| 63 | + local ostree_dir pkgtype pkgfile rolefile |
| 64 | + ostree_dir="$1" |
| 65 | + for pkgtype in "${pkgtypes[@]}"; do |
| 66 | + for suff in "" "-$distro" "-${distro}-${major_ver}" "-${distro}-${ver}"; do |
| 67 | + pkgfile="$ostree_dir/packages-${pkgtype}${suff}.txt" |
| 68 | + if [ -f "$pkgfile" ]; then |
| 69 | + cat "$pkgfile" |
| 70 | + fi |
| 71 | + done |
| 72 | + rolefile="$ostree_dir/roles-${pkgtype}.txt" |
| 73 | + if [ -f "$rolefile" ]; then |
| 74 | + local roles role rolepath |
| 75 | + roles="$(cat "$rolefile")" |
| 76 | + for role in $roles; do |
| 77 | + rolepath="$(get_rolepath "$ostree_dir" "$role")" |
| 78 | + get_packages "$rolepath" |
| 79 | + done |
| 80 | + fi |
| 81 | + done | sort -u |
| 82 | +} |
| 83 | + |
| 84 | +format_packages_json() { |
| 85 | + local comma pkgs pkg |
| 86 | + comma="" |
| 87 | + pkgs="[" |
| 88 | + while read -r pkg; do |
| 89 | + pkgs="${pkgs}${comma}\"${pkg}\"" |
| 90 | + comma=, |
| 91 | + done |
| 92 | + pkgs="${pkgs}]" |
| 93 | + echo "$pkgs" |
| 94 | +} |
| 95 | + |
| 96 | +format_packages_raw() { |
| 97 | + cat |
| 98 | +} |
| 99 | + |
| 100 | +format_packages_yaml() { |
| 101 | + while read -r pkg; do |
| 102 | + echo "- $pkg" |
| 103 | + done |
| 104 | +} |
| 105 | + |
| 106 | +format_packages_toml() { |
| 107 | + while read -r pkg; do |
| 108 | + echo "[[packages]]" |
| 109 | + echo "name = \"$pkg\"" |
| 110 | + echo "version = \"*\"" |
| 111 | + done |
| 112 | +} |
| 113 | + |
| 114 | +distro="${distro_ver%%-*}" |
| 115 | +ver="${distro_ver##*-}" |
| 116 | +if [[ "$ver" =~ ^([0-9]*) ]]; then |
| 117 | + major_ver="${BASH_REMATCH[1]}" |
| 118 | +else |
| 119 | + echo ERROR: cannot parse major version number from version "$ver" |
| 120 | + exit 1 |
| 121 | +fi |
| 122 | + |
| 123 | +"get_$category" "$ostree_dir" | "format_${category}_$format" |
0 commit comments