Skip to content

Instantly share code, notes, and snippets.

@levid0s
Last active December 22, 2024 06:01
Show Gist options
  • Save levid0s/2238a5758e4e18ac5671bdd702c57707 to your computer and use it in GitHub Desktop.
Save levid0s/2238a5758e4e18ac5671bdd702c57707 to your computer and use it in GitHub Desktop.
Download VS Code Server on OpenWRT x64
#!/bin/sh
# Copyright 2024 Khalifah K. Shabazz
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the “Software”),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# Original script: https://github.com/b01/dl-vscode-server/blob/f5c49fccdd156f429cf272ceb2a85cdad2cd5ad6/download-vs-code.sh
# This script: https://gist.github.com/levid0s/2238a5758e4e18ac5671bdd702c57707
set -o errexit -o pipefail
log() {
echo -e "$@" >&2
}
run() {
local verbose="${verbose-0}"
[ "$verbose" = 1 ] && log "EXEC: $@"
"$@"
}
# Get the latest VS Code commit sha.
get_latest_commit_sha() {
# "win32" "x64" "${BUILD}"
local platform="${1-win32}" # win32
local arch="${2-x64}" # x64
local bin_type="${3-stable}" # stable
local commit_id
# Grab the first commit SHA since as this script assumes it will be the latest.
local url="https://update.code.visualstudio.com/api/commits/${bin_type}/${platform}-${arch}"
log "\nFetching commit ids: $url"
commit_id=$(run curl -sf "$url" | grep -oE '[0-9a-f]{40}' | head -n 1)
log "Commit id: $commit_id"
# These work:
# https://update.code.visualstudio.com/api/commits/stable/win32-x64
# https://update.code.visualstudio.com/api/commits/stable/linux-x64
# https://update.code.visualstudio.com/api/commits/insider/linux-x64
# these do not work:
# https://update.code.visualstudio.com/api/commits/stable/darwin-x64
# https://update.code.visualstudio.com/api/commits/stable/linux-alpine
echo "$commit_id"
}
install_server() {
log "\nCreating directories:"
# Make the directories where the VS Code will search. There may be others not
# listed here.
# NOTE: VS Code will runas the logged in user, so ensure they have
# read/write to the following directories
mkdir -vp "$HOME/.vscode-server/bin/$commit_sha"
# VSCode Requirements for pre-installing extensions
mkdir -vp "$HOME/.vscode-server/extensions"
# found this in the VSCode remote extension output when connecting to an existing container
mkdir -vp "$HOME/.vscode-server/extensionsCache"
# This should handle installs for https://vscode.dev/
mkdir -vp "$HOME/.vscode/cli/servers/Stable-$commit_sha"
mkdir -vp "$HOME/.vscode-server/cli/servers/Stable-$commit_sha"
log 'OK.'
# Extract the tarball to the right location.
local src="/tmp/$archive"
local dst="$HOME/.vscode-server/bin/$commit_sha"
log "\nExtracting: $src -> $dst"
run tar -xz -C "$dst" --strip-components=1 --no-same-owner -f "$src"
log 'OK.'
# Add symlinks
log "\nProvisioning symlinks:"
ln -fvs "$HOME/.vscode-server/bin/${commit_sha}" "$HOME/.vscode-server/bin/default_version"
ln -fvs "$HOME/.vscode-server/bin/${commit_sha}" "$HOME/.vscode/cli/servers/Stable-${commit_sha}/server"
ln -fvs "$HOME/.vscode-server/bin/${commit_sha}" "$HOME/.vscode-server/cli/servers/Stable-${commit_sha}/server"
ln -fvs "$HOME/.vscode-server/bin/${commit_sha}/bin/code-server" $HOME/code-server
log "OK."
}
tidy_old_downloads() {
local keep_commit_sha="$1"
[ -z "$keep_commit_sha" ] && log "ERROR: commit ID not provided." && return 1
log "Deleting old downloads, EXCEPT with commit id: $keep_commit_sha"
run find "$HOME/.vscode-server/cli/servers" -type d -maxdepth 1 -regex '.*[0-9a-fA-F]\{40\}.*' ! -regex ".*$keep_commit_sha.*" | xargs -rt rm -rf
run find "$HOME/.vscode-server" -type f -maxdepth 1 -regex '.*[0-9a-fA-F]\{40\}.*' ! -regex ".*$keep_commit_sha.*" | xargs -rt rm -f
}
check_code_server_running() {
! pgrep -a "$HOME/.vscode-server/" >&2 && return 0
log "^- VS Code Server already running, exiting!"
return 1
}
LONG_OPTS=help,insider,dump-sha,cli,alpine,extensions:,use-commit:
OPTIONS=h
PLATFORM="alpine"
ARCH="x64"
BUILD="stable"
BIN_TYPE="server"
DUMP_COMMIT_SHA=""
USE_COMMIT=""
while [[ $# -gt 0 ]]; do
case "$1" in
--verbose|-v) verbose=1 ;;
--force) force=1 ;;
esac
shift
done
[ "$PLATFORM" = alpine ] && IS_ALPINE=1 || IS_ALPINE=0
# When non specified, then pull from the OS.
[ -z "$ARCH" ] && case "$(uname -m)" in
aarch64) ARCH="arm64" ;;
x86_64) ARCH="x64" ;;
armv7l) ARCH="armhf" ;;
esac
# Patch things when downloading VS Code Server for Alpine.
if [ "$BIN_TYPE" = "server" -a "$IS_ALPINE" -eq 1 ]; then
log "we need to hard set PLATFORM and ARCH for Alpine Musl"
PLATFORM="linux"
ARCH="alpine" # Alpine is NOT an Arch but a flavor of Linux, oh well.
fi
if [ -n "$USE_COMMIT" ]; then
log "Force using commit from env \$USE_COMMIT='$USE_COMMIT'"
commit_sha="$USE_COMMIT"
else
# We hard-code this because all but a few options returns a 404.
commit_sha=$(get_latest_commit_sha 'win32' 'x64' "${BUILD-stable}")
[ -z "$commit_sha" ] && { log "ERROR: Could not get VS Code commit sha, exiting."; exit 1; }
echo "$commit_sha" | grep -qE '[0-9a-f]{40}' || { log "ERROR: Unexpected value for commit sha: '$commit_sha'"; exit 1; }
fi
if [ "$DUMP_COMMIT_SHA" = "yes" ]; then
echo "$commit_sha"
exit 0
fi
if [ "$force" != 1 ]; then
check_code_server_running
fi
log "\nDownloading: VS Code ${BIN_TYPE}, commit: '$commit_sha'"
options="${BIN_TYPE}-${PLATFORM}-${ARCH}"
archive="vscode-${options}.tar.gz"
tar_dst="/tmp/${archive}"
# Download VS Code tarball to the current directory.
url="https://update.code.visualstudio.com/commit:${commit_sha}/${options}/${BUILD}"
log "Downloading: $url -> $tar_dst"
overwrite='default'
[ -f "$tar_dst" ] && { overwrite='n' && printf "File already exists. Overwrite? (y/N): "; read overwrite; }
if [ "$overwrite" = 'default' ] || [ "$overwrite" = 'y' ] || [ "$overwrite" = 'Y' ]; then
run curl -s --fail -L "$url" -o "$tar_dst"
elif [ "$overwrite" != 'default' ]; then
log "Skipping download, continuing with the existing file."
fi
# Based on the binary type chosen, perform the installation.
if [ "$BIN_TYPE" = "cli" ]; then
install_cli
else
install_server
rm -v "$tar_dst"
fi
log "\nVS Code ${BIN_TYPE} install completed"
if [ -n "${EXTENSIONS}" ]; then
log "\nDownloading extensions:"
## Auto-accept license terms and then install the extensions, by force if they already exist.
for extension in ${EXTENSIONS}; do
log "- $extension"
run "$HOME/code-server" --accept-server-license-terms --force --install-extension "$extension"
done
log "\nExtensions installation complete."
fi
tidy_old_downloads "$commit_sha"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment