|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +dn=$(dirname "$0") |
| 5 | +# shellcheck source=src/cmdlib.sh |
| 6 | +. "${dn}"/cmdlib.sh |
| 7 | + |
| 8 | +print_help() { |
| 9 | + cat 1>&2 <<'EOF' |
| 10 | +Usage: coreos-assembler build-with-buildah |
| 11 | + coreos-assembler build-with-buildah [OPTIONS]... |
| 12 | +
|
| 13 | + Build bootable container (ostree) and image base artifacts using the container runtime (buildah). |
| 14 | + `cosa build` will pivot to this script when the environment variable `COREOS_ASSEMBLER_BUILD_WITH_BUILDAH` is set. |
| 15 | +
|
| 16 | + The following options are supported: |
| 17 | + --versionary Use the versionary script from the source config to drive version. |
| 18 | +EOF |
| 19 | +} |
| 20 | + |
| 21 | +VERSIONARY= |
| 22 | +VERSION= |
| 23 | +rc=0 |
| 24 | +options=$(getopt --options h,v --longoptions help,versionary -- "$@") || rc=$? |
| 25 | +[ $rc -eq 0 ] || { |
| 26 | + print_help |
| 27 | + exit 1 |
| 28 | +} |
| 29 | +eval set -- "$options" |
| 30 | +while true; do |
| 31 | + case "$1" in |
| 32 | + -h | --help) |
| 33 | + print_help |
| 34 | + exit 0 |
| 35 | + ;; |
| 36 | + -v | --versionary) |
| 37 | + VERSIONARY=1 |
| 38 | + ;; |
| 39 | + --) |
| 40 | + shift |
| 41 | + break |
| 42 | + ;; |
| 43 | + -*) |
| 44 | + fatal "$0: unrecognized option: $1" |
| 45 | + ;; |
| 46 | + *) |
| 47 | + break |
| 48 | + ;; |
| 49 | + esac |
| 50 | + shift |
| 51 | +done |
| 52 | + |
| 53 | +if [ -n "${VERSIONARY}" ]; then |
| 54 | + # let error out if file does not exist |
| 55 | + VERSION=$(src/config/versionary) |
| 56 | + echo "New version will be ${VERSION}" |
| 57 | +fi |
| 58 | + |
| 59 | +build_with_buildah() { |
| 60 | + VERSION=$1 |
| 61 | + echo "Building with container runtime (buildah)..." |
| 62 | + |
| 63 | + tempdir=$(mktemp -d --tmpdir=tmp "build-with-buildah.XXXXXXXX") |
| 64 | + |
| 65 | + # the config dir virtiofs mount is mounted ro; copy it to the tempdir |
| 66 | + cp -r src/config/ "${tempdir}/src" |
| 67 | + |
| 68 | + tmp_oci_archive_path=$(realpath "${tempdir}/out.ociarchive") |
| 69 | + |
| 70 | + argsfile=build-args.conf |
| 71 | + if [ -n "${variant:-}" ]; then |
| 72 | + argsfile=build-args-${variant}.conf |
| 73 | + fi |
| 74 | + |
| 75 | + set -- build --security-opt=label=disable --cap-add=all --device /dev/fuse \ |
| 76 | + --build-arg-file "$argsfile" -v "$(realpath "${tempdir}/src")":/run/src \ |
| 77 | + -t oci-archive:"${tmp_oci_archive_path}" |
| 78 | + |
| 79 | + if [ -n "${VERSION}" ]; then |
| 80 | + set -- "$@" --build-arg VERSION="${VERSION}" |
| 81 | + fi |
| 82 | + |
| 83 | + /usr/lib/coreos-assembler/cmd-supermin-run --cache \ |
| 84 | + env -C "${tempdir}/src" TMPDIR="$(realpath cache)" buildah "$@" . |
| 85 | + |
| 86 | + /usr/lib/coreos-assembler/cmd-import "oci-archive:${tmp_oci_archive_path}" |
| 87 | + |
| 88 | + rm -rf "${tempdir}" |
| 89 | +} |
| 90 | + |
| 91 | +build_with_buildah "$VERSION" |
0 commit comments