---
- import_tasks: "mount_fs.yml"

- command: "ls -dZ /var/lib/pgsql"
  register: pgsql_ls

- name: update selinux context for postgress db dir if it's wrong
  command: "restorecon -vvRF /var/lib/pgsql"
  when: pgsql_ls.stdout is defined and 'postgresql_db_t' not in  pgsql_ls.stdout

- name: install daily custom copr-frontend-crontab
  copy: src=cron.daily/copr-frontend dest=/etc/cron.daily/

- name: install hourly custom copr-frontend-crontab
  copy: src=cron.hourly/copr-frontend dest=/etc/cron.hourly/

- name: install copr-frontend and copr-selinux
  dnf: state=latest name={{ item }}
  with_items:
  - copr-frontend
  - copr-selinux
  tags:
  - packages

  # we install python-alembic because https://bugzilla.redhat.com/show_bug.cgi?id=1536058
- name: install additional pkgs for copr-frontend
  dnf: state=present pkg={{ item }}
  with_items:
  - "bash-completion"
  - "mod_ssl"
  - redis
  - pxz
  - python3-alembic
  tags:
  - packages

- name: install copr configs
  template: src="copr.conf" dest=/etc/copr/copr.conf mode=600
  notify:
  - reload httpd
  tags:
  - config

- name: enable and start redis  # TODO: .service in copr-backend should depend on redis
  service: name=redis enabled=yes state=started

- name: enable and start pagure-events
  service: name=pagure-events enabled=yes state=started

- name: copy apache files to conf.d
  copy: src="httpd/{{ item }}" dest="/etc/httpd/conf.d/{{ item }}"
  with_items:
  - "welcome.conf"
  tags:
  - config

- name: copy crond conf
  copy: src="crond" dest="/etc/sysconfig/crond"

- name: copy apache files to conf.d (templates)
  template: src="httpd/{{ item }}" dest="/etc/httpd/conf.d/{{ item }}"
  with_items:
  - "coprs.conf"
  tags:
  - config

# https://bugzilla.redhat.com/show_bug.cgi?id=1535689
- name: Allow execmem for Apache
  seboolean:
    name: httpd_execmem
    state: yes
    persistent: yes

- import_tasks: "psql_setup.yml"

- name: upgrade db to head
  command: alembic-3 upgrade head
  become: yes
  become_user: copr-fe
  args:
    chdir: /usr/share/copr/coprs_frontend/

- name: set up admins
  command: ./manage.py alter_user --admin {{ item }}
  become: yes
  become_user: copr-fe
  args:
    chdir: /usr/share/copr/coprs_frontend/
  ignore_errors: yes
  with_items:
  - msuchy
  - sgallagh
  - spot
  - nb
  - kevin

- name: install ssl certificates for production
  import_tasks: "install_certs.yml"
  when: not devel
  tags:
  - config

- name: letsencrypt cert
  include_role: name=certbot
  when: devel
  tags:
  - config

- name: Check that cert file exists
  stat:
    path: "/etc/letsencrypt/live/{{ copr_frontend_public_hostname }}/cert.pem"
  register: stat_cert

- name: Should admin run certbot?
  fail:
    msg: Please see roles/certbot/README step (2) and manually run certbot
  when:
    - stat_cert.stat.exists == False
    - devel

- name: install copr-frontend ssl vhost
  template: src="httpd/coprs_ssl.conf.j2" dest="/etc/httpd/conf.d/coprs_ssl.conf"
  tags:
  - config

- name: Set SELinux context for certs generation
  sefcontext:
    target: "/srv/web/acme-challenge/.well-known"
    setype: httpd_sys_content_t
    state: present
  when: devel

- name: enable services
  service: state=started enabled=yes name={{ item }}
  with_items:
  - httpd

- name: set dev banner for dev instance
  when: devel
  copy: src=banner-include.html dest=/var/lib/copr/

- name: disallow robots on dev instance
  when: devel
  copy: src=robots.txt dest=/var/www/html/

- name: rebuild indexes
  command: ./manage.py update_indexes
  become: yes
  become_user: copr-fe
  args:
    chdir: /usr/share/copr/coprs_frontend/
