# Compiles new joystream/node and joystream/apps images if local images not found
# and runs a complete joystream development network
# To prevent build of docker images run docker-compose with "--no-build" arg
version: '3.4'
services:
  joystream-node:
    image: joystream/node:$JOYSTREAM_NODE_TAG
    build:
      # context is relative to the compose file
      context: .
      # dockerfile is relative to the context
      dockerfile: joystream-node.Dockerfile
    container_name: joystream-node
    volumes:
      - /data
    command: --dev --alice --validator --unsafe-ws-external --unsafe-rpc-external --rpc-methods Unsafe --rpc-cors=all --log runtime --base-path /data
    ports:
      - '127.0.0.1:9944:9944'
      - '127.0.0.1:9933:9933'

  colossus-1:
    image: node:14
    container_name: colossus-1
    restart: on-failure
    volumes:
      - /data
      - /keystore
      - type: bind
        source: .
        target: /joystream
    working_dir: /joystream/storage-node-v2
    ports:
      - '127.0.0.1:3333:3333'
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      # ACCOUNT_URI overrides command line arg --accountUri
      - ACCOUNT_URI=${COLOSSUS_1_ACCOUNT_URI}
    command: [
      'yarn', 'storage-node', 'server', '--worker=${COLOSSUS_1_WORKER_ID}', '--port=3333', '--uploads=/data',
      '--sync', '--syncInterval=1',
      '--queryNodeHost=${COLOSSUS_QUERY_NODE_HOST}',
      '--apiUrl=${JOYSTREAM_NODE_WS}'
    ]

  distributor-1:
    image: node:14
    container_name: distributor-1
    restart: on-failure
    volumes:
      - /data
      - /cache
      - /logs
      - type: bind
        source: .
        target: /joystream
    # let the working_dir be the distributor node to pickup the config.yml file
    working_dir: /joystream/distributor-node
    ports:
      - 127.0.0.1:3334:3334
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    # Node configuration can be overriden via env, for exampe:
    environment:
      JOYSTREAM_DISTRIBUTOR__ID: distributor-1
      JOYSTREAM_DISTRIBUTOR__ENDPOINTS__QUERY_NODE: ${DISTRIBUTOR_QUERY_NODE_URL}
      JOYSTREAM_DISTRIBUTOR__KEYS: "[{\"suri\":\"${DISTRIBUTOR_1_ACCOUNT_URI}\"}]"
      JOYSTREAM_DISTRIBUTOR__WORKER_ID: ${DISTRIBUTOR_1_WORKER_ID}
      JOYSTREAM_DISTRIBUTOR__PORT: 3334
      JOYSTREAM_DISTRIBUTOR__ENDPOINTS__JOYSTREAM_NODE_WS: ${JOYSTREAM_NODE_WS}
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__ASSETS: /data
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__CACHE_STATE: /cache
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__LOGS: /logs
    #   JOYSTREAM_DISTRIBUTOR__ENDPOINTS__ELASTIC_SEARCH: es-endpoint
    #   JOYSTREAM_DISTRIBUTOR__LOG__CONSOLE: "off"
    #   JOYSTREAM_DISTRIBUTOR__LOG__FILE: "off"
    #   JOYSTREAM_DISTRIBUTOR__LOG__ELASTIC: "off"
    #   JOYSTREAM_DISTRIBUTOR__LIMITS__STORAGE: 50G
    #   JOYSTREAM_DISTRIBUTOR__BUCKETS: "[1,2]"
    command: ['yarn', 'joystream-distributor', 'start']

  colossus-2:
    image: node:14
    container_name: colossus-2
    restart: on-failure
    volumes:
      - /data
      - /keystore
      - type: bind
        source: .
        target: /joystream
    working_dir: /joystream/storage-node-v2
    ports:
      - '127.0.0.1:3335:3333'
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      # ACCOUNT_URI overrides command line arg --accountUri
      - ACCOUNT_URI=${COLOSSUS_2_ACCOUNT_URI}
    command: [
      'yarn', 'storage-node', 'server', '--worker=${COLOSSUS_2_WORKER_ID}', '--port=3333', '--uploads=/data',
      '--sync', '--syncInterval=1',
      '--queryNodeHost=${COLOSSUS_QUERY_NODE_HOST}',
      '--apiUrl=${JOYSTREAM_NODE_WS}'
    ]

  distributor-2:
    image: node:14
    container_name: distributor-2
    restart: on-failure
    volumes:
      - /data
      - /cache
      - /logs
      - type: bind
        source: .
        target: /joystream
    # let the working_dir be the distributor node to pickup the config.yml file
    working_dir: /joystream/distributor-node
    ports:
      - 127.0.0.1:3336:3334
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    # Node configuration can be overriden via env, for exampe:
    environment:
      JOYSTREAM_DISTRIBUTOR__ID: distributor-2
      JOYSTREAM_DISTRIBUTOR__ENDPOINTS__QUERY_NODE: ${DISTRIBUTOR_QUERY_NODE_URL}
      JOYSTREAM_DISTRIBUTOR__KEYS: "[{\"suri\":\"${DISTRIBUTOR_2_ACCOUNT_URI}\"}]"
      JOYSTREAM_DISTRIBUTOR__WORKER_ID: ${DISTRIBUTOR_2_WORKER_ID}
      JOYSTREAM_DISTRIBUTOR__PORT: 3334
      JOYSTREAM_DISTRIBUTOR__ENDPOINTS__JOYSTREAM_NODE_WS: ${JOYSTREAM_NODE_WS}
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__ASSETS: /data
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__CACHE_STATE: /cache
      JOYSTREAM_DISTRIBUTOR__DIRECTORIES__LOGS: /logs
    #   JOYSTREAM_DISTRIBUTOR__ENDPOINTS__ELASTIC_SEARCH: es-endpoint
    #   JOYSTREAM_DISTRIBUTOR__LOG__CONSOLE: "off"
    #   JOYSTREAM_DISTRIBUTOR__LOG__FILE: "off"
    #   JOYSTREAM_DISTRIBUTOR__LOG__ELASTIC: "off"
    #   JOYSTREAM_DISTRIBUTOR__LIMITS__STORAGE: 50G
    #   JOYSTREAM_DISTRIBUTOR__BUCKETS: "[1,2]"
    command: ['yarn', 'joystream-distributor', 'start']

  db:
    image: postgres:12
    container_name: db
    restart: always
    ports:
      - '127.0.0.1:${DB_PORT}:${DB_PORT}'
    volumes:
      - /var/lib/postgresql/data
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASS}
      POSTGRES_DB: ${INDEXER_DB_NAME}

  graphql-server:
    image: node:14
    container_name: graphql-server
    restart: unless-stopped
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      - DB_HOST=db
      - WARTHOG_APP_PORT=${GRAPHQL_SERVER_PORT}
    ports:
      - '127.0.0.1:${GRAPHQL_SERVER_PORT}:${GRAPHQL_SERVER_PORT}'
    depends_on:
      - db
    volumes:
      - type: bind
        source: .
        target: /joystream
    working_dir: /joystream
    command: ['yarn', 'workspace', 'query-node-root', 'query-node:start:prod']

  processor:
    image: node:14
    container_name: processor
    restart: unless-stopped
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      - INDEXER_ENDPOINT_URL=${PROCESSOR_INDEXER_GATEWAY}
      - TYPEORM_HOST=db
      - TYPEORM_DATABASE=${DB_NAME}
    depends_on:
      - db
    volumes:
      - type: bind
        source: .
        target: /joystream
    working_dir: /joystream
    command: ['yarn', 'workspace', 'query-node-root', 'processor:start']

  indexer:
    image: joystream/hydra-indexer:3.0.0
    container_name: indexer
    restart: unless-stopped
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      - DB_NAME=${INDEXER_DB_NAME}
      - INDEXER_WORKERS=5
      - REDIS_URI=redis://redis:6379/0
      - TYPES_JSON=types.json
      - WS_PROVIDER_ENDPOINT_URI=${JOYSTREAM_NODE_WS}
      - DB_HOST=db
    depends_on:
      - db
      - redis
    volumes:
      - ./types/augment/all/defs.json:/home/hydra/packages/hydra-indexer/types.json
    command: >
      sh -c "yarn db:bootstrap && yarn start:prod"

  hydra-indexer-gateway:
    image: joystream/hydra-indexer-gateway:3.0.0
    container_name: hydra-indexer-gateway
    restart: unless-stopped
    env_file:
      # relative to working directory where docker-compose was run from
      - .env
    environment:
      - WARTHOG_STARTER_DB_DATABASE=${INDEXER_DB_NAME}
      - WARTHOG_STARTER_DB_HOST=db
      - WARTHOG_STARTER_DB_PASSWORD=${DB_PASS}
      - WARTHOG_STARTER_DB_PORT=${DB_PORT}
      - WARTHOG_STARTER_DB_USERNAME=${DB_USER}
      - WARTHOG_STARTER_REDIS_URI=redis://redis:6379/0
      - WARTHOG_APP_PORT=${HYDRA_INDEXER_GATEWAY_PORT}
      - PORT=${HYDRA_INDEXER_GATEWAY_PORT}
      - DEBUG=*
    ports:
      - '127.0.0.1:${HYDRA_INDEXER_GATEWAY_PORT}:${HYDRA_INDEXER_GATEWAY_PORT}'
    depends_on:
      - db
      - redis

  redis:
    image: redis:6.0-alpine
    container_name: redis
    restart: always
    ports:
      - '127.0.0.1:6379:6379'

  pioneer:
    image: nginx
    container_name: pioneer
    volumes:
      - ./pioneer/packages/apps/build:/usr/share/nginx/html
    ports:
      - "127.0.0.1:3000:80"
    environment:
      - NGINX_PORT=80
