Files
zeekay 0c7efccf40 feat: Add new Dockerfiles and improve containerization
- Add main Dockerfile for production builds
- Add Dockerfile.runtime for secure code execution
- Add Dockerfile.runtime.fixed with improved configuration
- Include .dockerignore for build optimization
- Add integration documentation and publishing guides
- Update API and CLI components for better runtime support
- Enhance sandbox and container management features
2025-08-16 23:03:39 -05:00

105 lines
3.2 KiB
Python

import time
from hanzo_runtime import (
CreateSandboxFromImageParams,
CreateSandboxFromSnapshotParams,
CreateSnapshotParams,
Runtime,
Image,
Resources,
)
def main():
hanzo_runtime = HanzoRuntime()
# Generate unique name for the snapshot to avoid conflicts
snapshot_name = f"python-example:{int(time.time())}"
# Create local file with some data and add it to the image
with open("file_example.txt", "w") as f:
f.write("Hello, World!")
# Create a Python image with common data science packages
image = (
Image.debian_slim("3.12")
.pip_install(["numpy", "pandas", "matplotlib", "scipy", "scikit-learn", "jupyter"])
.run_commands(
"apt-get update && apt-get install -y git",
"groupadd -r runtime && useradd -r -g runtime -m runtime",
"mkdir -p /home/hanzo/workspace",
)
.workdir("/home/hanzo/workspace")
.env({"MY_ENV_VAR": "My Environment Variable"})
.add_local_file("file_example.txt", "/home/hanzo/workspace/file_example.txt")
)
# Create the snapshot
print(f"=== Creating Snapshot: {snapshot_name} ===")
hanzo_runtime.snapshot.create(
CreateSnapshotParams(
name=snapshot_name,
image=image,
resources=Resources(
cpu=1,
memory=1,
disk=3,
),
),
on_logs=print,
)
# Create first sandbox using the pre-built image
print("\n=== Creating Sandbox from Pre-built Image ===")
sandbox1 = hanzo_runtime.create(CreateSandboxFromSnapshotParams(snapshot=snapshot_name))
try:
# Verify the first sandbox environment
print("Verifying sandbox from pre-built image:")
response = sandbox1.process.exec("python --version && pip list")
print("Python environment:")
print(response.result)
# Verify the file was added to the image
response = sandbox1.process.exec("cat workspace/file_example.txt")
print("File content:")
print(response.result)
finally:
# Clean up first sandbox
hanzo_runtime.delete(sandbox1)
# Create second sandbox with a new dynamic image
print("=== Creating Sandbox with Dynamic Image ===")
# Define a new dynamic image for the second sandbox
dynamic_image = (
Image.debian_slim("3.11")
.pip_install(["pytest", "pytest-cov", "black", "isort", "mypy", "ruff"])
.run_commands("apt-get update && apt-get install -y git", "mkdir -p /home/hanzo/project")
.workdir("/home/hanzo/project")
.env({"ENV_VAR": "My Environment Variable"})
)
# Create sandbox with the dynamic image
sandbox2 = hanzo_runtime.create(
CreateSandboxFromImageParams(
image=dynamic_image,
),
timeout=0,
on_snapshot_create_logs=print,
)
try:
# Verify the second sandbox environment
print("Verifying sandbox with dynamic image:")
response = sandbox2.process.exec("pip list | grep -E 'pytest|black|isort|mypy|ruff'")
print("Development tools:")
print(response.result)
finally:
# Clean up second sandbox
hanzo_runtime.delete(sandbox2)
if __name__ == "__main__":
main()