#
# This is a base koji_builder role. 
#
- name: set root passwd
  user: name=root password={{ builder_rootpw }} state=present
  tags:
  - koji_builder

- name: add mock user as 425
  user: name=mock uid=425 state=present home=/var/lib/mock createhome=yes system=yes
  tags:
  - koji_builder

- name: make mock homedir perms
  file: state=directory path=/var/lib/mock mode=2775 owner=root group=mock
  tags:
  - koji_builder

- name: add mock ssh dir
  file: state=directory path=/var/lib/mock/.ssh mode=700 owner=mock group=mock
  tags:
  - koji_builder

- name: add mock ssh keys
  copy: src=mock_auth_keys dest=/var/lib/mock/.ssh/authorized_keys mode=640 owner=mock group=mock
  tags:
  - koji_builder

- name: add kojibuilder
  user: name=kojibuilder groups=mock 
  tags:
  - koji_builder

- name: add mockbuilder
  user: name=mockbuilder groups=mock
  tags:
  - koji_builder

- name: mockbuilder .ssh dir
  file: state=directory path=/home/mockbuilder/.ssh mode=700 owner=mockbuilder group=mockbuilder
  tags:
  - koji_builder

- name: mockbuilder ssh key
  copy: src=ftbfs_auth_keys dest=/home/mockbuilder/.ssh/authorized_keys mode=644 owner=mockbuilder group=mockbuilder
  tags:
  - koji_builder

- name: make a bunch of dirs
  file: state=directory path={{ item }}
  with_items:
    - /pub
    - /mnt/fedora_koji
    - /pub/fedora
    - /pub/epel
  tags:
  - koji_builder

- name: make a bunch of dirs
  file: state=directory path={{ item }} owner=apache group=apache
  with_items:
    - /mnt/koji/packages
    - /mnt/koji/repos
    - /mnt/koji/work
    - /mnt/koji/scratch
  when: env == 'staging'
  tags:
  - koji_builder

- name: add pkgs
  yum: state=present pkg={{ item }}
  with_items:
    - yum-utils
    - koji-builder
    - strace
    - mock
    - kernel-firmware
    - ntp
    - ntpdate
    - rsyslog
    - audit
  tags:
  - koji_builder

- name: add oz/imagefctory pkgs on x86 and arm only. 
  yum: state=present pkg={{ item }}
  with_items:
    - oz
    - imagefactory
    - imagefactory-plugins-TinMan
    - imagefactory-plugins-Docker
    - imagefactory-plugins-vSphere
    - imagefactory-plugins-ovfcommon
    - imagefactory-plugins
    - imagefactory-plugins-OVA
    - imagefactory-plugins-EC2
    - imagefactory-plugins-RHEVM
    - python-psphere
    - VMDKstream
    - pykickstart
  when: ansible_architecture != 'ppc64'
  tags:
  - koji_builder

- name: /etc/kojid/kojid.conf
  copy: src=kojid.conf dest=/etc/kojid/kojid.conf
  when: not inventory_hostname.startswith(('arm01','arm03','koji01.stg','buildvm-01.stg'))
  notify:
  - restart kojid
  tags:
  - koji_builder

- name: arm /etc/kojid/kojid.conf
  copy: src=arm-kojid.conf dest=/etc/kojid/kojid.conf
  when: inventory_hostname.startswith(('arm01','arm03'))
  notify:
  - restart kojid
  tags:
  - koji_builder

- name: staging /etc/kojid/kojid.conf
  copy: src=stg-kojid.conf dest=/etc/kojid/kojid.conf
  when: inventory_hostname.startswith(('koji01.stg','buildvm-01.stg'))
  notify:
  - restart kojid
  tags:
  - koji_builder

- name: /etc/koji/koji.conf
  copy: src=koji.conf dest=/etc/koji.conf
  when: not inventory_hostname.startswith(('arm01','arm03'))
  tags:
  - koji_builder

