version: "3"

env:
  VERSION: 4.2

tasks:
  check:
    preconditions:
      - sh: command -v nc
        msg: netcat (nc) was not found. Please install it and try again.
      - sh: nc -vz 127.0.0.1 27017 || ! [ -z "$TEST_MONGO_URI" ]
        msg: |
          Mongo server not found, please start a server or start a cluster and set the
          appropriate connection variables

  standalone-docker:
    desc: Start a standalone MongoDB instance using a docker container
    cmds:
      - echo "Starting odmantic-mongo-test mongo container"
      - docker run --rm -p 27017:27017 --name mongodb-action-instance -d mongo:$VERSION
    status:
      - docker container inspect mongodb-action-instance
  standalone-docker:up:
    cmds:
      - task: standalone-docker
  standalone-docker:down:
    desc: Stop the standalone instance
    cmds:
      - docker stop mongodb-action-instance
    preconditions:
      - sh: docker container inspect mongodb-action-instance
        msg: The standalone mongo container is not running

  replica-compose:
    desc: Start a replica set MongoDB cluster using docker  compose
    cmds:
      - echo "Starting replica mongo cluster"
      - docker  compose up -d
      - ./replica/start.sh
      - echo "To run the tests using this cluster export the following environment variables:"
      - echo export TEST_MONGO_URI=mongodb://172.16.17.11:27017,172.16.17.12:27017,172.16.17.13:27017/?replicaSet=mongodb-action-replica-set
      - echo export TEST_MONGO_MODE=replicaSet
    status:
      - test -n "$(docker  compose ps --services --filter "status=running")"
    env:
      COMPOSE_FILE: ./replica/docker  compose.yml
  replica-compose:up:
    cmds:
      - task: replica-compose
  replica-compose:down:
    desc: Stop the replica set cluster
    cmds:
      - docker  compose down
    preconditions:
      - sh: test -n "$(docker  compose ps --services --filter "status=running")"
        msg: The replica cluster is not running
    env:
      COMPOSE_FILE: ./replica/docker  compose.yml

  sharded-compose:
    desc: Start a sharded MongoDB cluster using docker  compose
    cmds:
      - echo "Starting sharded mongo cluster"
      - docker  compose up -d
      - ./sharded/start.sh
      - echo "To run the tests using this cluster export the following environment variables:"
      - echo export TEST_MONGO_URI=mongodb://172.16.17.10:27017/?retryWrites=false
      - echo export TEST_MONGO_MODE=sharded
    status:
      - test -n "$(docker  compose ps --services --filter "status=running")"
    env:
      COMPOSE_FILE: ./sharded/docker  compose.yml
  sharded-compose:up:
    cmds:
      - task: sharded-compose
  sharded-compose:down:
    desc: Stop the sharded MongoDB cluster
    cmds:
      - docker  compose down
    preconditions:
      - sh: test -n "$(docker  compose ps --services --filter "status=running")"
        msg: The sharded cluster is not running
    env:
      COMPOSE_FILE: ./sharded/docker  compose.yml
