unaiverse.utils.sandbox¶
A node can run code that it did not write. The code of the roles of a world arrives with the world and runs inside the node of every agent that joins. This module runs a Python script inside an isolated container so that such code sees only the folders that the user mounts. It builds a container image with the dependencies of the SDK, mounts the folder of the SDK read-only, runs the script and removes the image at the end. The functions drive the command-line client of a container engine. That client must be installed and on PATH.
The sandbox does not start in the current version
sandbox() always mounts two writable folders that do not exist in the package: runners next to the unaiverse package folder and unaiverse/library inside it. run_in_docker() refuses a mount that is not an existing directory. The run therefore stops with Error: Writable host path '.../runners' does not exist or is not a directory. Cannot mount. and the process exits with status 1 after the image was built. Creating the two empty folders by hand works around it.
From a terminal:
| Option | Meaning |
|---|---|
script |
The Python file to run. It must end with .py. |
-p, --port |
Required. The first of the four consecutive ports of the node. It must be a positive integer and becomes the environment variable NODE_STARTING_PORT. |
-r, --read-only |
Folders mounted read-only, separated by : (for example /a:/b). |
-w, --writable |
Folders mounted writable, separated by :. |
"unaiverse-sandbox", the tag of the image that the module builds and removes.
"unaiverse-sandbox-container", the prefix of the container names. The cleanup removes every container whose name contains it.
CONTAINER_NAME_BASE followed by - and eight random hexadecimal digits. It is generated once per process when the module is imported.
DOCKERFILE_CONTENT
module-attribute
¶
DOCKERFILE_CONTENT = '\n\n# Debian image, automatically guessed architecture\nFROM python:3.12-slim-bookworm\n\n# Installing Go compiler\nRUN apt-get update && apt-get install -y --no-install-recommends build-essential curl git\nRUN rm -rf /var/lib/apt/lists/*\nRUN ARCH=$(dpkg --print-architecture) && curl -LO https://go.dev/dl/go1.24.5.linux-${ARCH}.tar.gz\nRUN ARCH=$(dpkg --print-architecture) && tar -C /usr/local -xzf go1.24.5.linux-${ARCH}.tar.gz\nRUN ARCH=$(dpkg --print-architecture) && rm go1.24.5.linux-${ARCH}.tar.gz\n\n# Set Go environment variables\nENV PATH="/usr/local/go/bin:${PATH}"\nENV GOPATH="/go"\nRUN mkdir -p /go/bin /go/src /go/pkg\n\n# Setting the working directory inside the container\nWORKDIR /unaiverse\n\n# Dependencies\nRUN <create_requirements.txt>\nRUN pip install --no-cache-dir -r requirements.txt --break-system-packages\n'
The template of the build file of the image. The image starts from Python 3.12 on Debian (slim), installs build-essential, curl and git and the compiler of the native networking library and works in /unaiverse. The placeholder line RUN <create_requirements.txt> becomes a command that writes the dependencies of the installed package to requirements.txt before pip install.
sandbox
¶
sandbox(file_to_run: str, read_only_paths: tuple[str] | list[str] | None = None, writable_paths: tuple[str] | list[str] | None = None) -> None
Source code in unaiverse/utils/sandbox.py
Builds the image, runs file_to_run in a container and cleans up.
| Argument | Type | Default | Meaning |
|---|---|---|---|
file_to_run |
str |
required | The script. The container runs python3 <absolute path>. |
read_only_paths |
sequence of str or None |
None |
Extra folders mounted read-only. |
writable_paths |
sequence of str or None |
None |
Extra folders mounted writable. |
The steps are these:
- The function checks that its own file is
unaiverse/utils/sandbox.pyand raisesAssertionError("Major security issue, stopping.")otherwise. The folder that contains theunaiversepackage (thesrcfolder of a checkout,site-packagesfor an installed package) is the root of the sandbox. - It runs
cleanup_docker_artifactson the root. - It reads the dependencies of the installed distribution
unaiversewithout the optional extras. The package must be installed because a bare checkout raisesimportlib.metadata.PackageNotFoundError. - It writes the build file
Dockerfileinto the root and builds the image with the whole root as build context. A failed build cleans up and exits the process with status1. - It mounts the root read-only and the extra read-only folders. It mounts writable the folders
<root>/runners,<root>/unaiverse/libraryand<root>/unaiverse/networking/p2pand the extra writable folders. Each folder appears at the same path inside the container. - It runs the container with
run_in_docker. A failure exits the process with status1without the final cleanup and leaves the image and the build file in place. - It runs the cleanup again.
The function prints its progress with print. It returns None.
run_in_docker
¶
run_in_docker(file_to_run: str, read_only_host_paths: list[str] | None = None, writable_host_paths: list[str] | None = None)
Source code in unaiverse/utils/sandbox.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
Runs python3 file_to_run in a new container of the image and returns True when the script exits with status 0.
| Argument | Type | Default | Meaning |
|---|---|---|---|
file_to_run |
str |
required | The absolute path of the script. |
read_only_host_paths |
list[str] or None |
None |
Folders mounted read-only at the same path. |
writable_host_paths |
list[str] or None |
None |
Folders mounted writable at the same path. |
The container is removed when it exits. It receives PYTHONUNBUFFERED=1 and the value of NODE_STARTING_PORT of the host. On Linux it shares the network of the host. On the other systems it publishes the four ports of the node when NODE_STARTING_PORT is positive: p and p+2 over TCP, p+1 and p+3 over UDP. A mount path that is not an existing directory makes the function print an error and return False before starting anything. The output of the container is copied line by line to the standard output. Ctrl-C stops the copy. A missing command-line client returns False.
build_docker_image
¶
Source code in unaiverse/utils/sandbox.py
Builds the image DOCKER_IMAGE_NAME from the build file and the context in the folder where. It returns True on success and False when the build fails.
cleanup_docker_artifacts
¶
Source code in unaiverse/utils/sandbox.py
Stops and removes every container whose name contains CONTAINER_NAME_BASE, removes the image DOCKER_IMAGE_NAME and deletes the file Dockerfile in where. The first step also removes the containers of other sandboxes running on the same machine. A missing image prints an error and the function continues.