---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "zookeeper.fullname" . }}
  labels:
    app: {{ template "zookeeper.name" . }}
    chart: {{ template "zookeeper.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    component: server
data:
    ok: |
      #!/bin/sh
      zkServer.sh status

    ready: |
      #!/bin/sh
      echo ruok | nc 127.0.0.1 ${1:-2181}

    run: |
      #!/bin/bash

      set -a
      ROOT=$(echo /apache-zookeeper-*)

      ZK_USER=${ZK_USER:-"zookeeper"}
      ZK_LOG_LEVEL=${ZK_LOG_LEVEL:-"INFO"}
      ZK_DATA_DIR=${ZK_DATA_DIR:-"/data"}
      ZK_DATA_LOG_DIR=${ZK_DATA_LOG_DIR:-"/data/log"}
      ZK_CONF_DIR=${ZK_CONF_DIR:-"/conf"}
      ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181}
      ZK_SERVER_PORT=${ZK_SERVER_PORT:-2888}
      ZK_ELECTION_PORT=${ZK_ELECTION_PORT:-3888}
      ZK_TICK_TIME=${ZK_TICK_TIME:-2000}
      ZK_INIT_LIMIT=${ZK_INIT_LIMIT:-10}
      ZK_SYNC_LIMIT=${ZK_SYNC_LIMIT:-5}
      ZK_HEAP_SIZE=${ZK_HEAP_SIZE:-2G}
      ZK_MAX_CLIENT_CNXNS=${ZK_MAX_CLIENT_CNXNS:-60}
      ZK_MIN_SESSION_TIMEOUT=${ZK_MIN_SESSION_TIMEOUT:- $((ZK_TICK_TIME*2))}
      ZK_MAX_SESSION_TIMEOUT=${ZK_MAX_SESSION_TIMEOUT:- $((ZK_TICK_TIME*20))}
      ZK_SNAP_RETAIN_COUNT=${ZK_SNAP_RETAIN_COUNT:-3}
      ZK_PURGE_INTERVAL=${ZK_PURGE_INTERVAL:-0}
      ID_FILE="$ZK_DATA_DIR/myid"
      ZK_CONFIG_FILE="$ZK_CONF_DIR/zoo.cfg"
      LOG4J_PROPERTIES="$ZK_CONF_DIR/log4j.properties"
      HOST=$(hostname)
      DOMAIN=`hostname -d`
      JVMFLAGS="-Xmx$ZK_HEAP_SIZE -Xms$ZK_HEAP_SIZE"

      APPJAR=$(echo $ROOT/*jar)
      CLASSPATH="${ROOT}/lib/*:${APPJAR}:${ZK_CONF_DIR}:"

      if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then
          NAME=${BASH_REMATCH[1]}
          ORD=${BASH_REMATCH[2]}
          MY_ID=$((ORD+1))
      else
          echo "Failed to extract ordinal from hostname $HOST"
          exit 1
      fi

      mkdir -p $ZK_DATA_DIR
      mkdir -p $ZK_DATA_LOG_DIR
      echo $MY_ID >> $ID_FILE

      echo "clientPort=$ZK_CLIENT_PORT" >> $ZK_CONFIG_FILE
      echo "dataDir=$ZK_DATA_DIR" >> $ZK_CONFIG_FILE
      echo "dataLogDir=$ZK_DATA_LOG_DIR" >> $ZK_CONFIG_FILE
      echo "tickTime=$ZK_TICK_TIME" >> $ZK_CONFIG_FILE
      echo "initLimit=$ZK_INIT_LIMIT" >> $ZK_CONFIG_FILE
      echo "syncLimit=$ZK_SYNC_LIMIT" >> $ZK_CONFIG_FILE
      echo "maxClientCnxns=$ZK_MAX_CLIENT_CNXNS" >> $ZK_CONFIG_FILE
      echo "minSessionTimeout=$ZK_MIN_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE
      echo "maxSessionTimeout=$ZK_MAX_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE
      echo "autopurge.snapRetainCount=$ZK_SNAP_RETAIN_COUNT" >> $ZK_CONFIG_FILE
      echo "autopurge.purgeInterval=$ZK_PURGE_INTERVAL" >> $ZK_CONFIG_FILE
      echo "4lw.commands.whitelist=*" >> $ZK_CONFIG_FILE

      for (( i=1; i<=$ZK_REPLICAS; i++ ))
      do
          echo "server.$i=$NAME-$((i-1)).$DOMAIN:$ZK_SERVER_PORT:$ZK_ELECTION_PORT" >> $ZK_CONFIG_FILE
      done

      rm -f $LOG4J_PROPERTIES

      echo "zookeeper.root.logger=$ZK_LOG_LEVEL, CONSOLE" >> $LOG4J_PROPERTIES
      echo "zookeeper.console.threshold=$ZK_LOG_LEVEL" >> $LOG4J_PROPERTIES
      echo "zookeeper.log.threshold=$ZK_LOG_LEVEL" >> $LOG4J_PROPERTIES
      echo "zookeeper.log.dir=$ZK_DATA_LOG_DIR" >> $LOG4J_PROPERTIES
      echo "zookeeper.log.file=zookeeper.log" >> $LOG4J_PROPERTIES
      echo "zookeeper.log.maxfilesize=256MB" >> $LOG4J_PROPERTIES
      echo "zookeeper.log.maxbackupindex=10" >> $LOG4J_PROPERTIES
      echo "zookeeper.tracelog.dir=$ZK_DATA_LOG_DIR" >> $LOG4J_PROPERTIES
      echo "zookeeper.tracelog.file=zookeeper_trace.log" >> $LOG4J_PROPERTIES
      echo "log4j.rootLogger=\${zookeeper.root.logger}" >> $LOG4J_PROPERTIES
      echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender" >> $LOG4J_PROPERTIES
      echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}" >> $LOG4J_PROPERTIES
      echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout" >> $LOG4J_PROPERTIES
      echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" >> $LOG4J_PROPERTIES

      if [ -n "$JMXDISABLE" ]
      then
          MAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
      else
          MAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dzookeeper.jmx.log4j.disable=$JMXLOG4J org.apache.zookeeper.server.quorum.QuorumPeerMain"
      fi

      set -x
      exec java -cp "$CLASSPATH" $JVMFLAGS $MAIN $ZK_CONFIG_FILE
