- name: setup the database
  hosts: db01.iad2.fedoraproject.org:db01.stg.iad2.fedoraproject.org
  gather_facts: no
  become: yes
  become_user: postgres
  vars_files:
    - /srv/web/infra/ansible/vars/global.yml
    - /srv/private/ansible/vars.yml
    - /srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml

  tasks:
    - name: maubot DB user
      postgresql_user:
        name: maubot
        password: "{{ (env == 'production')|ternary(maubot_prod_db_password, maubot_stg_db_password) }}"
    - name: maubot database creation
      postgresql_db:
        name: maubot
        owner: maubot
        encoding: UTF-8

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

  roles:
  - role: rabbit/user
    username: "maubot{{ env_suffix }}"
    sent_topics: ^org\.fedoraproject\.{{ env_short }}\.(meetbot|maubot)\..*

  - role: openshift/project
    app: maubot
    description: Maubot
    appowners:
    - ryanlerch
    - kevin

  - role: openshift/keytab
    app: maubot
    key: service.keytab
    secret_name: maubot-keytab
    service: HTTP
    host: "maubot.apps.ocp{{env_suffix}}.fedoraproject.org"

  - role: openshift/ipa-client
    app: maubot

  - role: openshift/object
    app: maubot
    file: imagestream.yml
    objectname: imagestream.yml

  - role: openshift/object
    app: maubot
    template: buildconfig.yml
    objectname: buildconfig.yml

  - role: openshift/object
    app: maubot
    template: meetbot-logs-volume.yml
    objectname: meetbot-logs-volume.yml

  - role: openshift/object
    app: maubot
    template: configmap.yml
    objectname: configmap.yml

  - role: openshift/secret-file
    app: maubot
    secret_name: maubot-fedora-messaging-key
    key: maubot.key
    privatefile: "rabbitmq/{{env}}/pki/private/maubot{{env_suffix}}.key"

  - role: openshift/secret-file
    app: maubot
    secret_name: maubot-fedora-messaging-crt
    key: maubot.crt
    privatefile: "rabbitmq/{{env}}/pki/issued/maubot{{env_suffix}}.crt"

  - role: openshift/secret-file
    app: maubot
    secret_name: maubot-fedora-messaging-ca
    key: maubot.ca
    privatefile: "rabbitmq/{{env}}/pki/ca.crt"

  - role: openshift/start-build
    app: maubot
    buildname: maubot-build
    objectname: maubot-build

  - role: openshift/object
    app: maubot
    file: service.yml
    objectname: service.yml

  - role: openshift/route
    app: maubot
    routename: maubot
    host: "maubot.apps.ocp{{ env_suffix }}.fedoraproject.org"
    serviceport: maubot
    servicename: maubot

  - role: openshift/object
    app: maubot
    file: deploymentconfig.yml
    objectname: deploymentconfig.yml

  - role: openshift/rollout
    app: maubot
    dcname: maubot

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