---
# tasks file for nextcloud

- name: Main... Set Nextcloud db_port to ProxySQL port instead of default MySQL
  set_fact:
    db_port: "6033"
  when: "'db_lbal_servers' in groups.keys()"

- name: Main... Ovewrite Nextcloud db_host if no SQL_loadBalancer
  set_fact:
    db_host: "{{ groups['db_servers'][0] }}"
  when: "('db_lbal_servers' not in groups.keys()) and ('db_servers' in groups.keys())"

- name: Main... Ovewrite Nextcloud db_host if no db_servers
  set_fact:
    db_host: localhost
  when: "'db_servers' not in groups.keys()"

- name: Main... Set variables based on web_servers group
  set_fact:
    nb_web_servers: "{{ groups['web_servers'] | length }}"
    main_web_server: "{{ groups['web_servers'][0] }}"
  when: "'web_servers' in groups.keys()"

- name: Main... Set variables if no web_servers group
  set_fact:
    nb_web_servers: 1
    main_web_server: "{{ hostvars[ansible_fqdn]['ansible_host'] }}"
  when: "'web_servers' not in groups.keys()"

- name: Main... Show Selinux variable
  debug: var=ansible_selinux

- include_tasks: "prep_os/{{ ansible_distribution }}.yml"

- name: Main... Create shared directories (web & data)
  file:
    path: "{{ item }}"
    state: directory
    owner: "{{ nextcloud_websrv_user }}"
    group: "{{ nextcloud_websrv_group }}"
    mode: 0750
  with_items:
    - "{{ nc_data_dir }}"
    - "{{ http_webroot }}"
  ignore_errors: yes

- name: Main... Mount shared directories
  include_tasks: "nc_gluster.yml"
  when: "(nb_web_servers | int) > 1 and 'gluster_servers' in groups.keys()"

- name: Main... Check if Nextcloud is already in the web repo
  stat:
    path: "{{ http_webroot }}/nextcloud/index.php"
  register: nc_nextcloud_downloaded
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Download and Install Nextcloud
  include_tasks: "nc_download.yml"
  args:
    apply:
      delegate_to: "{{ main_web_server }}"
  run_once: true
  when: (nc_nextcloud_downloaded.stat.isreg is undefined) or (not nc_nextcloud_downloaded.stat.isreg)

- name: Main... Start {{ http_service_name }} service
  service:
    name: "{{ http_service_name }}"
    state: started

- name: Main... Check Nextcloud status
  become: yes
  become_user: "{{ nextcloud_websrv_user }}"
  shell: "{{ php_bin }} occ status --output=json"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  register: jsoncontent
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Set Nextcloud variables status
  set_fact:
    nc_status: "{{ jsoncontent.stdout | from_json }}"
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Check the nc_status content
  debug: var=nc_status
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Loading Ceph object primary storage
  include_tasks: "nc_ceph.yml"
  when: "'ceph_servers' in groups.keys() and objectstore_s3_install"

- name: Main... Install and deploy Nextcloud if needed
  include_tasks: nc_install.yml
  args:
    apply:
      delegate_to: "{{ main_web_server }}"
  run_once: true
  when: nc_status.installed|bool == false

- name: Main... Setup nextcloud
  include_tasks: nc_setup.yml
  args:
    apply:
      delegate_to: "{{ main_web_server }}"
  run_once: true
  #when: nc_status.installed|bool == false

- name: Main... Set Trusted Local Domain lists
  set_fact:
    list_zero:
      - "{{ nc_global_name | default() }}"
    list_one: "{{ groups['lbal_servers'] | default([]) }}"
    list_two: "{{ groups['web_servers'] | default([]) }}"

- name: Main... Merge the Trusted lists
  set_fact:
    trusted_dom: "{{ list_zero + list_one + list_two }}"

      #- name: Main... Set a local redirect name
      #  set_fact:
      #    redirect_name: "{{ main_web_server }}"
      #
      #- name: Main... Set a global redirect name if loadbalancing
      #  set_fact:
      #    redirect_name: "{{ nc_global_name | default(nc.test) }}"
      #  when: groups['lbal_servers'] is defined and (groups['lbal_servers']|length>0)

- name: Main... Set Trusted Local Domain
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ config:system:set trusted_domains {{ occ_idx }} --value={{ item }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  loop: "{{ trusted_dom }}"
  loop_control:
    index_var: occ_idx
    pause: 2
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Set Nextcloud overwrite url and host
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} occ config:system:set {{ item.name }} --value={{ item.value }}"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  with_items:
    #- { name: 'overwritehost', value: '{{ redirect_name }}' }
    #- { name: 'overwrite.cli.url', value: 'https://{{ redirect_name }}' }
    - { name: 'overwritehost', value: '{{ nc_global_name }}' }
    - { name: 'overwrite.cli.url', value: 'https://{{ nc_global_name }}' }
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Setup... Configure Cron
  cron:
    name: "Nextcloud Cronjob"
    minute: "*/{{ nc_cron_period }}"
    user: "{{ nextcloud_websrv_user }}"
    job: "{{ php_bin }} -f {{ http_webroot }}/nextcloud/cron.php"
    cron_file: "nextcloud"
  when: (nc_background_cron | bool)
  delegate_to: "{{ main_web_server }}"
  run_once: true

- name: Main... Check Selinux
  include_tasks: "prep_os/selinux.yml"
  when: ansible_selinux.status == "enabled"

- name: Main... Restart {{ http_service_name }} service
  service:
    name: "{{ http_service_name }}"
    state: restarted

- name: Main... First run Cron
  become_user: "{{ nextcloud_websrv_user }}"
  become: true
  shell: "{{ php_bin }} -f cron.php"
  args:
    chdir: "{{ http_webroot }}/nextcloud"
  delegate_to: "{{ main_web_server }}"
  run_once: true
