version: '3.5'

networks:
    # Note that the network names are used by dctest.py's clean option.
    prplmesh:
        name: prplMesh-net-$RUN_ID
        driver: bridge
    ucc:
        # TODO: remove '1-' once docker-compose v3 has a way to set network priority
        name: 1-prplMesh-net-$RUN_ID-ucc
        driver: bridge

x-prplmesh: &prplmesh # Template for the prplmesh services
    image: registry.gitlab.com/prpl-foundation/prplmesh/prplmesh/prplmesh-builder-ubuntu-bionic:${IMAGE_TAG}
    privileged: true    # For the creation of the bridge to work
    networks:
        - ucc
        - prplmesh
    environment:
        - USER
        - INSTALL_DIR=${FINAL_ROOT_DIR}/build/install
        - ROOT_DIR=${FINAL_ROOT_DIR}
        - RUN_ID
        - IMAGE_TAG
    ports:
        - "8002"

services:
    # Controller and agent are run from inside the boardfarm container
    controller:
        <<: *prplmesh
        container_name: lan-$RUN_ID
        entrypoint: ["/root/start-prplmesh.sh", "start-controller-agent"]
        volumes:
            - "/lib/modules:/lib/modules:ro"
            - "$ROOT_DIR:${FINAL_ROOT_DIR}"
            - "$ROOT_DIR/logs/lan-$RUN_ID/:/tmp/beerocks/logs/"

    agent:
        <<: *prplmesh
        container_name: agent-$RUN_ID
        entrypoint: ["bash", "-c", "sleep 15; /root/start-prplmesh.sh start-agent"]
        volumes:
            - "/lib/modules:/lib/modules:ro"
            - "$ROOT_DIR:${FINAL_ROOT_DIR}"
            - "$ROOT_DIR/logs/agent-$RUN_ID/:/tmp/beerocks/logs/"
        depends_on:
          - controller

    agent_2:
        <<: *prplmesh
        container_name: lan2-$RUN_ID
        entrypoint: ["bash", "-c", "sleep 25; /root/start-prplmesh.sh start-agent"]
        volumes:
            - "/lib/modules:/lib/modules:ro"
            - "$ROOT_DIR:${FINAL_ROOT_DIR}"
            - "$ROOT_DIR/logs/lan2-$RUN_ID/:/tmp/beerocks/logs/"
        depends_on:
          - controller

    # Boardfarm image is launched from dctest.py, TARGET_DIR refers to the
    # path inside the controller, will be referred as $ROOT_DIR inside
    boardfarm:
        image: registry.gitlab.com/prpl-foundation/prplmesh/prplmesh/prplmesh-boardfarm-ci:${IMAGE_TAG}
        privileged: true
        container_name: boardfarm-$RUN_ID
        environment:
            - USER
            - ROOT_DIR
            - RUN_ID
            - IMAGE_TAG
            - FINAL_ROOT_DIR
            - PYTHONDONTWRITEBYTECODE=1
        network_mode: host
        volumes:
            - "$ROOT_DIR:$ROOT_DIR"
            - "/var/run/docker:/var/run/docker"
            - "/var/run/docker.sock:/var/run/docker.sock"
        working_dir: $ROOT_DIR/tests
        entrypoint: ["bash","-c","$ROOT_DIR/tools/docker/boardfarm-ci/start.sh; ./run_bf.sh --dut $DUT --test-suite $TEST_SUITE"]
        depends_on:
          - controller
          - agent
          - agent_2
