Skip to content

Commit 407e824

Browse files
committed
install.sh: Add remote install support
Closes coder/sshcode#185 and pretty much archives that repository.
1 parent e237589 commit 407e824

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

install.sh

+52-33
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -eu
77
usage() {
88
arg0="$0"
99
if [ "$0" = sh ]; then
10-
arg0="curl -fsSL https://code-server.dev/install.sh | sh -s --"
10+
arg0="curl -fsSL https://code-server.dev/install.sh | sh -s -- [user@host]"
1111
else
1212
not_curl_usage="The latest script is available at https://code-server.dev/install.sh
1313
"
@@ -20,7 +20,7 @@ After successful installation it explains how to start using code-server.
2020
${not_curl_usage-}
2121
Usage:
2222
23-
$arg0 [--dry-run] [--version X.X.X] [--method detect] [--prefix ~/.local]
23+
$arg0 [--dry-run] [--version X.X.X] [--method detect] [--prefix ~/.local] [user@host]
2424
2525
--dry-run
2626
Echo the commands for the install process without running them.
@@ -128,11 +128,18 @@ main() {
128128
--version=*)
129129
VERSION="$(parse_arg "$@")"
130130
;;
131+
--)
132+
shift
133+
break
134+
;;
131135
-h | --h | -help | --help)
132136
usage
133137
exit 0
134138
;;
135139
*)
140+
SSH_ARGS="$1"
141+
;;
142+
-*)
136143
echoerr "Unknown flag $1"
137144
echoerr "Run with --help to see usage."
138145
exit 1
@@ -149,11 +156,12 @@ main() {
149156
echoerr "Run with --help to see usage."
150157
exit 1
151158
fi
152-
STANDALONE_INSTALL_PREFIX="${STANDALONE_INSTALL_PREFIX-$HOME/.local}"
159+
RHOME="$(sh_f printenv HOME)"
160+
STANDALONE_INSTALL_PREFIX="${STANDALONE_INSTALL_PREFIX-$RHOME/.local}"
153161

154162
OS="$(os)"
155163
if [ ! "$OS" ]; then
156-
echoerr "Unsupported OS $(uname)."
164+
echoerr "Unsupported OS $(sh_f uname)."
157165
exit 1
158166
fi
159167

@@ -162,11 +170,11 @@ main() {
162170
ARCH="$(arch)"
163171
if [ ! "$ARCH" ]; then
164172
if [ "$METHOD" = standalone ]; then
165-
echoerr "No precompiled releases for $(uname -m)."
173+
echoerr "No precompiled releases for $(sh_f uname -m)."
166174
echoerr 'Please rerun without the "--method standalone" flag to install from npm.'
167175
exit 1
168176
fi
169-
echoh "No precompiled releases for $(uname -m)."
177+
echoh "No precompiled releases for $(sh_f uname -m)."
170178
install_npm
171179
return
172180
fi
@@ -245,7 +253,7 @@ fetch() {
245253
URL="$1"
246254
FILE="$2"
247255

248-
if [ -e "$FILE" ]; then
256+
if sh_f [ -e "$FILE" ]; then
249257
echoh "+ Reusing $FILE"
250258
return
251259
fi
@@ -303,7 +311,7 @@ install_aur() {
303311
sh_c mkdir -p "$CACHE_DIR/code-server-aur"
304312
sh_c "curl -#fsSL https://aur.archlinux.org/cgit/aur.git/snapshot/code-server.tar.gz | tar -xzC $CACHE_DIR/code-server-aur --strip-components 1"
305313
echo "+ cd $CACHE_DIR/code-server-aur"
306-
if [ ! "${DRY_RUN-}" ]; then
314+
if sh_f [ ! "${DRY_RUN-}" ]; then
307315
cd "$CACHE_DIR/code-server-aur"
308316
fi
309317
sh_c makepkg -si
@@ -319,11 +327,11 @@ install_standalone() {
319327
"$CACHE_DIR/code-server-$VERSION-$OS-$ARCH.tar.gz"
320328

321329
sh_c="sh_c"
322-
if [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
330+
if sh-f [ ! -w "$STANDALONE_INSTALL_PREFIX" ]; then
323331
sh_c="sudo_sh_c"
324332
fi
325333

326-
if [ -e "$STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION" ]; then
334+
if sh_f [ -e "$STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION" ]; then
327335
echoh
328336
echoh "code-server-$VERSION is already installed at $STANDALONE_INSTALL_PREFIX/lib/code-server-$VERSION"
329337
echoh "Remove it to reinstall."
@@ -341,7 +349,7 @@ install_standalone() {
341349
install_npm() {
342350
if command_exists yarn; then
343351
sh_c="sh_c"
344-
if [ ! -w "$(yarn global bin)" ]; then
352+
if sh_f [ ! -w "$(sh_f yarn global bin)" ]; then
345353
sh_c="sudo_sh_c"
346354
fi
347355
echoh "Installing with yarn."
@@ -350,7 +358,7 @@ install_npm() {
350358
return
351359
elif command_exists npm; then
352360
sh_c="sh_c"
353-
if [ ! -w "$(npm config get prefix)" ]; then
361+
if sh_f [ ! -w "$(sh_f npm config get prefix)" ]; then
354362
sh_c="sudo_sh_c"
355363
fi
356364
echoh "Installing with npm."
@@ -366,7 +374,7 @@ install_npm() {
366374
}
367375

368376
os() {
369-
case "$(uname)" in
377+
case "$(sh_f uname)" in
370378
Linux)
371379
echo linux
372380
;;
@@ -396,9 +404,9 @@ distro() {
396404
return
397405
fi
398406

399-
if [ -f /etc/os-release ]; then
407+
if sh_f [ -f /etc/os-release ]; then
400408
(
401-
. /etc/os-release
409+
ID="$(sh_f '. /etc/os-release && echo "$ID"')"
402410
case "$ID" in opensuse-*)
403411
# opensuse's ID's look like opensuse-leap and opensuse-tumbleweed.
404412
echo "opensuse"
@@ -414,25 +422,22 @@ distro() {
414422

415423
# os_name prints a pretty human readable name for the OS/Distro.
416424
distro_name() {
417-
if [ "$(uname)" = "Darwin" ]; then
418-
echo "macOS v$(sw_vers -productVersion)"
425+
if [ "$(sh_f uname)" = "Darwin" ]; then
426+
echo "macOS v$(sh_f sw_vers -productVersion)"
419427
return
420428
fi
421429

422-
if [ -f /etc/os-release ]; then
423-
(
424-
. /etc/os-release
425-
echo "$PRETTY_NAME"
426-
)
430+
if sh_f [ -f /etc/os-release ]; then
431+
sh_f '. /etc/os-release && echo "$PRETTY_NAME"'
427432
return
428433
fi
429434

430435
# Prints something like: Linux 4.19.0-9-amd64
431-
uname -sr
436+
sh_f uname -sr
432437
}
433438

434439
arch() {
435-
case "$(uname -m)" in
440+
case "$(sh_f uname -m)" in
436441
aarch64)
437442
echo arm64
438443
;;
@@ -446,18 +451,32 @@ arch() {
446451
}
447452

448453
command_exists() {
449-
command -v "$@" > /dev/null 2>&1
454+
sh_f command -v "$@" > /dev/null
450455
}
451456

452457
sh_c() {
453458
echoh "+ $*"
454459
if [ ! "${DRY_RUN-}" ]; then
460+
sh_f "$@"
461+
fi
462+
}
463+
464+
# Always runs.
465+
sh_f() {
466+
if [ "$SSH_ARGS" ]; then
467+
mkdir -p ~/.ssh/sockets
468+
ssh \
469+
-oControlPath=~/.ssh/sockets/%r@%n.sock \
470+
-oControlMaster=auto \
471+
-oControlPersist=yes \
472+
$SSH_ARGS "$*"
473+
else
455474
sh -c "$*"
456475
fi
457476
}
458477

459478
sudo_sh_c() {
460-
if [ "$(id -u)" = 0 ]; then
479+
if [ "$(sh_f id -u)" = 0 ]; then
461480
sh_c "$@"
462481
elif command_exists sudo; then
463482
sh_c "sudo $*"
@@ -473,10 +492,10 @@ sudo_sh_c() {
473492
}
474493

475494
echo_cache_dir() {
476-
if [ "${XDG_CACHE_HOME-}" ]; then
477-
echo "$XDG_CACHE_HOME/code-server"
478-
elif [ "${HOME-}" ]; then
479-
echo "$HOME/.cache/code-server"
495+
if [ "$(sh_f printenv XDG_CACHE_HOME)" ]; then
496+
echo "$(sh_f printenv XDG_CACHE_HOME)/code-server"
497+
elif [ "${RHOME-}" ]; then
498+
echo "$RHOME/.cache/code-server"
480499
else
481500
echo "/tmp/code-server-cache"
482501
fi
@@ -494,10 +513,10 @@ echoerr() {
494513
echoh "$@" >&2
495514
}
496515

497-
# humanpath replaces all occurances of " $HOME" with " ~"
498-
# and all occurances of '"$HOME' with the literal '"$HOME'.
516+
# humanpath replaces all occurances of " $RHOME" with " ~"
517+
# and all occurances of '"$RHOME' with the literal '"$RHOME'.
499518
humanpath() {
500-
sed "s# $HOME# ~#g; s#\"$HOME#\"\$HOME#g"
519+
sed "s# $RHOME# ~#g; s#\"$RHOME#\"\$RHOME#g"
501520
}
502521

503522
main "$@"

0 commit comments

Comments
 (0)