tools: add possibility to set ports from services yaml

This commit is contained in:
tylen 2025-03-23 20:45:10 +00:00
parent 15bd93abd1
commit fe9b77875a
7 changed files with 45 additions and 16 deletions

View File

@ -2,7 +2,7 @@ services:
audiobookshelf:
image: ghcr.io/advplyr/audiobookshelf:latest
ports:
- 13378:80
- ${SVC_PORT_1}:80
volumes:
- ${MEDIA_PATH}/Books/Audiobooks:/audiobooks
- ${MEDIA_PATH}/Podcasts:/podcasts

View File

@ -1,4 +1,3 @@
version: "3"
services:
server:
image: docker.gitea.com/gitea:1.23.5
@ -12,5 +11,5 @@ services:
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "3000:3000"
- "222:22"
- "${SVC_PORT_1}:3000"
- "${SVC_PORT_2}:22"

View File

@ -8,9 +8,7 @@ services:
MYSQL_PASSWORD: '${USER_PASSWORD}'
MYSQL_ROOT_PASSWORD: '${ROOT_PASSWORD}'
ports:
- '3306:3306'
expose:
- '3306'
- '${SVC_PORT_1}:3306'
volumes:
- my-db:/var/lib/mysql
volumes:

View File

@ -12,16 +12,23 @@ vm-media-100-55: &vm-media-100-55
services:
- name: "audiobookshelf"
ports:
- 13378
host:
<<: *vm-media-100-55
<<: *defaultServiceValues
- name: "gitea"
ports:
- 3000
- 222
host:
<<: *vm-tools-100-65
<<: *defaultServiceValues
- name: "ippoolmanager-sql-server"
ports:
- 3001
host:
<<: *vm-tools-100-65
<<: *defaultServiceValues

View File

@ -1,12 +1,22 @@
import sys
class Colors:
RESET = "\033[0m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
class BaseCli:
def info(self, message):
print(f'[INFO] {message}')
print(f'[{Colors.GREEN}INFO{Colors.RESET}] {message}')
def warning(self, message):
print(f'[WARNING] {message}')
print(f'[{Colors.YELLOW}WARNING{Colors.RESET}] {message}')
def error(self, message, exit_code=1):
print(f'[ERROR] {message}')
print(f'[{Colors.RED}ERROR{Colors.RESET}] {message}')
sys.exit(exit_code)

View File

@ -1,14 +1,22 @@
from dataclasses import dataclass
from dataclasses import dataclass, field, asdict
from typing import List
import yaml
@dataclass
class Host:
ip: str
user: str
def __repr__(self) -> str:
return yaml.dump(asdict(self), default_flow_style=False)
@dataclass
class Service:
name: str
host: Host
compose_file: str
env_file: str
env_file: str
host: Host = field(default_factory=Host)
ports: List[int] = field(default_factory=list)
def __repr__(self) -> str:
return yaml.dump(asdict(self), default_flow_style=False)

View File

@ -6,6 +6,7 @@ from dataclass.service import Service, Host
from base import BaseCli
import yaml
import subprocess
import os
from typing import List
from constants import SERVICES_DIR_RELATIVE_GIT_ROOT
@ -56,12 +57,19 @@ class DockerServiceCLI(BaseCli):
name=service_data['name'],
host=host,
compose_file=f"{project_path}/{service_data['composeFile']}",
env_file=f"{project_path}/{service_data['envFile']}"
env_file=f"{project_path}/{service_data['envFile']}",
ports=service_data.get('ports', [])
)
services.append(service)
return services
def run_docker_command(self, service: Service):
self.info(f"Processing service:\n{service}")
for i in range(0, len(service.ports)):
self.info(f'Set env SVC_PORT_{i+1}={str(service.ports[i])}')
os.environ[f'SVC_PORT_{i+1}'] = str(service.ports[i])
command = [
'docker', '-H', f"{service.host.user}@{service.host.ip}",
'compose', '-f', f"{service.compose_file}",
@ -71,7 +79,6 @@ class DockerServiceCLI(BaseCli):
try:
subprocess.run(command, check=True)
self.info(f"Service: {service.name} running on host: {service.host.ip}")
except subprocess.CalledProcessError as e:
self.error(f"Error executing command: {' '.join(command)}, return code: {e.returncode}")