---

- name: Install packages
  package: name={{ odoo_packages }}
  tags: odoo

- when: odoo_install_mode != 'none'
  block:
    - name: Clone or update the repo
      git:
        repo: "{{ odoo_git_url }}"
        dest: "{{ odoo_root_dir }}/app"
        depth: 1
        version: "{{ odoo_version }}"
        force: True
      notify: restart odoo-server

    - name: Add ACL
      command: |
        setfacl -R -b -k {{ odoo_root_dir }}
        setfacl -m u:nginx:X {{ odoo_root_dir }}
        setfacl -R -m u:nginx:rX {{ odoo_root_dir }}/data {{ odoo_root_dir }}/app/addons {{ odoo_root_dir }}/app/odoo/addons {{ odoo_root_dir }}/enterprise

  tags: odoo

- when: odoo_install_mode != 'none' and odoo_enterprise
  block:
    - name: Clone or update the enterprise repo
      git:
        repo: "{{ odoo_enterprise_git_url }}"
        dest: "{{ odoo_root_dir }}/enterprise"
        depth: 1
        version: "{{ odoo_version }}"
        force: True
      notify: restart odoo-server
  tags: odoo


- name: Create the virtualenv
  pip:
    state: "{{ (odoo_install_mode == 'none') | ternary('present', 'latest') }}"
    virtualenv: "{{ odoo_root_dir }}/venv"
    virtualenv_command: "{{ odoo_venv_command }}"
    requirements: "{{ odoo_root_dir }}/app/requirements.txt"
  environment:
    PATH: "{{ (pg_version | default('default') == 'default') | ternary('', '/usr/pgsql-' ~ pg_version ~ '/bin:') }}/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
  tags: odoo

- name: Install additional modules in the venv
  pip:
    name: "{{ item }}"
    state: "{{ (odoo_install_mode == 'none') | ternary('present', 'latest') }}"
    virtualenv: "{{ odoo_root_dir }}/venv"
    virtualenv_command: "{{ odoo_venv_command }}"
  loop:
    - phonenumbers
    - python-jose
    - flanker
    - redis
    - pdfminer
    - dnsq
  tags: odoo

# This is needed as the first import from flanker needs to create files not writeable by the Odoo user
# See https://github.com/Tecnativa/doodba/pull/487
- name: Finish flanker install
  command: "{{ odoo_root_dir }}/venv/bin/python3 -c 'from flanker.addresslib import address'"
  changed_when: False
  tags: odoo

- name: Create the PostgreSQL role
  postgresql_user:
    db: postgres
    name: "{{ odoo_db_user }}"
    password: "{{ odoo_db_pass }}"
    login_host: "{{ odoo_db_server }}"
    login_user: sqladmin
    login_password: "{{ pg_admin_pass }}"
    role_attr_flags: "LOGIN{{ odoo_db_manager | ternary(',CREATEDB,SUPERUSER','') }}"
  tags: odoo

- name: Create the PostgreSQL database
  postgresql_db:
    name: "{{ odoo_db_name }}"
    encoding: UTF-8
    template: template0
    owner: "{{ odoo_db_user }}"
    login_host: "{{ odoo_db_server }}"
    login_user: sqladmin
    login_password: "{{ pg_admin_pass }}"
  tags: odoo

- name: Deploy odoo service file
  template: src=odoo-server.service.j2 dest=/etc/systemd/system/odoo-server.service
  register: odoo_unit
  notify: restart odoo-server
  tags: odoo

- name: Reload systemd
  command: systemctl daemon-reload
  when: odoo_unit.changed
  tags: odoo

- name: Install backup hooks
  template: src={{ item }}-backup.sh.j2 dest=/etc/backup/{{ item }}.d/odoo.sh mode=750
  loop:
    - pre
    - post
  tags: odoo

