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

Commit 37c0e04

Browse files
committed
make gyzerok latest-linux, standalone
1 parent 50e859c commit 37c0e04

File tree

2 files changed

+54
-32
lines changed

2 files changed

+54
-32
lines changed

sshcode.go

+52-29
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,8 +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-
107+
dlScript, err := downloadScript(codeServerDir)
108+
if err != nil {
109+
return xerrors.New("failed to download latest code-server")
110+
}
108111
// Downloads the latest code-server and allows it to be executed.
109112
sshCmdStr := fmt.Sprintf("ssh %v %v '/usr/bin/env bash -l'", o.sshFlags, host)
110113
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
@@ -139,14 +142,19 @@ func sshCode(host, dir string, o options) error {
139142
}
140143
flog.Info("synced extensions in %s", time.Since(start))
141144
}
145+
codeServerFlags := []string{
146+
fmt.Sprintf("--bind-addr 127.0.0.1:%v", o.remotePort),
147+
"--auth none",
148+
}
149+
codeServerCmdStr := fmt.Sprintf("%v/code-server %v %v", codeServerDir, dir, strings.Join(codeServerFlags, " "))
142150

143151
flog.Info("starting code-server...")
144152

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

147155
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,
156+
fmt.Sprintf("ssh -tt -q -L %v:127.0.0.1:%v %v %v '%v'",
157+
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerCmdStr,
150158
)
151159
// Starts code-server and forwards the remote port.
152160
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr)
@@ -533,30 +541,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
533541
return nil
534542
}
535543

536-
func downloadScript(codeServerPath string) string {
544+
type release struct {
545+
TagName string `json:"tag_name"`
546+
}
547+
548+
func downloadScript(codeServerDir string) (string, error) {
549+
url := "https://api.github.com/repos/cdr/code-server/releases/latest"
550+
551+
req, err := http.Get(url)
552+
if err != nil {
553+
return "", err
554+
}
555+
defer req.Body.Close()
556+
557+
data := release{}
558+
json.NewDecoder(req.Body).Decode(&data)
559+
560+
assetName := fmt.Sprintf(`code-server-%v-linux-x86_64`, data.TagName)
561+
downloadURL := fmt.Sprintf(`https://github.com/cdr/code-server/releases/download/%v/%v.tar.gz`, data.TagName, assetName)
562+
537563
return fmt.Sprintf(
538564
`set -euxo pipefail || exit 1
539-
540-
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
541-
pkill -f %v || true
542-
mkdir -p $HOME/.local/share/code-server %v
543-
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-
)
565+
566+
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
567+
pkill -f %v || true
568+
mkdir -p %v
569+
cd %v
570+
if [ ! -d %v ]; then
571+
curl -L %v > release.tar.gz
572+
tar -xzf release.tar.gz
573+
rm release.tar.gz
574+
ln -sf ./%v/code-server code-server
575+
fi`,
576+
codeServerDir,
577+
codeServerDir,
578+
codeServerDir,
579+
assetName,
580+
downloadURL,
581+
assetName,
582+
), nil
560583
}
561584

562585
// 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)