Skip to content

Commit 0159b3e

Browse files
committed
feat(python-agent-driver): add subprocess demos and rootfs
Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent bb258c1 commit 0159b3e

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

examples/python-agent-driver/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ RUN set -eux; \
4848
find /deps -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true; \
4949
find /deps -type f -name '*.pyc' -delete 2>/dev/null || true; \
5050
find /deps -name '*.so*' -exec strip --strip-unneeded {} + 2>/dev/null || true
51+
# Download a small wheel for the pip-install demo
52+
RUN pip download --no-deps --dest /wheels six
5153

5254
# Stage 2: build hl_pydriver against the SAME Python 3.12-from-source
5355
# that produces local-python-base, so the glibc/libpython3.12.so ABI
@@ -71,6 +73,17 @@ COPY --from=deps /deps /usr/local/lib/python3.12/site-package
7173
COPY --from=driver-build /src/hl_pydriver /bin/hl_pydriver
7274
COPY pydoc_stub.py /usr/local/lib/python3.12/pydoc.py
7375

76+
# pip: the python-base image strips pip for size; bring it back from the
77+
# deps stage (python:3.12-slim) so `python3 -m pip install` works as a
78+
# subprocess.
79+
COPY --from=deps /usr/local/lib/python3.12/site-packages/pip \
80+
/usr/local/lib/python3.12/site-packages/pip
81+
82+
# stdlib modules that python-base strips but pip needs at runtime
83+
COPY --from=deps /usr/local/lib/python3.12/xmlrpc \
84+
/usr/local/lib/python3.12/xmlrpc
85+
86+
7487
# Stage 4: pack CPIO.
7588
FROM alpine:3.20 AS cpio
7689
RUN apk add --no-cache cpio findutils ca-certificates
@@ -88,4 +101,25 @@ RUN mkdir -p /rootfs/etc/ssl/certs /rootfs/usr/lib/ssl && \
88101
cp /etc/ssl/certs/ca-certificates.crt /rootfs/etc/ssl/certs/ && \
89102
ln -sf /etc/ssl/certs/ca-certificates.crt /rootfs/usr/lib/ssl/cert.pem
90103

104+
# pip configuration: disable version-check (no internet during warm-up)
105+
RUN mkdir -p /rootfs/etc && \
106+
printf '[global]\ndisable-pip-version-check = true\n' > /rootfs/etc/pip.conf
107+
108+
# /tmp for pip's temp files during install
109+
RUN mkdir -p /rootfs/tmp
110+
111+
# Pre-downloaded wheels for the pip-install demo
112+
COPY --from=deps /wheels /rootfs/wheels
113+
114+
# Busybox: use the static-PIE build from the shell-base image.
115+
# Alpine's busybox-static is ET_EXEC (non-PIE) which the elfloader
116+
# rejects; the shell-base image has a static-PIE (ET_DYN) build.
117+
COPY --from=ghcr.io/hyperlight-dev/hyperlight-unikraft/shell-base:latest \
118+
/bin/busybox /rootfs/bin/busybox
119+
RUN for cmd in sh echo ls cat grep find wc head tail sort cut sed awk \
120+
cp mv rm mkdir rmdir ln basename dirname env sleep date \
121+
hostname id uname whoami which test true tr uniq tee xargs; do \
122+
ln -sf busybox /rootfs/bin/$cmd; \
123+
done
124+
91125
RUN cd /rootfs && find . | cpio -o -H newc > /output.cpio 2>/dev/null
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import subprocess
2+
3+
cmds = [
4+
(["echo", "hello from hyperlight guest"], None),
5+
(["uname", "-a"], None),
6+
(["ls", "/bin"], None),
7+
(["grep", "nameserver", "/etc/resolv.conf"], None),
8+
(["find", "/etc", "-name", "*.conf"], None),
9+
(["wc", "-l", "/etc/resolv.conf"], None),
10+
(["sh", "-c", "echo hello from sh"], None),
11+
]
12+
13+
for cmd, stdin in cmds:
14+
label = " ".join(cmd)
15+
print(f"\n$ {label}")
16+
r = subprocess.run(cmd, capture_output=True, text=True, input=stdin)
17+
if r.stdout:
18+
print(r.stdout.rstrip())
19+
if r.stderr:
20+
print(f"stderr: {r.stderr.rstrip()}")
21+
if r.returncode != 0:
22+
print(f"exit code: {r.returncode}")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import subprocess, sys, os
2+
3+
target = "/tmp/pypackages"
4+
os.makedirs(target, exist_ok=True)
5+
6+
result = subprocess.run(
7+
[sys.executable, "-m", "pip", "install", "--target", target, "six"],
8+
capture_output=True, text=True,
9+
)
10+
print(result.stdout)
11+
if result.returncode != 0:
12+
print(result.stderr)
13+
sys.exit(result.returncode)
14+
15+
sys.path.insert(0, target)
16+
import six
17+
print(f"Installed and imported six {six.__version__}")

0 commit comments

Comments
 (0)