apiVersion: v1
kind: ConfigMap
metadata:
  name: publish-dashboard
data:
  publish-dashboard.sh: |
    #!/bin/bash

    # Check if redash is up
    while ! curl -Ls -X GET "http://redash-viewer/reports/login" | grep "Sign in to" ;
    do echo "Waiting for Redash viewer instance" ; sleep 5 ;
    done

    # Login to Redash
    creds=email=$SYSTEM_ADMIN_EMAIL\&password=$SYSTEM_ADMIN_PASS
    curl -L -c cookies.txt -d "$creds" http://redash-viewer/reports/login >/dev/null

    # Get Redash User API key
    api_key=$(curl -s -b cookies.txt -X POST "http://redash-viewer/reports/api/users/1/regenerate_api_key" | grep -oP '(?<="api_key": ")\w+')
    api_key="${api_key//\"}"

    # Publish dashboard
    java -jar -DREDASH_URL=http://redash-viewer/reports -DREDASH_API_KEY=$api_key -DPOSTGRES_PASSWORD=$PSQL_PASS -DPOSTGRES_USER=$PSQL_USER -DDB_NAME=audit -DDB_URL=citus-master -DDB_PORT=5432 -DPWD_ADMIN=$ADMIN_ROLE_PASS -DPWD_AUDITOR=$AUDIT_ROLE_PASS app.jar --auditor
    java -jar -DREDASH_URL=http://redash-viewer/reports -DREDASH_API_KEY=$api_key -DPOSTGRES_PASSWORD=$PSQL_PASS -DPOSTGRES_USER=$PSQL_USER -DDB_NAME=audit -DDB_URL=citus-master -DDB_PORT=5432 -DPWD_ADMIN=$ADMIN_ROLE_PASS -DPWD_AUDITOR=$AUDIT_ROLE_PASS app.jar --reports
