---

- name: Set install mode
  set_fact: framadate_install_mode='none'
  tags: framadate

- name: Install needed tools
  yum:
    name:
      - unzip
      - acl
      - tar
      - git
  tags: framadate

- name: Create user account for PHP
  user:
    name: "{{ framadate_php_user }}"
    comment: "PHP FPM {{ framadate_php_user }}"
    system: True
    shell: /sbin/nologin
  tags: framadate

- name: Check if framadate is already installed
  stat: path={{ framadate_root_dir }}/meta/ansible_version
  register: framadate_version_file
  tags: framadate

- name: Check framadate version
  command:  cat {{ framadate_root_dir }}/meta/ansible_version
  register: framadate_current_version
  changed_when: False
  when: framadate_version_file.stat.exists
  tags: framadate

- name: Set installation process to install
  set_fact: framadate_install_mode='install'
  when: not framadate_version_file.stat.exists
  tags: framadate

- name: Set installation process to upgrade
  set_fact: framadate_install_mode='upgrade'
  when:
    - framadate_version_file.stat.exists
    - framadate_current_version.stdout != framadate_version
    - framadate_manage_upgrade
  tags: framadate

- name: Create archive dir
  file: path={{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }} state=directory mode=700
  when: framadate_install_mode == 'upgrade'
  tags: framadate

- name: Archive current version
  synchronize:
    src: "{{ framadate_root_dir }}/web"
    dest: "{{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }}/"
    recursive: True
    delete: True
  delegate_to: "{{ inventory_hostname }}"
  when: framadate_install_mode == 'upgrade'
  tags: framadate

- name: Dump database
  mysql_db:
    state: dump
    name: "{{ framadate_mysql_db }}"
    target: "{{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }}/{{ framadate_mysql_db }}.sql"
    login_host: "{{ framadate_mysql_server }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
    quick: True
    single_transaction: True
  when: framadate_install_mode == 'upgrade'
  tags: framadate

- name: Create directory structure
  file: path={{ item }} state=directory
  with_items:
    - "{{ framadate_root_dir }}"
    - "{{ framadate_root_dir }}/web"
    - "{{ framadate_root_dir }}/web/tpl_c"
    - "{{ framadate_root_dir }}/tmp"
    - "{{ framadate_root_dir }}/sessions"
    - "{{ framadate_root_dir }}/logs"
    - "{{ framadate_root_dir }}/meta"
  tags: framadate

- name: Download Framadate
  get_url:
    url: "{{ framadate_zip_url }}"
    dest: "{{ framadate_root_dir }}/tmp/"
    checksum: "sha1:{{ framadate_zip_sha1 }}"
  when: framadate_install_mode != 'none'
  tags: framadate

- name: Extract framadate archive
  unarchive:
    src: "{{ framadate_root_dir }}/tmp/framadate-{{ framadate_version }}.zip"
    dest: "{{ framadate_root_dir }}/tmp/"
    remote_src: yes
  when: framadate_install_mode != 'none'
  tags: framadate

- name: Move the content of framadate to the correct top directory
  synchronize:
    src: "{{ framadate_root_dir }}/tmp/framadate-{{ framadate_version }}/"
    dest: "{{ framadate_root_dir }}/web/"
    recursive: True
    delete: True
  delegate_to: "{{ inventory_hostname }}"
  when: framadate_install_mode != 'none'
  tags: framadate

- name: Install libs using composer
  composer: command=install working_dir={{ framadate_root_dir }}/web executable=/bin/php{{ framadate_php_version }}
  environment:
    php: /bin/php{{ framadate_php_version }}
  tags: framadate

- name: Download custom logo
  get_url:
    url: "{{ framadate_logo_url }}"
    dest: "{{ framadate_root_dir }}/web/images"
  when: framadate_logo_url is search('https?://')
  tags: framadate

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

- name: Read database password
  command: cat {{ framadate_root_dir }}/meta/ansible_dbpass
  register: framadate_rand_pass
  when: framadate_mysql_pass is not defined
  changed_when: False
  tags: framadate

- name: Set database pass
  set_fact: framadate_mysql_pass={{ framadate_rand_pass.stdout }}
  when: framadate_mysql_pass is not defined
  tags: framadate

