apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "rabbitmq.fullname" . }}-scripts
  labels:
    {{- include "rabbitmq.labels" . | nindent 4 }}
data:
  init.sh: |
    #!/bin/sh
    echo "Initializing RabbitMQ instance..."
    echo "Copy configuration"
    cp /temp/rabbitmq/* /etc/rabbitmq
    if [ -d /extraconfigs ]; then
      echo "Add extra configs to rabbitmq config"
      cat /extraconfigs/* >>/etc/rabbitmq/rabbitmq.conf
    fi
    if [ -d /extraadvancedconfigs ]; then
      echo "Add extra advanced configs to rabbitmq advanced config"
      cat /extraadvancedconfigs/* >>/etc/rabbitmq/advanced.conf
    fi
    if [ -d /temp/plugins ]; then
      echo "Copy plugin configuration"
      cp /temp/plugins/* /etc/rabbitmq
    else
      echo "No plugins configured."
    fi
    mkdir -p /etc/rabbitmq/conf.d
    if [ ! -f /var/lib/rabbitmq/.erlang.cookie ]; then
      echo "Copy erlang cookie"
      echo $ERLANG_COOKIE >/var/lib/rabbitmq/.erlang.cookie
    else
      echo "Erlang cookie already exists."
    fi
    chmod 600 /var/lib/rabbitmq/.erlang.cookie
    echo "Finished."
  startup.sh: |
    {{- if .Values.clustering.forceBoot }}
    echo "Forcing boot of cluster instance" >/proc/1/fd/1
    rabbitmqctl force_boot
    {{- end }}
    {{- if .Values.clustering.rebalance }}
    until rabbitmqctl cluster_status >/dev/null; do
      echo "PostStart: Waiting for cluster readiness..." >/proc/1/fd/1
      sleep 5
    done
    echo "PostStart: Rebalancing all queues.." >/proc/1/fd/1
    rabbitmq-queues rebalance "all" >/proc/1/fd/1
    {{- end }}
  shutdown.sh: |
    {{- if and (.Values.terminationGracePeriodSeconds) (gt (int .Values.terminationGracePeriodSeconds) 10) }}
    if rabbitmqctl cluster_status; then
        echo "PreStop: Will wait up to {{ sub .Values.terminationGracePeriodSeconds 10 }} seconds for node to make sure cluster is healthy after node shutdown" >/proc/1/fd/1
        timeout {{ sub .Values.terminationGracePeriodSeconds 10 }} /scripts/safeshutdown.sh
    fi
    {{- end }}
    echo "PreStop: Stopping RabbitMQ" >/proc/1/fd/1
    rabbitmqctl stop_app
    echo "PreStop: RabbitMQ stopped" >/proc/1/fd/1
    sleep 5
  safeshutdown.sh: |
    while true; do
        waiting="false"
        if ! rabbitmq-diagnostics -q check_if_node_is_mirror_sync_critical >/proc/1/fd/1; then
            echo "PreStop: check_if_node_is_mirror_sync_critical returns error. Continuing to wait" >/proc/1/fd/1
            waiting="true"
        else
            echo "PreStop: check_if_node_is_mirror_sync_critical returns o.k." >/proc/1/fd/1
        fi
        if ! rabbitmq-diagnostics -q check_if_node_is_quorum_critical >/proc/1/fd/1; then
            echo "PreStop: check_if_node_is_quorum_critical returns error. Continuing to wait" >/proc/1/fd/1
            waiting="true"
        else
            echo "PreStop: check_if_node_is_quorum_critical returns o.k." >/proc/1/fd/1
        fi
        if [ $waiting = "true" ]; then
            sleep 1
        else
            break
        fi
    done
