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

Commit f221699

Browse files
committed
Add --code-server-version, default latest-preview
Allows users to change which code-server release to download. Changed downloads to use https://codesrv-ci.cdr.sh/releases/$release/linux-x86_64/code-server. Add last-version file in download script to keep track of which version is cached, otherwise changing versions might result in them not being downloaded (as curl thinks it's up to date).
1 parent 7e6845d commit f221699

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type rootCmd struct {
4242
bindAddr string
4343
sshFlags string
4444
uploadCodeServer string
45+
codeServerVersion string
4546
}
4647

4748
func (c *rootCmd) Spec() cli.CommandSpec {
@@ -59,7 +60,8 @@ func (c *rootCmd) RegisterFlags(fl *pflag.FlagSet) {
5960
fl.BoolVar(&c.noReuseConnection, "no-reuse-connection", false, "do not reuse SSH connection via control socket")
6061
fl.StringVar(&c.bindAddr, "bind", "", "local bind address for SSH tunnel, in [HOST][:PORT] syntax (default: 127.0.0.1)")
6162
fl.StringVar(&c.sshFlags, "ssh-flags", "", "custom SSH flags")
62-
fl.StringVar(&c.uploadCodeServer, "upload-code-server", "", "custom code-server binary to upload to the remote host")
63+
fl.StringVar(&c.uploadCodeServer, "upload-code-server", "", "custom code-server binary to upload to the remote host, takes precedence over --code-server-version")
64+
fl.StringVar(&c.codeServerVersion, "code-server-version", defaultCodeServerVersion, `custom code-server version to download on the remote host, accepts any release name, "latest", or "latest-preview" (changing this may cause issues)`)
6365
}
6466

6567
func (c *rootCmd) Run(fl *pflag.FlagSet) {
@@ -81,12 +83,13 @@ func (c *rootCmd) Run(fl *pflag.FlagSet) {
8183
}
8284

8385
err := sshCode(host, dir, options{
84-
skipSync: c.skipSync,
85-
sshFlags: c.sshFlags,
86-
bindAddr: c.bindAddr,
87-
syncBack: c.syncBack,
88-
reuseConnection: !c.noReuseConnection,
89-
uploadCodeServer: c.uploadCodeServer,
86+
skipSync: c.skipSync,
87+
sshFlags: c.sshFlags,
88+
bindAddr: c.bindAddr,
89+
syncBack: c.syncBack,
90+
reuseConnection: !c.noReuseConnection,
91+
uploadCodeServer: c.uploadCodeServer,
92+
codeServerVersion: c.codeServerVersion,
9093
})
9194

9295
if err != nil {

sshcode.go

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import (
2020
"golang.org/x/xerrors"
2121
)
2222

23-
const codeServerPath = "~/.cache/sshcode/sshcode-server"
23+
const (
24+
codeServerPath = "~/.cache/sshcode/sshcode-server"
25+
defaultCodeServerVersion = "latest-preview"
26+
)
2427

2528
const (
2629
sshDirectory = "~/.ssh"
@@ -29,14 +32,15 @@ const (
2932
)
3033

3134
type options struct {
32-
skipSync bool
33-
syncBack bool
34-
noOpen bool
35-
reuseConnection bool
36-
bindAddr string
37-
remotePort string
38-
sshFlags string
39-
uploadCodeServer string
35+
skipSync bool
36+
syncBack bool
37+
noOpen bool
38+
reuseConnection bool
39+
bindAddr string
40+
remotePort string
41+
sshFlags string
42+
uploadCodeServer string
43+
codeServerVersion string
4044
}
4145

4246
func sshCode(host, dir string, o options) error {
@@ -60,6 +64,10 @@ func sshCode(host, dir string, o options) error {
6064
return xerrors.Errorf("failed to find available remote port: %w", err)
6165
}
6266

67+
if o.codeServerVersion == "" {
68+
o.codeServerVersion = defaultCodeServerVersion
69+
}
70+
6371
// Check the SSH directory's permissions and warn the user if it is not safe.
6472
o.reuseConnection = checkSSHDirectory(sshDirectory, o.reuseConnection)
6573

@@ -102,7 +110,7 @@ func sshCode(host, dir string, o options) error {
102110
}
103111
} else {
104112
flog.Info("ensuring code-server is updated...")
105-
dlScript := downloadScript(codeServerPath)
113+
dlScript := downloadScript(o.codeServerVersion, codeServerPath)
106114

107115
// Downloads the latest code-server and allows it to be executed.
108116
sshCmdStr := fmt.Sprintf("ssh %v %v '/usr/bin/env bash -l'", o.sshFlags, host)
@@ -145,8 +153,8 @@ func sshCode(host, dir string, o options) error {
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 'cd %v; %v --host 127.0.0.1 --allow-http --no-auth --port=%v'",
149-
o.bindAddr, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort,
156+
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v 'cd %v; %v --host 127.0.0.1 --port %v %v'",
157+
o.bindAddr, o.remotePort, o.sshFlags, host, dir, codeServerPath, o.remotePort, dir,
150158
)
151159

152160
// Starts code-server and forwards the remote port.
@@ -518,25 +526,44 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
518526
return nil
519527
}
520528

521-
func downloadScript(codeServerPath string) string {
529+
func downloadURL(codeServerVersion string) string {
530+
const baseURL = "https://codesrv-ci.cdr.sh"
531+
532+
if codeServerVersion == "latest" || codeServerVersion == "latest-preview" {
533+
return fmt.Sprintf("%v/%v-linux", baseURL, codeServerVersion)
534+
}
535+
return fmt.Sprintf("%v/releases/%v/linux-x86_64/code-server", baseURL, codeServerVersion)
536+
}
537+
538+
func downloadScript(codeServerVersion string, codeServerPath string) string {
539+
url := downloadURL(codeServerVersion)
540+
522541
return fmt.Sprintf(
523542
`set -euxo pipefail || exit 1
524543
525544
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
526545
pkill -f %v || true
527546
mkdir -p ~/.local/share/code-server %v
528547
cd %v
548+
if [ ! -f last-version ] || [ "$(cat last-version)" != "%v" ]; then
549+
rm latest-linux %v
550+
fi
551+
echo %v > last-version
529552
curlflags="-o latest-linux"
530553
if [ -f latest-linux ]; then
531554
curlflags="$curlflags -z latest-linux"
532555
fi
533-
curl $curlflags https://codesrv-ci.cdr.sh/latest-linux
556+
curl $curlflags "%v"
534557
[ -f %v ] && rm %v
535558
ln latest-linux %v
536559
chmod +x %v`,
537560
codeServerPath,
538561
filepath.Dir(codeServerPath),
539562
filepath.Dir(codeServerPath),
563+
codeServerVersion,
564+
codeServerPath,
565+
codeServerVersion,
566+
url,
540567
codeServerPath,
541568
codeServerPath,
542569
codeServerPath,

0 commit comments

Comments
 (0)