---
# Configuration for the anitya webapp

- name: clean yum metadata
  command: yum clean all
  tags:
  - packages
  - anitya_backend

- name: install needed packages
  yum: pkg={{ item }} state=present
  with_items:
  - anitya
  - python-psycopg2
  - libsemanage-python
  - postgresql-server
  tags:
  - packages
  - anitya_backend

- name: Initialize postgres if necessary
  command: /usr/bin/postgresql-setup initdb
           creates=/var/lib/pgsql/data
  notify:
  - restart postgresql
  tags:
  - anitya_backend

- name: Set postgresql-server to run on boot
  service: name=postgresql enabled=yes
  ignore_errors: true
  notify:
  - restart postgresql
  tags:
  - service
  - anitya_backend

- name: Ensure postgres has a place to backup to
  file: dest=/backups state=directory owner=postgres
  tags:
  - anitya_backend

- name: Copy over backup scriplet
  copy: src=backup-database dest=/usr/local/bin/backup-database mode=0755
  tags:
  - anitya_backend

- name: Add our postgres config file.
  copy: >
    src={{ item }}
    dest=/var/lib/pgsql/data/{{ item }}
    owner=postgres
  with_items:
  - pg_hba.conf
  notify:
  - restart postgresql
  tags:
  - config
  - postgresql

- name: Let postgresql listen to '*'
  command: sed -i -e "s|#listen_addresses = 'localhost'|listen_addresses = '*'|" /var/lib/pgsql/data/postgresql.conf
  notify:
  - restart postgresql
  tags:
  - anitya_backend

- name: Set up some cronjobs to backup databases as configured
  template: >
    src=cron-backup-database
    dest=/etc/cron.d/cron-backup-database-{{ item }}
  with_items:
  - "{{ dbs_to_backup }}"
  when: dbs_to_backup != []
  tags:
  - cron
  - anitya_backend

- name: copy sundry anitya configuration
  template: src={{ item.file }}
            dest={{ item.location }}/{{ item.dest }}
            owner=root group=root mode=0600
  with_items:
  - { file: anitya_admin.cfg, location: /etc/anitya, dest: anitya.cfg }
#  - { file: alembic.ini, location: /etc/anitya, dest: alembic.ini }
  tags:
  - config
  - anitya_backend

- name: create the database scheme
  command: /usr/bin/python2 /usr/share/anitya/anitya_createdb.py
  environment:
      ANITYA_WEB_CONFIG: /etc/anitya/anitya.cfg

- name: Install the configuration file of anitya
  template: src={{ item.file }}
            dest={{ item.location }}/{{ item.file }}
            owner=root group=root mode=0600
  with_items:
  - { file: anitya.cfg, location: /etc/anitya }
  tags:
  - config
  - anitya_backend

- name: Install the cron job
  copy: src={{ item.file }}
            dest={{ item.location }}/{{ item.file }}
  with_items:
  - { file: 'anitya.cron', location: /etc/cron.d }
  tags:
  - cron
  - config
  - anitya_backend
