Skip to content
This repository was archived by the owner on Jan 17, 2021. It is now read-only.

Commit 5bc4292

Browse files
committed
use latest version of code-server
1 parent 50e859c commit 5bc4292

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

sshcode.go

+49-24
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"
@@ -81,14 +82,14 @@ func sshCode(host, dir string, o options) error {
8182
// Upload local code-server or download code-server from CI server.
8283
if o.uploadCodeServer != "" {
8384
flog.Info("uploading local code-server binary...")
84-
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerPath)
85+
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerDir)
8586
if err != nil {
8687
return xerrors.Errorf("failed to upload local code-server binary to remote server: %w", err)
8788
}
8889

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

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

108112
// Downloads the latest code-server and allows it to be executed.
109113
sshCmdStr := fmt.Sprintf("ssh %v %v '/usr/bin/env bash -l'", o.sshFlags, host)
@@ -140,13 +144,19 @@ func sshCode(host, dir string, o options) error {
140144
flog.Info("synced extensions in %s", time.Since(start))
141145
}
142146

147+
codeServerFlags := []string{
148+
fmt.Sprintf("--bind-addr 127.0.0.1:%v", o.remotePort),
149+
"--auth none",
150+
}
151+
codeServerCmdStr := fmt.Sprintf("%v/code-server %v %v", codeServerDir, dir, strings.Join(codeServerFlags, " "))
152+
143153
flog.Info("starting code-server...")
144154

145155
flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr)
146156

147157
sshCmdStr :=
148-
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v '%v %v --host 127.0.0.1 --auth none --port=%v'",
149-
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerPath, dir, o.remotePort,
158+
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v '%v'",
159+
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerCmdStr,
150160
)
151161
// Starts code-server and forwards the remote port.
152162
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
@@ -533,30 +543,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
533543
return nil
534544
}
535545

536-
func downloadScript(codeServerPath string) string {
546+
type release struct {
547+
TagName string `json:"tag_name"`
548+
}
549+
550+
func downloadScript(codeServerDir string) (string, error) {
551+
url := "https://api.github.com/repos/cdr/code-server/releases/latest"
552+
553+
req, err := http.Get(url)
554+
if err != nil {
555+
return "", err
556+
}
557+
defer req.Body.Close()
558+
559+
data := release{}
560+
json.NewDecoder(req.Body).Decode(&data)
561+
562+
assetName := fmt.Sprintf(`code-server-%v-linux-x86_64`, data.TagName)
563+
downloadURL := fmt.Sprintf(`https://github.com/cdr/code-server/releases/download/%v/%v.tar.gz`, data.TagName, assetName)
564+
537565
return fmt.Sprintf(
538566
`set -euxo pipefail || exit 1
539567
540568
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
541569
pkill -f %v || true
542-
mkdir -p $HOME/.local/share/code-server %v
570+
mkdir -p %v
543571
cd %v
544-
curlflags="-o latest-linux"
545-
if [ -f latest-linux ]; then
546-
curlflags="$curlflags -z latest-linux"
547-
fi
548-
curl $curlflags https://codesrv-ci.cdr.sh/latest-linux
549-
[ -f %v ] && rm %v
550-
ln latest-linux %v
551-
chmod +x %v`,
552-
codeServerPath,
553-
filepath.ToSlash(filepath.Dir(codeServerPath)),
554-
filepath.ToSlash(filepath.Dir(codeServerPath)),
555-
codeServerPath,
556-
codeServerPath,
557-
codeServerPath,
558-
codeServerPath,
559-
)
572+
if [ ! -d %v ]; then
573+
curl -L %v > release.tar.gz
574+
tar -xzf release.tar.gz
575+
rm release.tar.gz
576+
ln -sf ./%v/code-server code-server
577+
fi`,
578+
codeServerDir,
579+
codeServerDir,
580+
codeServerDir,
581+
assetName,
582+
downloadURL,
583+
assetName,
584+
), nil
560585
}
561586

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

sshcode_test.go

+2-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"
@@ -48,10 +47,10 @@ func TestSSHCode(t *testing.T) {
4847
waitForSSHCode(t, remotePort, time.Second*30)
4948

5049
// 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()
50+
out, err := exec.Command("sh", "-l", "-c", "stat "+codeServerDir+"/code-server").CombinedOutput()
5251
require.NoError(t, err, "%s", out)
5352

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

5756
wg.Wait()

0 commit comments

Comments
 (0)