---
- name: make the app be real
  hosts: os_control:os_control_stg
  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

  vars:
    - fedora_messaging_username: "coreos-ostree-importer{{ env_suffix }}"
    - fedora_messaging_queue_name: "coreos-ostree-importer{{ env_suffix }}"
    - fedora_messaging_ca_file: "coreos-ostree-importer-fedora-messaging-cacert.pem"
    - fedora_messaging_cert_file: "coreos-ostree-importer-fedora-messaging-cert.pem"
    - fedora_messaging_key_file: "coreos-ostree-importer-fedora-messaging-key.pem"

  # Set the fedora_messaging_routing_keys var based on the environment
  pre_tasks:
    - set_fact:
        fedora_messaging_routing_keys:
          - "org.fedoraproject.stg.coreos.build.request.ostree-import"
      when: env == 'staging'
    - set_fact:
        fedora_messaging_routing_keys:
          - "org.fedoraproject.prod.coreos.build.request.ostree-import"
      when: env != 'staging'

  ############################################
  # actions to create the project in OpenShift
  ############################################
  # to run: sudo rbac-playbook -l os_control_stg openshift-apps/coreos-ostree-importer.yml
  roles:
  - role: openshift/project
    app: coreos-ostree-importer
    description: Imports OSTree commits into Fedora from the Fedora CoreOS pipeline
    appowners:
    - dustymabe
    - jlebon
    - kevin

  # Fedora Messaging User/Queue information
  - role: rabbit/queue
    username: "{{ fedora_messaging_username }}"
    queue_name: "{{ fedora_messaging_queue_name }}"
    routing_keys: "{{ fedora_messaging_routing_keys }}"
    thresholds:
      warning: 10
      critical: 100
    sent_topics: ^org\.fedoraproject\.{{ env_short }}\.coreos\..*

  # Fedora Messaging secrets
  - role: openshift/secret-file
    app: coreos-ostree-importer
    secret_name: fedora-messaging-ca
    key: "{{ fedora_messaging_ca_file }}"
    privatefile: "rabbitmq/{{env}}/pki/ca.crt"
  - role: openshift/secret-file
    app: coreos-ostree-importer
    secret_name: fedora-messaging-crt
    key: "{{ fedora_messaging_cert_file }}"
    privatefile: "rabbitmq/{{env}}/pki/issued/coreos-ostree-importer{{env_suffix}}.crt"
  - role: openshift/secret-file
    app: coreos-ostree-importer
    secret_name: fedora-messaging-key
    key: "{{ fedora_messaging_key_file }}"
    privatefile: "rabbitmq/{{env}}/pki/private/coreos-ostree-importer{{env_suffix}}.key"

  # Fedora Messaging config
  - role: openshift/object
    app: coreos-ostree-importer
    template: configmap.yml
    objectname: configmap.yml

  - role: openshift/object
    app: coreos-ostree-importer
    template: imagestream.yml
    objectname: imagestream.yml

  - role: openshift/object
    app: coreos-ostree-importer
    template: buildconfig.yml
    objectname: buildconfig.yml

  - role: openshift/object
    app: coreos-ostree-importer
    template: deploymentconfig.yml
    objectname: deploymentconfig.yml

  - role: openshift/object
    app: coreos-ostree-importer
    template: pvc.yml
    objectname: pvc.yml

  ###############################################
  # actions to delete the project from OpenShift
  ###############################################
  # to run: sudo rbac-playbook -l os_control_stg -t delete openshift-apps/coreos-ostree-importer.yml
  - role: openshift/object-delete
    app: coreos-ostree-importer
    objecttype: project
    objectname: coreos-ostree-importer
    tags: [never, delete]
