Skip to content

Commit 398f7ed

Browse files
committed
Add launch_code_server script, complete with global tunneling.
1 parent 2c00530 commit 398f7ed

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

docs/fragments/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Documentation fragments
2+
3+
Sometimes, I have a feature that I implement that I don't want to lose track of, so I document it immediately, despite not having a full docs hierarchy that I could put it inside of.
4+
This folder contains these "documentation fragments", so that I can eventually stitch them all together into some kind of cohesive documentation.

docs/fragments/launch_code_server.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Debugging with VSCode in the browser
2+
3+
Occasionally, a build is so complex and annoying that you wish you had the ability to open an IDE directly in the build environment and hack away at the code until it compiles.
4+
This is now possible via the `launch_code_server` script, which, once invoked, downloads and sets up [a `code-server` instance](https://coder.com/docs/code-server), serving it both on the local machine at `http://localhost:8080`, and through a homemade tailscale funnel at `bb2.cflo.at`.
5+
The tailscale funnel address will be printed out by the `launch_code_server` script, and is meant for use in debugging difficult-to-reproduce issues on CI servers and the like.
6+
Opening a terminal in the vscode instance gives you the same shell prompt as the `--debug` session, including `bash` history with the commands from the build script.
7+
8+
## Future work
9+
10+
1. It would be nice to have a `--debug=vscode` flag to automatically run this, perhaps? Following onto that, perhaps we can figure out a way to securely allow this in Yggdrasil so maintainers can debug difficult builds?
11+
2. We should do something like `git init . && git add . && git commit -a -m "initial commit"` as part of this script, so that devs can easily see what changes they've applied to the source tree to get things to work.
12+
3. There should be a blessed path to exfiltrate patches; perhaps something like an automatic `git format-patch` that uploads to a `gist` or something?
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Get the architecture from MACHTYPE:
6+
case "${MACHTYPE}" in
7+
aarch64-*)
8+
CS_ARCH="aarch64"
9+
;;
10+
x86_64-*)
11+
CS_ARCH="x86_64"
12+
;;
13+
esac
14+
15+
# Download code_server JLL and just run it with some specific options
16+
CODE_SERVER_DIR="/tmp/code_server"
17+
CS_VERSION="4.99.2"
18+
BUILD_NUMBER="0"
19+
20+
mkdir -p "${CODE_SERVER_DIR}"
21+
if [[ ! -f "${CODE_SERVER_DIR}/bin/code-server" ]]; then
22+
echo "Downloading code-server..."
23+
curl -#L "https://github.com/JuliaBinaryWrappers/code_server_jll.jl/releases/download/code_server-v${CS_VERSION}%2B${BUILD_NUMBER}/code_server.v${CS_VERSION}.${CS_ARCH}-linux-gnu.tar.gz" | tar -C "${CODE_SERVER_DIR}" -zx
24+
fi
25+
26+
TAILSCALE_DIR="${CODE_SERVER_DIR}/tailscale"
27+
mkdir -p "${TAILSCALE_DIR}"
28+
if [[ ! -f "${TAILSCALE_DIR}/tailscale" ]]; then
29+
echo "Downloading tailscale..."
30+
case "${MACHTYPE}" in
31+
aarch64-*)
32+
TS_ARCH="arm64"
33+
;;
34+
x86_64-*)
35+
TS_ARCH="amd64"
36+
;;
37+
esac
38+
TS_VERSION="1.82.5"
39+
curl -#L "https://pkgs.tailscale.com/stable/tailscale_${TS_VERSION}_${TS_ARCH}.tgz" | tar -C "${TAILSCALE_DIR}" -zx --strip-components=1
40+
41+
fi
42+
43+
# Start tailscaled
44+
if [[ ! -f "${TAILSCALE_DIR}/tailscaled.pid" ]]; then
45+
echo "Launching tailscaled..."
46+
"${TAILSCALE_DIR}/tailscaled" \
47+
--tun=userspace-networking \
48+
--state=mem: \
49+
>>"${TAILSCALE_DIR}/tailscaled.log" 2>>"${TAILSCALE_DIR}/tailscaled.log" &
50+
fi
51+
52+
echo "Logging in to tailscale..."
53+
"${TAILSCALE_DIR}/tailscale" login \
54+
--hostname=${bb_build_identifier} \
55+
--login-server=https://bb2-headscale.cflo.at \
56+
--auth-key=530a716ebd6dbefb279d86453482d66a2928abefadc4e79b
57+
58+
echo "Launching code-server..."
59+
echo "To connect, use one of:"
60+
echo " http://localhost:8080"
61+
echo " https://bb2.cflo.at/${bb_build_identifier}/"
62+
63+
"${CODE_SERVER_DIR}/bin/code-server" \
64+
--bind-addr=0.0.0.0:8080 \
65+
--auth=none \
66+
--disable-telemetry \
67+
--disable-update-check \
68+
--disable-workspace-trust \
69+
--app-name "BB2 Debug UI" \
70+
--welcome-text "Welcome to the BinaryBuilder2 debug interface" \
71+
/workspace/srcdir >>"${CODE_SERVER_DIR}/code_server.log" 2>>"${CODE_SERVER_DIR}/code_server.log"
72+
73+
echo "code-server exited, dumping logs:"
74+
echo
75+
for LOG_FILE in "${CODE_SERVER_DIR}/code_server.log" "${TAILSCALE_DIR}/tailscaled.log"; do
76+
echo ">>>>>>> $(basename "${LOG_FILE}") <<<<<<<"
77+
cat ${LOG_FILE}
78+
echo; echo; echo
79+
done

src/build_api/BuildConfig.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ function BinaryBuilderSources.content_hash(config::BuildConfig)
259259
end
260260
buffer_data = take!(hash_buffer)
261261
config.content_hash[] = SHA1Hash(sha1(buffer_data))
262+
263+
# Add `bb_build_identifier` to our environment which is primarily used
264+
# as the hostname in our vscode tunnel to `bb2.cflo.at`.
265+
config.env["bb_build_identifier"] = string(
266+
config.src_name,
267+
"-v",
268+
config.src_version,
269+
"-",
270+
bytes2hex(content_hash(config))[1:8],
271+
)
262272
end
263273
return config.content_hash[]::SHA1Hash
264274
end

0 commit comments

Comments
 (0)