From ad9bdb7bd13cca2dd2ee85b6a7c7b98127cf30d7 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Thu, 19 May 2022 16:02:42 +0300 Subject: [PATCH] fix: More robust provisionersdk agent init scripts (#1551) Related #1544 Co-authored-by: Dean Sheather --- provisionersdk/agent.go | 22 ++++++++++++++++------ provisionersdk/agent_test.go | 5 ++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/provisionersdk/agent.go b/provisionersdk/agent.go index 00f6861ef9..59d06556e8 100644 --- a/provisionersdk/agent.go +++ b/provisionersdk/agent.go @@ -17,18 +17,28 @@ $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 +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}" +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 -curl -fsSL ${ACCESS_URL}bin/coder-darwin-${ARCH} -o $BINARY_LOCATION +set -eux pipefail +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}" export CODER_AGENT_URL="${ACCESS_URL}" diff --git a/provisionersdk/agent_test.go b/provisionersdk/agent_test.go index d9ebbb8d89..255d713c7c 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) }) }