scripts/setup-docker.py
2024-12-31 20:07:58 -05:00

133 lines
4.3 KiB
Python

from os import makedirs
from pathlib import Path
print("Docker configuration\n" + ("-" * 20))
print("You may need to run this with sudo")
possible_services = ["message", "blog"]
required_services = ["common", "auth"]
databases = {
"auth": "auth.sqlite3",
"message": "message.sqlite3",
"blog": "blog.sqlite3"
}
defaults = {
"common": { "port": "8888", "public_url": "http://localhost:8888" },
"auth": { "port": "8000", "public_url": "http://localhost:8000" },
"message": { "port": "8001", "public_url": "http://localhost:8001" },
"blog": { "port": "8002", "public_url": "http://localhost:8002" }
}
depends = {
"auth": "common",
"message": "auth"
"blog": "auth"
}
enabled_applications = {}
storage_location = Path(input("Enter the location you want to store databases (/var/tsuite/): ") or "/var/tsuite/")
try:
makedirs(storage_location)
except FileExistsError:
...
docker_compose = """version: "3.8"
services:
"""
def get_service_config(name: str, config: dict) -> tuple[str, str]:
return f""" {name}:
build: ./{name}{f'''
depends_on:
- {depends[name]}''' if name in depends else ""}
ports:
- "{config["port"]}:{config["port"]}"
restart: always{f'''
volumes:
- {storage_location}:{storage_location}''' if name in databases else ""}\n""", f"""FROM python:3.10
WORKDIR /usr/src/app
COPY . .
RUN pip install --no-cache-dir --upgrade -r requirements.txt{'''
CMD ["python", "manage.py", "migrate"]''' if name in databases else ""}
EXPOSE {config["port"]}
CMD ["python", "manage.py", "runserver", "0.0.0.0:{config["port"]}"]\n"""
common_port = 0
for service in required_services:
print(f"t{service.capitalize()} -")
port = input(f"What port do you want to use ({defaults[service]['port']})? Must be unique. ")
port = int(port) if port.isdigit() else defaults[service]["port"]
public_url = input(f"Enter the public url ({defaults[service]['public_url']}): ") or defaults[service]["public_url"]
strings = get_service_config(service, {
"port": port,
"public_url": public_url
})
docker_compose += strings[0]
if service == "common":
common_port = port
else:
enabled_applications[service] = (
f"Secret t{service.capitalize()}-specific token",
f"http://{service}:{port}",
public_url
)
with open(f"{service}/Dockerfile", "w") as f:
f.write(strings[1])
with open(f"{service}/config.py", "a") as f:
f.write(f"\nDB_PATH = '{storage_location}/{databases[service]}'" if service in databases else "")
f.write(f"\ntCOMMON_URL_INTERNAL = 'http://common:{common_port}'" if service != "common" else "")
print("\n" + ("-" * 20) + "\n")
for service in possible_services:
print(f"t{service.capitalize()} -")
if not (input("Enable? (Y/n): ").lower() or "y").startswith("y"):
continue
port = input(f"What port do you want to use ({defaults[service]['port']})? Must be unique. ")
port = int(port) if port.isdigit() else defaults[service]["port"]
public_url = input(f"Enter the public url ({defaults[service]['public_url']}): ") or defaults[service]["public_url"]
strings = get_service_config(service, {
"port": port,
"public_url": public_url
})
docker_compose += strings[0]
enabled_applications[service] = (
f"Secret t{service.capitalize()}-specific token",
f"http://{service}:{port}",
public_url
)
with open(f"{service}/Dockerfile", "w") as f:
f.write(strings[1])
with open(f"{service}/config.py", "a") as f:
f.write(f"\nDB_PATH = '{storage_location}/{databases[service]}'" if service in databases else "")
f.write(f"\ntCOMMON_URL_INTERNAL = 'http://common:{common_port}'" if service != "common" else "")
print("\n" + ("-" * 20) + "\n")
with open("docker-compose.yml", "w") as f:
f.write(docker_compose)
with open("common/config.py", "a") as f:
f.write(f"ENABLED_APPLICATIONS = {repr(enabled_applications)}")
print("Docker configuration files generated. You may have to manually edit the config.py files in each service. Be sure to change the tokens to a non-default value.\nWhen running the web server, you may also need to migrate the databases manually (sudo docker exec -id <container name> python3 manage.py migrate)")