print jupyter link now has default token if one is not found

This commit is contained in:
2026-01-28 14:09:14 +01:00
parent ef7a63da8a
commit 6afb5215c1
+23 -38
View File
@@ -1,15 +1,15 @@
#!/usr/bin/env bash
# Script to extract where is the jupyter session running by parsing the log file
# This is useful when re-loging to the same node or if one misses it when the new jupyter session is initiated with start_jupyter.sh
# Print the JupyterLab URL from the latest jupyterlab_*.log.
# On Cristallina the real token is redacted in the log ("token=..."),
# so if the token is missing or redacted we replace it with the most commonly used
# token "Cristallina" to produce a usable URL.
set -euo pipefail
# Find newest Jupyter log in $HOME
LOGFILE=$(
find "$HOME" -maxdepth 1 -type f -name "jupyterlab_*.log" -printf "%T@ %p\n" 2>/dev/null \
| sort -n \
| awk '{print $2}' \
| tail -n 1
| sort -n | awk '{print $2}' | tail -n 1
)
if [[ -z "${LOGFILE:-}" || ! -f "$LOGFILE" ]]; then
@@ -17,38 +17,23 @@ if [[ -z "${LOGFILE:-}" || ! -f "$LOGFILE" ]]; then
exit 1
fi
# Slurp the whole logfile and try multiple patterns.
perl -0777 -ne '
my $s = $_;
$s =~ s/\r//g;
$s =~ s/\n/ /g;
# Slurp logfile into one line to handle wrapped URLs
URL=$(
tr '\n' ' ' < "$LOGFILE" \
| grep -Eo 'https?://[^[:space:]]+' \
| grep -E '/(lab|tree)\?' \
| head -n 1 || true
)
# 1) Direct URL that already contains token=
if ($s =~ m{(https?://[^\s"'"'"']+\?token=[^\s"'"'"'&]+)} ) {
print "$1\n"; exit 0;
}
if ($s =~ m{(https?://[^\s"'"'"']+/lab\?token=[^\s"'"'"'&]+)} ) {
print "$1\n"; exit 0;
}
if ($s =~ m{(https?://[^\s"'"'"']+/tree\?token=[^\s"'"'"'&]+)} ) {
print "$1\n"; exit 0;
}
# 2) URL and token appear separately (wrapped / split)
my ($base) = ($s =~ m{(https?://[^\s"'"'"']+)}); # first URL-looking thing
my ($token) = ($s =~ m{token=([A-Za-z0-9]+)}); # token value
if ($base && $token) {
$base =~ s/\?token=.*$//;
print $base . "?token=" . $token . "\n";
exit 0;
}
exit 1;
' "$LOGFILE" || {
echo "Could not find a Jupyter token link in:"
if [[ -z "${URL:-}" ]]; then
echo "Could not find a Jupyter URL in:"
echo " $LOGFILE"
echo
echo "Try:"
echo " grep -n -E \"token=|http://|https://\" \"$LOGFILE\" | head -n 80"
exit 1
}
fi
# If token is missing or redacted, replace with a 'Cristallina' token
if [[ "$URL" != *"token="* || "$URL" == *"token=..."* ]]; then
URL="${URL%%\?*}?token=Cristallina"
fi
echo "$URL"