---
- name: "load docker, db and proxy for {{ application_id }}"
  include_role: 
    name: cmp-db-docker-proxy

- name: Check if config.php exists
  command: docker exec --user root {{ moodle_container }} test -f {{ moodle_config }}
  register: config_file_exists
  changed_when: false
  failed_when: false

- name: Check if config.php exists
  command: docker exec --user root {{ moodle_container }} test -f {{ moodle_config }}
  register: config_file_exists
  changed_when: false
  failed_when: false

- name: "Update database credentials"
  include_tasks: 01_patch_config.yml
  when: config_file_exists.rc == 0

- name: flush docker service
  meta: flush_handlers

- name: Wait until the Moodle container is healthy
  shell:    docker inspect --format '{% raw %}{{.State.Health.Status}}{% endraw %}' {{ moodle_container }}
  register: health_check
  until:    health_check.stdout.strip() == "healthy"
  retries:  120
  delay:    5

- name: "Include ownership settings tasks for moodle"
  include_tasks: 02_ownership.yml

- name: "Configure OIDC login for Moodle if enabled"
  include_tasks: 03_oidc.yml
  when: applications | get_app_conf(application_id, 'features.oidc', False)

# Deactivated because it doesn't give helpfull warnings back
#- name: Run Moodle system check
#  command: >
#    docker exec --user {{ bitnami_user }} {{ moodle_container }}
#    php /opt/bitnami/moodle/admin/cli/checks.php
#  register: moodle_checks
#  changed_when: false
#  failed_when: >
#    moodle_checks.rc != 0 or
#    "OK: All" not in moodle_checks.stdout
