version: '3.8'
services:
  nginx:
    image: nginx:1.19.7-alpine
    restart: always
    ports:
      - "4444:80"
    depends_on:
      frontend:
        condition: service_started
    command: nginx -g "daemon off;"
    volumes:
      - ./apps/web/ws-nginx.local.conf:/etc/nginx/conf.d/default.conf
      - ./lustre/:/lustre/aoc/cluster/pipeline/docker/workspaces
      - ./delivery_root:/tmp/delivery_root
  db:
    image: ${BASE_REGISTRY_URL}/db
    build:
      context: .
      dockerfile: ./ci/psql/Dockerfile.db
    restart: always
    ports:
      - "54322:5432"
    environment:
      POSTGRES_USER: archive
      POSTGRES_PASSWORD: docker
    command: postgres -c listen_addresses=*
    healthcheck:
      test: pg_isready -U archive -d archive
      start_period: 5s
      interval: 2s
      timeout: 2s
      retries: 5
    volumes:
      - ./pgdata:/var/lib/postgresql/data

  amqp:
    image: rabbitmq:3.8-management
    restart: always
    ports:
      - "15672:15672"
    healthcheck:
      test: rabbitmq-diagnostics -q ping
      start_period: 5s
      interval: 2s
      timeout: 2s
      retries: 5

  condor-cm:
    build:
      context: .
      dockerfile: ./config/htcondor/cm/Dockerfile.local
    ports:
      - 9618

  condor-execute:
    build:
      context: .
      dockerfile: ./config/htcondor/execute/Dockerfile.local
      secrets:
        - host_ssh_key # Needed to clone a repo from gitlab, will be mounted during the build
    volumes:
      - ./delivery_root:/tmp/delivery_root
      - ./lustre/aoc/cluster/pipeline/docker/workspaces:/lustre/aoc/cluster/pipeline/docker/workspaces
      - ./lustre/aoc/cluster/pipeline/vlass_docker:/lustre/aoc/cluster/pipeline/vlass_docker

  pex-watcher:
    build:
      context: .
      dockerfile: ./config/pex-watcher/Dockerfile.pex
    volumes:
      - ./config/pex-watcher:/config
      - ./apps/cli/executables/pexable/:/config/apps/cli/executables/pexable/
      - ./lustre/aoc/cluster/pipeline/docker/workspaces/sbin:/lustre/aoc/cluster/pipeline/docker/workspaces/sbin

  go-watcher:
    tty: true
    build:
      context: .
      dockerfile: ./config/go-watcher/Dockerfile
    volumes:
      - ./config/go-watcher:/config
      - ./apps/cli/executables/go/:/config/apps/cli/executables/go/
      - ./lustre/aoc/cluster/pipeline/docker/workspaces/sbin:/lustre/aoc/cluster/pipeline/docker/workspaces/sbin

  schema:
    build:
      context: .
      dockerfile: ./shared/workspaces/alembic/Dockerfile.local
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./shared:/code/shared
      - ./docker.properties:/home/ssa/capo/docker.properties

  # Used for doing a DB migration on dsoc-dev
  # Run the command `docker compose -f docker-compose.local.yml --profile schema-dev up schema-dev` and the migration will run
  schema-dev:
    build:
      context: .
      dockerfile: ./shared/workspaces/alembic/Dockerfile.local
    command: ["./bin/run-migrations.sh", "dsoc-dev"]
    volumes:
      - ./shared:/code/shared
      - ~/.capo:/home/ssa/capo
    profiles:
      - schema-dev

  # Used for doing a DB migration on dsoc-test
  # Run the command `docker compose -f docker-compose.local.yml --profile schema-test up schema-test` and the migration will run
  schema-test:
    build:
      context: .
      dockerfile: ./shared/workspaces/alembic/Dockerfile.local
    command: ["./bin/run-migrations.sh", "dsoc-test"]
    volumes:
      - ./shared:/code/shared
      - ~/.capo:/home/ssa/capo
    profiles:
      - schema-test

  # Used for doing a DB migration on dsoc-prod
  # Run the command `docker compose -f docker-compose.local.yml --profile schema-prod up schema-prod` and the migration will run
  schema-prod:
    build:
      context: .
      dockerfile: ./shared/workspaces/alembic/Dockerfile.local
    command: ["./bin/run-migrations.sh", "dsoc-prod"]
    volumes:
      - ./shared:/code/shared
      - ~/.capo:/home/ssa/capo
    profiles:
      - schema-prod

  workflow:
    build:
      context: .
      dockerfile: ./services/workflow/Dockerfile
      target: dev
      args:
        - CACHE_IMAGE_TAG=${TAG}
        - LOCAL_OR_SERVER_PEX=${LOCAL_OR_SERVER_PEX}
        - DEPLOY_ENV=${DEPLOY_ENV}
    ports:
      - "3456:3456"
      - 9618
    depends_on:
      schema:
        condition: service_completed_successfully
      amqp:
        condition: service_healthy
    extra_hosts:
      # Allow the workflow container to reach the RADIAL cluster for jobs that require it
      - "radialhead.nrao.radial.local:10.64.1.77"
    healthcheck:
      test: curl -f -LI localhost:3456/healthcheck
      interval: 5s
      retries: 5
      start_period: 5s
      timeout: 5s
    volumes:
      - ./services/workflow:/code/services/workflow
      - ./shared:/code/shared
      - ./apps/cli:/code/apps/cli
      - ./testing:/code/testing
      - ./lustre/aoc/cluster/pipeline/docker/workspaces:/lustre/aoc/cluster/pipeline/docker/workspaces
      - ./lustre/aoc/cluster/pipeline/vlass_docker:/lustre/aoc/cluster/pipeline/vlass_docker
      - ./delivery_root:/tmp/delivery_root
      - ~/.capo:/home/ssa/capo
      - ~/.ssh:/home/vlapipe/.ssh
      - ./docker.properties:/home/ssa/capo/docker.properties
      - condor:/var/spool/condor

  capability:
    build:
      context: .
      dockerfile: ./services/capability/Dockerfile
      target: dev
      args:
        - CACHE_IMAGE_TAG=${TAG}
    ports:
      - "3457:3457"
    depends_on:
      schema:
        condition: service_completed_successfully
      workflow:
        condition: service_healthy
      notification:
        condition: service_healthy
      amqp:
        condition: service_healthy
    healthcheck:
      test: curl -f -LI localhost:3457/healthcheck
      interval: 5s
      retries: 5
      start_period: 5s
      timeout: 5s
    volumes:
      - ./docker.properties:/home/ssa/capo/docker.properties
      - ./services/capability:/code/services/capability
      - ./shared:/code/shared
      - ./apps/cli:/code/apps/cli
      - ./testing:/code/testing


  notification:
    build:
      context: .
      dockerfile: ./services/notification/Dockerfile
      target: dev
      args:
        - CACHE_IMAGE_TAG=${TAG}
        - DEPLOY_ENV=${DEPLOY_ENV}
    ports:
      - "3458:3458"
    depends_on:
      schema:
        condition: service_completed_successfully
      amqp:
        condition: service_healthy
    healthcheck:
      test: curl -f -LI http://localhost:3458/healthcheck
      interval: 5s
      retries: 5
      start_period: 5s
      timeout: 5s
    volumes:
      - ./docker.properties:/home/ssa/capo/docker.properties
      - ./services/notification:/code/services/notification
      - ./shared:/code/shared
      - ./apps/cli:/code/apps/cli
      - ./testing:/code/testing


  frontend:
    build:
      context: .
      dockerfile: ./apps/web/Dockerfile
      target: dev
    environment:
      NG_APP_WS_VERSION: "local"
    init: true
    ports:
      - "4200:4200"
    depends_on:
      capability:
        condition: service_healthy
      workflow:
        condition: service_healthy
    volumes:
      - ./apps/web:/code
      - /code/node_modules

volumes:
  condor:

# Store the host's SSH key as a secret to have it removed after building the container, note only RSA for now
secrets:
  host_ssh_key:
    file: ~/.ssh/id_rsa