- import_tasks: "mount_fs.yml"

# pre-create users/groups and copy uids/gids from the current prod copr-dist-git
- group: name="packager" state=present gid=987
- group: name="copr-dist-git" state=present gid=1003
- group: name="apache" state=present gid=48

- name: create user for git repos and copr-dist-git service
  user: name="copr-dist-git" group=copr-dist-git groups=packager,apache uid=1002

- name: install packages
  dnf: state=present name={{ item }}
  with_items:
  - cgit
  - copr-dist-git
  - rdiff-backup
  - tmpwatch
  - highlight
  - scl-utils-build

- name: install latest dist-git and copr-dist-git
  dnf: state=latest name={{ item }}
  with_items:
    - dist-git
    - dist-git-selinux
    - copr-dist-git

- name: switch selinux to enforcing
  selinux: policy=targeted state=enforcing

- name: make sure directories are owned by copr-dist-git user
  file: path={{ item }} owner=copr-dist-git group=copr-dist-git state=directory recurse=yes
  with_items:
  - /var/log/copr-dist-git/

- name: install config for copr-dist-git
  template: src=copr-dist-git.conf.j2 dest=/etc/copr/copr-dist-git.conf mode=0644
  tags:
  - config

- name: set git variables for copr-dist-git user
  copy: src=".gitconfig" dest="/home/copr-dist-git/.gitconfig"

- name: set git config username
  git_config: name=user.name scope=global value="Copr dist git"

- name: install httpd config needed for COPR lookaside cache
  copy: src="httpd/dist-git/{{ item }}" dest="/etc/httpd/conf.d/dist-git/{{ item }}"
  with_items:
  - lookaside-copr.conf
  tags:
  - config
  notify:
  - reload httpd

- name: install copr-dist-git httpd config
  copy: src="httpd/{{ item }}" dest="/etc/httpd/conf.d/{{ item }}"
  with_items:
  - copr-dist-git.conf
  tags:
  - config
  notify:
  - reload httpd

- name: letsencrypt cert
  include_role: name=certbot

- template: src="ssl.conf.j2" dest="/etc/httpd/conf.d/ssl.conf" owner=root group=root mode=0644
  notify:
  - reload httpd

- name: temporary logrotation fix until copr-dist-git 0.26 is released & deployed
  copy: src="logrotate.d/copr-dist-git" dest="/etc/logrotate.d/copr-dist-git"

- copy: src="dist-git.conf" dest="/etc/dist-git/dist-git.conf" mode=0644
  tags:
  - config

- copy: src="copr-dist-git.hourly" dest="/etc/cron.hourly/copr-dist-git" mode=755
  tags:
  - config

- template: src="cgitrc" dest="/etc/cgitrc" owner=root group=root mode=0644

- template: src="cgitrc.slow" dest="/etc/cgitrc.slow" owner=root group=root mode=0644

- name: allow httpd to run cgit
  seboolean: name=httpd_enable_cgi state=true persistent=true

- name: install robots.txt
  copy: src="robots.txt" dest="/var/www/html/" mode=0644
  tags:
  - config

- name: set up tmpwatch cron job
  cron: name="clean tmp" minute="0" job="/usr/sbin/tmpwatch 2h /tmp/"

- name: ensure that services are enabled and started
  service: name="{{ item }}" enabled=yes state=started
  with_items:
  - "httpd"
  - "dist-git.socket"
  - "copr-dist-git"

- name: create place for our custom SELinux policy
  file: path=/usr/local/share/selinux state=directory

- name: copy over our custom selinux policy for cgit
  copy: src=git_script_t.pp dest=/usr/local/share/selinux/git_script_t.pp
  register: cgit_selinux_module
  tags:
  - selinux

- name: install our custom selinux policy for cgit
  command: semodule -i /usr/local/share/selinux/git_script_t.pp
  when: cgit_selinux_module is changed
  tags:
  - selinux

- name: Create /var/cache/cgit/repo-list.rc if does not exists (it last 30 minutes to generate)
  command: /etc/cron.hourly/copr-dist-git creates=/var/cache/cgit/repo-list.rc
