Skip to content

Commit

Permalink
feat(examples): Introduce Python 3 HTTP Server
Browse files Browse the repository at this point in the history
Introduce Python3 as binary compatibility starting an HTTP server.
Extract / Install Python using `Dockerfile`. Then run it with the `base`
kernel images from `../../kernels/`.

Add typical files for a bincompat app:

* `Kraftfile`: build / run rules, including pulling the `base` image
* `Dockerfile`: filesystem, including binary and libraries
* `Makefile`: used to generate the root filesystem from the `Dockerfile`
  rules
* `README.md`: instructions to set up, build and run the application
* `config.yaml`: configuration file to generate scripts to the
  application
* `server.py`: Python script to start an HTTP server

`config.yaml` is used to generate run scripts using the
`../../utils/bincompat/generate.py` script.

The kernels in `../../kernels` are generated by running the
`../../utils/bincompat/base-build-all.sh` script while inside the
`../../library/base/` directory.

Signed-off-by: Razvan Deaconescu <[email protected]>
  • Loading branch information
razvand committed Dec 19, 2023
1 parent 9a88dfc commit c2f1944
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions examples/http-python3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/rootfs/
/rootfs.cpio
/run-qemu*
/run-fc*
/kraft-run-*
/fc*.json
16 changes: 16 additions & 0 deletions examples/http-python3/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM --platform=linux/x86_64 python:3.12 AS build

RUN set -xe; \
/usr/sbin/ldconfig /usr/local/lib

FROM scratch

COPY --from=build /usr/local/lib /usr/local/lib
COPY --from=build /usr/local/bin/python3 /usr/local/bin/python3
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
COPY --from=build /lib/x86_64-linux-gnu/libm.so.6 /lib/x86_64-linux-gnu/libm.so.6
COPY --from=build /usr/lib/x86_64-linux-gnu/libz.so.1 /usr/lib/x86_64-linux-gnu/libz.so.1
COPY --from=build /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
COPY --from=build /etc/ld.so.cache /etc/ld.so.cache

COPY ./server.py /server.py
9 changes: 9 additions & 0 deletions examples/http-python3/Kraftfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spec: v0.6

name: http-python3

runtime: index.unikraft.io/unikraft.org/base:latest

rootfs: ./Dockerfile

cmd: ["/usr/local/bin/python3", "/server.py"]
3 changes: 3 additions & 0 deletions examples/http-python3/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
IMAGE_NAME = unikraft-python3

include ../../utils/bincompat/docker.Makefile
37 changes: 37 additions & 0 deletions examples/http-python3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# HTTP Server on Python3

This directory contains the definition to run an HTTP server with [Python3](https://www.python.org/) on Unikraft in binary compatibility mode.

Follow the instructions in the common `../README.md` file to set up and configure the application.

## Quick Run

Use `kraft` to run the image:

```console
kraft run -M 512M -p 8080:8080
```

Once executed, it will run the `server.py` script and wait for connections on port `8080`.

Query the server using:

```console
curl localhost:8080
```

It will print "Hello, World!".

## Scripted Run

Use the scripted runs, detailed in the common `../README.md`.
Once executed, scripts will run the `server.py` script and wait for connections on port `8080`.

Query the server using:

```console
curl 172.44.0.2:8080
```

It will print "Hello, World!".

3 changes: 3 additions & 0 deletions examples/http-python3/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
networking: True
accel: True
memory: 512
31 changes: 31 additions & 0 deletions examples/http-python3/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import argparse
from http.server import HTTPServer, BaseHTTPRequestHandler

class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("Hello, world!", "utf-8"))

def main(args):
server = HTTPServer((args.host, args.port), MyServer)

print("starting server at %s:%s" % (args.host, args.port))

try:
server.serve_forever()

except KeyboardInterrupt:
pass

print("server stopped")

def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--host", type=str, default="0.0.0.0")
parser.add_argument("--port", type=int, default=8080)
return parser.parse_args()

if __name__ == "__main__":
main(parse_args())

0 comments on commit c2f1944

Please sign in to comment.