Skip to content

Commit aeae326

Browse files
committed
install.sh: Add remote install support
1 parent 617cd38 commit aeae326

File tree

1 file changed

+99
-29
lines changed

1 file changed

+99
-29
lines changed

install.sh

+99-29
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,45 @@ usage() {
1717
Installs code-server for Linux, macOS and FreeBSD.
1818
It tries to use the system package manager if possible.
1919
After successful installation it explains how to start using code-server.
20+
21+
Pass --start to startup code-server immediately, print the URL it can be
22+
accessed at and the initial password. Then the script will tail code-server's logs.
23+
24+
Pass in user@host to install code-server on user@host over ssh.
25+
Pass --start to forward the code-server port and start it so that
26+
you can immediately access it.
27+
28+
If you rerun the script, code-server will be updated only if necessary.
2029
${not_curl_usage-}
2130
Usage:
2231
23-
$arg0 [--dry-run] [--version X.X.X] [--method detect] [--prefix ~/.local]
32+
$arg0 [--dry-run] [--version X.X.X] [--method detect] [--prefix ~/.local] [--start] [user@host]
2433
2534
--dry-run
2635
Echo the commands for the install process without running them.
36+
2737
--version X.X.X
2838
Install a specific version instead of the latest.
39+
2940
--method [detect | standalone]
3041
Choose the installation method. Defaults to detect.
3142
- detect detects the system package manager and tries to use it.
3243
Full reference on the process is further below.
3344
- standalone installs a standalone release archive into ~/.local
3445
Add ~/.local/bin to your \$PATH to use it.
46+
3547
--prefix <dir>
3648
Sets the prefix used by standalone release archives. Defaults to ~/.local
3749
The release is unarchived into ~/.local/lib/code-server-X.X.X
3850
and the binary symlinked into ~/.local/bin/code-server
3951
To install system wide pass ---prefix=/usr/local
4052
53+
--start
54+
Ensures code-server is running and prints the URL at which it can be accessed.
55+
Also will print code-server's password and when installing over ssh, will forward
56+
the code-server port so that it can be easily accessed locally.
57+
Will block on tailing code-server's logs.
58+
4159
- For Debian, Ubuntu and Raspbian it will install the latest deb package.
4260
- For Fedora, CentOS, RHEL and openSUSE it will install the latest rpm package.
4361
- For Arch Linux it will install the AUR package.
@@ -56,6 +74,8 @@ Usage:
5674
- The npm package builds the native modules on postinstall.
5775
5876
It will cache all downloaded assets into ~/.cache/code-server
77+
With ssh installation, assets will be transferred over via ssh
78+
as needed instead of being downloaded directly on the ssh host.
5979
6080
More installation docs are at https://github.com/cdr/code-server/blob/master/doc/install.md
6181
EOF
@@ -128,15 +148,28 @@ main() {
128148
--version=*)
129149
VERSION="$(parse_arg "$@")"
130150
;;
151+
--)
152+
shift
153+
break
154+
;;
131155
-h | --h | -help | --help)
132156
usage
133157
exit 0
134158
;;
135-
*)
159+
-*)
136160
echoerr "Unknown flag $1"
137161
echoerr "Run with --help to see usage."
138162
exit 1
139163
;;
164+
*)
165+
SSH_ARGS="$1"
166+
if ! sshs true; then
167+
echoerr "could not ssh into remote host"
168+
echoerr "failed: ssh $SSH_ARGS true"
169+
exit 1
170+
fi
171+
echoh "Installing remotely with ssh $SSH_ARGS"
172+
;;
140173
esac
141174

