code-server-2/install.sh

488 lines
11 KiB
Bash
Raw Normal View History

2020-05-20 08:50:16 +00:00
#!/bin/sh
set -eu
2020-05-22 19:38:03 +00:00
# code-server's automatic install script.
# See https://github.com/cdr/code-server/blob/master/doc/install.md
2020-05-20 08:50:16 +00:00
usage() {
2020-05-22 19:38:03 +00:00
arg0="$0"
if [ "$0" = sh ]; then
2020-05-22 19:38:03 +00:00
arg0="curl -fsSL https://code-server.dev/install.sh | sh -s --"
else
2020-05-22 19:38:03 +00:00
curl_usage="The latest script is available at https://code-server.dev/install.sh
"
fi
2020-05-22 19:38:03 +00:00
cat << EOF
2020-05-22 19:38:03 +00:00
Installs code-server for Linux and macOS.
2020-05-22 02:16:16 +00:00
It tries to use the system package manager if possible.
After successful installation it explains how to start using code-server.
2020-05-22 03:53:02 +00:00
${curl_usage-}
Usage:
2020-05-20 08:50:16 +00:00
2020-05-22 19:38:03 +00:00
$arg0 [--dry-run] [--version X.X.X] [--method detect] [--prefix ~/.local]
--dry-run
Echo the commands for the install process without running them.
--version X.X.X
Install a specific version instead of the latest.
--method [detect | archive]
Choose the installation method. Defaults to detect.
- detect detects the system package manager and tries to use it.
Full reference on the process is further below.
- archive installs a binary release archive into ~/.local
2020-05-22 19:38:03 +00:00
Add ~/.local/bin to your \$PATH to use it.
--prefix <dir>
Sets the prefix used by binary release archives. Defaults to ~/.local
2020-05-22 19:38:03 +00:00
The release is unarchived into ~/.local/lib/code-server-X.X.X
and the binary symlinked into ~/.local/bin/code-server
To install system wide pass ---prefix=/usr/local
2020-05-22 03:53:02 +00:00
2020-05-22 02:16:16 +00:00
- For Debian, Ubuntu and Raspbian it will install the latest deb package.
- For Fedora, CentOS, RHEL and openSUSE it will install the latest rpm package.
- For Arch Linux it will install the AUR package.
- For any unrecognized Linux operating system it will install the latest binary
2020-05-22 02:16:16 +00:00
release into ~/.local
2020-05-20 08:50:16 +00:00
- For macOS it will install the Homebrew package.
- If Homebrew is not installed it will install the latest binary release
2020-05-22 02:16:16 +00:00
into ~/.local
2020-05-20 08:50:16 +00:00
2020-05-22 02:16:16 +00:00
- If ran on an architecture with no binary releases, it will install the
npm package with yarn or npm.
- We only have binary releases for amd64 and arm64 presently.
2020-05-20 08:50:16 +00:00
2020-05-22 19:38:03 +00:00
It will cache all downloaded assets into ~/.cache/code-server
2020-05-22 03:53:02 +00:00
More installation docs are at https://github.com/cdr/code-server/blob/master/doc/install.md
2020-05-20 08:50:16 +00:00
EOF
}
echo_latest_version() {
# https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c#gistcomment-2758860
version="$(curl -fsSLI -o /dev/null -w "%{url_effective}" https://github.com/cdr/code-server/releases/latest)"
version="${version#https://github.com/cdr/code-server/releases/tag/v}"
echo "$version"
2020-05-20 08:50:16 +00:00
}
echo_archive_postinstall() {
2020-05-20 08:50:16 +00:00
echo
cat << EOF
Binary release has been installed into $ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION
2020-05-20 08:50:16 +00:00
Please extend your path to use code-server:
2020-05-22 19:38:03 +00:00
PATH="$ARCHIVE_INSTALL_PREFIX/bin:\$PATH"
2020-05-20 08:50:16 +00:00
Then you can run:
code-server
EOF
}
echo_systemd_postinstall() {
echo
cat << EOF
2020-05-20 08:50:16 +00:00
To have systemd start code-server now and restart on boot:
systemctl --user enable --now code-server
Or, if you don't want/need a background service you can run:
code-server
EOF
}
main() {
if [ "${TRACE-}" ]; then
set -x
fi
unset \
DRY_RUN \
2020-05-22 19:38:03 +00:00
METHOD \
ARCHIVE_INSTALL_PREFIX \
VERSION \
OPTIONAL
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run)
DRY_RUN=1
;;
2020-05-22 19:38:03 +00:00
--method)
METHOD="$(parse_arg "$@")"
shift
;;
--method=*)
METHOD="$(parse_arg "$@")"
;;
2020-05-22 19:38:03 +00:00
--prefix)
ARCHIVE_INSTALL_PREFIX="$(parse_arg "$@")"
shift
;;
--prefix=*)
ARCHIVE_INSTALL_PREFIX="$(parse_arg "$@")"
;;
--version)
VERSION="$(parse_arg "$@")"
shift
;;
--version=*)
VERSION="$(parse_arg "$@")"
;;
2020-05-22 19:38:03 +00:00
-h | --h | -help | --help)
usage
exit 0
;;
*)
echoerr "Unknown flag $1"
echoerr "Run with --help to see usage."
exit 1
;;
2020-05-20 08:50:16 +00:00
esac
shift
2020-05-20 08:50:16 +00:00
done
VERSION="${VERSION-$(echo_latest_version)}"
2020-05-22 19:38:03 +00:00
METHOD="${METHOD-detect}"
if [ "$METHOD" != detect ] && [ "$METHOD" != archive ]; then
echoerr "Unknown install method \"$METHOD\""
echoerr "Run with --help to see usage."
exit 1
fi
ARCHIVE_INSTALL_PREFIX="${ARCHIVE_INSTALL_PREFIX-$HOME/.local}"
2020-05-20 08:50:16 +00:00
OS="$(os)"
if [ ! "$OS" ]; then
echoerr "Unsupported OS $(uname)."
2020-05-20 08:50:16 +00:00
exit 1
fi
distro_name
ARCH="$(arch)"
if [ ! "$ARCH" ]; then
2020-05-22 19:38:03 +00:00
if [ "$METHOD" = archive ]; then
echoerr "No binary releases available for the architecture $(uname -m)."
2020-05-22 19:38:03 +00:00
echoerr 'Please rerun without the "--method archive" flag to install from npm.'
2020-05-20 08:50:16 +00:00
exit 1
fi
2020-05-22 19:38:03 +00:00
echo "No precompiled releases for $(uname -m)."
2020-05-20 08:50:16 +00:00
install_npm
2020-05-22 03:53:02 +00:00
return
2020-05-20 08:50:16 +00:00
fi
CACHE_DIR="$(echo_cache_dir)"
2020-05-20 08:50:16 +00:00
mkdir -p "$CACHE_DIR"
2020-05-22 19:38:03 +00:00
if [ "$METHOD" = archive ]; then
install_archive
2020-05-20 08:50:16 +00:00
return
fi
case "$(distro)" in
macos)
install_macos
;;
ubuntu | debian | raspbian)
install_deb
;;
centos | fedora | rhel | opensuse)
install_rpm
;;
arch)
2020-05-22 19:38:03 +00:00
install_aur
2020-05-20 08:50:16 +00:00
;;
*)
2020-05-22 19:38:03 +00:00
echo "Unsupported package manager."
install_archive
2020-05-20 08:50:16 +00:00
;;
esac
}
parse_arg() {
case "$1" in
*=*)
2020-05-27 19:48:03 +00:00
# Remove everything after first equal sign.
opt="${1%%=*}"
# Remove everything before first equal sign.
optarg="${1#*=}"
if [ ! "$optarg" ] && [ ! "${OPTIONAL-}" ]; then
echoerr "$opt requires an argument"
echoerr "Run with --help to see usage."
exit 1
fi
echo "$optarg"
2020-05-20 08:50:16 +00:00
return
;;
esac
2020-05-20 08:50:16 +00:00
case "${2-}" in
"" | -*)
if [ ! "${OPTIONAL-}" ]; then
echoerr "$1 requires an argument"
echoerr "Run with --help to see usage."
exit 1
fi
;;
*)
echo "$2"
return
;;
esac
2020-05-20 08:50:16 +00:00
}
fetch() {
URL="$1"
FILE="$2"
if [ -e "$FILE" ]; then
2020-05-27 19:48:03 +00:00
echo "+ Reusing $CACHE_DIR/${URL##*/}"
2020-05-20 08:50:16 +00:00
return
fi
sh_c curl \
-#fL \
2020-05-22 19:38:03 +00:00
-o "$FILE.incomplete" \
2020-05-20 08:50:16 +00:00
-C - \
"$URL"
2020-05-22 19:38:03 +00:00
sh_c mv "$FILE.incomplete" "$FILE"
2020-05-20 08:50:16 +00:00
}
install_macos() {
if command_exists brew; then
echo "Installing from Homebrew."
2020-05-22 19:38:03 +00:00
echo
sh_c brew install code-server
return
fi
2020-05-22 19:38:03 +00:00
echo "Homebrew not installed."
2020-05-22 19:38:03 +00:00
install_archive
}
2020-05-20 08:50:16 +00:00
install_deb() {
echo "Installing v$VERSION deb package from GitHub releases."
2020-05-22 19:38:03 +00:00
echo
2020-05-20 08:50:16 +00:00
2020-05-27 03:15:44 +00:00
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server_${VERSION}_$ARCH.deb" \
"$CACHE_DIR/code-server_${VERSION}_$ARCH.deb"
2020-05-20 08:50:16 +00:00
sudo_sh_c dpkg -i "$CACHE_DIR/code-server_${VERSION}_$ARCH.deb"
echo_systemd_postinstall
}
install_rpm() {
echo "Installing v$VERSION rpm package from GitHub releases."
2020-05-22 19:38:03 +00:00
echo
2020-05-20 08:50:16 +00:00
2020-05-27 03:15:44 +00:00
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-$ARCH.rpm" \
"$CACHE_DIR/code-server-$VERSION-$ARCH.rpm"
2020-05-20 08:50:16 +00:00
sudo_sh_c rpm -i "$CACHE_DIR/code-server-$VERSION-$ARCH.rpm"
echo_systemd_postinstall
}
install_aur() {
2020-05-20 08:50:16 +00:00
echo "Installing from the AUR."
2020-05-22 19:38:03 +00:00
echo
2020-05-20 08:50:16 +00:00
tmp_dir="$(mktemp -d)"
2020-05-22 19:38:03 +00:00
2020-05-27 19:48:03 +00:00
echo "+ Downloading PKGBUILD into $tmp_dir from https://aur.archlinux.org/cgit/aur.git/snapshot/code-server.tar.gz"
curl -fsSL https://aur.archlinux.org/cgit/aur.git/snapshot/code-server.tar.gz | tar -xzC "$tmp_dir"
VERSION="$(. "$tmp_dir/code-server/PKGBUILD" && echo "$pkgver")"
rm -R "$tmp_dir"
2020-05-22 19:38:03 +00:00
2020-05-27 19:48:03 +00:00
mkdir -p "$CACHE_DIR/code-server-$VERSION-aur"
sh_c cp -a "$tmp_dir/code-server/*" "$CACHE_DIR/code-server-$VERSION-aur"
echo "+ Installing $CACHE_DIR/code-server-$VERSION-aur"
cd "$CACHE_DIR/code-server-$VERSION-aur"
sh_c makepkg -si
2020-05-20 08:50:16 +00:00
echo_systemd_postinstall
}
2020-05-22 19:38:03 +00:00
install_archive() {
echo "Installing binary release archive v$VERSION"
2020-05-22 19:38:03 +00:00
echo
2020-05-20 08:50:16 +00:00
2020-05-22 19:38:03 +00:00
fetch "https://github.com/cdr/code-server/releases/download/v$VERSION/code-server-$VERSION-$OS-$ARCH.tar.gz" \
"$CACHE_DIR/code-server-$VERSION-$OS-$ARCH.tar.gz"
2020-05-20 08:50:16 +00:00
sh_c="sh_c"
2020-05-22 19:38:03 +00:00
if [ ! -w "$ARCHIVE_INSTALL_PREFIX" ]; then
2020-05-20 08:50:16 +00:00
sh_c="sudo_sh_c"
fi
2020-05-22 19:38:03 +00:00
if [ -e "$ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION" ]; then
echo
2020-05-22 19:38:03 +00:00
echo "code-server-$VERSION is already installed at $ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION"
echo "Remove it to reinstall."
exit 0
fi
2020-05-27 19:48:03 +00:00
"$sh_c" mkdir -p "$ARCHIVE_INSTALL_PREFIX/lib" "$ARCHIVE_INSTALL_PREFIX/bin"
2020-05-22 19:38:03 +00:00
"$sh_c" tar -C "$ARCHIVE_INSTALL_PREFIX/lib" -xzf "$CACHE_DIR/code-server-$VERSION-$OS-$ARCH.tar.gz"
"$sh_c" mv -f "$ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION-$OS-$ARCH" "$ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION"
"$sh_c" ln -fs "$ARCHIVE_INSTALL_PREFIX/lib/code-server-$VERSION/bin/code-server" "$ARCHIVE_INSTALL_PREFIX/bin/code-server"
2020-05-20 08:50:16 +00:00
echo_archive_postinstall
2020-05-20 08:50:16 +00:00
}
install_npm() {
if command_exists yarn; then
2020-05-27 19:48:03 +00:00
sh_c="sh_c"
if [ ! -w "$(yarn global bin)" ]; then
sh_c="sudo_sh_c"
fi
2020-05-20 08:50:16 +00:00
echo "Installing with yarn."
2020-05-22 19:38:03 +00:00
echo
2020-05-27 19:48:03 +00:00
"$sh_c" yarn global add code-server --unsafe-perm
2020-05-20 08:50:16 +00:00
return
elif command_exists npm; then
2020-05-27 19:48:03 +00:00
sh_c="sh_c"
if [ ! -w "$(npm config get prefix)" ]; then
sh_c="sudo_sh_c"
fi
2020-05-20 08:50:16 +00:00
echo "Installing with npm."
2020-05-22 19:38:03 +00:00
echo
2020-05-27 19:48:03 +00:00
"$sh_c" npm install -g code-server --unsafe-perm
2020-05-20 08:50:16 +00:00
return
fi
2020-05-22 19:38:03 +00:00
echo
echoerr "Please install npm or yarn to install code-server!"
2020-05-22 19:38:03 +00:00
echoerr "You will need at least node v12 and a few C dependencies."
echoerr "See the docs https://github.com/cdr/code-server#yarn-npm"
2020-05-20 08:50:16 +00:00
exit 1
}
os() {
case "$(uname)" in
Linux)
echo linux
;;
Darwin)
echo macos
;;
esac
}
# distro prints the detected operating system including linux distros.
#
# Example outputs:
# - macos
# - debian, ubuntu, raspbian
# - centos, fedora, rhel, opensuse
# - alpine
# - arch
#
# Inspired by https://github.com/docker/docker-install/blob/26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c/install.sh#L111-L120.
distro() {
if [ "$(uname)" = "Darwin" ]; then
echo "macos"
return
fi
if [ -f /etc/os-release ]; then
(
. /etc/os-release
case "$ID" in opensuse-*)
# opensuse's ID's look like opensuse-leap and opensuse-tumbleweed.
echo "opensuse"
return
;;
esac
echo "$ID"
)
2020-05-20 08:50:16 +00:00
return
fi
}
# os_name prints a pretty human readable name for the OS/Distro.
distro_name() {
if [ "$(uname)" = "Darwin" ]; then
echo "macOS v$(sw_vers -productVersion)"
return
fi
if [ -f /etc/os-release ]; then
2020-05-20 08:50:16 +00:00
(
. /etc/os-release
echo "$PRETTY_NAME"
)
return
2020-05-20 08:50:16 +00:00
fi
# Prints something like: Linux 4.19.0-9-amd64
uname -sr
}
arch() {
case "$(uname -m)" in
aarch64)
echo arm64
;;
x86_64)
echo amd64
;;
esac
}
command_exists() {
command -v "$@" > /dev/null 2>&1
2020-05-20 08:50:16 +00:00
}
sh_c() {
2020-05-27 19:48:03 +00:00
echo "+ $*"
2020-05-20 08:50:16 +00:00
if [ ! "${DRY_RUN-}" ]; then
sh -c "$*"
fi
}
sudo_sh_c() {
if [ "$(id -u)" = 0 ]; then
sh_c "$@"
elif command_exists sudo; then
sh_c "sudo $*"
elif command_exists su; then
sh_c "su -c '$*'"
else
echo
echoerr "This script needs to run the following command as root."
echoerr " $*"
2020-05-22 19:38:03 +00:00
echoerr "Please install sudo or su."
2020-05-20 08:50:16 +00:00
exit 1
fi
}
echo_cache_dir() {
2020-05-20 08:50:16 +00:00
if [ "${XDG_CACHE_HOME-}" ]; then
echo "$XDG_CACHE_HOME/code-server"
elif [ "${HOME-}" ]; then
echo "$HOME/.cache/code-server"
else
echo "/tmp/code-server-cache"
fi
}
2020-05-27 19:48:03 +00:00
echo() {
builtin echo "$@" | humanpath
}
cat() {
humanpath
}
echoerr() {
echo "$@" >&2
}
2020-05-27 19:48:03 +00:00
# humanpath replaces all occurances of $HOME with ~
humanpath() {
sed "s#$HOME#~#g"
}
2020-05-20 08:50:16 +00:00
main "$@"