---

- name: Set default install mode to none
  set_fact: odoo_install_mode="none"
  tags: odoo

- name: Check if odoo is installed
  stat: path={{ odoo_root_dir }}/meta/ansible_version
  register: odoo_version_file
  tags: odoo

- name: Check installed version
  command:  cat {{ odoo_root_dir }}/meta/ansible_version
  register: odoo_current_version
  changed_when: False
  when: odoo_version_file.stat.exists
  tags: odoo

- name: Set install mode to install
  set_fact: odoo_install_mode='install'
  when: not odoo_version_file.stat.exists
  tags: odoo

- name: Set install mode to upgrade
  set_fact: odoo_install_mode='upgrade'
  when:
    - odoo_version_file.stat.exists
    - odoo_current_version is defined
    - odoo_current_version.stdout != odoo_version | string + '-' + odoo_build | string
    - odoo_manage_upgrade
  tags: odoo

- name: Install dependencies
  yum:
    name:
      - rh-python36-python-virtualenv
      - rh-python36-python-pip
      - gcc
      - libxml2-devel
      - libxslt-devel
      - openldap-devel
      - nodejs-less
      - wkhtmltopdf
      - python-psycopg2
      - postgresql
  tags: odoo

- name: Create user account for odoo
  user:
    name: odoo
    system: True
    shell: /sbin/nologin
    home: "{{ odoo_root_dir }}"
  tags: odoo

- name: Create directories
  file: path={{ item.path }} state=directory owner={{ item.owner | default(omit) }} group={{ item.group | default(omit) }} mode={{ item.mode | default(omit) }}
  with_items:
    - path: "{{ odoo_root_dir }}"
      owner: "{{ odoo_user }}"
      mode: 700
    - path: "{{ odoo_root_dir }}/tmp"
    - path: "{{ odoo_root_dir }}/meta"
      mode: 700
    - path: "{{ odoo_root_dir }}/etc"
      group: "{{ odoo_user }}"
      mode: 750
    - path: "{{ odoo_root_dir }}/app"
    - path: "{{ odoo_root_dir }}/db_dumps"
      mode: 700
    - path: "{{ odoo_root_dir }}/data"
      group: "{{ odoo_user }}"
      mode: 770
  tags: odoo

- name: Fetch odoo sources
  get_url:
    url: "{{ odoo_archive_url }}"
    dest: "{{ odoo_root_dir }}/tmp"
  when: odoo_install_mode != "none"
  tags: odoo

- name: Extract odoo archive
  unarchive:
    src: "{{ odoo_root_dir }}/tmp/odoo_{{ odoo_version }}.{{ odoo_build }}.tar.gz"
    dest: "{{ odoo_root_dir }}/tmp"
    remote_src: yes
  when: odoo_install_mode != "none"
  tags: odoo

- name: Create archive dir
  file: path={{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }} state=directory mode=700
  when: odoo_install_mode == "upgrade"
  tags: odoo

- name: Stop the server during upgrade
  service: name=odoo-server state=stopped
  when: odoo_install_mode == "upgrade"
  tags: odoo

- name: Archive current Odoo install
  synchronize:
    src: "{{ odoo_root_dir }}/app"
    dest: "{{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }}/app"
    recursive: True
  delegate_to: "{{ inventory_hostname }}"
  when: odoo_install_mode == "upgrade"
  tags: odoo

- name: Dump database
  postgresql_db:
    name: "{{ odoo_db_name }}"
    state: dump
    login_host: "{{ odoo_db_server }}"
    login_user: sqladmin
    login_password: "{{ pg_admin_pass }}"
    target: "{{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }}/{{ odoo_db_name }}.sql.gz"
  when: odoo_install_mode == "upgrade"
  tags: odoo

- name: Compress previous version
  command: tar cf {{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }}.txz ./
  environment:
    XZ_OPT: -T0
  args:
    chdir: "{{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }}"
  when: odoo_install_mode == 'upgrade'
  tags: odoo

