builder: lib: context.py: add multiple cgroup supports

Signed-off-by: BigfootACA <bigfoot@classfun.cn>
This commit is contained in:
BigfootACA 2024-07-21 17:17:24 +08:00
parent c3297b2aa0
commit f6a1c73125
2 changed files with 7 additions and 3 deletions

View File

@ -5,6 +5,7 @@ from builder.lib import utils
from builder.component import user from builder.component import user
from builder.lib.config import ArchBuilderConfigError from builder.lib.config import ArchBuilderConfigError
from builder.lib.context import ArchBuilderContext from builder.lib.context import ArchBuilderContext
from builder.lib.cgroup import CGroup
log = getLogger(__name__) log = getLogger(__name__)
@ -14,6 +15,7 @@ def chroot_run(
cwd: str = None, cwd: str = None,
env: dict = None, env: dict = None,
stdin: str | bytes = None, stdin: str | bytes = None,
cgroup: CGroup = None,
) -> int: ) -> int:
""" """
Chroot into rootfs and run programs Chroot into rootfs and run programs
@ -24,7 +26,7 @@ def chroot_run(
path = ctx.get_rootfs() path = ctx.get_rootfs()
args = ["chroot", path] args = ["chroot", path]
args.extend(utils.parse_cmd_args(cmd)) args.extend(utils.parse_cmd_args(cmd))
return ctx.run_external(args, cwd, env, stdin) return ctx.run_external(args, cwd, env, stdin, cgroup)
def proc_mkdir(ctx: ArchBuilderContext, file: dict, path: str): def proc_mkdir(ctx: ArchBuilderContext, file: dict, path: str):

View File

@ -142,7 +142,8 @@ class ArchBuilderContext:
/, /,
cwd: str = None, cwd: str = None,
env: dict = None, env: dict = None,
stdin: str | bytes = None stdin: str | bytes = None,
cgroup: CGroup = None,
) -> int: ) -> int:
""" """
Run external command Run external command
@ -153,7 +154,8 @@ class ArchBuilderContext:
log.debug(f"running external command {argv}") log.debug(f"running external command {argv}")
fstdin = None if stdin is None else PIPE fstdin = None if stdin is None else PIPE
proc = Popen(args, cwd=cwd, env=env, stdin=fstdin) proc = Popen(args, cwd=cwd, env=env, stdin=fstdin)
self.cgroup.add_pid(proc.pid) if cgroup is None: cgroup = self.cgroup
cgroup.add_pid(proc.pid)
if stdin: if stdin:
if type(stdin) is str: stdin = stdin.encode() if type(stdin) is str: stdin = stdin.encode()
proc.stdin.write(stdin) proc.stdin.write(stdin)