mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Resource constraints for Docker VMs.
This commit is contained in:
1 parent
0afce89c0b
commit
d1c29c8bb7
5 files changed
+200
-31
No files matched your search
@@ -71,7 +71,7 @@ class DockerVM(BaseNode):
|
||||
|
||||
def __init__(self, name, node_id, project, manager, image, console=None, aux=None, start_command=None,
|
||||
adapters=None, environment=None, console_type="telnet", console_resolution="1024x768",
|
||||
console_http_port=80, console_http_path="/", extra_hosts=None, extra_volumes=[]):
|
||||
console_http_port=80, console_http_path="/", extra_hosts=None, extra_volumes=[], memory=0, cpus=0):
|
||||
|
||||
super().__init__(name, node_id, project, manager, console=console, aux=aux, allocate_aux=True, console_type=console_type)
|
||||
|
||||
@@ -94,6 +94,8 @@ class DockerVM(BaseNode):
|
||||
self._console_websocket = None
|
||||
self._extra_hosts = extra_hosts
|
||||
self._extra_volumes = extra_volumes or []
|
||||
self._memory = memory
|
||||
self._cpus = cpus
|
||||
self._permissions_fixed = False
|
||||
self._display = None
|
||||
self._closing = False
|
||||
@@ -132,6 +134,8 @@ class DockerVM(BaseNode):
|
||||
"node_directory": self.working_path,
|
||||
"extra_hosts": self.extra_hosts,
|
||||
"extra_volumes": self.extra_volumes,
|
||||
"memory": self.memory,
|
||||
"cpus": self.cpus
|
||||
}
|
||||
|
||||
def _get_free_display_port(self):
|
||||
@@ -211,6 +215,22 @@ class DockerVM(BaseNode):
|
||||
def extra_volumes(self, extra_volumes):
|
||||
self._extra_volumes = extra_volumes
|
||||
|
||||
@property
|
||||
def memory(self):
|
||||
return self._memory
|
||||
|
||||
@memory.setter
|
||||
def memory(self, memory):
|
||||
self._memory = memory
|
||||
|
||||
@property
|
||||
def cpus(self):
|
||||
return self._cpus
|
||||
|
||||
@cpus.setter
|
||||
def cpus(self, cpus):
|
||||
self._cpus = cpus
|
||||
|
||||
async def _get_container_state(self):
|
||||
"""
|
||||
Returns the container state (e.g. running, paused etc.)
|
||||
@@ -328,6 +348,10 @@ class DockerVM(BaseNode):
|
||||
if image_infos is None:
|
||||
raise DockerError("Cannot get information for image '{}', please try again.".format(self._image))
|
||||
|
||||
available_cpus = psutil.cpu_count(logical=True)
|
||||
if self._cpus > available_cpus:
|
||||
raise DockerError("You have allocated too many CPUs for the Docker container (max available is {} CPUs)".format(available_cpus))
|
||||
|
||||
params = {
|
||||
"Hostname": self._name,
|
||||
"Name": self._name,
|
||||
@@ -340,6 +364,8 @@ class DockerVM(BaseNode):
|
||||
"CapAdd": ["ALL"],
|
||||
"Privileged": True,
|
||||
"Binds": self._mount_binds(image_infos),
|
||||
"Memory": self._memory * (1024 * 1024), # convert memory to bytes
|
||||
"NanoCpus": int(self._cpus * 1e9) # convert cpus to nano cpus
|
||||
},
|
||||
"Volumes": {},
|
||||
"Env": ["container=docker"], # Systemd compliant: https://github.com/GNS3/gns3-server/issues/573
|
||||
|
||||
@@ -49,20 +49,22 @@ class DockerHandler:
|
||||
async def create(request, response):
|
||||
docker_manager = Docker.instance()
|
||||
container = await docker_manager.create_node(request.json.pop("name"),
|
||||
request.match_info["project_id"],
|
||||
request.json.get("node_id"),
|
||||
image=request.json.pop("image"),
|
||||
start_command=request.json.get("start_command"),
|
||||
environment=request.json.get("environment"),
|
||||
adapters=request.json.get("adapters"),
|
||||
console=request.json.get("console"),
|
||||
console_type=request.json.get("console_type"),
|
||||
console_resolution=request.json.get("console_resolution", "1024x768"),
|
||||
console_http_port=request.json.get("console_http_port", 80),
|
||||
console_http_path=request.json.get("console_http_path", "/"),
|
||||
aux=request.json.get("aux"),
|
||||
extra_hosts=request.json.get("extra_hosts"),
|
||||
extra_volumes=request.json.get("extra_volumes"))
|
||||
request.match_info["project_id"],
|
||||
request.json.get("node_id"),
|
||||
image=request.json.pop("image"),
|
||||
start_command=request.json.get("start_command"),
|
||||
environment=request.json.get("environment"),
|
||||
adapters=request.json.get("adapters"),
|
||||
console=request.json.get("console"),
|
||||
console_type=request.json.get("console_type"),
|
||||
console_resolution=request.json.get("console_resolution", "1024x768"),
|
||||
console_http_port=request.json.get("console_http_port", 80),
|
||||
console_http_path=request.json.get("console_http_path", "/"),
|
||||
aux=request.json.get("aux"),
|
||||
extra_hosts=request.json.get("extra_hosts"),
|
||||
extra_volumes=request.json.get("extra_volumes"),
|
||||
memory=request.json.get("memory", 0),
|
||||
cpus=request.json.get("cpus", 0))
|
||||
for name, value in request.json.items():
|
||||
if name != "node_id":
|
||||
if hasattr(container, name) and getattr(container, name) != value:
|
||||
@@ -317,7 +319,8 @@ class DockerHandler:
|
||||
props = [
|
||||
"name", "console", "aux", "console_type", "console_resolution",
|
||||
"console_http_port", "console_http_path", "start_command",
|
||||
"environment", "adapters", "extra_hosts", "extra_volumes"
|
||||
"environment", "adapters", "extra_hosts", "extra_volumes",
|
||||
"memory", "cpus"
|
||||
]
|
||||
|
||||
changed = False
|
||||
|
||||
@@ -103,6 +103,14 @@ DOCKER_CREATE_SCHEMA = {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"memory": {
|
||||
"description": "Maximum amount of memory the container can use in MB",
|
||||
"type": "integer",
|
||||
},
|
||||
"cpus": {
|
||||
"description": "Maximum amount of CPU resources the container can use",
|
||||
"type": "number",
|
||||
},
|
||||
"container_id": {
|
||||
"description": "Docker container ID Read only",
|
||||
"type": "string",
|
||||
@@ -214,6 +222,14 @@ DOCKER_OBJECT_SCHEMA = {
|
||||
"type": "string",
|
||||
}
|
||||
},
|
||||
"memory": {
|
||||
"description": "Maximum amount of memory the container can use in MB",
|
||||
"type": "integer",
|
||||
},
|
||||
"cpus": {
|
||||
"description": "Maximum amount of CPU resources the container can use",
|
||||
"type": "number",
|
||||
},
|
||||
"node_directory": {
|
||||
"description": "Path to the node working directory Read only",
|
||||
"type": "string"
|
||||
|
||||
@@ -82,6 +82,16 @@ DOCKER_TEMPLATE_PROPERTIES = {
|
||||
"type": "array",
|
||||
"default": []
|
||||
},
|
||||
"memory": {
|
||||
"description": "Maximum amount of memory the container can use in MB",
|
||||
"type": "integer",
|
||||
"default": 0
|
||||
},
|
||||
"cpus": {
|
||||
"description": "Maximum amount of CPU resources the container can use",
|
||||
"type": "number",
|
||||
"default": 0
|
||||
},
|
||||
"custom_adapters": CUSTOM_ADAPTERS_ARRAY_SCHEMA
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user