- name: /etc/koji/koji.conf
  copy: src=arm-koji.conf dest=/etc/koji.conf
  when: inventory_hostname.startswith(('arm01','arm03'))
  tags:
  - koji_builder

# setup for oz/imagefactory
- name: make .psphere dir
  file: state=directory path=/root/.psphere mode=775 owner=root group=root
  tags:
  - koji_builder

- name: make .psphere/templates dir
  file: state=directory path=/root/.psphere/templates mode=775 owner=root group=root
  tags:
  - koji_builder

- name: copy over /root/.psphere/config.yaml
  copy: src={{ private }}/files/koji/config.yaml dest=/root/.psphere/config.yaml
  tags:
  - koji_builder
# done oz/imagefactory

- name: copy over koji ca cert
  copy: src="{{ private }}/files/koji/buildercerts/fedora-ca.cert" dest=/etc/kojid/cacert.pem

- name: copy over /etc/security/limits.conf
  copy: src=limits.conf dest=/etc/security/limits.conf
  tags:
  - koji_builder

- name: copy over builder cert to /etc/kojid/kojibuilder.pem
  copy: src="{{ private }}/files/koji/buildercerts/{{ inventory_hostname }}.pem" dest=/etc/kojid/kojibuilder.pem mode=600   
  tags:
  - koji_builder

# idmapd and make sure it's set to run
- name: idmapd.conf
  copy: src=idmapd.conf dest=/etc/idmapd.conf
  tags:
  - configs
  - koji_builder

- name: make a mnt/koji link
  file: state=link src=/mnt/fedora_koji/koji dest=/mnt/koji
  when: inventory_hostname.startswith('build') and datacenter == 'phx2'
  tags:
  - koji_builder

# mock configs for pungify job
- name: put extra special  mock configs in
  copy: src=builders/{{ item }} dest="/etc/mock/{{ item }}" mode=644
  with_items:
    - fedora-branched-pungi-armhfp.cfg
    - fedora-branched-pungi-i386.cfg
    - fedora-branched-pungi-x86_64.cfg
    - fedora-rawhide-pungi-i386.cfg
    - fedora-rawhide-pungi-x86_64.cfg
    - fedora-rawhide-pungi-armhfp.cfg
  tags:
  - koji_builder

- name: mock site-defaults.cfg
  copy: src=builders/site-defaults.cfg dest=/etc/mock/site-defaults.cfg mode=0644 owner=root group=mock
  when: not inventory_hostname.startswith('bkernel')
  tags:
  - koji_builder

- name: ntp steptickers
  copy: src="{{ files }}/common/step-tickers" dest=/etc/ntp/step-tickers
  tags:
  - koji_builder

- name: ntp.conf
  copy: src="{{ files }}/common/ntp.conf" dest=/etc/ntp.conf
  tags:
  - koji_builder

#
# We want more loop devices on builders to allow more image creates
#

- name: check for max_loop with grub2
  command: cat /etc/grub2.cfg
  register: max_loop
  always_run: yes
  changed_when: '1 != 1'
  when: ansible_distribution == 'Fedora' and ansible_architecture == 'x86_64'
  tags:
  - koji_builder

- name: check for max_loop with grub1
  command: cat /etc/grub.conf
  register: max_loop
  always_run: yes
  changed_when: '1 != 1'
  when: ansible_distribution == 'RedHat' and ansible_architecture == 'x86_64'
  tags:
  - koji_builder

- name: set kernel params for more loops
  action: command /sbin/grubby --update-kernel=ALL --args=max_loop=64 
  when: max_loop is defined and max_loop.stdout.find("max_loop=64") == -1
  tags:
  - koji_builder

#
# x86_64 builders run pungify, that needs hfs module in order to make
# The efi/mac images. This module is only needed on rhel. 
#

- name: special pkgs for the x86_64 builders
  yum: state=present pkg={{ item }}
  with_items:
  - kmod-hfsplus    
  when: is_rhel is defined and ansible_architecture == 'x86_64'
  tags:
  - koji_builder
