|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -ue |
| 3 | + |
| 4 | +if [[ -z "${GIT_ROOT:-}" ]] && command -v git &>/dev/null; then |
| 5 | + GIT_ROOT=$(git rev-parse --show-toplevel) |
| 6 | +fi |
| 7 | +: ${GIT_ROOT} |
| 8 | +DIR=$(realpath --relative-to "$GIT_ROOT" .) |
| 9 | + |
| 10 | +: ${DOCKER:=podman} |
| 11 | +: ${DOCKER_PLATFORM:=--platform linux\/amd64} |
| 12 | +: ${DOCKER_USER:=root} |
| 13 | +: ${DOCKER_IMAGE:=ghcr.io/uq-pac/basil-tools-docker} |
| 14 | + |
| 15 | +if [[ $# -lt 1 ]] || [[ "$1" == --help ]]; then |
| 16 | + echo "usage: $(basename $0) (pull | push | build | start | stop | shell | hash | env [--unset] | COMMAND...)" |
| 17 | + ! [[ $# -lt 1 ]] |
| 18 | + exit |
| 19 | +fi |
| 20 | + |
| 21 | +DOCKER_CMD="$(realpath $0)" |
| 22 | + |
| 23 | +if [[ -z "${DOCKER_FLAKE:-}" ]] && [[ -r "$(dirname $DOCKER_CMD)/docker-flake.txt" ]]; then |
| 24 | + DOCKER_FLAKE="$(cat $(dirname $DOCKER_CMD)/docker-flake.txt)" |
| 25 | +fi |
| 26 | + |
| 27 | +: $DOCKER_FLAKE |
| 28 | + |
| 29 | +# create unique names depending on the flake reference, to ensure the correct container |
| 30 | +# is used. |
| 31 | +# unique names depend only on DOCKER_FLAKE, allowing them to be computed without nix. |
| 32 | +commit=$(printf '%s' "$DOCKER_FLAKE" | grep --only-matching -E '[0-9a-fA-F]{40}' | head -c8) |
| 33 | +flake_hash=flake-$(printf '%s' "$DOCKER_FLAKE" | md5sum | cut -d' ' -f1 | head -c4) |
| 34 | + |
| 35 | +if [[ -z "${DOCKER_TAG:-}" ]]; then |
| 36 | + DOCKER_TAG="$flake_hash-$commit" |
| 37 | +fi |
| 38 | + |
| 39 | +unique_image="$DOCKER_IMAGE:$DOCKER_TAG" |
| 40 | +unique_container="container-$DOCKER_TAG" |
| 41 | + |
| 42 | +# this allows the env subcommand to output syntax compatible with multiple shells |
| 43 | +shell=$(basename $SHELL) |
| 44 | +if [[ $shell == fish ]]; then |
| 45 | + unset='set --erase' |
| 46 | + unalias='functions --erase' |
| 47 | + eval='(' |
| 48 | +else |
| 49 | + unset=unset |
| 50 | + unalias=unalias |
| 51 | + eval='$(' |
| 52 | +fi |
| 53 | + |
| 54 | + |
| 55 | +if [[ "$1" == pull ]]; then |
| 56 | + # pulls the unique image from the registry |
| 57 | + set -x |
| 58 | + exec $DOCKER pull $DOCKER_PLATFORM "$unique_image" |
| 59 | + |
| 60 | +elif [[ "$1" == push ]]; then |
| 61 | + # pushes the unique image to the registry. image must already exist locally. |
| 62 | + set -x |
| 63 | + exec $DOCKER push "$unique_image" |
| 64 | + |
| 65 | +elif [[ "$1" == build ]]; then |
| 66 | + # builds the docker image for running tools. |
| 67 | + # safe to re-run. if docker image is already up-to-date, should be reasonably fast. |
| 68 | + nix build "$DOCKER_FLAKE" --no-link |
| 69 | + nix build "$DOCKER_FLAKE.conf" --no-link |
| 70 | + conf=$(nix build "$DOCKER_FLAKE.conf" --no-link --print-out-paths) |
| 71 | + tag=$(nix eval --expr "with builtins; (fromJSON (unsafeDiscardStringContext (readFile $conf))).repo_tag" --impure --raw) |
| 72 | + if ! [[ "$tag" == "$DOCKER_IMAGE":* ]]; then |
| 73 | + printf '%s %s %s.\n' \ |
| 74 | + "ERROR: docker image names do not match!" \ |
| 75 | + "nix flake will build '$tag', but" \ |
| 76 | + "DOCKER_IMAGE is '$DOCKER_IMAGE'" >&2 |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + set -x |
| 80 | + $(nix build "$DOCKER_FLAKE" --no-link --print-out-paths) | "$DOCKER" image load |
| 81 | + $DOCKER image tag "$tag" $unique_image |
| 82 | + exit |
| 83 | + |
| 84 | +elif [[ "$1" == start ]]; then |
| 85 | + # starts an instance of the docker image. |
| 86 | + set -x |
| 87 | + exec $DOCKER run $DOCKER_PLATFORM -v"$GIT_ROOT:$GIT_ROOT" --rm -td --user $DOCKER_USER --name $unique_container $unique_image |
| 88 | + |
| 89 | +elif [[ "$1" == stop ]]; then |
| 90 | + # stops the instance of the docker image. |
| 91 | + set -x |
| 92 | + exec $DOCKER stop -t 1 $unique_container |
| 93 | + # since --rm is given to `docker run`, this will also remove the container. |
| 94 | + |
| 95 | +elif [[ "$1" == shell ]]; then |
| 96 | + # enters an interactive shell within the container. |
| 97 | + set -x |
| 98 | + exec $DOCKER exec -it --user $DOCKER_USER -w "$GIT_ROOT/$DIR" -eshell=1 $unique_container /usr/bin/_exec bash |
| 99 | + |
| 100 | +elif [[ "$1" == hash ]]; then |
| 101 | + # outputs information about the docker image's version to stdout. |
| 102 | + echo "$DOCKER_FLAKE" |
| 103 | + echo |
| 104 | + exec "$DOCKER_CMD" bash -c 'ls -1 /nix/store | sort -k1.33' # sort /nix/store contents by name, not hash |
| 105 | + |
| 106 | +elif [[ "$1" == env ]]; then |
| 107 | + # outputs commands to set the environment to stdout. |
| 108 | + # when passed to `eval`, these commands should prepare the shell for running |
| 109 | + # basil tests through docker. |
| 110 | + |
| 111 | + # if --unset is used, removes all definitions |
| 112 | + isunset=$([[ $# -ge 2 ]] && [[ "$2" == --unset ]] && echo true || echo false) |
| 113 | + # if --reset is used, removes all definitions, then re-adds them based on defaults |
| 114 | + isreset=$([[ $# -ge 2 ]] && [[ "$2" == --reset ]] && echo true || echo false) |
| 115 | + |
| 116 | + if $isreset; then |
| 117 | + isunset=true |
| 118 | + fi |
| 119 | + |
| 120 | + function echoexport() { |
| 121 | + if $isunset; then |
| 122 | + echo echo "$unset" "$1" ';' |
| 123 | + echo "$unset" "$1" ';' |
| 124 | + return |
| 125 | + fi |
| 126 | + printf 'echo "%s = %s";\n' "$1" "$2" |
| 127 | + printf 'export %s="%s";\n' "$1" "$2" |
| 128 | + } |
| 129 | + |
| 130 | + echoexport USE_DOCKER "1" |
| 131 | + echoexport DOCKER_FLAKE "$DOCKER_FLAKE" |
| 132 | + echoexport DOCKER_IMAGE "$DOCKER_IMAGE" |
| 133 | + echoexport DOCKER_TAG "$DOCKER_TAG" |
| 134 | + echoexport DOCKER_PLATFORM "$DOCKER_PLATFORM" |
| 135 | + echoexport DOCKER "$DOCKER" |
| 136 | + echoexport DOCKER_USER "$DOCKER_USER" |
| 137 | + echoexport DOCKER_CMD "$DOCKER_CMD" |
| 138 | + echoexport GIT_ROOT "$GIT_ROOT" |
| 139 | + echo 'echo;' |
| 140 | + echoexport GCC "$DOCKER_CMD aarch64-unknown-linux-gnu-gcc" |
| 141 | + echoexport CLANG "$DOCKER_CMD aarch64-unknown-linux-gnu-clang" |
| 142 | + echoexport READELF "$DOCKER_CMD aarch64-unknown-linux-gnu-readelf" |
| 143 | + echoexport BAP "$DOCKER_CMD bap" |
| 144 | + echoexport DDISASM "$DOCKER_CMD ddisasm" |
| 145 | + echoexport PROTO_JSON "$DOCKER_CMD proto-json.py" |
| 146 | + # echoexport PROTO_JSON "/home/rina/progs/gtirb-semantics/scripts/proto-json.py" |
| 147 | + echoexport DEBUG_GTS "$DOCKER_CMD debug-gts.py" |
| 148 | + echoexport GTIRB_SEMANTICS "$DOCKER_CMD gtirb-semantics" |
| 149 | + echo 'echo;' |
| 150 | + if $isunset; then |
| 151 | + echo "echo $unalias docker-helper.sh;" |
| 152 | + echo "$unalias docker-helper.sh;" |
| 153 | + else |
| 154 | + echo "echo alias docker-helper.sh = '$DOCKER_CMD';" |
| 155 | + echo "alias 'docker-helper.sh=$DOCKER_CMD';" |
| 156 | + fi |
| 157 | + |
| 158 | + if $isreset; then |
| 159 | + echo "eval $eval$DOCKER_CMD env);" |
| 160 | + fi |
| 161 | + exit |
| 162 | +fi |
| 163 | + |
| 164 | +if [[ -n "${NIX_BUILD_TOP:-}" ]]; then |
| 165 | + set -x |
| 166 | + # if already inside a Nix shell, simply execute |
| 167 | + exec /usr/bin/_exec "$@" |
| 168 | +else |
| 169 | + set -x |
| 170 | + # for other commands, execute within the container. |
| 171 | + exec $DOCKER exec --user $DOCKER_USER -w "$GIT_ROOT/$DIR" $unique_container /usr/bin/_exec "$@" |
| 172 | +fi |
0 commit comments