---
# Configuration for the fedbadges consumer

- name: install needed packages
  package:
    name: "{{ item }}"
    state: present
  with_items:
  - python-fedbadges
  - python-psycopg2
  - git
  - python-dogpile-cache
  - postgresql
  - python-gssapi
  - python-requests-gssapi
  - python-requests
  tags:
  - packages
  - badges
  - badges/backend

- name: copy database configuration
  template:
    src: "{{ item }}"
    dest: "/etc/fedmsg.d/{{ item }}"
    owner: fedmsg
    group: fedmsg
    mode: 0600
  with_items:
  - datanommer.py
  - badges-awarder.py
  tags:
  - config
  - badges
  - badges/backend
  notify:
  - restart fedmsg-hub

- name: copy alembic configuration
  template:
    src: alembic.ini
    dest: /usr/share/tahrir_api/alembic.ini
    owner: fedmsg
    group: fedmsg
    mode: 0600
  tags:
  - config
  - badges
  - badges/backend

- name: make badge definition directory
  file:
    path: /usr/share/badges/rules
    state: directory
    owner: fedmsg
    group: fedmsg
    mode: 0755
  tags:
  - config
  - badges
  - badges/backend

- name: create checkout dir
  file:
    name: /srv/badges_checkout
    state: directory
  tags:
  - badges
  - badges/backend

- name: clone directory
  git:
    repo: https://pagure.io/fedora-badges.git
    dest: /srv/badges_checkout
  tags:
  - badges
  - badges/backend

- name: add script
  copy:
    dest: /usr/local/bin/update_checkout.sh
    src: update_checkout.sh
    mode: 0755
  tags:
  - badges
  - badges/backend

- name: add cron for update
  cron:
    name: "update checkout"
    job: /usr/local/bin/update_checkout.sh
    minute: "*/15"
  tags:
  - badges
  - badges/backend

- name: ensure the fedmsg user has a homedir for cron to work
  file:
    state: directory
    path: /usr/share/fedmsg
    mode: 0700
    owner: fedmsg
    group: fedmsg
  when: env != "staging"
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: ensure badges cron directories exist
  file:
    state: directory
    path: "{{ item }}"
    mode: 0755
    owner: root
  when: env != "staging"
  with_items:
  - /usr/share/badges/cronjobs/
  - /etc/cron.d/
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: oldschool badge award scripts
  copy:
    src: "cron/{{ item }}"
    dest: "/usr/share/badges/cronjobs/{{ item }}"
    owner: fedmsg
    mode: 0744
  when: env != "staging"
  with_items:
  - award-oldschool-badges
  - award-libravatar-badge
  - award-flock-paparazzi-badge
  - award-mirror-badge
  - award-lifecycle-badges
  - award-badges-dev-badge
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: oldschool badge award cronjobs
  copy: 
    src: "cron/{{ item }}.cron"
    dest: "/etc/cron.d/{{ item }}"
    owner: root
    mode: 0644
  when: env != "staging"
  with_items:
  - award-oldschool-badges
  - award-libravatar-badge
  - award-flock-paparazzi-badge
  - award-mirror-badge
  - award-lifecycle-badges
  - award-badges-dev-badge
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: copy creds for the flock paparazzi cronjob
  template:
    src: flock-paparazzi.ini
    dest: /etc/flock-paparazzi.ini
    owner: fedmsg
    group: fedmsg
    mode: 0600
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: remove old logrotate file we used to put in place.
  file:
    dest: /etc/logrotate.d/cron-badges-logrotate
    state: absent
  tags:
  - config
  - cron
  - badges
  - badges/backend

- name: copy over the badge-admin one-off scripts
  copy:
    src: "{{ item }}"
    dest: "/usr/local/bin/{{ item }}"
    owner: root
    group: sysadmin-badges
    mode: 0750
  with_items:
  - edit-badge
  - award-badge
  - revoke-badge
  - delete-badge
  - grant-authorization
  - revoke-authorization
  - get-badges-person-id
  tags:
  - scripts
  - badges
  - badges/backend

- name: copy get-sar-person-details
  copy:
    src: get-sar-person-details
    dest: /usr/local/bin/get-sar-person-details
    owner: fedmsg
    group: fedmsg
    mode: 0700
  tags:
  - scripts
  - badges
  - badges/backend
  - sar

# https://raw.githubusercontent.com/fedora-infra/datanommer/python2/datanommer.models/datanommer/models/__init__.py
- name: hotfix datanommer.models with version from python2 branch
  copy:
    src: datanommer.models__init__.py
    dest: /usr/lib/python2.7/site-packages/datanommer/models/__init__.py
  tags:
  - badges
  - badges/backend
