Skip to content

Commit 9e0d094

Browse files
committed
download actual latest release
1 parent 61bf3a6 commit 9e0d094

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

sshcode.go

+43-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"math/rand"
78
"net"
@@ -21,7 +22,7 @@ import (
2122
"golang.org/x/xerrors"
2223
)
2324

24-
const codeServerPath = "~/.cache/sshcode/sshcode-server"
25+
const codeServerDir = "~/.sshcode-server"
2526

2627
const (
2728
sshDirectory = "~/.ssh"
@@ -82,14 +83,14 @@ func sshCode(host, dir string, o options) error {
8283
// Upload local code-server or download code-server from CI server.
8384
if o.uploadCodeServer != "" {
8485
flog.Info("uploading local code-server binary...")
85-
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerPath)
86+
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerDir)
8687
if err != nil {
8788
return xerrors.Errorf("failed to upload local code-server binary to remote server: %w", err)
8889
}
8990

9091
sshCmdStr :=
9192
fmt.Sprintf("ssh %v %v 'chmod +x %v'",
92-
o.sshFlags, host, codeServerPath,
93+
o.sshFlags, host, codeServerDir,
9394
)
9495

9596
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
@@ -104,7 +105,10 @@ func sshCode(host, dir string, o options) error {
104105
}
105106
} else {
106107
flog.Info("ensuring code-server is updated...")
107-
dlScript := downloadScript(codeServerPath)
108+
dlScript, err := downloadScript(codeServerDir)
109+
if err != nil {
110+
return xerrors.New("failed to download latest code-server")
111+
}
108112

109113
// Downloads the latest code-server and allows it to be executed.
110114
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 {
142146
}
143147

144148
codeServerFlags := []string{
145-
"--host 127.0.0.1",
146-
fmt.Sprintf("--port %v", o.remotePort),
149+
fmt.Sprintf("--bind-addr 127.0.0.1:%v", o.remotePort),
147150
"--auth none",
148151
}
149152
if o.disableTelemetry {
150153
codeServerFlags = append(codeServerFlags, "--disable-telemetry")
151154
}
152-
codeServerCmdStr := fmt.Sprintf("%v %v %v", codeServerPath, dir, strings.Join(codeServerFlags, " "))
155+
codeServerCmdStr := fmt.Sprintf("%v/code-server %v %v", codeServerDir, dir, strings.Join(codeServerFlags, " "))
153156

154157
flog.Info("starting code-server...")
155158

@@ -544,30 +547,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
544547
return nil
545548
}
546549

547-
func downloadScript(codeServerPath string) string {
550+
type release struct {
551+
TagName string `json:"tag_name"`
552+
}
553+
554+
func downloadScript(codeServerDir string) (string, error) {
555+
url := "https://api.github.com/repos/cdr/code-server/releases/latest"
556+
557+
req, err := http.Get(url)
558+
if err != nil {
559+
return "", err
560+
}
561+
defer req.Body.Close()
562+
563+
data := release{}
564+
json.NewDecoder(req.Body).Decode(&data)
565+
566+
assetName := fmt.Sprintf(`code-server-%v-linux-x86_64`, data.TagName)
567+
downloadURL := fmt.Sprintf(`https://github.com/cdr/code-server/releases/download/%v/%v.tar.gz`, data.TagName, assetName)
568+
548569
return fmt.Sprintf(
549570
`set -euxo pipefail || exit 1
550571
551572
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
552573
pkill -f %v || true
553-
mkdir -p $HOME/.local/share/code-server %v
574+
mkdir -p %v
554575
cd %v
555-
curlflags="-o latest-linux"
556-
if [ -f latest-linux ]; then
557-
curlflags="$curlflags -z latest-linux"
558-
fi
559-
curl $curlflags https://codesrv-ci.cdr.sh/latest-linux
560-
[ -f %v ] && rm %v
561-
ln latest-linux %v
562-
chmod +x %v`,
563-
codeServerPath,
564-
filepath.ToSlash(filepath.Dir(codeServerPath)),
565-
filepath.ToSlash(filepath.Dir(codeServerPath)),
566-
codeServerPath,
567-
codeServerPath,
568-
codeServerPath,
569-
codeServerPath,
570-
)
576+
if [ ! -d %v ]; then
577+
curl -L %v > release.tar.gz
578+
tar -xzf release.tar.gz
579+
rm release.tar.gz
580+
ln -sf ./%v/code-server code-server
581+
fi`,
582+
codeServerDir,
583+
codeServerDir,
584+
codeServerDir,
585+
assetName,
586+
downloadURL,
587+
assetName,
588+
), nil
571589
}
572590

573591
// ensureDir creates a directory if it does not exist.

sshcode_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"net/http"
99
"os/exec"
10-
"path/filepath"
1110
"strconv"
1211
"sync"
1312
"testing"
@@ -40,6 +39,7 @@ func TestSSHCode(t *testing.T) {
4039
bindAddr: net.JoinHostPort("127.0.0.1", localPort),
4140
remotePort: remotePort,
4241
noOpen: true,
42+
skipSync: true,
4343
})
4444
require.NoError(t, err)
4545
}()
@@ -48,10 +48,10 @@ func TestSSHCode(t *testing.T) {
4848
waitForSSHCode(t, remotePort, time.Second*30)
4949

5050
// Typically we'd do an os.Stat call here but the os package doesn't expand '~'
51-
out, err := exec.Command("sh", "-l", "-c", "stat "+codeServerPath).CombinedOutput()
51+
out, err := exec.Command("sh", "-l", "-c", "stat "+codeServerDir+"/code-server").CombinedOutput()
5252
require.NoError(t, err, "%s", out)
5353

54-
out, err = exec.Command("pkill", filepath.Base(codeServerPath)).CombinedOutput()
54+
out, err = exec.Command("pkill", "-f", codeServerDir).CombinedOutput()
5555
require.NoError(t, err, "%s", out)
5656

5757
wg.Wait()

0 commit comments

Comments
 (0)