|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -xeuo |
| 4 | + |
| 5 | +# This is an isntallation script for skopeo. |
| 6 | +# skopeo is needed by the e2e-gcp-op-ocl test suite since it pushes and pulls images around. |
| 7 | +# |
| 8 | +# I would much rather use a build-root image for this (see: |
| 9 | +# https://docs.ci.openshift.org/docs/architecture/ci-operator/#build-root-image). |
| 10 | +# However, that process is a bit more involved. While it would ultimately pay |
| 11 | +# off, it is not in-scope with the bug I am trying to resolve. Hence the need |
| 12 | +# for this script. |
| 13 | +# |
| 14 | +# Because of limitations within the builder container image and the context it |
| 15 | +# runs in, certain adaptations must be made: |
| 16 | +# - The builder image does not run as a privileged user and is denied privilege |
| 17 | +# escalation. This means that running dnf install -y skopeo cannot be done. |
| 18 | +# - The builder image does not have jq installed, but it does have Python, |
| 19 | +# which has a built-in JSON parser in its stdlib. This means we need to use |
| 20 | +# Python for any JSON parsing we need to perform. |
| 21 | +# - Because this runs as a non-privileged user, we cannot run the make install |
| 22 | +# step for skopeo. Instead, we need to append /tmp/skopeo/bin to the PATH. This |
| 23 | +# is done in the Makefile and only for the go test invocation. |
| 24 | + |
| 25 | +OPENSHIFT_CI="${OPENSHIFT_CI:-""}" |
| 26 | + |
| 27 | +install_skopeo() { |
| 28 | + # If we've already built skopeo once, check if it works and then return. |
| 29 | + if [ -f /tmp/skopeo/bin/skopeo ]; then |
| 30 | + echo "Prebuilt skopeo found at /tmp/skopeo/bin/skopeo, skipping installation" |
| 31 | + /tmp/skopeo/bin/skopeo --version |
| 32 | + return 0 |
| 33 | + fi |
| 34 | + |
| 35 | + # Get the most recent tagged version of skopeo. |
| 36 | + skopeo_version="$(curl -s https://api.github.com/repos/containers/skopeo/releases/latest | python3 -c 'import sys, json; print(json.load(sys.stdin)["tag_name"])')" |
| 37 | + |
| 38 | + skopeo_clone_dir="/tmp/skopeo" |
| 39 | + |
| 40 | + mkdir -p "$skopeo_clone_dir" |
| 41 | + |
| 42 | + # Shallow-clone the skopeo repo to the local repo dir. |
| 43 | + git clone --branch "$skopeo_version" --depth 1 https://github.com/containers/skopeo.git "$skopeo_clone_dir" |
| 44 | + |
| 45 | + cd "$skopeo_clone_dir" |
| 46 | + |
| 47 | + # Build skopeo |
| 48 | + make bin/skopeo |
| 49 | +} |
| 50 | + |
| 51 | +# Check if we have skopeo installed first. |
| 52 | +if command -v skopeo >/dev/null 2>&1 ; then |
| 53 | + # If we do, just output its version. |
| 54 | + skopeo --version |
| 55 | +else |
| 56 | + # Check if we're running in CI. |
| 57 | + if [ "$OPENSHIFT_CI" == "true" ]; then |
| 58 | + # Only when we're in CI should we install skopeo. |
| 59 | + install_skopeo |
| 60 | + else |
| 61 | + # Otherwise, we should exit with a clear error. |
| 62 | + echo "Missing required binary 'skopeo'" |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +fi |
0 commit comments