compose.ymlΒΆ

---
# Param: compose_service_up_targets  compose service keys compose-service-up builds and recreates without their dependencies.
- name: "πŸ›‘οΈ Guard instance dir at compose flush"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/guard_instance_dir.yml') }}"
  listen:
    - compose-up
    - compose-build
    - container-recreate
    - compose-service-up

- name: "πŸ” Update the config and secret hashes in compose.yml"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/rehash_configs.yml') }}"
  listen:
    - compose-up
    - container-recreate
    - compose-service-up

- name: πŸ“₯ compose pull
  ansible.builtin.script:
    cmd: >-
      python/pull.py
      --chdir {{ lookup('container', application_id, 'directories.instance') }}
      --project {{ application_id | entity_name }}
      --compose-files {{ lookup('compose_file_args', application_id, include_ca=False) | quote }}
      {% if lookup('container', application_id, 'files.env') is file %}
      --env-file {{ lookup('container', application_id, 'files.env') }}
      {% endif %}
      --lock-dir {{ DIR_COMPOSE_PULL_LOCK }}
      --lock-key {{ lookup('container', application_id, 'directories.instance') | hash('sha1') }}
      --ignore-buildable
  register: pull
  changed_when: "'pulled' in pull.stdout"
  timeout: "{{ lookup('timeout', 3600) | int }}"
  retries: 10
  delay: 30
  until: pull is succeeded
  listen:
    - compose-up

- name: πŸ”¨ Build compose
  ansible.builtin.shell: |
    set -euo pipefail
    compose build --pull || compose build
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_build
  timeout: "{{ lookup('timeout', 3600) | int }}"
  retries: 3
  delay: 30
  until: compose_build is succeeded
  ignore_errors: true
  listen:
    - compose-build

- name: πŸ”¨ Rebuild compose without cache once the retries are exhausted
  ansible.builtin.shell: |
    set -euo pipefail
    compose build --no-cache --pull
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_rebuild
  timeout: "{{ lookup('timeout', 3600) | int }}"
  retries: 3
  delay: 30
  until: compose_rebuild is succeeded
  when: compose_build is failed
  listen:
    - compose-build

- name: πŸ“ Generate CA trust override
  ansible.builtin.shell: |
    set -euo pipefail
    {{ lookup('compose_ca_inject_cmd', application_id) }}
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_ca_override
  timeout: "{{ lookup('timeout', 1200) | int }}"
  retries: 7
  delay: 20
  until: compose_ca_override is succeeded
  listen:
    - compose-up
  when: lookup('ca_injected', application_id)

- name: βœ… Validate Docker Compose configuration
  ansible.builtin.shell: |
    set -euo pipefail
    compose config --quiet
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: dc_validate
  changed_when: false
  failed_when: dc_validate.rc != 0
  listen:
    - compose-up
  when: MODE_ASSERT_LOCAL | bool

- name: "🌐 compose: pre-create external provider networks for the stack"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/network/create.yml') }}"
  vars:
    network_role_id: "{{ external_network_role }}"
  loop: "{{ lookup('compose_external_networks') }}"
  loop_control:
    loop_var: external_network_role
  listen:
    - compose-up

- name: 🐳 compose up
  ansible.builtin.shell: |
    set -euo pipefail
    compose up -d --force-recreate --remove-orphans
  register: dc_up
  changed_when: true
  timeout: "{{ lookup('timeout', 1800) | int }}"
  environment:
    COMPOSE_PARALLEL_LIMIT: "{{ '4' if (DOCKER_IN_CONTAINER | bool) else omit }}"
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  listen:
    - compose-up
    - container-recreate

- name: "🧹 Clear the pending compose up marker"
  ansible.builtin.file:
    path: "{{ COMPOSE_UP_PENDING_MARKER }}"
    state: absent
  listen:
    - compose-up
    - container-recreate

- name: πŸ”¨ Build the services queued for a lone recreate
  ansible.builtin.shell: |
    set -euo pipefail
    compose build --pull {{ compose_service_up_targets | map('quote') | join(' ') }} || compose build {{ compose_service_up_targets | map('quote') | join(' ') }}
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_service_up_build
  changed_when: false
  timeout: "{{ lookup('timeout', 600) | int }}"
  retries: 3
  delay: 30
  until: compose_service_up_build is succeeded
  ignore_errors: true
  listen:
    - compose-service-up

- name: πŸ”¨ Rebuild the queued services without cache once the retries are exhausted
  ansible.builtin.shell: |
    set -euo pipefail
    compose build --no-cache --pull {{ compose_service_up_targets | map('quote') | join(' ') }}
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_service_up_rebuild
  timeout: "{{ lookup('timeout', 600) | int }}"
  retries: 3
  delay: 30
  until: compose_service_up_rebuild is succeeded
  when: compose_service_up_build is failed
  listen:
    - compose-service-up

- name: 🐳 compose up the queued services alone
  ansible.builtin.shell: |
    set -euo pipefail
    compose up -d --no-deps {{ compose_service_up_targets | map('quote') | join(' ') }}
  args:
    chdir: "{{ lookup('container', application_id, 'directories.instance') }}"
    executable: /bin/bash
  register: compose_service_up_result
  changed_when: "(compose_service_up_result.stdout ~ compose_service_up_result.stderr) is search('(Recreated|Created|Started)')"
  timeout: "{{ lookup('timeout', 600) | int }}"
  listen:
    - compose-service-up