Skip to content

Commit

Permalink
Hijack PGET with FUSE
Browse files Browse the repository at this point in the history
  • Loading branch information
nevillelyh committed Oct 2, 2024
1 parent 9d0e00d commit 042f05a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ builder() {
"$PREFIX/bin/uv" --version

log "Installing pget..."
curl -fsSL -o "$PREFIX/bin/pget" "$PGET_URL"
chmod +x "$PREFIX/bin/pget"
"$PREFIX/bin/pget" version
curl -fsSL -o "$PREFIX/bin/pget-bin" "$PGET_URL"
chmod +x "$PREFIX/bin/pget-bin"
"$PREFIX/bin/pget-bin" version

# PGET FUSE wrapper
cp /srv/r8/monobase/pget "$PREFIX/bin/pget"

log "Running builder..."
uv run --python "$BUILDER_PYTHON" /srv/r8/monobase/build.py "$@"
Expand Down
49 changes: 49 additions & 0 deletions src/pget
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# Hijack PGET and serve file lazily with FUSE RPC

set -euo pipefail

PGET_BIN="/usr/local/bin/pget-bin"

url=''
dst=''
for arg in "$@"; do
if [[ $arg == https://* ]]; then
url="$arg"
continue
fi
# Destination is right after URL
if [ -n "$url" ]; then
dst="$arg"
break
fi
done

if [ -z "$url" ] || [ -z "$dst" ]; then
# Fail to parse arguments, use vanilla PGET
exec "$PGET_BIN" "$@"
else
# Finger print object name with URL, size, ETag, and last modified
echo "$(date --iso-8601=seconds --utc) pget $*"
out="$(curl -fsSL -I "$url" | tr '[:upper:]' '[:lower:]' | grep '^\(content-length:\|etag:\|last-modified:\)')"
fingerprint="$({
echo "URL: $url"
echo "$out" | grep "^content-length:" | tail -n 1
echo "$out" | grep "^etag:" | tail -n 1
echo "$out" | grep "^last-modified:" | tail -n 1
})"
echo "Fingerprint:"
echo "$fingerprint"

sha256="$(echo "$fingerprint" | sha256sum | cut -d ' ' -f 1)"
name="pget/sha256/$sha256"
size="$(echo "$fingerprint" | grep -oP '(?<=^content-length: )[0-9]+')"

# Request file from FUSE
echo "{\"name\":\"$name\",\"size\":$size,\"url\":\"$url\"}" > /srv/r8/fuse-rpc/proc/pget

# Symlink file to destination
mkdir -p "$(dirname "$dst")"
ln -sf "/srv/r8/fuse-rpc/$name" "$dst"
fi

0 comments on commit 042f05a

Please sign in to comment.