Skip to content

Commit 0fe27ed

Browse files
Cavid DoneeCavid Donee
authored andcommitted
Fix trap ordering and cleanup temp files
1 parent f7d6480 commit 0fe27ed

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

private/tools/diff.bash

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ set -o pipefail -o errexit -o nounset
44
# ./private/tools/diff.bash --head-ref test --base-ref test --query-bazel --registry-spawn --report ./report.log
55

66
REGISTRY_TMPDIR=
7+
STDERR=$(mktemp)
8+
STDERR_IS_TEMP="1"
9+
CHANGED_IMAGES_FILE=$(mktemp)
10+
CHANGED_IMAGES_FILE_IS_TEMP="1"
711

812
# Upon exiting, stop the registry and print STDERR on non-zero exit code.
913
on_exit() {
@@ -22,6 +26,12 @@ on_exit() {
2226
if [[ -n "${REGISTRY_TMPDIR:-}" && -d "${REGISTRY_TMPDIR}" ]]; then
2327
rm -rf "${REGISTRY_TMPDIR}"
2428
fi
29+
if [[ "${STDERR_IS_TEMP:-0}" == "1" ]]; then
30+
rm -f "${STDERR}"
31+
fi
32+
if [[ "${CHANGED_IMAGES_FILE_IS_TEMP:-0}" == "1" ]]; then
33+
rm -f "${CHANGED_IMAGES_FILE}"
34+
fi
2535
pkill -P $$
2636
}
2737
trap "on_exit" EXIT
@@ -33,8 +43,6 @@ QUERY_FILE=
3343
REPORT_FILE=
3444
REGISTRY=
3545
JOBS=
36-
STDERR=$(mktemp)
37-
CHANGED_IMAGES_FILE=$(mktemp)
3846
SET_GITHUB_OUTPUT="0"
3947
ONLY=
4048
SKIP_INDEX="0"
@@ -84,6 +92,7 @@ while (($# > 0)); do
8492
;;
8593
--logs)
8694
STDERR="$2"
95+
STDERR_IS_TEMP="0"
8796
shift 2
8897
;;
8998
--only)
@@ -133,16 +142,19 @@ if [[ "${QUERY_FILE}" == "bazel" ]]; then
133142
QUERY_FILE=$(bazel cquery --output=files :sign_and_push.query)
134143
fi
135144

136-
if [[ "${REGISTRY}" == "spawn_https" ]]; then
137-
# Make a self signed cert
145+
if [[ "${REGISTRY}" == "spawn_https" || "${REGISTRY}" == "spawn" ]]; then
138146
umask 077
139147
REGISTRY_TMPDIR="$(mktemp -d)"
140148
DISK_STORAGE="${REGISTRY_TMPDIR}/diff-storage"
149+
mkdir -p "${DISK_STORAGE}"
150+
REGISTRY="localhost:4564"
151+
fi
152+
153+
if [[ "${REGISTRY}" == "spawn_https" ]]; then
154+
# Make a self signed cert
141155
CFG_JSON="${REGISTRY_TMPDIR}/cfg.json"
142156
CERT_PATH="${REGISTRY_TMPDIR}/localhost.pem"
143157
KEY_PATH="${REGISTRY_TMPDIR}/localhost-key.pem"
144-
rm -rf "${DISK_STORAGE}"
145-
mkdir -p "${DISK_STORAGE}"
146158
mkcert -install
147159
mkcert -cert-file "${CERT_PATH}" -key-file "${KEY_PATH}" localhost
148160
cat >"${CFG_JSON}" <<EOF
@@ -159,17 +171,11 @@ if [[ "${REGISTRY}" == "spawn_https" ]]; then
159171
"storage": { "rootDirectory": "${DISK_STORAGE}" }
160172
}
161173
EOF
162-
REGISTRY="localhost:4564"
163174
zot serve "${CFG_JSON}" 1>&2 &
164175
sleep 1
165176
fi
166177

167178
if [[ "${REGISTRY}" == "spawn" ]]; then
168-
umask 077
169-
REGISTRY_TMPDIR="$(mktemp -d)"
170-
DISK_STORAGE="${REGISTRY_TMPDIR}/diff-storage"
171-
mkdir -p "${DISK_STORAGE}"
172-
REGISTRY="localhost:4564"
173179
crane registry serve --address "$REGISTRY" --disk "$DISK_STORAGE" &
174180
fi
175181

0 commit comments

Comments
 (0)