services:
    mongo-1-1:
        container_name: "mongo-1-1"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_S1_PRIMARY_PORT:-17001}:27017"
        command: mongod --replSet rs1 --shardsvr --port 27017  --oplogSize 16  --bind_ip 0.0.0.0
        networks:
            - rs1
        depends_on:
          - "mongo-1-2"
          - "mongo-1-3"
          - "mongo-1-arbiter"

    mongo-1-2:
        container_name: "mongo-1-2"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_S1_SECONDARY1_PORT:-17002}:27017"
        command: mongod --replSet rs1 --shardsvr --port 27017  --oplogSize 16  --bind_ip 0.0.0.0
        networks:
            - rs1

    mongo-1-3:
        container_name: "mongo-1-3"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_S1_SECONDARY2_PORT:-17003}:27017"
        command: mongod --replSet rs1 --shardsvr --port 27017  --oplogSize 16  --bind_ip 0.0.0.0
        networks:
            - rs1

    mongo-1-arbiter:
        container_name: "mongo-1-arbiter"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_S1_ARBITER:-17011}:27017"
        command: mongod --replSet rs1 --shardsvr --port 27017  --oplogSize 16
        networks:
            - rs1

    mongo-rs1-setup:
        container_name: "mongo-rs1-setup"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        depends_on:
            - "mongo-1-1"
            - "mongo-1-2"
            - "mongo-1-3"
            - "mongo-1-arbiter"
        volumes:
            - ./test-setup/scripts:/scripts
        environment:
            - MONGO1=mongo-1-1
            - MONGO2=mongo-1-2
            - MONGO3=mongo-1-3
            - ARBITER=mongo-1-arbiter
            - RS=rs1
            - VERSION=${TEST_MONGODB_IMAGE}
        entrypoint: [ "/scripts/setup.sh" ]
        networks:
            - rs1

    mongo-2-2:
        container_name: "mongo-2-2"
        build:
            dockerfile: test-setup/mongodb-auth.dockerfile
        environment:
            - MONGO_INITDB_ROOT_USERNAME=${TEST_MONGODB_USERNAME:-admin}
            - MONGO_INITDB_ROOT_PASSWORD=${TEST_MONGODB_PASSWORD:-admin}
        ports:
            - "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017"
        command: mongod --replSet rs2 --port 27017  --oplogSize 16 --auth --keyFile=/opt/keyfile
        networks:
            - rs2

    mongo-2-3:
        container_name: "mongo-2-3"
        build:
            dockerfile: test-setup/mongodb-auth.dockerfile
        ports:
            - "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017"
        command: mongod --replSet rs2 --port 27017  --oplogSize 16 --auth --keyFile=/opt/keyfile
        networks:
            - rs2

    mongo-2-1:
        container_name: "mongo-2-1"
        build:
            dockerfile: test-setup/mongodb-auth.dockerfile
        ports:
            - "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017"
        command: mongod --replSet rs2 --port 27017  --oplogSize 16 --auth --keyFile=/opt/keyfile
        networks:
            - rs2

    mongo-2-arbiter:
        container_name: "mongo-2-arbiter"
        build:
            dockerfile: test-setup/mongodb-auth.dockerfile
        ports:
            - "${TEST_MONGODB_S2_ARBITER:-17012}:27017"
        command: mongod --replSet rs2 --port 27017  --oplogSize 16 --auth --keyFile=/opt/keyfile
        networks:
            - rs2

    pbm-mongo-2-1:
        image: percona/percona-backup-mongodb:2.5.0-multi
        container_name: "pbm-mongo-2-1"
        restart: on-failure:5
        depends_on:
            -   mongo-2-1
        environment:
            - PBM_MONGODB_URI=mongodb://admin:admin@mongo-2-1:27017
        volumes:
            - ./test-setup/pbm/config:/etc/config
            - ./test-setup/scripts:/scripts
            - pbm-backups:/opt/backups
        networks:
            - rs2

    pbm-mongo-2-2:
        image: percona/percona-backup-mongodb:2.5.0-multi
        container_name: "pbm-mongo-2-2"
        restart: on-failure:5
        depends_on:
            -   mongo-2-2
        environment:
            - PBM_MONGODB_URI=mongodb://admin:admin@mongo-2-2:27017
        volumes:
            - ./test-setup/pbm/config:/etc/config
            - ./test-setup/scripts:/scripts
            - pbm-backups:/opt/backups
        networks:
            - rs2

    pbm-mongo-2-3:
        image: percona/percona-backup-mongodb:2.5.0-multi
        container_name: "pbm-mongo-2-3"
        restart: on-failure:5
        depends_on:
            -   mongo-2-3
        environment:
            - PBM_MONGODB_URI=mongodb://admin:admin@mongo-2-3:27017
        volumes:
            - ./test-setup/pbm/config:/etc/config
            - ./test-setup/scripts:/scripts
            - pbm-backups:/opt/backups
        networks:
            - rs2

    mongo-rs2-setup:
        container_name: "mongo-rs2-setup"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        depends_on:
            - "mongo-2-1"
            - "mongo-2-2"
            - "mongo-2-3"
            - "mongo-2-arbiter"
        volumes:
            - ./test-setup/scripts:/scripts
        environment:
            - MONGO1=mongo-2-2
            - MONGO2=mongo-2-1
            - MONGO3=mongo-2-3
            - ARBITER=mongo-2-arbiter
            - RS=rs2
            - VERSION=${TEST_MONGODB_IMAGE}
            - MONGO_INITDB_ROOT_USERNAME=${TEST_MONGODB_USERNAME:-admin}
            - MONGO_INITDB_ROOT_PASSWORD=${TEST_MONGODB_PASSWORD:-admin}
        entrypoint: [ "/scripts/setup.sh" ]
        networks:
            - rs2

    # Config servers
    mongo-cnf-2:
        container_name: "mongo-cnf-2"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
        command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
        networks:
            - cnf-serv

    mongo-cnf-3:
        container_name: "mongo-cnf-3"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
        command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
        networks:
            - cnf-serv

    mongo-cnf-1:
        container_name: "mongo-cnf-1"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
        command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
        networks:
            - cnf-serv
        depends_on:
            - mongo-cnf-2
            - mongo-cnf-3

    mongo-cnf-setup:
        container_name: "mongo-cnf-setup"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        depends_on:
            - "mongo-cnf-1"
            - "mongo-cnf-2"
            - "mongo-cnf-3"
        volumes:
            - ./test-setup/scripts:/scripts
        environment:
            - MONGO1=mongo-cnf-1
            - MONGO2=mongo-cnf-2
            - MONGO3=mongo-cnf-3
            - RS=cnf-serv
            - PORT=27017
            - VERSION=${TEST_MONGODB_IMAGE}
        entrypoint: [ "/scripts/setup.sh","cnf_servers" ]
        networks:
            - cnf-serv

    mongos:
        container_name: "mongos"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_MONGOS_PORT:-17000}:27017"
        networks:
            - mongo-shard
            - rs1
            - rs2
            - cnf-serv
        depends_on:
            - "mongo-rs1-setup"
            - "mongo-rs2-setup"
            - "mongo-cnf-setup"
        command: mongos --configdb cnf-serv/mongo-cnf-1:27017,mongo-cnf-2:27017,mongo-cnf-3:27017 --port 27017 --bind_ip 0.0.0.0

    mongo-shard-setup:
        container_name: "mongo-shard-setup"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        depends_on:
            - "mongos"
        networks:
            - mongo-shard
            - rs1
            - rs2
            - cnf-serv
        volumes:
            - ./test-setup/scripts:/scripts
        environment:
            - MONGOS=mongos
            - MONGO11=mongo-1-1
            - MONGO12=mongo-1-2
            - MONGO13=mongo-1-3
            - MONGO21=mongo-2-1
            - MONGO22=mongo-2-2
            - MONGO23=mongo-2-3
            - RS1=rs1
            - RS2=rs2
            - PORT=27017
            - PORT1=27017
            - PORT2=27017
            - PORT3=27017
            - VERSION=${TEST_MONGODB_IMAGE}
        entrypoint: [ "/scripts/setup-shard.sh" ]
        restart: on-failure:20

    standalone:
        container_name: "standalone"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_STANDALONE_PORT:-27017}:27017"
        command: mongod --port 27017  --oplogSize 16

    standalone-encrypted:
        user: root
        container_name: "standalone-encrypted"
        image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
        ports:
            - "${TEST_MONGODB_STANDALONE_ENCRYPTED_PORT:-27027}:27017"
        volumes:
            - ./test-setup/secret/mongodb_secrets.txt:/secret/mongodb_secrets.txt
            - ./test-setup/scripts:/scripts
        command: /scripts/run-mongodb-encrypted.sh

    kerberos:
        build:
            dockerfile: test-setup/kerberos.dockerfile
        container_name: kerberos
        hostname: kerberos
        environment:
            - KRB5_TRACE=/dev/stderr
            - MONGO_HOST=psmdb-kerberos
            - KERBEROS_HOST=kerberos
            - KRB5_CONFIG=/krb5/krb5.conf
            - MONGO_KERBEROS_USERNAME=pmm-test
            - MONGO_KERBEROS_PASSWORD=password1
        depends_on:
            -   psmdb-kerberos
        volumes:
            - kerberos-config:/krb5
            - ./test-setup/scripts:/scripts
        ports:
            - "88:88/udp"
        entrypoint: [ "sh", "/scripts/setup-krb5-server.sh" ]
        healthcheck:
            test: [ "CMD", "kadmin.local", "-q", "listprincs" ]
            interval: 5s
            timeout: 5s
            retries: 5

    psmdb-kerberos:
        image: percona/percona-server-mongodb:8.0
        container_name: psmdb-kerberos
        hostname: psmdb-kerberos
        ports:
            - 29017:27017
        environment:
            - KRB5_KTNAME=/krb5/mongodb.keytab
            - KRB5CCNAME=/krb5/krb5cc_0
            - KRB5_CONFIG=/krb5/krb5.conf
            - MONGO_INITDB_ROOT_USERNAME=admin
            - MONGO_INITDB_ROOT_PASSWORD=adminpassword
        volumes:
            - ./test-setup/scripts:/scripts
            - kerberos-config:/krb5
        command: >
            mongod --bind_ip_all --auth --setParameter authenticationMechanisms=GSSAPI,SCRAM-SHA-256
volumes:
    pbm-backups:
    kerberos-config:

networks:
    rs1:
    rs2:
    cnf-serv:
    mongo-shard:
