Skip to content

Instantly share code, notes, and snippets.

@b01
Last active May 16, 2024 18:49
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
@b01
Copy link
Author

b01 commented May 6, 2024

@remchuk and @lcmen. So I was able to verify both your observations. In fact I observed that the script looks for all those different locations when it is establishing a tunnel between VS code client running on the host machine and the server in the container. It seems on the Host machine it looks for several paths, but I only seems to look for the one path in the container (.vscode-server/bin/<commit>). Here are two crops of the same output from using the VS Code Dev Containers: Attach to a running container... option.

This is at the beginning of the output and the locations it looks for on your Host machine, I also noticed it ran these test in a WSL terminal:

[416 ms] Start: Run in host: test -f '/home/khalifahks/.vscode-server/cli/servers/Stable-b58957e67ee1e712cebf466b995adf4c5307b2bd/server/node'
[416 ms] 
[416 ms] 
[417 ms] Exit code 1
[417 ms] Start: Run in host: test -f '/home/khalifahks/.vscode/cli/servers/Stable-b58957e67ee1e712cebf466b995adf4c5307b2bd/server/node'
[417 ms] 
[417 ms] 
[417 ms] Exit code 1
[417 ms] Start: Run in host: test -f '/home/khalifahks/.vscode-server/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd/node'
[418 ms] 
[418 ms] 
[418 ms] Exit code 1
[418 ms] Start: Run in host: test -f '/home/khalifahks/.vscode-remote-containers/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd/node'
[419 ms] 
[419 ms] 
[419 ms] Start: Run in host: test -f '/home/khalifahks/.vscode-remote-containers/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd/node_modules/node-pty/package.json'
[419 ms] 
[419 ms] 
[419 ms] Start: Run in host: test -f '/home/khalifahks/.vscode-remote-containers/dist/vscode-remote-containers-server-0.362.0.js'

This is the location it looked for inside the running container:

2526 ms] Start: Run in container: test -d '/home/app/.vscode-server'
[2527 ms] 
[2527 ms] 
[2527 ms] Start: Run in container: test ! -f '/home/app/.vscode-server/data/Machine/.writeMachineSettingsMarker' && set -o noclobber && mkdir -p '/home/app/.vscode-server/data/Machine' && { > '/home/app/.vscode-server/data/Machine/.writeMachineSettingsMarker' ; } 2> /dev/null
[2529 ms] 
[2529 ms] 
[2529 ms] Start: Run in container: cat /home/app/.vscode-server/data/Machine/settings.json
[2530 ms] 
[2530 ms] cat: cannot open '/home/app/.vscode-server/data/Machine/settings.json': No such file or directory
[2530 ms] Exit code 1
[2531 ms] 
Support for Alpine Linux is in preview.

[2531 ms] Start: Run in container: test -d '/home/app/.vscode-server/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd'
[2532 ms] 
[2532 ms] 
[2532 ms] Start: Run in container: test -x '/home/app/.vscode-server/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd/bin/helpers/check-requirements.sh'
[2533 ms] 
[2533 ms] 
[2533 ms] Start: Run in container: '/home/app/.vscode-server/bin/b58957e67ee1e712cebf466b995adf4c5307b2bd/bin/helpers/check-requirements.sh'
[2537 ms] Warning: alpine distro detected, skipping GLIBCXX check

The locations it looks for on the host, my guess is those are setup by the VS Code Remote extension.

Though I'm not sure why it searches all the previous locations it uses or which is the current. I feel the need to dig into the vscode (where I believe the server also lives) repo or the remote extension should review what is current. I just hope it wont take me long to discover.

@remchuk
Copy link

remchuk commented May 6, 2024

@b01 I ended up using an older version of this script and it worked greatly!

#!/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() {
    tag=$(curl --silent "https://api.github.com/repos/${1}/releases/tags/${2}" | # Get latest release from GitHub API
        grep '"tag_name":'                                              | # Get tag line
    sed -E 's/.*"([^"]+)".*/\1/'                                    ) # Pluck JSON value

    tag_data=$(curl --silent "https://api.github.com/repos/${1}/git/ref/tags/${tag}")

    sha=$(echo "${tag_data}"           | # Get latest release from GitHub API
        grep '"sha":'                | # Get tag line
    sed -E 's/.*"([^"]+)".*/\1/' ) # Pluck JSON value

    sha_type=$(echo "${tag_data}"           | # Get latest release from GitHub API
        grep '"type":'                    | # Get tag line
    sed -E 's/.*"([^"]+)".*/\1/'      ) # Pluck JSON value

    if [ "${sha_type}" != "commit" ]; then
        combo_sha=$(curl -s "https://api.github.com/repos/${1}/git/tags/${sha}" | # Get latest release from GitHub API
            grep '"sha":'                                                     | # Get tag line
        sed -E 's/.*"([^"]+)".*/\1/'                                      ) # Pluck JSON value

        # Remove the tag sha, leaving only the commit sha;
        # this won't work if there are ever more than 2 sha,
        # and use xargs to remove whitespace/newline.
        sha=$(echo "${combo_sha}" | sed -E "s/${sha}//" | xargs)
    fi

    printf "${sha}"
}

ARCH="x64"
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

archive="vscode-server-linux-${ARCH}.tar.gz"
owner='microsoft'
repo='vscode'
version='1.89.0'
commit_sha=$(get_latest_release "${owner}/${repo}" "${version}")

if [ -n "${commit_sha}" ]; then
    echo "will attempt to download VS Code Server version = '${commit_sha}'"

    # Download VS Code Server tarball to tmp directory.
    curl -L "https://update.code.visualstudio.com/commit:${commit_sha}/server-linux-${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

export PATH="$PATH:/root/.vscode-server/bin/${commit_sha}/bin"

for FILE in /opt/vscode-server/*.vsix; do code-server --install-extension $FILE; done

I changed it a bit so I can specify what version I want, but putting the latest worked greatly!

@b01
Copy link
Author

b01 commented May 7, 2024

@remchuk I'm glad you got it working. Does this confirm that the folder structure in the tar file is correct and it has not changed? I'm curious as to why the older script worked, do you know?

Here is the Git repository for future updates and contributions: https://github.com/b01/dl-vscode-server

@remchuk
Copy link

remchuk commented May 8, 2024

@remchuk I'm glad you got it working. Does this confirm that the folder structure in the tar file is correct and it has not changed? I'm curious as to why the older script worked, do you know?

Here is the Git repository for future updates and contributions: https://github.com/b01/dl-vscode-server

I'm honestly not sure, Look at the folder structure is looks the same as before.
But for some reason it doesn't work in vscode 1.88 but works well in 1.89

@b01
Copy link
Author

b01 commented May 9, 2024

@remchuk If you would mind, could you test version 0.1.2 please?
@daixtrose The Git Repos was made.
@sp7412 I believe there is a way.

I was able to fix some thing and allow the script to perform more consistently. The thing to figure out now is all the folders that VS Code Server is expected to be installed. I had to pay close attention between local and vscode.dev, it was looking in .vscode and not .vscode-server. Where I was finally able to observer such a difference.

Just wanted to point that out. This is likely my last post on this Gist. In the preset and future, please go to the Git repo to comment, open an issue, or PR: https://github.com/b01/dl-vscode-server

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