#!/bin/bash

set -eux

export KUBE_MASTER_IP=0.0.0.0
export KUBERNETES_MASTER=http://$KUBE_MASTER_IP

# The test is a bit spammy with files, head over to /tmp to discard when done
cd /tmp

# Need to update the script with the proposed IP of the web-head pod
# Currently its set to 10.1.4.89

sed -i 's/pass_http=0/exit 0/' /opt/kubernetes/examples/k8petstore/k8petstore.sh
/opt/kubernetes/examples/k8petstore/k8petstore.sh

# Loop until the daemon has come online
counter=0
KUBE_POD_STATUS="Pending"
while [ "$KUBE_POD_STATUS" == "Pending" ]
do
    if [[ counter -eq 200 ]]; then
	echo "Pod failed to come online. Timeout reached"
	exit 1
    fi
    CUR_STATUS=$(kubectl get pods -o json | $CHARM_DIR/actions/lib/parse_get_pods)
    if [ ! -z "$CUR_STATUS" ]; then
	KUBE_POD_STATUS=$CUR_STATUS
    fi
    sleep 1
    counter+=1
done

sed -i 's/exit 0/pass_http=0/' /opt/kubernetes/examples/k8petstore/k8petstore.sh
sed -i "s/PUBLIC_IP=\"10.1.4.89\"/PUBLIC_IP=\"$KUBE_POD_STATUS\"/"  /opt/kubernetes/examples/k8petstore/k8petstore.sh
/opt/kubernetes/examples/k8petstore/k8petstore.sh

# Return execution to the CHARM_DIR for further actions
cd $CHARM_DIR

