# http://blog.phymata.com/2017/07/29/inject-an-executable-script-into-a-container-in-kubernetes/
kind: ConfigMap
apiVersion: v1
metadata:
  name: helpers
  namespace: default
data:
  ready-probe.sh: |-
    # status=$(cqlsh -e "select bootstrapped from system.local" | sed -n 2p | tr -d " ")
    if [[ $(nodetool status | grep $POD_IP) == *"UN"* ]]; then
      if [[ $DEBUG ]]; then
        echo "Node is up!";
      fi
      exit 0;
    else
      if [[ $DEBUG ]]; then
        echo "Node is not up!";
      fi
      exit 1;
    fi

  pre-stop.sh: |-
    if [[ $(nodetool status | grep $POD_IP) == *"UN"* ]]; then
      if [[ $DEBUG ]]; then
        echo "running nodetool drain";
      fi
      echo "decommissioning node"
      nodetool decommission \
      && rm -rf /var/lib/cassandra/data/* \
      && rm -rf /var/lib/cassandra/commitlog \
      && rm -rf /var/lib/cassandra/saved_caches \
      && rm -rf /var/log/cassandra/
    else
      exit 0;
    fi

  post-start.sh: |-
    #!/bin/bash
    index=$(hostname | rev | cut -d- -f1 | rev) 
    if [[ $index == "0" ]]; then
      while true; do
        sleep 20;
        if [[ $(nodetool status | grep $POD_IP) == *"UN"* ]]; then
          break;
        fi
      done

      sleep 5
      output=$(cqlsh -u cassandra -p cassandra -e "describe keyspaces")
      if [[ "${output}" == *"Bad credentials"* ]]; then
        echo "User already created"
      else
        cqlsh -u cassandra -p cassandra -e "CREATE ROLE IF NOT EXISTS $CASSANDRA_USERNAME WITH PASSWORD = '$CASSANDRA_PASSWORD' AND SUPERUSER = true AND LOGIN = true;"
        sleep 1
        random_pass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
        cqlsh -u $CASSANDRA_USERNAME -p $CASSANDRA_PASSWORD -e "ALTER ROLE cassandra WITH PASSWORD='$random_pass' AND SUPERUSER=false;"
        cqlsh -u $CASSANDRA_USERNAME -p $CASSANDRA_PASSWORD -e "ALTER KEYSPACE system_auth WITH REPLICATION = {class : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 2}";
      fi
    fi
    exit 0

  demo-data: |-
    CREATE KEYSPACE testing2 WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 2};
    use testing2;
    CREATE TABLE employee( emp_id int PRIMARY KEY, emp_name text, emp_city text, emp_sal varint, emp_phone varint);

    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(1,'David', 'San Francisco', 50000, 983210987);
    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(2,'Robin', 'San Jose', 55000, 9848022339);
    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(3,'Bob', 'Austin', 45000, 9848022330);
    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(4, 'Monica','San Jose', 55000, 9458022330);
    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(5, 'Maros','San Jose', 55000, 9458022330);
    INSERT INTO employee (emp_id, emp_name, emp_city,emp_sal,emp_phone) VALUES(6, 'Mama','San Jose', 55000, 9458022330);
    use testing2; select * from employee;

  entrypoint.sh: |-
    #!/bin/bash
    set -e

    # first arg is `-f` or `--some-option`
    # or there are no args
    if [ "$#" -eq 0 ] || [ "${1#-}" != "$1" ]; then
      set -- cassandra -f "$@"
    fi

    # allow the container to be started with `--user`
    if [ "$1" = 'cassandra' -a "$(id -u)" = '0' ]; then
      chown -R cassandra /var/lib/cassandra /var/log/cassandra "$CASSANDRA_CONFIG"
      exec gosu cassandra "$BASH_SOURCE" "$@"
    fi

    if [ "$1" = 'cassandra' ]; then
      : ${CASSANDRA_RPC_ADDRESS='0.0.0.0'}

      : ${CASSANDRA_LISTEN_ADDRESS='auto'}
      if [ "$CASSANDRA_LISTEN_ADDRESS" = 'auto' ]; then
        CASSANDRA_LISTEN_ADDRESS="$(hostname --ip-address)"
      fi

      : ${CASSANDRA_BROADCAST_ADDRESS="$CASSANDRA_LISTEN_ADDRESS"}

      if [ "$CASSANDRA_BROADCAST_ADDRESS" = 'auto' ]; then
        CASSANDRA_BROADCAST_ADDRESS="$(hostname --ip-address)"
      fi
      : ${CASSANDRA_BROADCAST_RPC_ADDRESS:=$CASSANDRA_BROADCAST_ADDRESS}

      if [ -n "${CASSANDRA_NAME:+1}" ]; then
        : ${CASSANDRA_SEEDS:="cassandra"}
      fi
      : ${CASSANDRA_SEEDS:="$CASSANDRA_BROADCAST_ADDRESS"}
      
      sed -ri 's/(- seeds:).*/\1 "'"$CASSANDRA_SEEDS"'"/' "$CASSANDRA_CONFIG/cassandra.yaml"

      for yaml in \
        broadcast_address \
        broadcast_rpc_address \
        cluster_name \
        endpoint_snitch \
        listen_address \
        num_tokens \
        rpc_address \
        start_rpc \
        authenticator \
      ; do
        var="CASSANDRA_${yaml^^}"
        val="${!var}"
        if [ "$val" ]; then
          sed -ri 's/^(# )?('"$yaml"':).*/\2 '"$val"'/' "$CASSANDRA_CONFIG/cassandra.yaml"
        fi
      done

      for rackdc in dc rack; do
        var="CASSANDRA_${rackdc^^}"
        val="${!var}"
        if [ "$val" ]; then
          sed -ri 's/^('"$rackdc"'=).*/\1 '"$val"'/' "$CASSANDRA_CONFIG/cassandra-rackdc.properties"
        fi
      done
    fi

    exec "$@"