---
- name: Install packages
  yum: name={{ offen_packages }}
  tags: offen

- when: offen_install_mode != 'none'
  block:

    - name: Download offen
      get_url:
        url: "{{ offen_archive_url }}"
        dest: "{{ offen_root_dir }}/tmp"
        checksum: "sha256:{{ offen_archive_sha256 }}"

    - name: Create temporary dir
      file: path={{ offen_root_dir }}/tmp/{{ offen_version }} state=directory

    - name: Unarchive offen
      unarchive:
        src: "{{ offen_root_dir }}/tmp/offen-v{{ offen_version }}.tar.gz"
        dest: "{{ offen_root_dir }}/tmp/{{ offen_version }}"
        remote_src: True

    - name: Install offen binary
      copy:
        src: "{{ offen_root_dir }}/tmp/{{ offen_version }}/offen-linux-amd64"
        dest: "{{ offen_root_dir }}/bin/offen"
        mode: 755
        remote_src: True
      notify: restart offen

  tags: offen

- name: Deploy offen service unit
  template: src=offen.service.j2 dest=/etc/systemd/system/offen.service
  register: offen_unit
  notify: restart offen
  tags: offen

- name: Reload systemd
  systemd: daemon_reload=True
  when: offen_unit.changed
  tags: offen

  # Create MySQL database
- import_tasks: ../includes/webapps_create_mysql_db.yml
  vars:
    - db_name: "{{ offen_db_name }}"
    - db_user: "{{ offen_db_user }}"
    - db_server: "{{ offen_db_server }}"
    - db_pass: "{{ offen_db_pass }}"
  tags: offen

- name: Deploy pre/post backup scripts
  template: src={{ item }}-backup.j2 dest=/etc/backup/{{ item }}.d/offen mode=0750
  with_items:
    - pre
    - post
  tags: offen

