name: <%= siteSlug %>
env_file:
  - .env
proxy:
  nginx:
    - <%= siteSlug %>.<%= domain %>
<% if ( multisite ) { %>
    - '*.<%= siteSlug %>.<%= domain %>'
<% } %>
<% if ( phpmyadmin ) { %>
  phpmyadmin:
    - <%= siteSlug %>-pma.<%= domain %>
<% } %>
<% if ( mailpit ) { %>
  mailpit:
    - <%= siteSlug %>-mailpit.<%= domain %>:8025
<% } %>

keys: false

services:
  nginx:
    type: compose
    ssl: true
    sslExpose: false
    services:
      image: ghcr.io/automattic/vip-container-images/nginx:latest
      volumes:
        - ./nginx/extra.conf:/etc/nginx/conf.extra/extra.conf
<% wpVolumes() %>
      depends_on:
        php:
          condition: service_started
    entrypoint: /usr/sbin/nginx -g "daemon off;"

  php:
    type: compose
    services:
      image: <%= php %>
      command: run.sh
      working_dir: /wp
      env_file:
        - .env
      environment:
        XDEBUG: <%= xdebug ? 'enable' : 'disable' %>
<% if ( xdebugConfig ) { %>
        XDEBUG_CONFIG: "<%= xdebugConfig %>"
<% } %>
<% if ( autologinKey ) { %>
        VIP_DEV_AUTOLOGIN_KEY: "<%= autologinKey %>"
<% } %>
<% if ( cron ) { %>
        ENABLE_CRON: 1
<% } %>
        LANDO_NO_USER_PERMS: 'enable'
        LANDO_NEEDS_EXEC: 1
      depends_on:
        database:
          condition: service_started
        memcached:
          condition: service_started
<% if ( elasticsearch ) { %>
        elasticsearch:
          condition: service_started
<% } %>
        wordpress:
          condition: service_completed_successfully
<% if ( muPlugins.mode == 'image' ) { %>
        vip-mu-plugins:
          condition: service_started
<% } %>
<% if ( appCode.mode == 'image' ) { %>
        demo-app-code:
          condition: service_completed_successfully
<% } %>
      volumes:
        - type: volume
          source: devtools
          target: /dev-tools
          volume:
            nocopy: true
        - type: volume
          source: scripts
          target: /scripts
          volume:
            nocopy: true
<% wpVolumes() %>
    run_as_root:
      - chown www-data:www-data /wp/wp-content/mu-plugins /wp/config /wp/log /wp/wp-content/uploads /wp
<% if ( appCode.mode == 'image' ) { %>
      - chown www-data:www-data /wp/wp-content/plugins
<% } %>
    run:
      - >
        sh /dev-tools/setup.sh
        --host database
        --user root
        --domain "http://<%= siteSlug %>.<%= domain %>/"
        --title "<%= wpTitle %>"
        --wpadmin_password "<%= adminPassword %>"
        <% if ( multisite ) { %>--ms-domain "<%= siteSlug %>.<%= domain %>" <% if ( multisite === true || multisite === 'subdomain' ) { %>--subdomain <% } %> <% } %>

  database:
    type: compose
    services:
<% if ( mariadb ) { %>
      image: mariadb:<%= mariadb %>
      command: docker-entrypoint.sh mysqld --sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --max_allowed_packet=67M
<% } else { %>
      image: mysql:8.4
      command: docker-entrypoint.sh mysqld --sql-mode=ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION --max_allowed_packet=67M --mysql-native-password=ON
<% } %>
      ports:
        - ":3306"
      environment:
        MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
        MYSQL_USER: wordpress
        MYSQL_PASSWORD: wordpress
        MYSQL_DATABASE: wordpress
        LANDO_NO_USER_PERMS: 1
        LANDO_NO_SCRIPTS: 1
        LANDO_NEEDS_EXEC: 1
      volumes:
        - database_data:/var/lib/mysql
    volumes:
      database_data:

  memcached:
    type: compose
    services:
      image: memcached:1.6-alpine
      command: memcached -m 64
      environment:
        LANDO_NO_USER_PERMS: 1
        LANDO_NO_SCRIPTS: 1
        LANDO_NEEDS_EXEC: 1

