@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
7
"math/rand"
7
8
"net"
@@ -21,7 +22,7 @@ import (
21
22
"golang.org/x/xerrors"
22
23
)
23
24
24
- const codeServerPath = "~/.cache/sshcode/ sshcode-server"
25
+ const codeServerDir = "~/.sshcode-server"
25
26
26
27
const (
27
28
sshDirectory = "~/.ssh"
@@ -82,14 +83,14 @@ func sshCode(host, dir string, o options) error {
82
83
// Upload local code-server or download code-server from CI server.
83
84
if o .uploadCodeServer != "" {
84
85
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 )
86
87
if err != nil {
87
88
return xerrors .Errorf ("failed to upload local code-server binary to remote server: %w" , err )
88
89
}
89
90
90
91
sshCmdStr :=
91
92
fmt .Sprintf ("ssh %v %v 'chmod +x %v'" ,
92
- o .sshFlags , host , codeServerPath ,
93
+ o .sshFlags , host , codeServerDir ,
93
94
)
94
95
95
96
sshCmd := exec .Command ("sh" , "-l" , "-c" , sshCmdStr )
@@ -104,7 +105,10 @@ func sshCode(host, dir string, o options) error {
104
105
}
105
106
} else {
106
107
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
+ }
108
112
109
113
// Downloads the latest code-server and allows it to be executed.
110
114
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 {
142
146
}
143
147
144
148
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 ),
147
150
"--auth none" ,
148
151
}
149
152
if o .disableTelemetry {
150
153
codeServerFlags = append (codeServerFlags , "--disable-telemetry" )
151
154
}
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 , " " ))
153
156
154
157
flog .Info ("starting code-server..." )
155
158
@@ -544,30 +547,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
544
547
return nil
545
548
}
546
549
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
+
548
569
return fmt .Sprintf (
549
570
`set -euxo pipefail || exit 1
550
571
551
572
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
552
573
pkill -f %v || true
553
- mkdir -p $HOME/.local/share/code-server %v
574
+ mkdir -p %v
554
575
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
571
589
}
572
590
573
591
// ensureDir creates a directory if it does not exist.
0 commit comments