---
- name: start httpd (provided in the apache role)
  service: name=httpd state=started

- name: ensure packages required for resultsdb are installed (dnf)
  dnf: name={{ item }} state=present
  with_items:
    - resultsdb
    - python3-mod_wsgi
    - python3-psycopg2
    - python2-libsemanage
  when: ansible_cmdline.ostree is not defined and deployment_type in ['dev', 'stg']

###########
## TODO: remove when transition to buildbot 1.7 is complete
- name: ensure packages required for resultsdb are installed (dnf)
  dnf: name={{ item }} state=present
  with_items:
    - resultsdb
    - mod_wsgi
    - python-psycopg2
    - libsemanage-python
  when: ansible_cmdline.ostree is not defined and deployment_type in ['prod']
###########

- name: ensure database is created
  delegate_to: "{{ resultsdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_db: db={{ resultsdb_db_name }}

- name: ensure resultsdb db user has access to database
  delegate_to: "{{ resultsdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_user: db={{ resultsdb_db_name }} user={{ resultsdb_db_user }} password={{ resultsdb_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure selinux lets httpd talk to postgres
  seboolean: name=httpd_can_network_connect_db persistent=yes state=yes

- name: generate resultsdb config
  template: src=settings.py.j2 dest=/etc/resultsdb/settings.py owner=root group=root mode=0644
  notify:
    - reload httpd

- name: generate resultsdb apache config
  template: src=resultsdb.conf.j2 dest=/etc/httpd/conf.d/resultsdb.conf owner=root group=root mode=0644
  notify:
    - reload httpd

- name: create directory for fedora-messaging
  file:
    path: "{{ item }}"
    owner: root
    group: root
    mode: 0755
    state: directory
  with_items:
    - "/etc/pki/rabbitmq/"
    - "/etc/fedora-messaging"
  when: env == "staging"
  tags:
    - fedora-messaging

- name: copy fedora-messaging certificates
  copy:
    src: "{{ item.src }}"
    dest: "/etc/pki/rabbitmq/{{ item.dest }}"
    owner: "{{ item.owner }}"
    group: "{{ item.group }}"
    mode: "{{ item.mode }}"
  with_items:
    - src: "{{private}}/files/rabbitmq/{{env}}/pki/issued/resultsdb{{ env_suffix }}.crt"
      dest: resultsdb.crt
      owner: root
      group: apache
      mode: "444"
    - src: "{{private}}/files/rabbitmq/{{env}}/pki/private/resultsdb{{ env_suffix }}.key"
      dest: resultsdb.key
      owner: root
      group: apache
      mode: "440"
    - src: "{{private}}/files/rabbitmq/{{env}}/pki/ca.crt"
      dest: resultsdb.ca
      owner: root
      group: apache
      mode: "444"
  when: env == "staging"
  tags:
    - fedora-messaging

- name: generate fedora-messaging config
  template: src=config.toml.j2 dest=/etc/fedora-messaging/config.toml owner=root group=root mode=0644
  notify:
    - reload httpd
  when: env == "staging"
  tags:
    - fedora-messaging

# this command will clear the exising database and it's now being run every time, regardless of whether
# /etc/resultsdb-is-init exists or not. We've already lost a lot of data, so commenting this command out
# while we figure out how to handle the problem
#- name: initialize resultsdb database
#  shell: PROD='true' resultsdb init_db && touch /etc/resultsdb/db-is-init creates=/etc/resultsdb-is-init
