forked from microsoft/TaskWeaver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Execution inside container (microsoft#217)
- add option for running code inside container - add stop function at chat end --------- Co-authored-by: Shilin HE <[email protected]>
- Loading branch information
Showing
24 changed files
with
881 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use the official Python 3.10 image as the base image | ||
FROM python:3.10 | ||
|
||
# Set the working directory to /app | ||
WORKDIR /app | ||
|
||
# Copy the requrements file | ||
COPY requirements.txt /app/requirements.txt | ||
|
||
# Install any necessary dependencies | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
# Install additional dependencies below for the plugins | ||
# e.g., RUN pip install your-package-name | ||
|
||
# Copy the project code | ||
COPY taskweaver/ces /app/taskweaver/ces | ||
COPY taskweaver/plugin /app/taskweaver/plugin | ||
COPY taskweaver/module /app/taskweaver/module | ||
COPY taskweaver/__init__.py /app/taskweaver/__init__.py | ||
COPY ces_container/docker_entry.py /app/docker_entry.py | ||
|
||
ENV PYTHONPATH "${PYTHONPATH}:/app" | ||
|
||
CMD ["python", "docker_entry.py"] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
import signal | ||
import time | ||
|
||
from taskweaver.ces import Environment, EnvMode | ||
|
||
env_id = os.getenv( | ||
"TASKWEAVER_ENV_ID", | ||
"local", | ||
) | ||
env_dir = os.getenv( | ||
"TASKWEAVER_ENV_DIR", | ||
"/app", | ||
) | ||
session_id = os.getenv( | ||
"TASKWEAVER_SESSION_ID", | ||
"session_id", | ||
) | ||
port_start = int( | ||
os.getenv( | ||
"TASKWEAVER_PORT_START", | ||
"12345", | ||
), | ||
) | ||
kernel_id = os.getenv( | ||
"TASKWEAVER_KERNEL_ID", | ||
"kernel_id", | ||
) | ||
|
||
env = Environment(env_id, env_dir, env_mode=EnvMode.InsideContainer) | ||
|
||
|
||
def signal_handler(sig, frame): | ||
print("Received termination signal. Shutting down the environment.") | ||
env.stop_session(session_id) | ||
exit(0) | ||
|
||
|
||
# Register the signal handler | ||
signal.signal(signal.SIGINT, signal_handler) | ||
signal.signal(signal.SIGTERM, signal_handler) | ||
|
||
if __name__ == "__main__": | ||
env.start_session( | ||
session_id=session_id, | ||
port_start_inside_container=port_start, | ||
kernel_id_inside_container=kernel_id, | ||
) | ||
|
||
print(f"Session {session_id} is running at {env_dir} inside a container.") | ||
|
||
# Keep the script running until it receives a termination signal | ||
try: | ||
# Keep the script running indefinitely | ||
while True: | ||
time.sleep(10) # Sleep for 10 seconds | ||
except KeyboardInterrupt: | ||
# Handle Ctrl-C or other interruption signals | ||
pass | ||
finally: | ||
# Clean up and shut down the kernel | ||
env.stop_session(session_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
$scriptDirectory = $PSScriptRoot | ||
Write-Host "The script directory is: $scriptDirectory" | ||
|
||
$imageName = "taskweaver/executor" | ||
$taskweaverPath = Join-Path -Path $scriptDirectory -ChildPath "..\taskweaver" | ||
$dockerfilePath = Join-Path -Path $scriptDirectory -ChildPath "..\ces_container\Dockerfile" | ||
$contextPath = Join-Path -Path $scriptDirectory -ChildPath "..\" | ||
|
||
if (Test-Path $taskweaverPath) { | ||
Write-Host "Found module files from $taskweaverPath" | ||
Write-Host "Dockerfile path: $dockerfilePath" | ||
Write-Host "Context path: $contextPath" | ||
} else { | ||
Write-Host "Local files not found." | ||
exit 1 | ||
} | ||
|
||
# Build the Docker image | ||
docker build -t $imageName -f $dockerfilePath $contextPath | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
|
||
# Get the directory containing the script file | ||
scriptDirectory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
echo "The script directory is: $scriptDirectory" | ||
|
||
imageName="taskweaver/executor" | ||
taskweaverPath="$scriptDirectory/../taskweaver" | ||
dockerfilePath="$scriptDirectory/../ces_container/Dockerfile" | ||
contextPath="$scriptDirectory/../" | ||
|
||
if [ -d "$taskweaverPath" ]; then | ||
echo "Found module files from $taskweaverPath" | ||
echo "Dockerfile path: $dockerfilePath" | ||
echo "Context path: $contextPath" | ||
else | ||
echo "Local files not found." | ||
exit 1 | ||
fi | ||
|
||
# Build the Docker image | ||
docker build -t "$imageName" -f "$dockerfilePath" "$contextPath" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from typing import Literal | ||
|
||
from taskweaver.ces.common import Manager | ||
from taskweaver.ces.environment import Environment, EnvMode | ||
from taskweaver.ces.manager.sub_proc import SubProcessManager | ||
|
||
|
||
def code_execution_service_factory(env_dir: str) -> Manager: | ||
return SubProcessManager(env_dir=env_dir) | ||
def code_execution_service_factory( | ||
env_dir: str, | ||
kernel_mode: Literal["local", "container"] = "local", | ||
) -> Manager: | ||
return SubProcessManager( | ||
env_dir=env_dir, | ||
kernel_mode=kernel_mode, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.