Skip to content

Instantly share code, notes, and snippets.

@b01
Last active May 4, 2024 21:41
Show Gist options
  • Save b01/0a16b6645ab7921b0910603dfb85e4fb to your computer and use it in GitHub Desktop.
Save b01/0a16b6645ab7921b0910603dfb85e4fb to your computer and use it in GitHub Desktop.
Linux script to download latest VS Code Server, good for Docker (tested in Alpine).
#!/bin/sh
# Copyright 2023 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.
set -e
# Auto-Get the latest commit sha via command line.
get_latest_release() {
platform=${1}
arch=${2}
# Grab the first commit SHA since as this script assumes it will be the
# latest.
platform="win32"
arch="x64"
commit_id=$(curl --silent "https://update.code.visualstudio.com/api/commits/stable/${platform}-${arch}" | sed s'/^\["\([^"]*\).*$/\1/')
printf "%s" "${commit_id}"
}
PLATFORM="${1}"
ARCH="${2}"
if [ -z "${PLATFORM}" ]; then
echo "please enter a platform, acceptable values are win32, linux, darwin, or alpine"
exit 1
fi
if [ -z "${ARCH}" ]; then
U_NAME=$(uname -m)
if [ "${U_NAME}" = "aarch64" ]; then
ARCH="arm64"
elif [ "${U_NAME}" = "x86_64" ]; then
ARCH="x64"
elif [ "${U_NAME}" = "armv7l" ]; then
ARCH="armhf"
fi
fi
commit_sha=$(get_latest_release "${PLATFORM}" "${ARCH}")
if [ -n "${commit_sha}" ]; then
echo "will attempt to download VS Code Server version = '${commit_sha}'"
prefix="server-${PLATFORM}"
if [ "${PLATFORM}" = "alpine" ]; then
prefix="cli-${PLATFORM}"
fi
archive="vscode-${prefix}-${ARCH}.tar.gz"
# Download VS Code Server tarball to tmp directory.
curl -L "https://update.code.visualstudio.com/commit:${commit_sha}/${prefix}-${ARCH}/stable" -o "/tmp/${archive}"
# Make the parent directory where the server should live.
# NOTE: Ensure VS Code will have read/write access; namely the user running VScode or container user.
mkdir -vp ~/.vscode-server/bin/"${commit_sha}"
# Extract the tarball to the right location.
tar --no-same-owner -xzv --strip-components=1 -C ~/.vscode-server/bin/"${commit_sha}" -f "/tmp/${archive}"
# Add symlink
cd ~/.vscode-server/bin && ln -s "${commit_sha}" default_version
else
echo "could not pre install vscode server"
fi
@daixtrose
Copy link

Is it time to make this gist a full GitHub project that can receive PR's?

@b01
Copy link
Author

b01 commented Apr 26, 2024

@daixtrose Yes. I'll get that done. Before I post, it needs some testing. There seem to be a problem with setting up latest VS Code server that I want to fix before releasing. I'll elaborate when I have more conclusive details.

@remchuk
Copy link

remchuk commented May 4, 2024

@lcmen Not sure if this is all you need to do since the directory structure underwent further changes. See my question on Stackoverflow. The new directory structure adds some more files. Especially the code-<sha> directory leaves me concerned:

.vscode-server
├── cli
│   └── servers
├── code-e170252f762678dec6ca2cc69aba1570769a5d39
├── data
│   ├── CachedProfilesData
│   ├── logs
│   ├── Machine
│   ├── machineid
│   └── User
└── extensions
    └── extensions.json

Do you perhaps know how to change the previous code that the structure was :

.vscode-server
└── bin
    ├── default_version -> e170252f762678dec6ca2cc69aba1570769a5d39
    └── e170252f762678dec6ca2cc69aba1570769a5d39

into a code working with the new structure?

.vscode-server
├── cli
│   └── servers
├── code-e170252f762678dec6ca2cc69aba1570769a5d39
├── data
│   ├── CachedProfilesData
│   ├── logs
│   ├── Machine
│   ├── machineid
│   └── User
└── extensions
    └── extensions.json

Becasue I'm a bit lost with what @lcmen replyed.

Thank you @b01 for the initial script and every one else for the improvements.

I'm not sure if things have changed recently but extracting vscode-server to mkdir -vp ~/.vscode-server/bin/"${commit_sha}" still causes an installation script to kick in when I ssh into a docker container for the first time. After some investigation, I've found out the correct installation path:

# Make the parent directory where the server should live.
# NOTE: Ensure VS Code will have read/write access; namely the user running VScode or container user.
mkdir -vp ~/.vscode-server/cli/servers/Stable-"${commit_sha}/server"

# Extract the tarball to the right location.
tar --no-same-owner -xz --strip-components=1 -C ~/.vscode-server/cli/servers/Stable-"${commit_sha}"/server -f "/tmp/${archive}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment