# vim: set ft=yaml.ansible :
---
- name: Installing specific db
  shell: |
    helm upgrade --install -n db {{ db_name }} -f /tmp/{{ db_name }}.yaml ./db/{{ db_name }} --wait --create-namespace
  args:
    executable: /bin/bash
  when: db_name|length > 0
  tags: db
- name: installing dbs
  shell: |
    helm upgrade --install -n db "{{ item }}" "./db/{{ item }}" -f "/tmp/{{ item }}.yaml" --wait --create-namespace &>> "{{ playbook_dir }}"/db_helm.log
  with_items: "{{ db_list }}"
- name: Wait for all pods to be up
  pause:
    minutes: 2
- name: Initializing data
  block:
    - name: Restoring postgres data
      shell: |
        file="{{ item|basename }}"
        kubectl exec -n db postgresql-postgresql-0 -- /bin/bash -c "rm -rf /tmp/$file"
        kubectl cp -n db $file postgresql-postgresql-0:/tmp/
        kubectl exec -n db postgresql-postgresql-0 -- /bin/bash -c "PGPASSWORD=asayerPostgres psql -U postgres -f /tmp/$file" &> "{{ playbook_dir }}"/postgresql_init.log
      args:
        chdir: db/init_dbs/postgresql
      with_fileglob:
        - db/init_dbs/postgresql/*.sql
      retries: 3
      delay: 60
      register: result
      until: result.rc == 0
    - name: Restoring clickhouse data
      shell: |
        file="{{ item|basename }}"
        kubectl exec -n db clickhouse-0 -- /bin/bash -c "rm -rf /tmp/$file"
        kubectl cp -n db $file clickhouse-0:/tmp/
        kubectl exec -n db clickhouse-0 -- /bin/bash -c "clickhouse-client < /tmp/$file" 2>&1 | tee -a "{{ playbook_dir }}"/clickhouse_init.log
      args:
        chdir: db/init_dbs/clickhouse/create
      with_fileglob:
        - db/init_dbs/clickhouse/create/*.sql
      retries: 3
      delay: 60
      register: result
      until: result.rc == 0
      when: enterprise_edition_license|length > 0
    - name: Initializing Minio
      shell: |
        minio_pod=$(kubectl get po -n db -l app.kubernetes.io/name=minio -n db --output custom-columns=name:.metadata.name | tail -n+2)
        kubectl cp -n db  db/bucket_policy.sh $minio_pod:/tmp/bucket_policy.sh
        kubectl exec -n db $minio_pod -- bash /tmp/bucket_policy.sh "{{ minio_access_key }}" "{{ minio_secret_key }}"
      retries: 3
      delay: 60
      register: result
      until: result.rc == 0
      retries: 3
      delay: 60
      register: result
      until: result.rc == 0
    - name: Creating kafka topics
      shell: |
        # Creating topic
        kubectl exec -n db kafka-0 -- /bin/bash -c '/opt/bitnami/kafka/bin/kafka-topics.sh --create  --zookeeper kafka-zookeeper.db.svc.cluster.local:2181 --replication-factor 2 --partitions 16 --topic {{ item.name }} || true'
        ### Set retention time:
        kubectl exec -n db kafka-0 -- /bin/bash -c '/opt/bitnami/kafka/bin/kafka-configs.sh --zookeeper kafka-zookeeper.db.svc.cluster.local:2181 --entity-type topics --alter --add-config retention.ms={{ item.retention }} --entity-name {{ item.name }}'
      with_items:
        - {name: "messages", retention: "3456000000"}
        - {name: "events", retention: "3456000000"}
        - {name: "raw", retention: "3456000000"}
        - {name: "trigger", retention: "3456000000"}
      retries: 3
      delay: 60
      register: result
      until: result.rc == 0
      when: enterprise_edition_license|length > 0
- name: Downloading frontend archive
  unarchive:
    src: "https://static.openreplay.com/frontend.tar.gz"
    dest: "{{ role_path }}"
    remote_src: true
  tags:
    - frontend
- name: initializing frontend
  shell: |
    # Download frontend archive
    minio_pod=$(kubectl get po -n db -l app.kubernetes.io/name=minio -n db --output custom-columns=name:.metadata.name | tail -n+2)
    kubectl -n db cp frontend $minio_pod:/data/
  args:
    chdir: "{{ role_path }}"
  tags:
    - frontend
