import requests import configparser import docker import subprocess # Get config config = configparser.ConfigParser() config.read("env.toml") git_server_url = config["DEFAULT"]["git_server_url"] git_server_user = config["DEFAULT"]["git_username"] git_server_pass = config["DEFAULT"]["git_password"] repo_path = config["DEFAULT"]["repo_path"] docker_username = config["DEFAULT"]["docker_username"] docker_password = config["DEFAULT"]["docker_password"] docker_registry = config["DEFAULT"]["docker_registry"] home_dir = config["DEFAULT"]["home_dir"] host = config["DEFAULT"]["host"] # Get branches branches = requests.get(f"{git_server_url}/api/v1/repos/{repo_path}/branches") branch_names = [branch["name"] for branch in branches.json()] # Get docker registry images client = docker.DockerClient(base_url="unix://var/run/docker.sock") creds = client.login(username=docker_username, password=docker_password, registry=docker_registry) all_images = client.images.pull(f"{docker_registry}/test/test", auth_config=creds, all_tags=True) registry_images = [i.attrs["RepoTags"][0] for i in all_images if len(i.attrs["RepoTags"])] # Map all branch names into images branch_reg_map = {name: branch for name in branch_names for branch in registry_images if branch.split(":")[-1] == name} print(branch_reg_map) def generate_service_str(branch_name, image_path): return f""" {branch_name}: image: "{image_path}" container_name: "{branch_name}" labels: - "traefik.enable=true" - "traefik.http.routers.{branch_name}.rule=Host(`{host}`) && PathPrefix(`/branch/{branch_name}`)" - "traefik.http.routers.{branch_name}.entrypoints=web" - "traefik.http.routers.{branch_name}.tls=false" - "traefik.http.middlewares.{branch_name}-strip.stripprefix.prefixes=/branch/{branch_name}" - "traefik.http.middlewares.{branch_name}-strip.stripprefix.forceslash=false" - "traefik.http.routers.{branch_name}.middlewares={branch_name}-strip" environment: PERSEUS_BASE_PATH: "/branch/{branch_name}" PERSEUS_PORT: "80" PERSEUS_HOST: "0.0.0.0" """ # Generate docker config compose_str = f""" version: "3.8" services: traefik: image: "traefik:v2.10" container_name: "traefik" command: - "--api.insecure=true" - "--providers.docker=true" - "--providers.docker.exposedbydefault=false" - "--entrypoints.web.address=:80" - "--accesslog=true" - "--accesslog.fields.defaultmode=keep" - "--log.level=DEBUG" ports: - "80:80" - "8080:8080" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" """ for name, image in branch_reg_map.items(): compose_str += generate_service_str(name, image) # Write the docker compose with open(f"{home_dir}/docker-compose.yaml", "w") as text_file: text_file.write(compose_str) # Bring docker compose down and up try: out = subprocess.run("docker compose down", shell=True, check=True, cwd=f"{home_dir}") print(out) except Exception: pass out = subprocess.run("docker compose pull", shell=True, check=True, cwd=f"{home_dir}") print(out) out = subprocess.run("docker compose up -d", shell=True, check=True, cwd=f"{home_dir}") print(out) exit(0)