- name: Remove the archive directory
  file: path={{ odoo_root_dir }}/archives/{{ odoo_current_version.stdout }} state=absent
  tags: odoo

- name: Create the virtualenv
  pip:
    state: latest
    virtualenv: "{{ odoo_root_dir }}"
    virtualenv_command: /opt/rh/rh-python36/root/usr/bin/virtualenv
    requirements: "{{ odoo_root_dir }}/tmp/odoo-{{ odoo_version }}.post{{ odoo_build }}/requirements.txt"
  when: odoo_install_mode != "none"
  tags: odoo

- name: Install additional python modules
  pip:
    name: "{{ item }}"
    state: latest
    virtualenv: "{{ odoo_root_dir }}"
    virtualenv_command: /opt/rh/rh-python36/root/usr/bin/virtualenv
  with_items:
    - phonenumbers
  tags: odoo

- name: Copy odoo application to its final directory
  synchronize:
    src: "{{ odoo_root_dir }}/tmp/odoo-{{ odoo_version }}.post{{ odoo_build }}/"
    dest: "{{ odoo_root_dir }}/app/"
    recursive: True
    delete: True
  when: odoo_install_mode != "none"
  delegate_to: "{{ inventory_hostname }}"
  tags: odoo

- name: Install odoo
  command: "{{ odoo_root_dir }}/bin/python3 {{ odoo_root_dir }}/app/setup.py install"
  args:
    chdir: "{{ odoo_root_dir }}/app/"
  when: odoo_install_mode != "none"
  tags: odoo

- name: Generate a random pass for the database
  shell: openssl rand -base64 45 > {{ odoo_root_dir }}/meta/ansible_dbpass
  args:
    creates: "{{ odoo_root_dir }}/meta/ansible_dbpass"
  when: odoo_db_pass is not defined
  tags: odoo

- name: Read database password
  command: cat {{ odoo_root_dir }}/meta/ansible_dbpass
  register: odoo_rand_pass
  when: odoo_db_pass is not defined
  changed_when: False
  tags: odoo

- name: Set database pass
  set_fact: odoo_db_pass={{ odoo_rand_pass.stdout }}
  when: odoo_db_pass is not defined
  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 }}"
  tags: odoo

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

- name: Handle odoo ports
  iptables_raw:
    name: odoo_ports
    state: "{{ (odoo_src_ip | length > 0) | ternary('present','absent') }}"
    rules: "-A INPUT -m state --state NEW -p tcp -m multiport --dports {{ odoo_ports | join(',') }} -s {{ odoo_src_ip | join(',') }} -j ACCEPT"
  tags: [firewall,odoo]

- name: Deploy server configuration
  template: src=odoo-server.conf.j2 dest={{ odoo_root_dir }}/etc/odoo-server.conf group={{ odoo_user }} mode=640
  notify: restart odoo-server
  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: Update modules
  command: "{{ odoo_root_dir }}/bin/python3 {{ odoo_root_dir }}/app/setup/odoo -u all --stop-after-init --syslog"
  become_user: "{{ odoo_user }}"
  when: odoo_install_mode == "upgrade"
  tags: odoo

- name: Write version
  copy: content={{ odoo_version }}-{{ odoo_build }} dest={{ odoo_root_dir }}/meta/ansible_version
  when: odoo_install_mode != "none"
  tags: odoo

- name: Start the service
  service: name=odoo-server state=started enabled=True
  tags: odoo

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

- name: Remove temp files
  file: path={{ item }} state=absent
  with_items:
    - "{{ odoo_root_dir }}/tmp/odoo_{{ odoo_version }}.{{ odoo_build }}.tar.gz"
    - "{{ odoo_root_dir }}/tmp/odoo-{{ odoo_version }}.post{{ odoo_build }}"
  tags: odoo
