Answer architecture round 1: 0 🔴, 3 🟡 (0 misread); keep the slot's share beside ROOM_SHARE in machine_load, move the rule's predicate into rule_hook, and have capped set both limits itself instead of leaning on docker taking the last option
This commit is contained in:
+29
-19
@@ -1,16 +1,18 @@
|
||||
"""What a docker command asks of docker: its subcommand, and whether a container it starts has a memory limit and a swap limit."""
|
||||
"""What a docker command asks of docker: its subcommand, whether a container it makes has a memory limit and a swap limit, and those arguments with the limits a container lacks."""
|
||||
import re
|
||||
|
||||
from shell_command import launched
|
||||
|
||||
# The options of docker, docker buildx and docker compose that take a value, so the value is not read as a subcommand.
|
||||
DOCKER_VALUED = frozenset({"--ansi", "--builder", "--config", "--context", "--env-file", "--file", "--host", "--log-level", "--parallel", "--profile", "--progress", "--project-directory", "--project-name", "--tlscacert", "--tlscert", "--tlskey", "-c", "-f", "-H", "-l", "-p"})
|
||||
# The subcommands that make a container, whose memory limits docker reads from the same options.
|
||||
STARTS_CONTAINER = (["container", "create"], ["container", "run"], ["create"], ["run"])
|
||||
# A size as docker reads one, such as 4g, 512M or 1.5GiB: the pattern go-units' RAMInBytes parses.
|
||||
SIZE = r"\d+(?:\.\d+)*[kKmMgGtTpP]?[iI]?[bB]?"
|
||||
MEMORY_OPTIONS = ("-m", "--memory")
|
||||
SWAP_OPTIONS = ("--memory-swap",)
|
||||
# A swap limit is a size other than 0: docker reads --memory-swap -1 as unlimited swap, and 0 as unset.
|
||||
SWAP = rf"(?!0+(?:\.0+)*[kKmMgGtTpP]?[iI]?[bB]?$){SIZE}"
|
||||
# Any value --memory-swap takes, -1 and 0 among them.
|
||||
ANY_SWAP = rf"-1|{SIZE}"
|
||||
|
||||
|
||||
def docker_subcommand(arguments: list[str]) -> list[str]:
|
||||
@@ -35,39 +37,47 @@ def unlimited(arguments: list[str]) -> bool:
|
||||
The container's own arguments, past its image, are read too: telling them apart needs every value-taking option of docker run. So a -m 4g there reads as a limit, while python -m pytest does not, since pytest is no size.
|
||||
"""
|
||||
subcommand = docker_subcommand(arguments)
|
||||
limited = option_value(arguments, ("-m", "--memory"), SIZE) and option_value(arguments, ("--memory-swap",), SWAP)
|
||||
limited = option_value(arguments, MEMORY_OPTIONS, SIZE) and option_value(arguments, SWAP_OPTIONS, SWAP)
|
||||
return subcommand in STARTS_CONTAINER and not limited and not helped(arguments, subcommand)
|
||||
|
||||
|
||||
def capped(arguments: list[str], share: str) -> list[str]:
|
||||
"""docker's arguments with the limits a container they make lacks, put right after the subcommand: share for each, except a swap limit beside the container's own memory limit, which takes that limit, as docker refuses less swap than memory."""
|
||||
"""docker's arguments with both limits set right after the subcommand, where a container they make lacks one: the container's own memory limit, or else its swap limit, or else share, for each. Its swap limit stays where it has one; an unlimited or unset one gives way."""
|
||||
if not unlimited(arguments):
|
||||
return arguments
|
||||
memory = option_value(arguments, ("-m", "--memory"), SIZE)
|
||||
after = arguments.index(docker_subcommand(arguments)[-1]) + 1
|
||||
limits = ["--memory-swap", memory] if memory else ["--memory", share, "--memory-swap", share]
|
||||
return [*arguments[:after], *limits, *arguments[after:]]
|
||||
memory = option_value(arguments, MEMORY_OPTIONS, SIZE) or option_value(arguments, SWAP_OPTIONS, SWAP) or share
|
||||
swap = option_value(arguments, SWAP_OPTIONS, SWAP) or memory
|
||||
rest = without(without(arguments, MEMORY_OPTIONS, SIZE), SWAP_OPTIONS, ANY_SWAP)
|
||||
after = rest.index(docker_subcommand(rest)[-1]) + 1
|
||||
return [*rest[:after], "--memory", memory, "--memory-swap", swap, *rest[after:]]
|
||||
|
||||
|
||||
def option_value(arguments: list[str], options: tuple[str, ...], pattern: str) -> str | None:
|
||||
"""The value the arguments give one of the options, where pattern matches it, as its next word or attached, as in -m4g or --memory=4g; None where they give none."""
|
||||
for word, following in zip(arguments, [*arguments[1:], ""]):
|
||||
found = option_at(arguments, options, pattern)
|
||||
return found[2] if found else None
|
||||
|
||||
|
||||
def without(arguments: list[str], options: tuple[str, ...], pattern: str) -> list[str]:
|
||||
"""The arguments with each of the options removed, with its value, where pattern matches the value."""
|
||||
while found := option_at(arguments, options, pattern):
|
||||
start, length, _ = found
|
||||
arguments = [*arguments[:start], *arguments[start + length:]]
|
||||
return arguments
|
||||
|
||||
|
||||
def option_at(arguments: list[str], options: tuple[str, ...], pattern: str) -> tuple[int, int, str] | None:
|
||||
"""Where the arguments first give one of the options a value pattern matches: its index, its length in words and the value."""
|
||||
for at, (word, following) in enumerate(zip(arguments, [*arguments[1:], ""])):
|
||||
for option in options:
|
||||
attached = word.removeprefix(option if len(option) == 2 else option + "=")
|
||||
if attached != word and re.fullmatch(pattern, attached):
|
||||
return attached
|
||||
return at, 1, attached
|
||||
if word == option and re.fullmatch(pattern, following):
|
||||
return following
|
||||
return at, 2, following
|
||||
return None
|
||||
|
||||
|
||||
def unlimited_container(words: list[str]) -> bool:
|
||||
"""Whether the simple command, directly or through a launcher, starts a container that lacks a memory limit or a swap limit."""
|
||||
docker = launched(words, {"docker"})
|
||||
# Under run-heavy, its docker adds the limits a container lacks.
|
||||
return docker is not None and "run-heavy" not in docker.launchers and unlimited(docker.arguments)
|
||||
|
||||
|
||||
def helped(arguments: list[str], subcommand: list[str]) -> bool:
|
||||
"""Whether docker only prints its help: a --help before the subcommand or as the first word after it, not one the container's own command takes."""
|
||||
after = arguments.index(subcommand[-1]) + 1
|
||||
|
||||
Reference in New Issue
Block a user