- name: install packages
  yum: name={{ item }} state=installed
  with_items:
  - koschei
  - lbzip2
  - postgresql
  tags:
  - koschei
  - packages

- name: install config files
  template: >
    src="{{ item }}.j2"
    dest="/etc/koschei/{{ item }}"
    owner=koschei
    group=koschei
    mode=0400
  with_items:
  - config.cfg
  - config-admin.cfg
  notify:
  - restart koschei-polling
  - restart koschei-resolver
  - restart koschei-scheduler
  - restart koschei-watcher
  - restart httpd
  tags:
  - koschei
  - config

- name: install koschei fedmsg config
  copy: src=koschei.py dest=/etc/fedmsg.d/koschei.py
  notify:
  - restart koschei-polling
  - restart koschei-resolver
  - restart koschei-scheduler
  - restart koschei-watcher
  tags:
  - koschei
  - config
  - fedmsgdconfig

- name: install koji key and cert
  copy: >
    src="{{ private }}/files/koschei/koschei.pem"
    dest="/etc/koschei/koschei.pem"
    owner=koschei
    group=koschei
    mode=0400
  notify:
  - restart koschei-scheduler
  tags:
  - koschei
  - config

- name: install koji ca cert
  copy: >
    src="{{ puppet_private }}/fedora-ca.cert"
    dest="/etc/koschei/fedora-ca.cert"
    owner=root
    group=root
    mode=0644
  notify:
  - restart koschei-polling
  - restart koschei-resolver
  - restart koschei-scheduler
  - restart koschei-watcher
  - restart httpd
  tags:
  - koschei
  - config

- name: install httpd config file
  template: >
    src="httpd.conf.j2"
    dest="/etc/httpd/conf.d/koschei.conf"
  notify:
  - restart httpd
  tags:
  - koschei
  - config

- name: enable services to start on boot
  action: service name={{ item }} state=running enabled=true
  with_items:
  - koschei-polling
  - koschei-resolver
  - koschei-scheduler
  - koschei-watcher
  tags:
  - koschei
  - service

- name: allow httpd to connect to db
  seboolean: name={{ item }} state=yes persistent=yes
  with_items:
  - httpd_can_network_connect
  - httpd_can_network_connect_db
  notify:
  - restart httpd
  tags:
  - koschei
  - selinux

- name: Copy the cleanup cronjob
  template: src=cron-db-cleanup.j2 dest=/etc/cron.d/cron-db-cleanup
  tags:
  - koschei
  - config

# See https://github.com/msimacek/koschei/issues/16
- name: HOTFIX koschei resolver.py
  copy: >
    src=resolver.py
    dest=/usr/lib/python2.7/site-packages/koschei/resolver.py
    owner=root group=root mode=0644
  when: env == "production"
  notify:
  - restart koschei-resolver
  tags:
  - koschei
  - hotfix

- name: access to postgres for scripts
  template: src=pgpass.j2 dest=/var/cache/koschei/.pgpass
            owner=koschei group=koschei mode=0600
  tags:
  - koschei
  - config

- name: Create libexec/koschei dir
  file: path=/usr/libexec/koschei state=directory
  when: env == "production"
  tags:
  - koschei
  - hotfix

- name: Create symlinks to python to get specific executable names
  file: path="/usr/libexec/koschei/koschei-{{ item }}"
        src=/usr/bin/python
        state=link
  when: env == "production"
  with_items:
  - polling
  - resolver
  - scheduler
  - watcher
  tags:
  - koschei
  - hotfix

- name: Run using specific symlinks
  lineinfile: dest="/usr/lib/systemd/system/koschei-{{ item }}.service"
              regexp="^ExecStart"
              line="ExecStart=/usr/libexec/koschei/koschei-{{ item }} -m koschei.main {{ item }}"
  when: env == "production"
  with_items:
  - polling
  - resolver
  - scheduler
  - watcher
  tags:
  - koschei
  - hotfix
  notify:
  - reload systemd
  - restart koschei-polling
  - restart koschei-resolver
  - restart koschei-scheduler
  - restart koschei-watcher
