Skip to content

Commit 8a59178

Browse files
authored
provisionersdk: extract and embed agent bootstrap scripts (coder#2886)
This commit extracts the existing hard-coded agent scripts to their own files and embeds them using go:embed. We can more easily use e.g. shellcheck to validate our agent scripts.
1 parent 8d8c1a1 commit 8a59178

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

provisionersdk/agent.go

+9-41
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
package provisionersdk
22

33
import (
4+
_ "embed"
45
"fmt"
56
"strings"
67
)
78

89
var (
9-
// On Windows, VS Code Remote requires a parent process of the
10-
// executing shell to be named "sshd", otherwise it fails. See:
11-
// https://github.com/microsoft/vscode-remote-release/issues/5699
12-
windowsScript = `$ProgressPreference = "SilentlyContinue"
13-
Invoke-WebRequest -Uri ${ACCESS_URL}bin/coder-windows-${ARCH}.exe -OutFile $env:TEMP\sshd.exe
14-
Set-MpPreference -DisableRealtimeMonitoring $true -ExclusionPath $env:TEMP\sshd.exe
15-
$env:CODER_AGENT_AUTH = "${AUTH_TYPE}"
16-
$env:CODER_AGENT_URL = "${ACCESS_URL}"
17-
Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru`
18-
19-
linuxScript = `#!/usr/bin/env sh
20-
set -eux pipefail
21-
BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
22-
BINARY_NAME=coder
23-
BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH}
24-
cd $BINARY_DIR
25-
if command -v curl >/dev/null 2>&1; then
26-
curl -fsSL --compressed "${BINARY_URL}" -o "${BINARY_NAME}"
27-
elif command -v wget >/dev/null 2>&1; then
28-
wget -q "${BINARY_URL}" -O "${BINARY_NAME}"
29-
elif command -v busybox >/dev/null 2>&1; then
30-
busybox wget -q "${BINARY_URL}" -O "${BINARY_NAME}"
31-
else
32-
echo "error: no download tool found, please install curl, wget or busybox wget"
33-
exit 1
34-
fi
35-
chmod +x $BINARY_NAME
36-
export CODER_AGENT_AUTH="${AUTH_TYPE}"
37-
export CODER_AGENT_URL="${ACCESS_URL}"
38-
exec ./$BINARY_NAME agent`
39-
40-
darwinScript = `#!/usr/bin/env sh
41-
set -eux pipefail
42-
BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
43-
BINARY_NAME=coder
44-
cd $BINARY_DIR
45-
curl -fsSL --compressed "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_NAME}"
46-
chmod +x $BINARY_NAME
47-
export CODER_AGENT_AUTH="${AUTH_TYPE}"
48-
export CODER_AGENT_URL="${ACCESS_URL}"
49-
exec ./$BINARY_NAME agent`
10+
// These used to be hard-coded, but after growing significantly more complex
11+
// it made sense to put them in their own files (e.g. for linting).
12+
//go:embed scripts/bootstrap_windows.ps1
13+
windowsScript string
14+
//go:embed scripts/bootstrap_linux.sh
15+
linuxScript string
16+
//go:embed scripts/bootstrap_darwin.sh
17+
darwinScript string
5018

5119
// A mapping of operating-system ($GOOS) to architecture ($GOARCH)
5220
// to agent install and run script. ${DOWNLOAD_URL} is replaced
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env sh
2+
set -eux pipefail
3+
BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
4+
BINARY_NAME=coder
5+
cd "$BINARY_DIR"
6+
curl -fsSL --compressed "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_NAME}"
7+
chmod +x $BINARY_NAME
8+
export CODER_AGENT_AUTH="${AUTH_TYPE}"
9+
export CODER_AGENT_URL="${ACCESS_URL}"
10+
exec ./$BINARY_NAME agent
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env sh
2+
set -eux pipefail
3+
BINARY_DIR=$(mktemp -d -t coder.XXXXXX)
4+
BINARY_NAME=coder
5+
BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH}
6+
cd "$BINARY_DIR"
7+
if command -v curl >/dev/null 2>&1; then
8+
curl -fsSL --compressed "${BINARY_URL}" -o "${BINARY_NAME}"
9+
elif command -v wget >/dev/null 2>&1; then
10+
wget -q "${BINARY_URL}" -O "${BINARY_NAME}"
11+
elif command -v busybox >/dev/null 2>&1; then
12+
busybox wget -q "${BINARY_URL}" -O "${BINARY_NAME}"
13+
else
14+
echo "error: no download tool found, please install curl, wget or busybox wget"
15+
exit 1
16+
fi
17+
chmod +x $BINARY_NAME
18+
export CODER_AGENT_AUTH="${AUTH_TYPE}"
19+
export CODER_AGENT_URL="${ACCESS_URL}"
20+
exec ./$BINARY_NAME agent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

0 commit comments

Comments
 (0)