- name: provision koschei
  hosts: os-masters-stg[0]
  user: root
  gather_facts: False

  vars_files:
    - /srv/web/infra/ansible/vars/global.yml
    - /srv/private/ansible/vars.yml
    - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml
    - /srv/web/infra/ansible/roles/openshift-apps/koschei/vars/main.yml
    - /srv/web/infra/ansible/roles/openshift-apps/koschei/vars/{{ env }}.yml

  roles:
  - openshift/project

  - role: openshift/keytab
    secret_name: keytab
    key: krb5.keytab
    service: koschei
    host: "koschei-backend01{{ env_suffix }}.phx2.fedoraproject.org"

  - role: openshift/route
    routename: frontend
    host: "koschei{{ env_suffix }}.fedoraproject.org"
    serviceport: web
    servicename: frontend

  tasks:
    - name: Apply objects
      include_role: name=openshift/object
      vars:
        template: "{{ item }}.yml"
        objectname: "{{ item }}.yml"
      with_items:
        - frontend-config
        - backend-config
        - frontend-deployment
        - frontend-service

    - name: Apply DeploymentConfig
      include_role: name=openshift/object
      vars:
        template: backend-deployment.yml
        objectname: "{{ item.name }}-deployment.yml"
        service: "{{ item.name }}"
        min_cpu: "{{ item.cpu[0] }}"
        max_cpu: "{{ item.cpu[1] }}"
        min_mem: "{{ item.memory[0] }}"
        max_mem: "{{ item.memory[1] }}"
      with_items:
        - { name: polling,        cpu: [ 1000, 1500 ],  memory: [  256,  512 ] }
        - { name: scheduler,      cpu: [  200,  500 ],  memory: [   64,  128 ] }
        - { name: build-resolver, cpu: [ 1000, 1500 ],  memory: [ 1024, 4096 ] }
        - { name: repo-resolver,  cpu: [ 2000, 8000 ],  memory: [ 1024, 4096 ] }
      loop_control:
        label: "{{ item.name }}"

    - name: Apply CronJob
      include_role: name=openshift/object
      vars:
        template: cronjob.yml
        objectname: "{{ item.name }}-cronjob.yml"
        name: "{{ item.name }}"
        schedule: "{{ item.schedule }}"
        command: "{{ item.command }}"
      with_items:
        - name: db-cleanup
          schedule: "0 3 * * *"
          command: "koschei-admin cleanup --older-than 6"
      loop_control:
        label: "{{ item.name }}"
