6
6
# Coder enterprise features).
7
7
8
8
SCRIPT_DIR=$( dirname " ${BASH_SOURCE[0]} " )
9
- # shellcheck disable=SC1091,SC1090
9
+ # shellcheck source=scripts/lib.sh
10
10
source " ${SCRIPT_DIR} /lib.sh"
11
11
12
12
# Allow toggling verbose output
@@ -51,29 +51,70 @@ make -j "build/coder_${GOOS}_${GOARCH}"
51
51
# Use the coder dev shim so we don't overwrite the user's existing Coder config.
52
52
CODER_DEV_SHIM=" ${PROJECT_ROOT} /scripts/coder-dev.sh"
53
53
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
+
54
86
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
55
87
# to kill both at the same time. For more details, see:
56
88
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
57
89
(
58
90
# If something goes wrong, just bail and tear everything down
59
91
# 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
+
61
95
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
63
97
64
98
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"
66
104
67
105
if [ ! -f " ${PROJECT_ROOT} /.coderv2/developsh-did-first-setup" ]; then
68
106
# 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
70
112
echo ' Failed to create admin user. To troubleshoot, try running this command manually.'
113
+ fi
71
114
72
115
# Try to create a regular user.
73
116
" ${CODER_DEV_SHIM} " users create --email=member@coder.com --username=member --password=" ${password} " ||
74
117
echo ' Failed to create regular user. To troubleshoot, try running this command manually.'
75
-
76
- touch " ${PROJECT_ROOT} /.coderv2/developsh-did-first-setup"
77
118
fi
78
119
79
120
# 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"
97
138
fi
98
139
99
140
# 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
101
155
156
+ # Space padding used after the URLs to align "==".
157
+ space_padding=26
102
158
log
103
159
log " ===================================================================="
104
160
log " == =="
105
161
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
108
168
log " == =="
109
169
log " == Use ./scripts/coder-dev.sh to talk to this instance! =="
110
170
log " ===================================================================="
111
171
log
112
172
113
173
# Wait for both frontend and backend to exit.
114
- wait
174
+ wait_cmds
115
175
)
0 commit comments