From c7ac82ad7e35368c179dd58f05b48c59c63033eb Mon Sep 17 00:00:00 2001 From: Chaskiel Grundman Date: Mon, 16 Dec 2024 15:03:08 -0500 Subject: [PATCH] Allow specifying the docker network Allow for the use of docker networks other than "bridge" and "none" for for autograding containers. The network name can be specified in config.py or via the preallocator. The network must exist on the target docker host for the containers to run. --- config.template.py | 2 ++ restful_tango/tangoREST.py | 7 ++++++- vmms/localDocker.py | 3 +++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/config.template.py b/config.template.py index 400e51cd..0fea8475 100644 --- a/config.template.py +++ b/config.template.py @@ -89,6 +89,8 @@ class Config(object): # Docker autograding container resource limits DOCKER_CORES_LIMIT = None DOCKER_MEMORY_LIMIT = None # in MB + # Docker network to attach to (must already exist) + DOCKER_NETWORK = None # Maximum size for input files in bytes MAX_INPUT_FILE_SIZE = 10 * 1024 * 1024 * 1024 # 10GB diff --git a/restful_tango/tangoREST.py b/restful_tango/tangoREST.py index 52296771..0a253bc6 100644 --- a/restful_tango/tangoREST.py +++ b/restful_tango/tangoREST.py @@ -115,9 +115,14 @@ def createTangoMachine(self, image, vmms=Config.VMMS_NAME, vmObj=None): """createTangoMachine - Creates a tango machine object from image""" cores = getattr(Config, "DOCKER_CORES_LIMIT", None) memory = getattr(Config, "DOCKER_MEMORY_LIMIT", None) + docker_network = getattr(Config, "DOCKER_NETWORK", None) + if docker_network: + network = {'docker_attachment': docker_network} if vmObj and "cores" in vmObj and "memory" in vmObj: cores = vmObj["cores"] memory = vmObj["memory"] + if vmObj and "network" in vmObj: + network = vmObj["network"] return TangoMachine( name=image, vmms=vmms, @@ -125,7 +130,7 @@ def createTangoMachine(self, image, vmms=Config.VMMS_NAME, vmObj=None): cores=cores, memory=memory, disk=None, - network=None, + network=network, ) def convertJobObj(self, dirName, jobObj): diff --git a/vmms/localDocker.py b/vmms/localDocker.py index 45dda03d..8e51188a 100644 --- a/vmms/localDocker.py +++ b/vmms/localDocker.py @@ -11,6 +11,7 @@ import os import sys import shutil +from collections.abc import Mapping import config from tangoObjects import TangoMachine @@ -163,6 +164,8 @@ def runJob(self, vm, runTimeout, maxOutputFileSize, disableNetwork): args = args + ["-m", f"{vm.memory}m"] if disableNetwork: args = args + ["--network", "none"] + elif vm.network and isinstance(vm.network, Mapping) and "docker_attachment" in vm.network: + args.append("--network", vm.network["docker_attachment"]) args = args + [vm.image] args = args + ["sh", "-c"]