This repository has been archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
fix #167 use latest version of code-server #182
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package main | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"net" | ||
|
@@ -21,7 +22,7 @@ import ( | |
"golang.org/x/xerrors" | ||
) | ||
|
||
const codeServerPath = "~/.cache/sshcode/sshcode-server" | ||
const codeServerDir = "~/.sshcode-server" | ||
|
||
const ( | ||
sshDirectory = "~/.ssh" | ||
|
@@ -81,14 +82,14 @@ func sshCode(host, dir string, o options) error { | |
// Upload local code-server or download code-server from CI server. | ||
if o.uploadCodeServer != "" { | ||
flog.Info("uploading local code-server binary...") | ||
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerPath) | ||
err = copyCodeServerBinary(o.sshFlags, host, o.uploadCodeServer, codeServerDir) | ||
if err != nil { | ||
return xerrors.Errorf("failed to upload local code-server binary to remote server: %w", err) | ||
} | ||
|
||
sshCmdStr := | ||
fmt.Sprintf("ssh %v %v 'chmod +x %v'", | ||
o.sshFlags, host, codeServerPath, | ||
o.sshFlags, host, codeServerDir, | ||
) | ||
|
||
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr) | ||
|
@@ -103,7 +104,10 @@ func sshCode(host, dir string, o options) error { | |
} | ||
} else { | ||
flog.Info("ensuring code-server is updated...") | ||
dlScript := downloadScript(codeServerPath) | ||
dlScript, err := downloadScript(codeServerDir) | ||
if err != nil { | ||
return xerrors.New("failed to download latest code-server") | ||
} | ||
|
||
// Downloads the latest code-server and allows it to be executed. | ||
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 { | |
flog.Info("synced extensions in %s", time.Since(start)) | ||
} | ||
|
||
codeServerFlags := []string{ | ||
fmt.Sprintf("--bind-addr 127.0.0.1:%v", o.remotePort), | ||
"--auth none", | ||
} | ||
codeServerCmdStr := fmt.Sprintf("%v/code-server %v %v", codeServerDir, dir, strings.Join(codeServerFlags, " ")) | ||
Comment on lines
+147
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is mostly a groundwork for #178 which I will resubmit (with additional changes I've discovered necessary) after this PR get merged. |
||
|
||
flog.Info("starting code-server...") | ||
|
||
flog.Info("Tunneling remote port %v to %v", o.remotePort, o.bindAddr) | ||
|
||
sshCmdStr := | ||
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v '%v %v --host 127.0.0.1 --auth none --port=%v'", | ||
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerPath, dir, o.remotePort, | ||
fmt.Sprintf("ssh -tt -q -L %v:localhost:%v %v %v '%v'", | ||
o.bindAddr, o.remotePort, o.sshFlags, host, codeServerCmdStr, | ||
) | ||
// Starts code-server and forwards the remote port. | ||
sshCmd := exec.Command("sh", "-l", "-c", sshCmdStr) | ||
|
@@ -533,30 +543,45 @@ func rsync(src string, dest string, sshFlags string, excludePaths ...string) err | |
return nil | ||
} | ||
|
||
func downloadScript(codeServerPath string) string { | ||
type release struct { | ||
TagName string `json:"tag_name"` | ||
} | ||
|
||
func downloadScript(codeServerDir string) (string, error) { | ||
url := "https://api.github.com/repos/cdr/code-server/releases/latest" | ||
|
||
req, err := http.Get(url) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer req.Body.Close() | ||
|
||
data := release{} | ||
json.NewDecoder(req.Body).Decode(&data) | ||
|
||
assetName := fmt.Sprintf(`code-server-%v-linux-x86_64`, data.TagName) | ||
downloadURL := fmt.Sprintf(`https://github.com/cdr/code-server/releases/download/%v/%v.tar.gz`, data.TagName, assetName) | ||
|
||
return fmt.Sprintf( | ||
`set -euxo pipefail || exit 1 | ||
|
||
[ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1 | ||
pkill -f %v || true | ||
mkdir -p $HOME/.local/share/code-server %v | ||
mkdir -p %v | ||
cd %v | ||
curlflags="-o latest-linux" | ||
if [ -f latest-linux ]; then | ||
curlflags="$curlflags -z latest-linux" | ||
fi | ||
curl $curlflags https://codesrv-ci.cdr.sh/latest-linux | ||
[ -f %v ] && rm %v | ||
ln latest-linux %v | ||
chmod +x %v`, | ||
codeServerPath, | ||
filepath.ToSlash(filepath.Dir(codeServerPath)), | ||
filepath.ToSlash(filepath.Dir(codeServerPath)), | ||
codeServerPath, | ||
codeServerPath, | ||
codeServerPath, | ||
codeServerPath, | ||
) | ||
if [ ! -d %v ]; then | ||
curl -L %v > release.tar.gz | ||
tar -xzf release.tar.gz | ||
rm release.tar.gz | ||
ln -sf ./%v/code-server code-server | ||
fi`, | ||
codeServerDir, | ||
codeServerDir, | ||
codeServerDir, | ||
assetName, | ||
downloadURL, | ||
assetName, | ||
), nil | ||
} | ||
|
||
// ensureDir creates a directory if it does not exist. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made this directory change so it is similar to how
.vscode-server
stores itself. Also a lot of other tools do the same - nvm, npm, docker, cocoapods and etc.Hopefully this will make sense.