@@ -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,8 +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
-
107
+ dlScript , err := downloadScript (codeServerDir )
108
+ if err != nil {
109
+ return xerrors .New ("failed to download latest code-server" )
110
+ }
108
111
// Downloads the latest code-server and allows it to be executed.
109
112
sshCmdStr := fmt .Sprintf ("ssh %v %v '/usr/bin/env bash -l'" , o .sshFlags , host )
110
113
sshCmd := exec .Command ("sh" , "-l" , "-c" , sshCmdStr )
@@ -139,14 +142,19 @@ func sshCode(host, dir string, o options) error {
139
142
}
140
143
flog .Info ("synced extensions in %s" , time .Since (start ))
141
144
}
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 , " " ))
142
150
143
151
flog .Info ("starting code-server..." )
144
152
145
153
flog .Info ("Tunneling remote port %v to %v" , o .remotePort , o .bindAddr )
146
154
147
155
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 ,
150
158
)
151
159
// Starts code-server and forwards the remote port.
152
160
sshCmd := exec .Command ("sh" , "-l" , "-c" , sshCmdStr )
@@ -533,30 +541,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err
533
541
return nil
534
542
}
535
543
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
+
537
563
return fmt .Sprintf (
538
564
`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
560
583
}
561
584
562
585
// ensureDir creates a directory if it does not exist.
0 commit comments