alert-dawn-51425
03/10/2023, 5:47 PM"""Quick commands for running docker-compose environment."""
from typing import Iterable
from pants.base.build_root import BuildRoot
from pants.core.util_rules.system_binaries import BinaryPathRequest, BinaryPaths
from pants.engine.console import Console
from pants.engine.fs import Digest, PathGlobs, Paths
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.process import Process, ProcessResult
from pants.engine.rules import Get, Rule, collect_rules, goal_rule
from pants.option.option_types import BoolOption, StrOption
class DockerComposeSubsystem(GoalSubsystem):
"""Run docker compose commands."""
name = "docker-compose"
help = "Run docker-compose commands"
command = StrOption(default="", help="Command to run")
up = BoolOption(default=False, help="Spin up docker environment in background")
up_fg = BoolOption(default=False, help="Spin up docker environment in foreground")
down = BoolOption(default=False, help="Spin down docker environment")
ps = BoolOption(default=False, help="List running processes")
class DockerComposeGoal(Goal):
"""Result of running commands."""
subsystem_cls = DockerComposeSubsystem
@goal_rule
async def docker_compose(
console: Console, buildroot: BuildRoot, subsystem: DockerComposeSubsystem
) -> DockerComposeGoal:
docker_compose_paths = await Get(
BinaryPaths,
BinaryPathRequest(
binary_name="docker-compose", search_path=["/usr/bin", "/bin", "/usr/local/bin"]
),
)
docker_compose_bin = docker_compose_paths.first_path
if docker_compose_bin is None:
raise OSError("Could not find 'docker-compose'.")
paths = await Get(Paths, PathGlobs(["docker-compose/*.yml"]))
command_additions = [
paths.files[idx // 2] if idx % 2 == 1 else "-f" for idx in range(len(paths.files) * 2)
]
digest = await Get(Digest, PathGlobs(["docker-compose/*.yml"]))
if subsystem.up:
await Get(
ProcessResult,
Process(
argv=[docker_compose_bin] + command_additions + ["up", "-d"],
description="Spin up docker-compose in background",
input_digest=digest,
),
)
elif subsystem.down:
await Get(
ProcessResult,
Process(
argv=[docker_compose_bin] + command_additions + ["down"],
description="Spin down docker-compose",
input_digest=digest,
),
)
elif <http://subsystem.ps|subsystem.ps>:
await Get(
ProcessResult,
Process(
argv=[docker_compose_bin] + command_additions + ["ps"],
description="List docker-compose running environment",
input_digest=digest,
),
)
return DockerComposeGoal(exit_code=0)
def rules() -> Iterable[Rule]:
return collect_rules()
However, when I try to run this, I get errors that the filesystem changed during the run. I am trying to understand how to prevent thatwitty-crayon-22786
03/10/2023, 5:50 PM.pants.d/pants.log
? i expect that you have a type error in the Process
constructor. that’s no longer fatal on main
, but has been in past releases.witty-crayon-22786
03/10/2023, 5:51 PMmypy
.alert-dawn-51425
03/10/2023, 5:56 PMwitty-crayon-22786
03/10/2023, 5:57 PMProcess
is very isolated and sandboxedwitty-crayon-22786
03/10/2023, 5:57 PMwitty-crayon-22786
03/10/2023, 5:58 PMInteractiveProcess
, which runs in the foreground instead.witty-crayon-22786
03/10/2023, 5:58 PMalert-dawn-51425
03/10/2023, 5:58 PMalert-dawn-51425
03/10/2023, 6:14 PMwitty-crayon-22786
03/10/2023, 6:15 PMInteractiveProcess
uses the entire env by default, so it shouldn’t be necessary to expand it explicitlywitty-crayon-22786
03/10/2023, 6:16 PMwitty-crayon-22786
03/10/2023, 6:17 PM