<% if ( phpmyadmin ) { %>
  phpmyadmin:
    type: compose
    ssl: true
    sslExpose: false
    services:
      image: phpmyadmin:5
      command: /docker-entrypoint.sh apache2-foreground
      environment:
        MYSQL_ROOT_PASSWORD: ''
        PMA_HOSTS: database
        PMA_PORT: 3306
        PMA_USER: root
        PMA_PASSWORD: ''
        UPLOAD_LIMIT: 4G
        LANDO_NO_USER_PERMS: 1
        LANDO_NEEDS_EXEC: 1
      ports:
        - 127.0.0.1::80
      volumes:
        - pma_www:/var/www/html
    volumes:
      pma_www:
<% } %>

<% if ( elasticsearch ) { %>
  elasticsearch:
    type: compose
    services:
      image: elasticsearch:8.18.2
      command: /usr/local/bin/docker-entrypoint.sh
      deploy:
        resources:
          limits:
            memory: 1GB
      environment:
        ELASTICSEARCH_IS_DEDICATED_NODE: 'no'
        ELASTICSEARCH_CLUSTER_NAME: 'bespin'
        ELASTICSEARCH_NODE_NAME: 'lando'
        ELASTICSEARCH_PORT_NUMBER: 9200
        discovery.type: 'single-node'
        xpack.security.enabled: 'false'
        LANDO_NO_USER_PERMS: 1
        LANDO_NO_SCRIPTS: 1
        LANDO_NEEDS_EXEC: 1
      ports:
        - ":9200"
      volumes:
        - search_data:/usr/share/elasticsearch/data
    volumes:
      search_data:
<% } %>

  wordpress:
    type: compose
    services:
      image: ghcr.io/automattic/vip-container-images/wordpress:<%= wordpress.tag %>
      volumes:
        - ./wordpress:/shared
        - devtools:/dev-tools
        - scripts:/scripts
    initOnly: true
    entrypoint: /bin/sh -c '/usr/bin/rsync -ac --delete --chown=${LANDO_HOST_USER_ID}:${LANDO_HOST_GROUP_ID} /wp/ /shared/; /usr/bin/rsync -ac --chown=${LANDO_HOST_USER_ID}:${LANDO_HOST_GROUP_ID} --delete /dev-tools-orig/ /dev-tools/'
    volumes:
      devtools:
      scripts:

<% if ( muPlugins.mode == 'image' ) { %>
  vip-mu-plugins:
    type: compose
    services:
      image: ghcr.io/automattic/vip-container-images/mu-plugins:0.1
      command: /bin/sh /run.sh
      volumes:
        - mu-plugins:/shared
        - type: volume
          source: scripts
          target: /scripts
          volume:
            nocopy: true
      environment:
        LANDO_NO_SCRIPTS: 1
        LANDO_NEEDS_EXEC: 1
        LANDO_HOST_UID: ${LANDO_HOST_USER_ID}
        LANDO_HOST_GID: ${LANDO_HOST_GROUP_ID}
    volumes:
      mu-plugins: {}
    initOnly: true
<% } %>

<% if ( appCode.mode == 'image' ) { %>
  demo-app-code:
    type: compose
    services:
      image: ghcr.io/automattic/vip-container-images/skeleton:latest
      command: exit 0
      volumes:
        - clientcode_clientmuPlugins:/clientcode/client-mu-plugins
        - clientcode_images:/clientcode/images
        - clientcode_languages:/clientcode/languages
        - clientcode_plugins:/clientcode/plugins
        - clientcode_private:/clientcode/private
        - clientcode_themes:/clientcode/themes
        - clientcode_vipconfig:/clientcode/vip-config
      environment:
        LANDO_HOST_UID: ${LANDO_HOST_USER_ID}
        LANDO_HOST_GID: ${LANDO_HOST_GROUP_ID}
    volumes:
      clientcode_clientmuPlugins: {}
      clientcode_images: {}
      clientcode_languages: {}
      clientcode_plugins: {}
      clientcode_private: {}
      clientcode_themes: {}
      clientcode_vipconfig: {}
    initOnly: true
<% } %>

