From 9e0d09428836dcbd8c5bffd58461751cf7a4ab06 Mon Sep 17 00:00:00 2001 From: Fedor Nezhivoi Date: Mon, 11 May 2020 10:02:40 +1000 Subject: [PATCH] download actual latest release --- sshcode.go | 68 +++++++++++++++++++++++++++++++------------------ sshcode_test.go | 6 ++--- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/sshcode.go b/sshcode.go index 160d280..58af829 100644 --- a/sshcode.go +++ b/sshcode.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "fmt" "math/rand" "net" @@ -21,7 +22,7 @@ import ( "golang.org/x/xerrors" ) -const codeServerPath = "~/.cache/sshcode/sshcode-server" +const codeServerDir = "~/.sshcode-server" const ( sshDirectory = "~/.ssh" @@ -82,14 +83,14 @@ func sshCode(host, dir string, o options) error { // Upload local code-server or download code-server from CI server. if o.uploadCodeServer != "" { flog.Info("uploading local code-server binary...") - err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerPath) + err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerDir) if err != nil { return xerrors.Errorf("failed to upload local code-server binary to remote server: %w", err) } sshCmdStr := fmt.Sprintf("ssh %v %v 'chmod +x %v'", - o.sshFlags, host, codeServerPath, + o.sshFlags, host, codeServerDir, ) sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr) @@ -104,7 +105,10 @@ func sshCode(host, dir string, o options) error { } } else { flog.Info("ensuring code-server is updated...") - dlScript := downloadScript(codeServerPath) + dlScript, err := downloadScript(codeServerDir) + if err != nil { + return xerrors.New("failed to download latest code-server") + } // Downloads the latest code-server and allows it to be executed. sshCmdStr := fmt.Sprintf("ssh %v %v '/usr/bin/env bash -l'", o.sshFlags, host) @@ -142,14 +146,13 @@ func sshCode(host, dir string, o options) error { } codeServerFlags := []string{ - "--host 127.0.0.1", - fmt.Sprintf("--port %v", o.remotePort), + fmt.Sprintf("--bind-addr 127.0.0.1:%v", o.remotePort), "--auth none", } if o.disableTelemetry { codeServerFlags = append(codeServerFlags, "--disable-telemetry") } - codeServerCmdStr := fmt.Sprintf("%v %v %v", codeServerPath, dir, strings.Join(codeServerFlags, " ")) + codeServerCmdStr := fmt.Sprintf("%v/code-server %v %v", codeServerDir, dir, strings.Join(codeServerFlags, " ")) flog.Info("starting code-server...") @@ -544,30 +547,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err return nil } -func downloadScript(codeServerPath string) string { +type release struct { + TagName string `json:"tag_name"` +} + +func downloadScript(codeServerDir string) (string, error) { + url := "https://api.github.com/repos/cdr/code-server/releases/latest" + + req, err := http.Get(url) + if err != nil { + return "", err + } + defer req.Body.Close() + + data := release{} + json.NewDecoder(req.Body).Decode(&data) + + assetName := fmt.Sprintf(`code-server-%v-linux-x86_64`, data.TagName) + downloadURL := fmt.Sprintf(`https://github.com/cdr/code-server/releases/download/%v/%v.tar.gz`, data.TagName, assetName) + return fmt.Sprintf( `set -euxo pipefail || exit 1 [ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1 pkill -f %v || true -mkdir -p $HOME/.local/share/code-server %v +mkdir -p %v cd %v -curlflags="-o latest-linux" -if [ -f latest-linux ]; then - curlflags="$curlflags -z latest-linux" -fi -curl $curlflags https://codesrv-ci.cdr.sh/latest-linux -[ -f %v ] && rm %v -ln latest-linux %v -chmod +x %v`, - codeServerPath, - filepath.ToSlash(filepath.Dir(codeServerPath)), - filepath.ToSlash(filepath.Dir(codeServerPath)), - codeServerPath, - codeServerPath, - codeServerPath, - codeServerPath, - ) +if [ ! -d %v ]; then + curl -L %v > release.tar.gz + tar -xzf release.tar.gz + rm release.tar.gz + ln -sf ./%v/code-server code-server +fi`, + codeServerDir, + codeServerDir, + codeServerDir, + assetName, + downloadURL, + assetName, + ), nil } // ensureDir creates a directory if it does not exist. diff --git a/sshcode_test.go b/sshcode_test.go index 096bff6..2610c1a 100644 --- a/sshcode_test.go +++ b/sshcode_test.go @@ -7,7 +7,6 @@ import ( "net" "net/http" "os/exec" - "path/filepath" "strconv" "sync" "testing" @@ -40,6 +39,7 @@ func TestSSHCode(t *testing.T) { bindAddr: net.JoinHostPort("127.0.0.1", localPort), remotePort: remotePort, noOpen: true, + skipSync: true, }) require.NoError(t, err) }() @@ -48,10 +48,10 @@ func TestSSHCode(t *testing.T) { waitForSSHCode(t, remotePort, time.Second*30) // Typically we'd do an os.Stat call here but the os package doesn't expand '~' - out, err := exec.Command("sh", "-l", "-c", "stat "+codeServerPath).CombinedOutput() + out, err := exec.Command("sh", "-l", "-c", "stat "+codeServerDir+"/code-server").CombinedOutput() require.NoError(t, err, "%s", out) - out, err = exec.Command("pkill", filepath.Base(codeServerPath)).CombinedOutput() + out, err = exec.Command("pkill", "-f", codeServerDir).CombinedOutput() require.NoError(t, err, "%s", out) wg.Wait()