forked from app-sre/deployment-validation-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer-make
executable file
·48 lines (38 loc) · 1.77 KB
/
container-make
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
if [[ "$1" == "-h"* ]] || [[ "$1" == "--h"* ]]; then
echo "Usage: $0 {arguments to the real 'make'}"
echo "Runs 'make' in the boilerplate backing container."
echo "If the command fails, starts a shell in the container so you can debug."
exit -1
fi
source ${0%/*}/common.sh
CONTAINER_ENGINE=$(command -v podman || command -v docker)
[[ -n "$CONTAINER_ENGINE" ]] || err "Couldn't find a container engine. Are you already in a container?"
CONTAINER_ENGINE_SHORT=${CONTAINER_ENGINE##*/}
# Make sure the mount inside the container is named in such a way that
# - openapi-gen (which relies on GOPATH) produces absolute paths; and
# - other go-ish paths are writeable, e.g. for `go mod download`.
CONTAINER_MOUNT=/go/src/$(repo_import $REPO_ROOT)
# First set up a detached container with the repo mounted.
banner "Starting the container"
if [[ $CONTAINER_ENGINE_SHORT == "podman" ]]; then
container_id=$($CONTAINER_ENGINE run --userns keep-id -d -v "$REPO_ROOT":"$CONTAINER_MOUNT":Z $IMAGE_PULL_PATH tail -f /dev/null)
else
container_id=$($CONTAINER_ENGINE run -d -v "$REPO_ROOT":"$CONTAINER_MOUNT" $IMAGE_PULL_PATH tail -f /dev/null)
fi
if [[ $? -ne 0 ]] || [[ -z "$container_id" ]]; then
err "Couldn't start detached container"
fi
# Now run our `make` command in it with the right UID and working directory
args="exec -it -u $(id -u):0 -w $CONTAINER_MOUNT $container_id"
banner "Running: make $@"
$CONTAINER_ENGINE $args make "$@"
rc=$?
# If it failed, drop into the container in a shell
if [[ $rc -ne 0 ]]; then
banner "The 'make' command failed! Starting a shell in the container for debugging. Just 'exit' when done."
$CONTAINER_ENGINE $args /bin/bash
fi
# Finally, remove the container
banner "Cleaning up the container"
$CONTAINER_ENGINE rm -f $container_id >/dev/null