<% if ( mailpit ) { %>
  mailpit:
    ssl: true
    sslExpose: false
    type: compose
    services:
      image: axllent/mailpit:latest
      command: /mailpit
      ports:
        - ":1025"
        - ":8025"
      environment:
        LANDO_NO_USER_PERMS: 1
        LANDO_NEEDS_EXEC: 1
<% } %>

<% if ( photon ) { %>
  photon:
    type: compose
    services:
      image: ghcr.io/automattic/vip-container-images/photon:latest
      command: /usr/sbin/php-fpm
      environment:
        LANDO_NO_USER_PERMS: 1
        LANDO_NO_SCRIPTS: 1
        LANDO_NEEDS_EXEC: 1
      volumes:
        - ./uploads:/usr/share/webapps/photon/uploads:ro
<% } %>

tooling:
  wp:
    service: php
    description: "Run WP-CLI command"
    user: www-data
    cmd:
      - wp

  db-myloader:
    service: php
    description: "Run mydumper's myloader to import database dumps generated by mydumper"
    user: root
    cmd:
      - myloader -h database -u wordpress -p wordpress --database wordpress

  db:
    service: php
    description: "Connect to the DB using mysql client (e.g. allow to run imports)"
    cmd:
      - mysql -hdatabase -uwordpress -pwordpress -Dwordpress

<% function wpVolumes() { %>
        - ./config:/wp/config
        - ./log:/wp/log
        - ./uploads:/wp/wp-content/uploads
        - ./wordpress:/wp
        - ./integrations-config:/wp/config/integrations-config
<% if ( muPlugins.mode == 'image' ) { %>
        - type: volume
          source: mu-plugins
          target: /wp/wp-content/mu-plugins
          volume:
            nocopy: true
<% } else { %>
        - <%= muPlugins.dir %>:/wp/wp-content/mu-plugins
<% } %>
<% if ( appCode.mode == 'image' ) { %>
        - type: volume
          source: clientcode_clientmuPlugins
          target: /wp/wp-content/client-mu-plugins
          volume:
            nocopy: true
        - type: volume
          source: clientcode_images
          target: /wp/wp-content/images
          volume:
            nocopy: true
        - type: volume
          source: clientcode_languages
          target: /wp/wp-content/languages
          volume:
            nocopy: true
        - type: volume
          source: clientcode_plugins
          target: /wp/wp-content/plugins
          volume:
            nocopy: true
        - type: volume
          source: clientcode_private
          # FIXME: Do we need this to be out of /wp for local development?
          target: /wp/wp-content/private
          volume:
            nocopy: true
        - type: volume
          source: clientcode_themes
          target: /wp/wp-content/themes
          volume:
            nocopy: true
        - type: volume
          source: clientcode_vipconfig
          target: /wp/vip-config
          volume:
            nocopy: true
<% } else { %>
        - <%= appCode.dir %>/client-mu-plugins:/wp/wp-content/client-mu-plugins
        - <%= appCode.dir %>/images:/wp/wp-content/images
        - <%= appCode.dir %>/languages:/wp/wp-content/languages
        - <%= appCode.dir %>/plugins:/wp/wp-content/plugins
        - <%= appCode.dir %>/private:/wp/wp-content/private
        - <%= appCode.dir %>/themes:/wp/wp-content/themes
        - <%= appCode.dir %>/vip-config:/wp/vip-config
<% } %>
<% } %>
