@@ -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"
@@ -81,14 +82,14 @@ func sshCode(host, dir string, o options) error {
81
82
// Upload local code-server or download code-server from CI server.
82
83
if o .uploadCodeServer != "" {
83
84
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 )
85
86
if err != nil {
86
87
return xerrors .Errorf ("failed to upload local code-server binary to remote server: %w" , err )
87
88
}
88
89
89
90
sshCmdStr :=
90
91
fmt .Sprintf ("ssh %v %v 'chmod +x %v'" ,
91
- o .sshFlags , host , codeServerPath ,
92
+ o .sshFlags , host , codeServerDir ,
92
93
)
93
94
94
95
sshCmd := exec .Command ("sh" , "-l" , "-c" , sshCmdStr )
@@ -103,7 +104,10 @@ func sshCode(host, dir string, o options) error {
103
104
}
104
105
} else {
105
106
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
+ }
107
111
108
112
// Downloads the latest code-server and allows it to be executed.
109
113
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 {
140
144
flog .Info ("synced extensions in %s" , time .Since (start ))
141
145
}
142
146
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
+
143
153
flog .Info ("starting code-server..." )
144
154
145
155
flog .Info ("Tunneling remote port %v to %v" , o .remotePort , o .bindAddr )
146
156
147
157
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 ,
150
160
)
151
161
// Starts code-server and forwards the remote port.
152
162
sshCmd := exec .Command ("sh" , "-l" , "-c" , sshCmdStr )
@@ -533,30 +543,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
533
543
return nil
534
544
}
535
545
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
+
537
565
return fmt .Sprintf (
538
566
`set -euxo pipefail || exit 1
539
567
540
568
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1
541
569
pkill -f %v || true
542
- mkdir -p $HOME/.local/share/code-server %v
570
+ mkdir -p %v
543
571
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
560
585
}
561
586
562
587
// ensureDir creates a directory if it does not exist.
0 commit comments