fix(runners): canonicalize deploy limits block generation
This commit is contained in:
@@ -340,56 +340,45 @@ def upsert_service_deploy_limits(compose_text, service_names, memory_value, cpu_
|
||||
i += 1
|
||||
continue
|
||||
|
||||
service_indent, block_start, block_end = service_block_bounds(lines, i)
|
||||
service_indent, block_start, service_block_end = service_block_bounds(lines, i)
|
||||
deploy_indent = " " * (service_indent + 2)
|
||||
resources_indent = " " * (service_indent + 4)
|
||||
limits_indent = " " * (service_indent + 6)
|
||||
memory_indent = " " * (service_indent + 8)
|
||||
value_indent = " " * (service_indent + 8)
|
||||
deploy_prefix = f"{deploy_indent}deploy:"
|
||||
|
||||
memory_idx = None
|
||||
cpus_idx = None
|
||||
pids_idx = None
|
||||
for idx in range(block_start, block_end):
|
||||
if lines[idx].strip().startswith("memory:"):
|
||||
memory_idx = idx
|
||||
if lines[idx].strip().startswith("cpus:"):
|
||||
cpus_idx = idx
|
||||
if lines[idx].strip().startswith("pids:"):
|
||||
pids_idx = idx
|
||||
# Remove an existing deploy block under this service so we can rewrite it canonically.
|
||||
deploy_idx = None
|
||||
deploy_end = None
|
||||
for idx in range(block_start, service_block_end):
|
||||
if lines[idx].startswith(deploy_prefix):
|
||||
deploy_idx = idx
|
||||
j = idx + 1
|
||||
while j < service_block_end:
|
||||
cur = lines[j]
|
||||
cur_stripped = cur.strip()
|
||||
if cur_stripped:
|
||||
cur_indent = len(cur) - len(cur.lstrip(" "))
|
||||
if cur_indent <= service_indent + 2:
|
||||
break
|
||||
j += 1
|
||||
deploy_end = j
|
||||
break
|
||||
|
||||
if memory_idx is not None:
|
||||
lines[memory_idx] = f"{memory_indent}memory: {memory_value}"
|
||||
if cpus_idx is not None:
|
||||
lines[cpus_idx] = f"{memory_indent}cpus: '{cpu_value}'"
|
||||
if pids_idx is not None:
|
||||
lines[pids_idx] = f"{memory_indent}pids: {pids_value}"
|
||||
if deploy_idx is not None and deploy_end is not None:
|
||||
lines = lines[:deploy_idx] + lines[deploy_end:]
|
||||
service_block_end -= deploy_end - deploy_idx
|
||||
|
||||
if memory_idx is not None and cpus_idx is not None and pids_idx is not None:
|
||||
i = block_end
|
||||
continue
|
||||
|
||||
if memory_idx is None and cpus_idx is None and pids_idx is None:
|
||||
insert_lines = [
|
||||
f"{deploy_indent}deploy:",
|
||||
f"{resources_indent}resources:",
|
||||
f"{limits_indent}limits:",
|
||||
f"{memory_indent}memory: {memory_value}",
|
||||
f"{memory_indent}cpus: '{cpu_value}'",
|
||||
f"{memory_indent}pids: {pids_value}",
|
||||
]
|
||||
lines = lines[:block_start] + insert_lines + lines[block_start:]
|
||||
i = block_end + len(insert_lines)
|
||||
continue
|
||||
|
||||
missing_lines = []
|
||||
if memory_idx is None:
|
||||
missing_lines.append(f"{memory_indent}memory: {memory_value}")
|
||||
if cpus_idx is None:
|
||||
missing_lines.append(f"{memory_indent}cpus: '{cpu_value}'")
|
||||
if pids_idx is None:
|
||||
missing_lines.append(f"{memory_indent}pids: {pids_value}")
|
||||
lines = lines[:block_start] + missing_lines + lines[block_start:]
|
||||
i = block_end + len(missing_lines)
|
||||
insert_lines = [
|
||||
f"{deploy_indent}deploy:",
|
||||
f"{resources_indent}resources:",
|
||||
f"{limits_indent}limits:",
|
||||
f"{value_indent}memory: {memory_value}",
|
||||
f"{value_indent}cpus: '{cpu_value}'",
|
||||
f"{value_indent}pids: {pids_value}",
|
||||
]
|
||||
lines = lines[:block_start] + insert_lines + lines[block_start:]
|
||||
i = service_block_end + len(insert_lines)
|
||||
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
@@ -534,6 +523,7 @@ def build_compose(
|
||||
runner_services = {"act_runner_1", "act_runner_2", "runner1", "runner2"}
|
||||
updated = remove_service_key(updated, runner_services, "mem_limit")
|
||||
updated = remove_service_key(updated, runner_services, "pids_limit")
|
||||
updated = remove_service_key(updated, runner_services, "pids")
|
||||
updated = upsert_service_key(updated, runner_services, "memswap_limit", runner_memswap_limit)
|
||||
updated = upsert_service_deploy_limits(
|
||||
updated,
|
||||
|
||||
Reference in New Issue
Block a user