| 61 | |
| 62 | |
| 63 | class DockerComposeExecutor(DockerExecutorBase): |
| 64 | def __init__(self, compose_file: str, project_name: str): |
| 65 | super().__init__() |
| 66 | self.compose_file = os.path.abspath(compose_file) |
| 67 | self.project_name = project_name |
| 68 | |
| 69 | async def run_command(self, command: str, *args) -> Tuple[int, str, str]: |
| 70 | if platform.system() == 'Windows': |
| 71 | cmd = self._build_windows_command(command, *args) |
| 72 | else: |
| 73 | cmd = self._build_unix_command(command, *args) |
| 74 | return await self.executor.execute(cmd) |
| 75 | |
| 76 | def _build_windows_command(self, command: str, *args) -> str: |
| 77 | compose_file = self.compose_file.replace('\\', '/') |
| 78 | return (f'cd "{os.path.dirname(compose_file)}" && docker compose ' |
| 79 | f'-f "{os.path.basename(compose_file)}" ' |
| 80 | f'-p {self.project_name} {command} {" ".join(args)}') |
| 81 | |
| 82 | def _build_unix_command(self, command: str, *args) -> list[str]: |
| 83 | return [ |
| 84 | self.docker_cmd, |
| 85 | "compose", |
| 86 | "-f", self.compose_file, |
| 87 | "-p", self.project_name, |
| 88 | command, |
| 89 | *args |
| 90 | ] |
| 91 | |
| 92 | async def down(self) -> Tuple[int, str, str]: |
| 93 | return await self.run_command("down", "--volumes") |
| 94 | |
| 95 | async def pull(self) -> Tuple[int, str, str]: |
| 96 | return await self.run_command("pull") |
| 97 | |
| 98 | async def up(self) -> Tuple[int, str, str]: |
| 99 | return await self.run_command("up", "-d") |
| 100 | |
| 101 | async def ps(self) -> Tuple[int, str, str]: |
| 102 | return await self.run_command("ps") |