Files
status-python-sdk/bot/utils.py
T

50 lines
1.9 KiB
Python
Raw Normal View History

2026-06-18 07:39:20 +03:00
import shutil, os, subprocess, sys, time
2026-05-24 20:46:07 +03:00
from pathlib import Path
2026-06-29 22:28:21 +03:00
from typing import Optional
2026-06-18 07:39:20 +03:00
from .logger import Logger
2026-06-20 08:41:22 +03:00
from . import exceptions
2026-05-24 20:46:07 +03:00
2026-06-29 22:28:21 +03:00
def launch_docker_container(commit: Optional[str] = None, wait_seconds: int = 5):
2026-05-24 20:46:07 +03:00
"""
Launch the Status Backend Docker container using `docker-compose.yaml`
2026-06-18 07:39:20 +03:00
Parameters:
2026-06-29 22:28:21 +03:00
- `commit` - the commit SHA. If no commit is provided, the latest version is pulled
2026-06-18 07:39:20 +03:00
- `wait_seconds` - nunmber of seconds to wait before the code resumes. Sleep prevents calling `class Account` faster than launching the docker container. This only happens when the container already exists and it is must be turned on.
2026-05-24 20:46:07 +03:00
"""
2026-06-18 07:39:20 +03:00
logger = Logger()
platform = sys.platform
is_windows = platform == "win32"
2026-05-24 20:46:07 +03:00
if not shutil.which("docker"):
2026-06-20 08:41:22 +03:00
raise exceptions.DockerError("Please install Docker.")
2026-05-24 20:46:07 +03:00
2026-06-18 07:39:20 +03:00
logger.info(f"Running Docker on {platform}")
2026-06-29 22:28:21 +03:00
ref = commit if commit else "develop"
2026-05-24 20:46:07 +03:00
DOCKER_COMPOSE_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "docker-compose.yaml")
docker_path = DOCKER_COMPOSE_PATH
if is_windows:
p = Path(DOCKER_COMPOSE_PATH)
drive = p.drive.rstrip(":").lower()
docker_path = f"/mnt/{drive}/" + "/".join(p.parts[1:])
2026-06-29 22:28:21 +03:00
cmd = ["env", f"STATUS_GO_REF={ref}", "docker", "compose", "-f", docker_path, "up", "-d", "--build"]
2026-05-24 20:46:07 +03:00
if is_windows:
if not shutil.which("wsl"):
2026-06-20 08:41:22 +03:00
raise exceptions.DockerError("Please install wsl - https://learn.microsoft.com/en-us/windows/wsl/install.")
2026-05-24 20:46:07 +03:00
cmd.insert(0, "wsl")
2026-06-18 07:39:20 +03:00
logger.info(f"Running:\n{' '.join(cmd)}")
2026-05-24 20:46:07 +03:00
result = subprocess.run(
cmd,
cwd=os.path.dirname(DOCKER_COMPOSE_PATH),
stderr=subprocess.PIPE,
text=True,
)
2026-06-17 18:56:45 +03:00
2026-05-24 20:46:07 +03:00
if result.returncode != 0:
2026-06-20 08:41:22 +03:00
raise exceptions.DockerError(result.stderr.strip())
2026-06-18 07:39:20 +03:00
logger.info(f"Sleeping for {wait_seconds}s")
time.sleep(wait_seconds)