-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdev.sh
executable file
·92 lines (76 loc) · 2.34 KB
/
dev.sh
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
declare -g CHECKSUM_APP
declare -g CONTAINER_APP
declare -g CONTAINER_IMAGE='pysudoers:dev'
declare -g LABEL_PREFIX='org.broadinstitute.pysudoers'
declare -g OPTS
declare -g SCRIPT_DIR
declare -ag SSH_FORWARD
declare -ga TRACK_FILES=( Containerfile poetry.lock pyproject.toml )
declare -ga SUDO
declare -ag TTY
SCRIPT_DIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if hash sha256sum 2>/dev/null; then
CHECKSUM_APP='sha256sum'
elif hash shasum 2>/dev/null; then
CHECKSUM_APP='shasum -a 256'
elif hash md5sum 2>/dev/null; then
CHECKSUM_APP='md5sum'
else
echo 'Could not find a checksumming program. Exiting!'
exit 1
fi
if hash docker 2>/dev/null; then
CONTAINER_APP='docker'
elif hash podman 2>/dev/null; then
CONTAINER_APP='podman'
else
echo 'Container environment cannot be found. Exiting!'
exit 2
fi
function build() {
if [[ -z "$1" ]]; then
echo 'Image name not provided to build. Exiting!'
exit 1
fi
CONTAINER_IMAGE=$1
if ! git diff --quiet; then
echo 'Branch is dirty. The build can only happen on an unmodified branch.'
exit 2
fi
local -a labels
for tfile in "${TRACK_FILES[@]}"; do
label="$( $CHECKSUM_APP "$tfile" | awk -v PREFIX="$LABEL_PREFIX" '{print PREFIX"."$2"="$1}' )"
labels+=( --label "$label" )
done
"${SUDO[@]}" "$CONTAINER_APP" build --pull -t "$CONTAINER_IMAGE" "${labels[@]}" .
}
if [[ "$TERM" != 'dumb' ]]; then
TTY=( '-it' )
fi
if [[ "$( uname -s )" != 'Darwin' ]]; then
if [[ ! -w "$DOCKER_SOCKET" ]]; then
SUDO=( sudo )
fi
fi
pushd "$SCRIPT_DIR" >/dev/null || exit 1
CONTAINER_ID="$( "$CONTAINER_APP" image ls -qf "reference=${CONTAINER_IMAGE}" )"
if [ -z "$CONTAINER_ID" ]; then
build "$CONTAINER_IMAGE"
fi
rebuild='0'
for tfile in "${TRACK_FILES[@]}"; do
current="$( "$CHECKSUM_APP" "$tfile" | cut -d' ' -f1 )"
stored="$( "$CONTAINER_APP" image inspect --format="{{index .Config.Labels \"${LABEL_PREFIX}.${tfile}\"}}" "$CONTAINER_IMAGE" )"
if [[ "$current" != "$stored" ]]; then
echo "$tfile changed. Rebuilding."
rebuild='1'
fi
done
if [[ "$rebuild" == "1" ]]; then
build "$CONTAINER_IMAGE"
fi
"${SUDO[@]}" "$CONTAINER_APP" run "${TTY[@]}" --rm \
-v "${SCRIPT_DIR}:/working" \
"$CONTAINER_IMAGE" "$@"
popd >/dev/null || exit 1