Skip to content

Commit 4942335

Browse files
committed
make scripts loop forever
1 parent 9586595 commit 4942335

File tree

3 files changed

+81
-43
lines changed

3 files changed

+81
-43
lines changed
Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
11
#!/usr/bin/env sh
22
set -eux pipefail
3-
trap 'echo === Agent script exited with non-zero code. Sleeping 24h to preserve logs... && sleep 86400' EXIT
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.
6+
waitonexit() {
7+
echo '=== Agent script exited with non-zero code. Sleeping 24h to preserve logs...'
8+
sleep 86400
9+
}
10+
trap waitonexit EXIT
411
BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
512
BINARY_NAME=coder
13+
BINARY_URL=${ACCESS_URL}bin/coder-darwin-${ARCH}
614
cd "$BINARY_DIR"
7-
curl -fsSL --compressed "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_NAME}"
8-
chmod +x $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+
curl -fsSL --compressed "${BINARY_URL}" -o "${BINARY_NAME}" && break
20+
status=$?
21+
echo "error: failed to download coder agent using curl"
22+
echo "curl exit code: ${status}"
23+
echo "trying again in 30 seconds..."
24+
sleep 30
25+
done
26+
27+
if ! chmod +x $BINARY_NAME; then
28+
echo "Failed to make $BINARY_NAME executable"
29+
exit 1
30+
fi
31+
932
export CODER_AGENT_AUTH="${AUTH_TYPE}"
1033
export CODER_AGENT_URL="${ACCESS_URL}"
1134
exec ./$BINARY_NAME agent
Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env sh
22
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.
36
waitonexit() {
47
echo '=== Agent script exited with non-zero code. Sleeping 24h to preserve logs...'
58
sleep 86400
@@ -9,43 +12,41 @@ BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
912
BINARY_NAME=coder
1013
BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH}
1114
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
1923
status=$?
2024
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
2628
status=$?
29+
test "${status}" -eq 0 && break
2730
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
3435
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"
4047
exit 1
4148
fi
42-
chmod +x $BINARY_NAME || (
43-
echo "Failed to make $BINARY_NAME executable" && sleep 30 && exit 1
44-
)
49+
4550
export CODER_AGENT_AUTH="${AUTH_TYPE}"
4651
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
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
# On Windows, VS Code Remote requires a parent process of the
2-
# executing shell to be named "sshd", otherwise it fails. See:
3-
# https://github.com/microsoft/vscode-remote-release/issues/5699
4-
$ProgressPreference = "SilentlyContinue"
5-
Invoke-WebRequest -Uri ${ACCESS_URL}bin/coder-windows-${ARCH}.exe -OutFile $env:TEMP\sshd.exe
6-
Set-MpPreference -DisableRealtimeMonitoring $true -ExclusionPath $env:TEMP\sshd.exe
7-
$env:CODER_AGENT_AUTH = "${AUTH_TYPE}"
8-
$env:CODER_AGENT_URL = "${ACCESS_URL}"
9-
Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru
1+
while ($true) {
2+
try {
3+
$ProgressPreference = "SilentlyContinue"
4+
# On Windows, VS Code Remote requires a parent process of the
5+
# executing shell to be named "sshd", otherwise it fails. See:
6+
# https://github.com/microsoft/vscode-remote-release/issues/5699
7+
$BINARY_URL="${ACCESS_URL}/bin/coder-windows-${ARCH}.exe"
8+
Invoke-WebRequest -Uri "${BINARY_URL}" -OutFile $env:TEMP\sshd.exe
9+
Set-MpPreference -DisableRealtimeMonitoring $true -ExclusionPath $env:TEMP\sshd.exe
10+
$env:CODER_AGENT_AUTH = "${AUTH_TYPE}"
11+
$env:CODER_AGENT_URL = "${ACCESS_URL}"
12+
Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru
13+
} catch [System.Net.WebException],[System.IO.IOException] {
14+
Write-Error "error: failed to download coder agent from ${ACCESS_URL}"
15+
Write-Error $_.ScriptStackTrace
16+
} catch {
17+
Write-Error "error: unhandled exception fetching and starting coder agent:"
18+
Write-Error $_.ScriptStackTrace
19+
} finally {
20+
Write-Output "trying again in 30 seconds..."
21+
Start-Sleep -Seconds 30
22+
}
23+
}

0 commit comments

Comments
 (0)