142175
shift
@@ -153,7 +186,7 @@ main() {
153186

154187
OS="$(os)"
155188
if [ ! "$OS" ]; then
156-
echoerr "Unsupported OS $(uname)."
189+
echoerr "Unsupported OS $(sh_f uname)."
157190
exit 1
158191
fi
159192

@@ -162,11 +195,11 @@ main() {
162195
ARCH="$(arch)"
163196
if [ ! "$ARCH" ]; then
164197
if [ "$METHOD" = standalone ]; then
165-
echoerr "No precompiled releases for $(uname -m)."
198+
echoerr "No precompiled releases for $(sh_f uname -m)."
166199
echoerr 'Please rerun without the "--method standalone" flag to install from npm.'
167200
exit 1
168201
fi
169-
echoh "No precompiled releases for $(uname -m)."
202+
echoh "No precompiled releases for $(sh_f uname -m)."
170203
install_npm
171204
return
172205
fi
@@ -242,20 +275,25 @@ parse_arg() {
242275
}
243276

244277
fetch() {
245-
URL="$1"
246-
FILE="$2"
278+
RHOME="$(sh_f printenv HOME)"
247279

248-
if [ -e "$FILE" ]; then
280+
URL="$(echo "$1" | sed "s#$HOME#$RHOME#g")"
281+
FILE="$(echo "$2" | sed "s#$HOME#$RHOME#g")"
282+
283+
if sh_f [ -e "$FILE" ]; then
249284
echoh "+ Reusing $FILE"
250285
return
251286
fi
252287

253288
sh_c mkdir -p "$CACHE_DIR"
254-
sh_c curl \
289+
SSH_ARGS= sh_c curl \
255290
-#fL \
256291
-o "$FILE.incomplete" \
257292
-C - \
258293
"$URL"
294+
if [ "${SSH_ARGS}" ]; then
295+
sh_c cat '>' "$FILE.incomplete"
296+
fi
259297
sh_c mv "$FILE.incomplete" "$FILE"
260298
}
261299

@@ -319,11 +357,11 @@ install_standalone() {
319357
"$CACHE_DIR/code-server-$VERSION-$OS-$ARCH.tar.gz"
320358

321359
sh_c="sh_c"
322-
if [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
360+
if sh_f [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
323361
sh_c="sudo_sh_c"
324362
fi
325363

326-
if [ -e "$STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION" ]; then
364+
if sh_f [ -e "$STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION" ]; then
327365
echoh
328366
echoh "code-server-$VERSION is already installed at $STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION"
329367
echoh "Remove it to reinstall."
@@ -341,7 +379,7 @@ install_standalone() {
341379
install_npm() {
342380
if command_exists yarn; then
343381
sh_c="sh_c"
344-
if [ ! -w "$(yarn global bin)" ]; then
382+
if sh_f [ ! -w "$(sh_f yarn global bin)" ]; then
345383
sh_c="sudo_sh_c"
346384
fi
347385
echoh "Installing with yarn."
@@ -350,7 +388,7 @@ install_npm() {
350388
return
351389
elif command_exists npm; then
352390
sh_c="sh_c"
353-
if [ ! -w "$(npm config get prefix)" ]; then
391+
if sh_f [ ! -w "$(sh_f npm config get prefix)" ]; then
354392
sh_c="sudo_sh_c"
355393
fi
356394
echoh "Installing with npm."
@@ -366,7 +404,7 @@ install_npm() {
366404
}
367405

368406
os() {
369-
case "$(uname)" in
407+
case "$(sh_f uname)" in
370408
Linux)
371409
echo linux
372410
;;
@@ -396,9 +434,9 @@ distro() {
396434
return
397435
fi
398436

399-
if [ -f /etc/os-release ]; then
437+
if sh_f [ -f /etc/os-release ]; then
400438
(
401-
. /etc/os-release
439+
ID="$(sh_f '. /etc/os-release && echo "$ID"')"
402440
case "$ID" in opensuse-*)
403441
# opensuse's ID's look like opensuse-leap and opensuse-tumbleweed.
404442
echo "opensuse"
@@ -414,25 +452,22 @@ distro() {
414452

415453
# os_name prints a pretty human readable name for the OS/Distro.
416454
distro_name() {
417-
if [ "$(uname)" = "Darwin" ]; then
418-
echo "macOS v$(sw_vers -productVersion)"
455+
if [ "$(sh_f uname)" = "Darwin" ]; then
456+
echo "macOS v$(sh_f sw_vers -productVersion)"
419457
return
420458
fi
421459

422-
if [ -f /etc/os-release ]; then
423-
(
424-
. /etc/os-release
425-
echo "$PRETTY_NAME"
426-
)
460+
if sh_f [ -f /etc/os-release ]; then
461+
sh_f '. /etc/os-release && echo "$PRETTY_NAME"'
427462
return
428463
fi
429464

430465
# Prints something like: Linux 4.19.0-9-amd64
431-
uname -sr
466+
sh_f uname -sr
432467
}
433468

434469
arch() {
435-
case "$(uname -m)" in
470+
case "$(sh_f uname -m)" in
436471
aarch64)
437472
echo arm64
438473
;;
@@ -446,18 +481,53 @@ arch() {
446481
}
447482

448483
command_exists() {
449-
command -v "$@" > /dev/null 2>&1
484+
sh_f command -v "$@" > /dev/null
450485
}
451486

452487
sh_c() {
453488
echoh "+ $*"
454489
if [ ! "${DRY_RUN-}" ]; then
490+
sh_f "$@"
491+
fi
492+
}
493+
494+
sshs() {
495+
mkdir -p ~/.ssh/sockets
496+
chmod 700 ~/.ssh
497+
498+
set -- \
499+
-oControlPath=~/.ssh/sockets/%r@%n.sock \
500+
-oControlMaster=auto \
501+
-oControlPersist=yes \
502+
-oConnectTimeout=5 \
503+
$SSH_ARGS \
504+
"$@"
505+
506+
if ssh "$@"; then
507+
return
508+
fi
509+
510+
if ssh -O exit "$@"; then
511+
# Control master has been deleted so we ought to try once more.
512+
if ssh "$@"; then
513+
return
514+
fi
515+
fi
516+
517+
return 1
518+
}
519+
520+
# Always runs.
521+
sh_f() {
522+
if [ "${SSH_ARGS-}" ]; then
523+
sshs "$*"
524+
else
455525
sh -c "$*"
456526
fi
457527
}
458528

459529
sudo_sh_c() {
460-
if [ "$(id -u)" = 0 ]; then
530+
if [ "$(sh_f id -u)" = 0 ]; then
461531
sh_c "$@"
462532
elif command_exists sudo; then
463533
sh_c "sudo $*"
@@ -473,8 +543,8 @@ sudo_sh_c() {
473543
}
474544

475545
echo_cache_dir() {
476-
if [ "${XDG_CACHE_HOME-}" ]; then
477-
echo "$XDG_CACHE_HOME/code-server"
546+
if [ "$(sh_f printenv XDG_CACHE_HOME)" ]; then
547+
echo "$(sh_f printenv XDG_CACHE_HOME)/code-server"
478548
elif [ "${HOME-}" ]; then
479549
echo "$HOME/.cache/code-server"
480550
else

0 commit comments

Comments
 (0)