-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdev.bash
executable file
·89 lines (73 loc) · 2.4 KB
/
dev.bash
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
#!/usr/bin/env bash
declare -g CHECKSUM_APP
declare -g CONTAINER_APP
declare -g CONTAINER_IMAGE='cert_manager:dev'
declare -g LABEL_PREFIX='org.broadinstitute.cert-manager'
declare -g SCRIPT_DIR
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
if [[ "${TERM}" != 'dumb' ]]; then
TTY=( -it )
fi
if [[ "${CONTAINER_APP}" = 'docker' ]] && [[ "$( uname -s )" != 'Darwin' ]]; then
if [[ ! -w "${DOCKER_SOCKET}" ]]; then
SUDO=( sudo )
fi
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[@]}" .
}
pushd "${SCRIPT_DIR}" >/dev/null || exit 1
if ! "${SUDO[@]}" "${CONTAINER_APP}" image ls | awk '{print $1":"$2}' | grep --quiet -E "^(localhost\/)*${CONTAINER_IMAGE}"; 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