1
1
#! /usr/bin/env sh
2
2
set -eux pipefail
3
+ # Sleep for a good long while before exiting.
4
+ # This is to allow folks to exec into a failed workspace and poke around to
5
+ # troubleshoot.
3
6
waitonexit () {
4
7
echo ' === Agent script exited with non-zero code. Sleeping 24h to preserve logs...'
5
8
sleep 86400
@@ -9,43 +12,41 @@ BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
9
12
BINARY_NAME=coder
10
13
BINARY_URL=${ACCESS_URL} bin/coder-linux-${ARCH}
11
14
cd " $BINARY_DIR "
12
- # In the below invocations, we sleep for 30 seconds before exiting.
13
- # This is because some providers (e.g. kreuzwerker/docker) will
14
- # automatically remove a Docker container that exits within 15
15
- # seconds, making troubleshooting a failed workspace build
16
- # extremely difficult.
17
- if command -v curl > /dev/null 2>&1 ; then
18
- curl -fsSL --compressed " ${BINARY_URL} " -o " ${BINARY_NAME} " || (
15
+ # Attempt to download the coder agent.
16
+ # This could fail for a number of reasons, many of which are likely transient.
17
+ # So just keep trying!
18
+ while true ; do
19
+ # Try a number of different download tools, as we don't know what we'll
20
+ # have available
21
+ if command -v curl > /dev/null 2>&1 ; then
22
+ curl -fsSL --compressed " ${BINARY_URL} " -o " ${BINARY_NAME} " && break
19
23
status=$?
20
24
echo " error: failed to download coder agent using curl"
21
- sleep 30
22
- exit $status
23
- )
24
- elif command -v wget > /dev/null 2>&1 ; then
25
- wget -q " ${BINARY_URL} " -O " ${BINARY_NAME} " || (
25
+ echo " curl exit code: ${status} "
26
+ elif command -v wget > /dev/null 2>&1 ; then
27
+ wget -q " ${BINARY_URL} " -O " ${BINARY_NAME} " && break
26
28
status=$?
29
+ test " ${status} " -eq 0 && break
27
30
echo " error: failed to download coder agent using wget"
28
- sleep 30
29
- exit $status
30
- )
31
- elif command -v busybox > /dev/null 2>&1 ; then
32
- busybox wget -q " ${BINARY_URL} " -O " ${BINARY_NAME} " || (
33
- status=$?
31
+ echo " wget exit code: ${status} "
32
+ elif command -v busybox > /dev/null 2>&1 ; then
33
+ busybox wget -q " ${BINARY_URL} " -O " ${BINARY_NAME} " && break
34
+ test " ${status} " -eq 0 && break
34
35
echo " error: failed to download coder agent using busybox wget"
35
- sleep 30
36
- exit $status
37
- )
38
- else
39
- echo " error: no download tool found, please install curl, wget or busybox wget"
36
+ echo " busybox wget exit code: ${status} "
37
+ else
38
+ echo " error: no download tool found, please install curl, wget or busybox wget"
39
+ exit 127
40
+ fi
41
+ echo " trying again in 30 seconds..."
42
+ sleep 30
43
+ done
44
+
45
+ if ! chmod +x $BINARY_NAME ; then
46
+ echo " Failed to make $BINARY_NAME executable"
40
47
exit 1
41
48
fi
42
- chmod +x $BINARY_NAME || (
43
- echo " Failed to make $BINARY_NAME executable" && sleep 30 && exit 1
44
- )
49
+
45
50
export CODER_AGENT_AUTH=" ${AUTH_TYPE} "
46
51
export CODER_AGENT_URL=" ${ACCESS_URL} "
47
- exec ./$BINARY_NAME agent || (
48
- echo " Failed to exec ${BINARY_NAME} "
49
- sleep 30
50
- exit 126
51
- )
52
+ exec ./$BINARY_NAME agent
0 commit comments