|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +_package_name="containernetworking-plugins" |
| 6 | +_scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 7 | + |
| 8 | +if [ $# -ne 1 ]; then |
| 9 | + echo "Usage: $(basename "$0") <copr-repo-name>" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +COPR_REPO_NAME="$1" |
| 14 | + |
| 15 | +[ -z "${COPR_REPO_NAME}" ] && echo "ERROR: COPR_REPO_NAME is not set" && exit 1 |
| 16 | +echo "COPR_REPO_NAME: '${COPR_REPO_NAME}'" |
| 17 | + |
| 18 | +latest_tag=$(curl -fsSL --retry 3 \ |
| 19 | + -H "Accept: application/vnd.github+json" \ |
| 20 | + -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 21 | + https://api.github.com/repos/containernetworking/plugins/releases/latest | jq -r '.tag_name') |
| 22 | + |
| 23 | +if [ -z "${latest_tag}" ] || [ "${latest_tag}" = "null" ]; then |
| 24 | + echo "ERROR: Failed to retrieve latest tag from containernetworking/plugins" >&2 |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +echo "### containernetworking/plugins latest tag: '${latest_tag}'" |
| 29 | +version="${latest_tag#v}" |
| 30 | + |
| 31 | +echo "### Checking if package ${_package_name} ${version} already exists in the COPR repository" |
| 32 | +cni_pkg="$(copr-cli list-packages "${COPR_REPO_NAME}" | jq -r '.[] | select(.name == "'${_package_name}'")')" |
| 33 | +if [ -n "${cni_pkg}" ]; then |
| 34 | + existing_package_version=$(copr-cli get-package \ |
| 35 | + --name "${_package_name}" \ |
| 36 | + --with-latest-succeeded-build \ |
| 37 | + "${COPR_REPO_NAME}" \ |
| 38 | + | jq -r '.latest_succeeded_build.source_package.version') |
| 39 | + |
| 40 | + if [[ "${existing_package_version}" == "1:${version}-1" ]]; then |
| 41 | + echo "### Package ${_package_name} ${version} already exists in the COPR repository" |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +fi |
| 45 | + |
| 46 | +temp_dir="$(mktemp -d "/tmp/containernetworking-plugins-${version}.XXXXXX")" |
| 47 | +cp "${_scriptdir}/containernetworking-plugins.spec" "${temp_dir}/" |
| 48 | + |
| 49 | +pushd "${temp_dir}" >/dev/null |
| 50 | + |
| 51 | +echo "### Downloading the CNI plugins x86_64 and aarch64 releases for ${version}" |
| 52 | +curl -L -o amd64.tgz "https://github.com/containernetworking/plugins/releases/download/v${version}/cni-plugins-linux-amd64-v${version}.tgz" |
| 53 | +curl -L -o arm64.tgz "https://github.com/containernetworking/plugins/releases/download/v${version}/cni-plugins-linux-arm64-v${version}.tgz" |
| 54 | + |
| 55 | +mkdir -p "containernetworking-plugins-${version}"/{x86_64,aarch64} |
| 56 | + |
| 57 | +tar xf amd64.tgz -C "containernetworking-plugins-${version}/x86_64" |
| 58 | +tar xf arm64.tgz -C "containernetworking-plugins-${version}/aarch64" |
| 59 | +cp \ |
| 60 | + "containernetworking-plugins-${version}/x86_64/LICENSE" \ |
| 61 | + "containernetworking-plugins-${version}/x86_64/README.md" \ |
| 62 | + "containernetworking-plugins-${version}/" |
| 63 | + |
| 64 | +tar czf "containernetworking-plugins-${version}.tar.gz" -C "containernetworking-plugins-${version}" . |
| 65 | + |
| 66 | +mkdir -p buildroot/{RPMS,SRPMS,SOURCES,SPECS,BUILD} |
| 67 | +mv "containernetworking-plugins-${version}.tar.gz" buildroot/SOURCES/ |
| 68 | + |
| 69 | +cat > buildroot/SPECS/containernetworking-plugins.spec <<EOF |
| 70 | +%global ver ${version} |
| 71 | +
|
| 72 | +EOF |
| 73 | +cat containernetworking-plugins.spec >> buildroot/SPECS/containernetworking-plugins.spec |
| 74 | + |
| 75 | +echo "### Building the SRPM" |
| 76 | +rpmbuild -bs --define "_topdir ./buildroot" ./buildroot/SPECS/containernetworking-plugins.spec |
| 77 | + |
| 78 | +echo "### Pushing the SRPM to COPR (${COPR_REPO_NAME}) and waiting for the build" |
| 79 | +# Just epel-10 chroots because of the obsolesence of the original package in the CentOS Stream 10. |
| 80 | +if copr-cli build "${COPR_REPO_NAME}" \ |
| 81 | + --chroot epel-10-aarch64 --chroot epel-10-x86_64 \ |
| 82 | + "${temp_dir}/buildroot/SRPMS/containernetworking-plugins-${version}-1.src.rpm"; then |
| 83 | + copr-cli regenerate-repos "${COPR_REPO_NAME}" |
| 84 | +else |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +popd >/dev/null |
| 89 | +rm -rf "${temp_dir}" |
0 commit comments