- name: Create MySQL database
  mysql_db:
    name: "{{ framadate_mysql_db }}"
    login_host: "{{ framadate_mysql_server }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
    state: present
  register: framadate_mysql_created
  tags: framadate

- name: Create MySQL User
  mysql_user:
    name: "{{ framadate_mysql_user }}"
    password: "{{ framadate_mysql_pass }}"
    priv: "{{ framadate_mysql_db }}.*:ALL"
    host: "{{ (framadate_mysql_server == 'localhost') | ternary('localhost', item) }}"
    login_host: "{{ framadate_mysql_server }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
    state: present
  with_items: "{{ ansible_all_ipv4_addresses }}"
  tags: framadate

- name: Copy SQL structure
  copy: src=framadate.sql dest={{ framadate_root_dir }}/tmp/framadate.sql
  when: framadate_install_mode != 'none'
  tags: framadate

- name: Inject MySQL schema
  mysql_db:
    name: "{{ framadate_mysql_db }}"
    state: import
    target: "{{ framadate_root_dir }}/tmp/framadate.sql"
    login_host: "{{ framadate_mysql_server }}"
    login_user: sqladmin
    login_password: "{{ mysql_admin_pass }}"
  when: framadate_install_mode == 'install'
  tags: framadate

- name: Remove temp files
  file: path={{ item }} state=absent
  with_items:
    - "{{ framadate_root_dir }}/tmp/framadate-{{ framadate_version }}"
    - "{{ framadate_root_dir }}/tmp/framadate-{{ framadate_version }}.zip"
    - "{{ framadate_root_dir }}/tmp/framadate.sql"
  tags: framadate

- name: Deploy permission script
  template: src=perms.sh.j2 dest={{ framadate_root_dir}}/perms.sh mode=755
  tags: framadate

- name: Deploy httpd configuration
  template: src=httpd.conf.j2 dest=/etc/httpd/ansible_conf.d/10-framadate_{{ framadate_id }}.conf
  notify: reload httpd
  tags: framadate

- name: Deploy PHP configuration
  template: src=php.conf.j2 dest=/etc/opt/remi/php{{ framadate_php_version }}/php-fpm.d/framadate_{{ framadate_id }}.conf
  notify: restart php-fpm
  tags: framadate

- name: Remove PHP configuration from other versions
  file: path=/etc/opt/remi/php{{ item }}/php-fpm.d/framadate_{{ framadate_id }}.conf state=absent
  with_items: "{{ httpd_php_versions | difference([ framadate_php_version ]) }}"
  notify: restart php-fpm
  tags: framadate

- name: Remove PHP configuration (using a custom pool)
  file: path=/etc/opt/remi/php{{ framadate_php_version }}/php-fpm.d/framadate_{{ framadate_id }}.conf state=absent
  when: framadate_php_fpm_pool is defined
  notify: restart php-fpm
  tags: framadate

- name: Deploy framadate configuration
  template: src=config.php.j2 dest={{ framadate_root_dir }}/web/app/inc/config.php owner=root group={{ framadate_php_user }} mode=640
  tags: framadate

- name: Set correct SELinux context
  sefcontext:
    target: "{{ framadate_root_dir }}(/.*)?"
    setype: httpd_sys_content_t
    state: present
  when: ansible_selinux.status == 'enabled'
  tags: framadate

- name: Restrict permissions
  command: "{{ framadate_root_dir }}/perms.sh"
  changed_when: False
  tags: framadate

- name: Compress previous version
  command: tar cJf {{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }}.txz ./
  environment:
    XZ_OPT: -T0
  args:
    chdir: "{{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }}"
    warn: False
  when: framadate_install_mode == 'upgrade'
  tags: framadate

- name: Remove archive directory
  file: path={{ framadate_root_dir }}/archives/{{ framadate_current_version.stdout }} state=absent
  when: framadate_install_mode == 'upgrade'
  tags: framadate

- name: Write version number
  copy: content={{ framadate_version }} dest={{ framadate_root_dir }}/meta/ansible_version
  when: framadate_install_mode != 'none'
  tags: framadate

...
