|
| 1 | +#!/bin/bash |
| 2 | +__SUMMARY__=$(cat <<__end__ |
| 3 | +create-alma10-base |
| 4 | +
|
| 5 | +Create and push the Almalinux 10 image that will be used as a base for |
| 6 | +EL 10 builds. We need a single manifest that contains both an |
| 7 | +ARM image and an x86-64 image but the x86-64 image should be |
| 8 | +x86-64-v2-compatible. |
| 9 | +
|
| 10 | +This script should be run on an x86-64 host. |
| 11 | +
|
| 12 | +This script uses podman because docker's manifest manipulation commands |
| 13 | +aren't as good (for example, 'docker manifest add' is missing). |
| 14 | +
|
| 15 | +We go through some hoops to keep podman from sneakily downloading an |
| 16 | +image with the platform of the host system instead of the one we're |
| 17 | +trying to include in the manifest. |
| 18 | +__end__ |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +REGISTRY=hub.opensciencegrid.org |
| 23 | +BASE_IMAGE=docker.io/almalinux/10-base:10 |
| 24 | +ARM_IMAGE=alma10-arm.${RANDOM} |
| 25 | +X86_64_IMAGE=alma10-v2.${RANDOM} |
| 26 | +COMBINED_MANIFEST=${REGISTRY}/osg-htc/alma10-base:combo |
| 27 | + |
| 28 | + |
| 29 | +Prog=${0##*/} |
| 30 | + |
| 31 | + |
| 32 | +ask_yn () { |
| 33 | + while read -rn1 -p "$* (y/n) " |
| 34 | + do |
| 35 | + case $REPLY in |
| 36 | + [Yy]) return 0;; |
| 37 | + [Nn]) return 1;; |
| 38 | + *) echo >&2 "Enter y or n";; |
| 39 | + esac |
| 40 | + done |
| 41 | + return 2 # EOF |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +fail () { |
| 46 | + set +exu |
| 47 | + local ret=${1} |
| 48 | + shift |
| 49 | + echo -e "$Prog:" "$@" >&2 |
| 50 | + exit "$ret" |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +warn () { |
| 55 | + local ret=${1} |
| 56 | + shift |
| 57 | + echo -e "$Prog:" "$@" >&2 |
| 58 | + return "$ret" |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +usage () { |
| 63 | + echo >&2 "$__SUMMARY__" |
| 64 | + echo >&2 |
| 65 | + echo >&2 "Usage: $Prog" |
| 66 | + exit "$1" |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +require_program () { |
| 71 | + command -v "$1" &>/dev/null || |
| 72 | + fail 127 "Required program '$1' not found in PATH" |
| 73 | +} |
| 74 | + |
| 75 | +if [[ $* == -h || $* == --help ]]; then |
| 76 | + usage 0 |
| 77 | +fi |
| 78 | + |
| 79 | +require_program podman |
| 80 | + |
| 81 | + |
| 82 | +set -o nounset |
| 83 | + |
| 84 | + |
| 85 | +on_exit () { |
| 86 | + if podman image exists ${BASE_IMAGE}.bak |
| 87 | + then |
| 88 | + podman untag ${BASE_IMAGE} |
| 89 | + podman tag ${BASE_IMAGE}.bak ${BASE_IMAGE} || warn "Unable to restore old ${BASE_IMAGE}" |
| 90 | + fi |
| 91 | + if podman image exists ${X86_64_IMAGE} |
| 92 | + then |
| 93 | + podman untag ${X86_64_IMAGE} |
| 94 | + fi |
| 95 | + if podman image exists ${ARM_IMAGE} |
| 96 | + then |
| 97 | + podman untag ${ARM_IMAGE} |
| 98 | + fi |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +# If the user has an existing base image, back it up |
| 103 | +if podman image exists ${BASE_IMAGE} |
| 104 | +then |
| 105 | + podman untag ${BASE_IMAGE}.bak &>/dev/null || : |
| 106 | + podman tag ${BASE_IMAGE} ${BASE_IMAGE}.bak || fail 3 "Unable to back up old ${BASE_IMAGE}" |
| 107 | +fi |
| 108 | + |
| 109 | +trap on_exit EXIT |
| 110 | + |
| 111 | +# Build the x86-64 image (based on the x86-64-v2 image from Docker Hub) |
| 112 | +podman build -t ${X86_64_IMAGE} -f- <<__end__ |
| 113 | +# Copies all of the x86-64-v2 almalinux/10-base image into a new linux/amd64 image, |
| 114 | +# changing the platform docker thinks the image is (to avoid a platform mismatch |
| 115 | +# warning every time we try to run the image). |
| 116 | +
|
| 117 | +FROM --platform=linux/amd64/v2 ${BASE_IMAGE} AS alma10base |
| 118 | +
|
| 119 | +FROM --platform=linux/amd64 scratch |
| 120 | +COPY --from=alma10base / / |
| 121 | +CMD ["/bin/bash"] |
| 122 | +__end__ |
| 123 | +# shellcheck disable=SC2181 |
| 124 | +if [[ $? != 0 ]] |
| 125 | +then |
| 126 | + fail 4 "Unable to build x86_64 image" |
| 127 | +fi |
| 128 | + |
| 129 | +# Untag the image we downloaded as part of the build and replace it with the ARM image |
| 130 | +podman untag ${BASE_IMAGE} || fail 5 "Unable to untag old ${BASE_IMAGE}" |
| 131 | +podman pull --platform=linux/arm64 ${BASE_IMAGE} || fail 6 "Unable to pull ARM image" |
| 132 | +# Rename the ARM image (otherwise podman will replace it with an x86-64 image when we try to add it to the manifest) |
| 133 | +podman tag ${BASE_IMAGE} ${ARM_IMAGE} |
| 134 | + # Create a new, empty manifest |
| 135 | +podman manifest rm ${COMBINED_MANIFEST} &>/dev/null || : |
| 136 | +( |
| 137 | + set -e |
| 138 | + podman manifest create ${COMBINED_MANIFEST} |
| 139 | + # Add the images for our two platforms. |
| 140 | + podman manifest add ${COMBINED_MANIFEST} ${X86_64_IMAGE} |
| 141 | + podman manifest add ${COMBINED_MANIFEST} ${ARM_IMAGE} |
| 142 | +) || fail 5 "Unable to create manifest" |
| 143 | + |
| 144 | +echo "Manifest created: ${COMBINED_MANIFEST}" |
| 145 | +if ask_yn "Log in and push to ${REGISTRY}?" |
| 146 | +then |
| 147 | + podman login ${REGISTRY} |
| 148 | + podman manifest push ${COMBINED_MANIFEST} || fail 6 "Unable to push manifest;\n" \ |
| 149 | + "once you have resolved the problem, you may push manually by running\n" \ |
| 150 | + "\n" \ |
| 151 | + "podman manifest push ${COMBINED_MANIFEST}" |
| 152 | +else |
| 153 | + echo "Not pushing. You may push manually by running" |
| 154 | + echo |
| 155 | + echo "podman manifest push ${COMBINED_MANIFEST}" |
| 156 | +fi |
| 157 | + |
| 158 | + |
| 159 | +# vim:et:sw=4:sts=4:ts=8 |
0 commit comments