commit cd746d915c084fad017962794274223cfb3ecfa6 Author: trinkey Date: Tue Dec 24 13:52:15 2024 -0500 Initial commit diff --git a/clone-all.sh b/clone-all.sh new file mode 100755 index 0000000..6109822 --- /dev/null +++ b/clone-all.sh @@ -0,0 +1,9 @@ +# This script clones all relevant repositories into respective folders in the current working directory. + +git_url="https://git.trinkey.com/t" +declare -a repositories=("common" "auth" "message") + +for service in "${repositories[@]}" +do + git clone "$git_url/$service.git" $service +done diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..19a5f04 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.8" + +services: + common: + build: ./common + ports: + - "8888:8888" + restart: always + auth: + build: ./auth + depends_on: + - common + ports: + - "8800:8800" + restart: always + volumes: + - /home/trinkey/test/tsuite/:/home/trinkey/test/tsuite/ + message: + build: ./message + depends_on: + - auth + ports: + - "8001:8001" + restart: always + volumes: + - /home/trinkey/test/tsuite/:/home/trinkey/test/tsuite/ diff --git a/setup-docker.py b/setup-docker.py new file mode 100644 index 0000000..1b8f1cf --- /dev/null +++ b/setup-docker.py @@ -0,0 +1,126 @@ +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"] +required_services = ["common", "auth"] + +databases = { + "auth": "auth.sqlite3", + "message": "message.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" } +} + +depends = { + "auth": "common", + "message": "auth" +} + +enabled_applications = {} + +storage_location = Path(input("Enter the location you want to store databases (/var/tsuite/): ") or "/var/tsuite/") +makedirs(storage_location) + +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 python3 manage.py migrate)") diff --git a/update-all.sh b/update-all.sh new file mode 100755 index 0000000..f20aaf5 --- /dev/null +++ b/update-all.sh @@ -0,0 +1,11 @@ +# This script pulls new changes to all relevant repositories into respective folders in the current working directory. + +git_url="https://git.trinkey.com/t" +declare -a repositories=("common" "auth" "message") + +for service in "${repositories[@]}" +do + cd $service + git stash && git pull && git stash pop + cd .. +done