---
- name: ensure grokmirror packages are installed (yum)
  package: name={{ item }} state=present
  with_items:
      - git
      - python-grokmirror
  when: ansible_distribution_major_version|int < 22

- name: ensure grokmirror packages are installed (dnf)
  dnf: name={{ item }} state=present
  with_items:
      - git
      - python-grokmirror
  when: ansible_distribution_major_version|int > 21 and ansible_cmdline.ostree is not defined

- name: create grokmirror user
  user: name={{ grokmirror_user }}

- name: add buildslave cert to grokmirror for cloning
  authorized_key: user={{ grokmirror_user }} key="{{ buildslave_ssh_pubkey }}"

- name: create grokmirror conf dir
  file: path=/etc/grokmirror/ state=directory owner=root group=root mode=1755

- name: create grokmirror root directory
  file: path={{ grokmirror_basedir }} state=directory owner={{ grokmirror_user }} group={{ grokmirror_user }} mode=1755

- name: create log directory for grokmirror
  file: path=/var/log/grokmirror state=directory owner={{ grokmirror_user }} group={{ grokmirror_user }} mode=1775

- name: create directory for grokmirror locks
  file: path=/var/lock/grokmirror state=directory owner={{ grokmirror_user }} group={{ grokmirror_user }} mode=1755

- name: clone initial git repos
  git: repo={{ item.url }} bare=yes dest={{ grokmirror_basedir }}/{{ item.name }} update=no
  become: true
  become_user: "{{ grokmirror_user }}"
  with_items: "{{ grokmirror_repos }}"

- name: set up default branch
  command: chdir={{ grokmirror_basedir }}/{{ item.name }} git symbolic-ref HEAD refs/heads/{{ grokmirror_default_branch }}
  become: true
  become_user: "{{ grokmirror_user }}"
  with_items: "{{ grokmirror_repos }}"

- name: generate grokmirror config
  template: src=repos.conf.j2 dest=/etc/grokmirror/repos.conf owner={{ grokmirror_user }} group={{ grokmirror_user }} mode=0644

- name: install cron jobs to update repos
  cron:
    name="pull repo for {{ item.name }}"
    user={{ grokmirror_user }}
    special_time=hourly
    job="cd {{ grokmirror_basedir }}/{{ item.name }} && git fetch origin {{ grokmirror_default_branch }}:{{ grokmirror_default_branch }}"
  with_items: "{{ grokmirror_repos }}"
