From 6a9497172994cb68231aac958bb6ec13c74e9d15 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 18 May 2022 17:21:37 +0300 Subject: [PATCH 1/5] fix: More robust provisionersdk agent init scripts Related #1544 --- provisionersdk/agent.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/provisionersdk/agent.go b/provisionersdk/agent.go index 00f6861ef9136..ff94d003895db 100644 --- a/provisionersdk/agent.go +++ b/provisionersdk/agent.go @@ -17,17 +17,27 @@ $env:CODER_AGENT_URL = "${ACCESS_URL}" Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru` linuxScript = `#!/usr/bin/env sh -set -eu pipefail -export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXX)/coder -curl -fsSL ${ACCESS_URL}bin/coder-linux-${ARCH} -o $BINARY_LOCATION +set -eux pipefail +export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder +export BINARY_URL="${ACCESS_URL}bin/coder-linux-${ARCH}" +if which curl >/dev/null 2>&1; then + curl -fsSL "${BINARY_URL}" -o $BINARY_LOCATION +elif which wget >/dev/null 2>&1; then + wget -q "${BINARY_URL}" -O $BINARY_LOCATION +elif which busybox >/dev/null 2>&1; then + busybox wget -q "${BINARY_URL}" -O $BINARY_LOCATION +else + echo "error: no download tool found, please install curl, wget or busybox wget" + exit 1 +fi chmod +x $BINARY_LOCATION export CODER_AGENT_AUTH="${AUTH_TYPE}" export CODER_AGENT_URL="${ACCESS_URL}" exec $BINARY_LOCATION agent` darwinScript = `#!/usr/bin/env sh -set -eu pipefail -export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXX)/coder +set -eux pipefail +export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder curl -fsSL ${ACCESS_URL}bin/coder-darwin-${ARCH} -o $BINARY_LOCATION chmod +x $BINARY_LOCATION export CODER_AGENT_AUTH="${AUTH_TYPE}" From 9fc6a3160a4141e68fcecec57b110048b2d3ccdc Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 18 May 2022 18:09:50 +0300 Subject: [PATCH 2/5] Update provisionersdk/agent.go Co-authored-by: Dean Sheather --- provisionersdk/agent.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provisionersdk/agent.go b/provisionersdk/agent.go index ff94d003895db..aa7c1a2540df2 100644 --- a/provisionersdk/agent.go +++ b/provisionersdk/agent.go @@ -21,7 +21,7 @@ set -eux pipefail export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder export BINARY_URL="${ACCESS_URL}bin/coder-linux-${ARCH}" if which curl >/dev/null 2>&1; then - curl -fsSL "${BINARY_URL}" -o $BINARY_LOCATION + curl -fsSL "${BINARY_URL}" -o "${BINARY_LOCATION}" elif which wget >/dev/null 2>&1; then wget -q "${BINARY_URL}" -O $BINARY_LOCATION elif which busybox >/dev/null 2>&1; then From 2e4b0e6d91493dafba874b8f768d171655ffacbb Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 18 May 2022 18:10:13 +0300 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: Dean Sheather --- provisionersdk/agent.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/provisionersdk/agent.go b/provisionersdk/agent.go index aa7c1a2540df2..19bd4cfde7a33 100644 --- a/provisionersdk/agent.go +++ b/provisionersdk/agent.go @@ -19,13 +19,13 @@ Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru` linuxScript = `#!/usr/bin/env sh set -eux pipefail export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder -export BINARY_URL="${ACCESS_URL}bin/coder-linux-${ARCH}" +BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH} if which curl >/dev/null 2>&1; then curl -fsSL "${BINARY_URL}" -o "${BINARY_LOCATION}" elif which wget >/dev/null 2>&1; then - wget -q "${BINARY_URL}" -O $BINARY_LOCATION + wget -q "${BINARY_URL}" -O "${BINARY_LOCATION}" elif which busybox >/dev/null 2>&1; then - busybox wget -q "${BINARY_URL}" -O $BINARY_LOCATION + busybox wget -q "${BINARY_URL}" -O "${BINARY_LOCATION}" else echo "error: no download tool found, please install curl, wget or busybox wget" exit 1 @@ -38,7 +38,7 @@ exec $BINARY_LOCATION agent` darwinScript = `#!/usr/bin/env sh set -eux pipefail export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder -curl -fsSL ${ACCESS_URL}bin/coder-darwin-${ARCH} -o $BINARY_LOCATION +curl -fsSL "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_LOCATION}" chmod +x $BINARY_LOCATION export CODER_AGENT_AUTH="${AUTH_TYPE}" export CODER_AGENT_URL="${ACCESS_URL}" From 5af7052e248d7bb8e1b9acf754203ca45c368887 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Wed, 18 May 2022 18:19:06 +0300 Subject: [PATCH 4/5] Unexport BINARY_LOCATION as well, not used outside init --- provisionersdk/agent.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/provisionersdk/agent.go b/provisionersdk/agent.go index 19bd4cfde7a33..59d06556e8799 100644 --- a/provisionersdk/agent.go +++ b/provisionersdk/agent.go @@ -18,7 +18,7 @@ Start-Process -FilePath $env:TEMP\sshd.exe -ArgumentList "agent" -PassThru` linuxScript = `#!/usr/bin/env sh set -eux pipefail -export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder +BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder BINARY_URL=${ACCESS_URL}bin/coder-linux-${ARCH} if which curl >/dev/null 2>&1; then curl -fsSL "${BINARY_URL}" -o "${BINARY_LOCATION}" @@ -37,7 +37,7 @@ exec $BINARY_LOCATION agent` darwinScript = `#!/usr/bin/env sh set -eux pipefail -export BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder +BINARY_LOCATION=$(mktemp -d -t tmp.coderXXXXXX)/coder curl -fsSL "${ACCESS_URL}bin/coder-darwin-${ARCH}" -o "${BINARY_LOCATION}" chmod +x $BINARY_LOCATION export CODER_AGENT_AUTH="${AUTH_TYPE}" From 85bcb2a4265617bbe2ffd0a7c4a28c401af7278c Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 May 2022 12:45:55 +0000 Subject: [PATCH 5/5] Fix provisionersdk agent test broken by `set -x` output --- provisionersdk/agent_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/provisionersdk/agent_test.go b/provisionersdk/agent_test.go index d9ebbb8d8936a..255d713c7c8a2 100644 --- a/provisionersdk/agent_test.go +++ b/provisionersdk/agent_test.go @@ -49,8 +49,11 @@ func TestAgentScript(t *testing.T) { output, err := exec.Command("sh", "-c", script).CombinedOutput() t.Log(string(output)) require.NoError(t, err) + // Ignore debug output from `set -x`, we're only interested in the last line. + lines := strings.Split(strings.TrimSpace(string(output)), "\n") + lastLine := lines[len(lines)-1] // Because we use the "echo" binary, we should expect the arguments provided // as the response to executing our script. - require.Equal(t, "agent", strings.TrimSpace(string(output))) + require.Equal(t, "agent", lastLine) }) }