---
-
 name: "Installing prerequisites Debian"
 apt: name={{ item }} update_cache=yes
 sudo: true
 with_items:
  - libxml2-dev
  - libxslt1-dev
  - python-lxml
  - libcrack2-dev
  - csstidy
  - ldap-utils
  - python-cracklib
  - git
  - apache2-dev
  - python-mysqldb
  - python-pip
  - python-pyasn1
  - libapache2-mod-wsgi
  - gcc
  - libmysqlclient-dev
  - python-dev
  - python-matplotlib
  - python-numpy
 when: ansible_os_family == "Debian"

-
 name: "Installing prerequisites Redhat"
 yum: name={{ item }} state=latest
 sudo: true
 with_items:
  - libxml2-devel
  - libxslt-devel
  - python-lxml
  - openldap-clients
  - cracklib-python
  - git
  - gcc
  - gcc-c++
  - gcc-gfortran
  - freetype-devel
  - libpng-devel
  - lapack-devel
  - blas-devel
  - libffi-devel
 when: ansible_os_family == "RedHat" 
     #-
     # name: Install the latest pip
     # shell: easy_install pip
     # when: ansible_os_family == "RedHat"

-
 name: "Getting Karaage from Github"
 git: repo="https://github.com/monash-merc/karaage.git" dest="/root/karaage3.1.7" {% if karaage_source_version is defined %}version="{{ karaage_source_version }}" {% endif %} force=yes
 sudo: true

-
 name: "Installing Karaage Dependencies"
 pip: name={{ item }}
 sudo: true
 with_items:
  - six
  - slimit
  - ply
  - cython
  - django-celery
  - pyasn1
  - ldap3
#  - mod_wsgi
-
 name: "Restrict Django version to 1.7.8"
 sudo: true
 replace: dest=/root/karaage3.1.7/setup.py regexp="Django >= 1.7" replace="Django == 1.7.8"
-
 name: "Installing Karaage from source"
 shell: "env python setup.py install"
 args:
  chdir: /root/karaage3.1.7
  creates: /root/karaage3.1.7/build/bdist.linux-x86_64
 sudo: true
-
 name: "Templating Karaage settings"
 template: src=settings.py.j2 dest=/etc/karaage3/settings.py owner=root group={{ apache_user }} mode=0640
 sudo: true
-
 name: "Templating project conf"
 template: src=kginit.conf.j2 dest=/etc/karaage3/kginit.conf owner=root group={{ apache_user }} mode=0640
 sudo: true
-
 name: "Templating project init script"
 template: src=kg_init.j2 dest=/usr/bin/kg_init owner=root mode=755
 sudo: true
-
 name: "Templating adding admin role script"
 template: src=kg_add_admin.j2 dest=/usr/bin/kg_add_admin owner=root mode=755
 sudo: true
-
 name: "Creating karaage3 in /var directories log, lib "
 file: path={{ item }} state=directory owner=root group={{ apache_user }} mode=0775
 with_items:
  - /var/log/karaage3
  - /var/lib/karaage3
  - /var/cache/karaage3
 sudo: true

- name: install karaage3-wsgi.conf
  template: src=karaage3-wsgi.conf.j2 dest=/etc/apache2/conf-available/karaage3-wsgi.conf
  sudo: true

- name: install karaage3-wsgi.conf
  template: src=index.html.j2 dest=/var/www/index.html
  sudo: true

- name: install karaage3-wsgi.conf
  template: src=kg-idps.j2 dest=/usr/bin/kg-idps mode=755
  sudo: true

- name: install shibboleth cache file 
  template: src="files/{{ shibboleth_deploy }}_metadata.aaf.xml.j2" dest=/tmp/metadata.aaf.xml 

-
 name: "enabling Karaage configuration"
 shell: a2enconf karaage3-wsgi
 sudo: true
 when: ansible_os_family == "Debian"
-
 name: "enabling Karaage configuration"
 shell: cp -rvpf /root/karaage3.1.7/conf/karaage3-wsgi.conf /etc/httpd/conf.d/karaage3-wsgi.conf
 sudo: true
 when: ansible_os_family == "RedHat"


-
 name: "Set Secret Key"
 lineinfile: dest=/etc/karaage3/settings.py regexp="SECRET_KEY = ''" line="SECRET_KEY = '{{ karaageSecretKey }}'" state=present
 sudo: true

-
 name: "Check karaage DB has been initialized or not"
 shell: mysql -h {{ karaageDbHost }}  -u {{ karaageDbName }} --password={{ karaagePassword }} -Bse 'use karaage; show tables;' | wc -l 
 register: karaage_db_init

- name: enable wsgi
  shell: a2enmod wsgi
  sudo: true

- name: enable shibboleth
  shell: a2enmod shib2
  sudo: true

-
 name: " Create DB tables"
 shell: kg-manage migrate 
 sudo: true
 when: karaage_db_init.stdout.find("0") == 0

-
 name: "Create IDP institutes (disable it as cache is not available)"
 shell: kg-idps /tmp/metadata.aaf.xml 
 sudo: true
 when: karaage_db_init.stdout.find("0") == 0

-
 name: "Create projects"
 shell: kg_init /etc/karaage3/kginit.conf {{ admin_password }}
 sudo: true
 when: karaage_db_init.stdout.find("0") == 0

- name: install postfix
  apt: name=postfix state=present
  sudo: true

- name: configure postfix
  template: src=main_cf.j2 dest=/etc/postfix/main.cf
  sudo: true
  notify: restart postfix

-
 name: "Reloading apache"
 service: name=apache2 state=reloaded
 sudo: true
 when: ansible_os_family == "Debian"

-
 name: "Reloading apache"
 service: name=httpd state=reloaded
 sudo: true
 when: ansible_os_family == "RedHat"

- name: "Start cron job for creating idps"
  cron: name=idps job=/usr/bin/kg-idps user=root day=*/1 state=present
  sudo: true

-
 name: "Templating username list"
 template: src=files/{{ item }} dest=/{{ user_id_file_dir }}/{{ item }}
 with_items: user_id_file
 sudo: true
 when: user_id_file is defined and user_id_file_dir is defined

