---
# tasklist for setting up the mirrormanager web application

- name: install needed packages
  package: name={{ item }} state=present update_cache=yes
  with_items:
  - mirrormanager2
  - python-psycopg2
  - python-openid-cla
  - python-openid-teams
  - python-memcached
  - libsemanage-python
  tags:
  - mm2_frontend
  - packages

- name: install configuration file
  template: src={{ item.file }} dest={{ item.dest }}
            owner=apache group=apache mode=0600
  with_items:
  - { file: mirrormanager.conf, dest: /etc/httpd/conf.d/mirrormanager.conf }
  - { file: mirrormanager2.cfg, dest: /etc/mirrormanager/mirrormanager2.cfg }
  - { file: alembic.ini, dest: /etc/mirrormanager/alembic.ini }
  - { file: mirrormanager2.wsgi, dest: /var/www/mirrormanager2.wsgi }
  notify:
  - reload httpd
  tags:
  - mm2_frontend
  - config

- name: create the database scheme
  command: /usr/bin/python2 /usr/share/mirrormanager2/mirrormanager2_createdb.py
  environment:
      MM2_CONFIG: /etc/mirrormanager/mirrormanager2.cfg
  tags:
  - mm2_frontend
  - config

- name: set sebooleans so mirrormanager can connect to its db
  seboolean: name=httpd_can_network_connect_db
                    state=true
                    persistent=true
  tags:
  - mm2_frontend
  - config

- name: set sebooleans so mirrormanager can send exception mails
  seboolean: name=httpd_can_sendmail
                    state=true
                    persistent=true
  tags:
  - mm2_frontend
  - config

- name: startup apache
  service: name=httpd enabled=yes state=started
  tags:
  - mm2_frontend
  - config

- name: Create mirrormanager user
  user: name=mirrormanager state=present home=/var/lib/mirrormanager createhome=yes shell=/bin/bash
  tags:
  - mm2_frontend
  - config

- name: create mirrormanager frontend directories
  file: path={{ item }} state=directory owner=mirrormanager group=mirrormanager mode=0755
  with_items:
  - /var/log/mirrormanager
  - /var/log/mirrormanager/crawler
  - /var/www/mirrormanager-statistics
  - /var/www/mirrormanager-statistics/data
  - /var/www/mirrormanager-statistics/data/propagation
  - /var/www/mirrormanager-statistics/map
  - /var/www/mirrormanager-statistics/map/img
  - /var/www/mirrormanager-statistics/map/theme
  - /var/www/mirrormanager-statistics/map/theme/default
  tags:
  - mm2_frontend
  - config

- name: apply selinux type to crawler log and statistics files
  file: >
    dest={{ item }}
    setype=httpd_sys_content_t
    state=directory
    recurse=yes
  with_items:
  - /var/log/mirrormanager
  - /var/log/mirrormanager/crawler
  - /var/www/mirrormanager-statistics
  - /var/www/mirrormanager-statistics/data
  - /var/www/mirrormanager-statistics/map
  tags:
  - mm2_frontend
  - config

- name: install crawler log sync
  template: src=sync-crawler-logs.sh dest=/usr/local/bin/sync-crawler-logs.sh mode=0755
  tags:
  - mm2_frontend

- name: install the cron job
  template: src=frontend.cron dest=/etc/cron.d/mm2_frontend.cron
  tags:
  - config
  - mm2_frontend
  when: env != "staging" and inventory_hostname.startswith('mm-frontend0')

# mirrormanager user ssh key(s) (used to transfer statistics from backend to frontends)
- name: add authorized_keys for mirrormanager
  authorized_key: key="{{ item }}" user=mirrormanager state=present
  with_file:
  - "{{ roles_path }}/mirrormanager/mirrorlist2/files/mm-authorized_key"
  tags:
  - mm2_frontend

- name: apply selinux type to mirrormanager public_key
  file: dest=/var/lib/mirrormanager/.ssh/authorized_keys setype=user_home_t state=file
  tags:
  - mm2_frontend
  - config

- name: install interactive map file
  copy: src={{ roles_path }}/membership-map/build/files/www/{{ item }}
            dest=/var/www/mirrormanager-statistics/map/{{ item }}
            owner=mirrormanager group=mirrormanager mode=0444
  with_items:
  - OpenLayers.js
  - img/east-mini.png
  - img/layer-switcher-maximize.png
  - img/layer-switcher-minimize.png
  - img/north-mini.png
  - img/slider.png
  - img/south-mini.png
  - img/west-mini.png
  - img/zoombar.png
  - img/zoom-minus-mini.png
  - img/zoom-plus-mini.png
  - theme/default/style.css
  tags:
  - mm2_frontend

- name: install interactive map file
  copy: src={{ item }} dest=/var/www/mirrormanager-statistics/map/{{ item }}
            owner=mirrormanager group=mirrormanager mode=0444
  with_items:
  - mirrors.html
  - f-dot.png
  tags:
  - mm2_frontend
