Skip to content

Commit a25deb9

Browse files
authored
fix: Misc improvements to scripts/develop.sh (#4995)
* Use new `/healthz` endpoint for checking API liveness * Improved credential handling/retrying in failure scenarios * Separate site (`vite`) logs with prefix and date, additionally this method also disables the `vite` clearing of the screen * Show all interfaces coder API is listening on (due to `0.0.0.0`) * Improved shutdown procedure / interrupt handling
1 parent 766a2ad commit a25deb9

File tree

1 file changed

+71
-11
lines changed

1 file changed

+71
-11
lines changed

scripts/develop.sh

+71-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Coder enterprise features).
77

88
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
9-
# shellcheck disable=SC1091,SC1090
9+
# shellcheck source=scripts/lib.sh
1010
source "${SCRIPT_DIR}/lib.sh"
1111

1212
# Allow toggling verbose output
@@ -51,29 +51,70 @@ make -j "build/coder_${GOOS}_${GOARCH}"
5151
# Use the coder dev shim so we don't overwrite the user's existing Coder config.
5252
CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
5353

54+
pids=()
55+
exit_cleanup() {
56+
set +e
57+
# Set empty interrupt handler so cleanup isn't interrupted.
58+
trap '' INT
59+
# Send interrupt to the entire process group to start shutdown procedures.
60+
kill -INT -$$
61+
# Remove exit trap to avoid infinite loop.
62+
trap - EXIT
63+
64+
# Just in case, send interrupts to our children.
65+
kill -INT "${pids[@]}" >/dev/null 2>&1
66+
# Use the hammer if things take too long.
67+
{ sleep 5 && kill -TERM -$$ >/dev/null 2>&1; } &
68+
69+
# Wait for all children to exit (this can be aborted by hammer).
70+
wait_cmds
71+
exit 1
72+
}
73+
start_cmd() {
74+
echo "== CMD: $*" >&2
75+
"$@" || fatal "CMD: $*" &
76+
pids+=("$!")
77+
}
78+
wait_cmds() {
79+
wait "${pids[@]}" >/dev/null 2>&1
80+
}
81+
fatal() {
82+
echo "== FAIL: $*" >&2
83+
exit_cleanup
84+
}
85+
5486
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
5587
# to kill both at the same time. For more details, see:
5688
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
5789
(
5890
# If something goes wrong, just bail and tear everything down
5991
# rather than leaving things in an inconsistent state.
60-
trap 'kill -TERM -$$' ERR
92+
trap 'exit_cleanup' INT EXIT
93+
trap 'fatal "Script encountered an error"' ERR
94+
6195
cdroot
62-
"${CODER_DEV_SHIM}" server --address 0.0.0.0:3000 || kill -INT -$$ &
96+
start_cmd "${CODER_DEV_SHIM}" server --address 0.0.0.0:3000
6397

6498
echo '== Waiting for Coder to become ready'
65-
timeout 60s bash -c 'until curl -s --fail http://localhost:3000 > /dev/null 2>&1; do sleep 0.5; done'
99+
curl --silent --fail --connect-timeout 1 --max-time 1 --retry 60 --retry-delay 1 --retry-max-time 60 --retry-all-errors 'http://localhost:3000/healthz' ||
100+
fatal 'Coder did not become ready in time'
101+
102+
# Check if credentials are already set up to avoid setting up again.
103+
"${CODER_DEV_SHIM}" list >/dev/null 2>&1 && touch "${PROJECT_ROOT}/.coderv2/developsh-did-first-setup"
66104

67105
if [ ! -f "${PROJECT_ROOT}/.coderv2/developsh-did-first-setup" ]; then
68106
# Try to create the initial admin user.
69-
"${CODER_DEV_SHIM}" login http://127.0.0.1:3000 --first-user-username=admin --first-user-email=admin@coder.com --first-user-password="${password}" ||
107+
if "${CODER_DEV_SHIM}" login http://127.0.0.1:3000 --first-user-username=admin --first-user-email=admin@coder.com --first-user-password="${password}"; then
108+
# Only create this file if an admin user was successfully
109+
# created, otherwise we won't retry on a later attempt.
110+
touch "${PROJECT_ROOT}/.coderv2/developsh-did-first-setup"
111+
else
70112
echo 'Failed to create admin user. To troubleshoot, try running this command manually.'
113+
fi
71114

72115
# Try to create a regular user.
73116
"${CODER_DEV_SHIM}" users create --email=member@coder.com --username=member --password="${password}" ||
74117
echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
75-
76-
touch "${PROJECT_ROOT}/.coderv2/developsh-did-first-setup"
77118
fi
78119

79120
# If we have docker available and the "docker" template doesn't already
@@ -97,19 +138,38 @@ CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
97138
fi
98139

99140
# Start the frontend once we have a template up and running
100-
CODER_HOST=http://127.0.0.1:3000 yarn --cwd=./site dev || kill -INT -$$ &
141+
CODER_HOST=http://127.0.0.1:3000 start_cmd yarn --cwd=./site dev --host | {
142+
while read -r line; do
143+
echo "[SITE] $(date -Iseconds): $line"
144+
done
145+
}
146+
147+
interfaces=(localhost)
148+
if which ip >/dev/null 2>&1; then
149+
# shellcheck disable=SC2207
150+
interfaces+=($(ip a | awk '/inet / {print $2}' | cut -d/ -f1))
151+
elif which ifconfig >/dev/null 2>&1; then
152+
# shellcheck disable=SC2207
153+
interfaces+=($(ifconfig | awk '/inet / {print $2}'))
154+
fi
101155

156+
# Space padding used after the URLs to align "==".
157+
space_padding=26
102158
log
103159
log "===================================================================="
104160
log "== =="
105161
log "== Coder is now running in development mode. =="
106-
log "== API: http://localhost:3000 =="
107-
log "== Web UI: http://localhost:8080 =="
162+
for iface in "${interfaces[@]}"; do
163+
log "$(printf "== API: http://%s:3000%$((space_padding - ${#iface}))s==" "$iface" "")"
164+
done
165+
for iface in "${interfaces[@]}"; do
166+
log "$(printf "== Web UI: http://%s:8080%$((space_padding - ${#iface}))s==" "$iface" "")"
167+
done
108168
log "== =="
109169
log "== Use ./scripts/coder-dev.sh to talk to this instance! =="
110170
log "===================================================================="
111171
log
112172

113173
# Wait for both frontend and backend to exit.
114-
wait
174+
wait_cmds
115175
)

0 commit comments

Comments
 (0)