|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from subprocess import CompletedProcess, run |
| 5 | +from time import strftime |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from yaspin import yaspin |
| 9 | + |
| 10 | +from .manage_json import read_json |
| 11 | + |
| 12 | + |
| 13 | +class ComposeManager: |
| 14 | + |
| 15 | + def __init__(self) -> None: |
| 16 | + self.composer_file = Path.cwd().joinpath("docker-compose.yml") |
| 17 | + if not self.composer_file.exists(): |
| 18 | + exit( |
| 19 | + "ERROR: docker-compose.yml was not located in current directory." |
| 20 | + ) |
| 21 | + |
| 22 | + def __run( |
| 23 | + self, *args: str, capture_output: bool = False |
| 24 | + ) -> CompletedProcess[str]: |
| 25 | + command = ["docker", "compose", "-f", str(self.composer_file), *args] |
| 26 | + result = run(command, text=True, capture_output=capture_output) |
| 27 | + if result.returncode != 0: |
| 28 | + print("ERROR:\n", result.stderr) |
| 29 | + else: |
| 30 | + print("Command run:\n", result.stdout) |
| 31 | + return result |
| 32 | + |
| 33 | + @yaspin("Building Container...") |
| 34 | + def build( |
| 35 | + self, no_cache: bool = False, pull: bool = False |
| 36 | + ) -> CompletedProcess[str]: |
| 37 | + args = ["build"] |
| 38 | + if no_cache: |
| 39 | + args.append("--no-cache") |
| 40 | + if pull: |
| 41 | + args.append("--pull") |
| 42 | + return self.__run(*args) |
| 43 | + |
| 44 | + @yaspin("Stopping Services...") |
| 45 | + def stop(self) -> CompletedProcess[str]: |
| 46 | + return self.__run("stop") |
| 47 | + |
| 48 | + @yaspin("Starting Services...") |
| 49 | + def start(self) -> CompletedProcess[str]: |
| 50 | + return self.__run("start") |
| 51 | + |
| 52 | + @yaspin("Removing Container...") |
| 53 | + def down(self, remove_volumes: bool = False) -> CompletedProcess[str]: |
| 54 | + args = ["down"] |
| 55 | + if remove_volumes: |
| 56 | + args.append("-v") |
| 57 | + return self.__run(*args) |
| 58 | + |
| 59 | + @yaspin("Putting Up Container...") |
| 60 | + def up(self, detached: bool = True) -> CompletedProcess[str]: |
| 61 | + args = ["up"] |
| 62 | + if detached: |
| 63 | + args.append("-d") |
| 64 | + return self.__run(*args) |
| 65 | + |
| 66 | + @yaspin("Backing Up Container...") |
| 67 | + def back_up(self, cwd: Path = Path.cwd()) -> None: |
| 68 | + backup_path = cwd.joinpath(".backup") |
| 69 | + compose_json = cwd.joinpath("data.json") |
| 70 | + |
| 71 | + backup_path.mkdir(exist_ok=True) |
| 72 | + data: dict[str, Any] = read_json(compose_json) |
| 73 | + services = data.get("composer", {}).get("services", []) or [] |
| 74 | + names: list[str] = [svc.get("name") for svc in services if svc.get("name") is not None] # type: ignore |
| 75 | + for svc_name in names: |
| 76 | + container_path = svc_name |
| 77 | + tar_file = backup_path.joinpath( |
| 78 | + f"{svc_name}_{strftime("%d-%m-%Y_%H:%M:%S")}.tar.gz" |
| 79 | + ) |
| 80 | + container_name = self.__get_container_name(svc_name) |
| 81 | + if container_name: |
| 82 | + run( |
| 83 | + [ |
| 84 | + "docker", |
| 85 | + "compose", |
| 86 | + "cp", |
| 87 | + f"{container_name}:{container_path}", |
| 88 | + tar_file, |
| 89 | + ], |
| 90 | + check=True, |
| 91 | + ) |
| 92 | + |
| 93 | + def __get_container_name(self, service_name: str) -> str | None: |
| 94 | + result = self.__run("ps", capture_output=True) |
| 95 | + lines = result.stdout.splitlines() |
| 96 | + for line in lines[2:]: # skip header |
| 97 | + if service_name in line: |
| 98 | + return line.split()[0] |
| 99 | + return None |
0 commit comments