---
- name: Install APT dependencies
  apt:
    update_cache: true
    name:
      - nginx
      - uwsgi
      - uwsgi-plugin-python3
      - python3-django
      - python3-django-cas-server
      - python3-psycopg2
      - python3-ldap3
  register: apt_result
  retries: 3
  until: apt_result is succeeded

- name: Clone Django CAS project repository
  git:
    repo: http://gitlab.adm.crans.org/nounous/django-cas.git
    dest: /var/local/django-cas
    version: master
    umask: '002'

- name: Configure Django CAS
  template:
    src: cas/settings_local.py.j2
    dest: /var/local/django-cas/cas/settings_local.py
    mode: 0600
    owner: www-data
  notify: Restart uwsgi

- name: Configure NGINX site
  template:
    src: nginx/sites-available/cas.j2
    dest: /etc/nginx/sites-available/cas
    mode: 0644
  notify: Restart nginx

- name: Enable nginx site
  file:
    src: /etc/nginx/sites-available/cas
    dest: /etc/nginx/sites-enabled/cas
    state: link
  notify: Restart nginx

- name: Configure UWSGI app
  template:
    src: uwsgi/apps-available/cas.ini.j2
    dest: /etc/uwsgi/apps-available/cas.ini
    mode: 0644
  notify: Restart uwsgi

- name: Enable uwsgi app
  file:
    src: /etc/uwsgi/apps-available/cas.ini
    dest: /etc/uwsgi/apps-enabled/cas.ini
    state: link
  notify: Restart uwsgi

- name: Install CAS crons
  template:
    src: cron.d/cas.j2
    dest: /etc/cron.d/cas
    mode: 0644

- name: Indicate role in motd
  template:
    src: update-motd.d/05-service.j2
    dest: /etc/update-motd.d/05-cas
    mode: 0755
