# This is a sample of how to get started with Traffic Parrot in OpenShift
# These commands were tested on Ubuntu 22.04.2 LTS

# First, make sure you have installed and activated the Traffic Parrot License Server
export LICENSE_SERVER=https://trafficparrot-license-usage-usage-trafficparrot-license-usage.apps-crc.testing

# Download the release zip file and your trafficparrot.license key file
wget https://.../trafficparrot.license -P ./trafficparrot-image
wget https://.../trafficparrot-linux-x64-jre-5.37.7.zip -P ./trafficparrot-image

# Extract trafficparrot.properties in case you need to modify any configuration
unzip -p ./trafficparrot-image/trafficparrot-*.zip '*/trafficparrot.properties' > ./trafficparrot-image/trafficparrot.properties

# Replace the trafficparrot.license.usage.server property with LICENSE_SERVER location
sed -i "s~^trafficparrot.license.usage.server=.*~trafficparrot.license.usage.server=${LICENSE_SERVER}~g" ./trafficparrot-image/trafficparrot.properties

# Login to oc
OC_LOGIN_USERNAME=developer
OC_LOGIN_URL=https://api.crc.testing:6443
eval $(crc oc-env)
oc login -u ${OC_LOGIN_USERNAME} ${OC_LOGIN_URL}

# Use a context where you have the right permissions
oc config get-contexts
oc config use-context <context with the right permissions>

# Create a project
export APP_NAME=trafficparrot
export REGISTRY=default-route-openshift-image-registry.apps-crc.testing
oc new-project ${APP_NAME} || oc project ${APP_NAME}

# Build the Docker image
podman login -u kubeadmin -p $(oc whoami -t) ${REGISTRY} --tls-verify=false
podman build \
-t ${REGISTRY}/${APP_NAME}/${APP_NAME}:latest \
-t ${REGISTRY}/${APP_NAME}/${APP_NAME}:$(date '+%d-%b-%YT%H-%M-%S') \
./trafficparrot-image

podman push ${REGISTRY}/${APP_NAME}/${APP_NAME}:latest --tls-verify=false
oc set image-lookup ${APP_NAME} && oc set image-lookup

# Use the Helm chart to deploy Traffic Parrot
helm --debug uninstall --wait trafficparrot
export HELM_ARGUMENTS="./trafficparrot-chart
--namespace ${APP_NAME}
--create-namespace
--set appid=${APP_NAME}
--set image=${APP_NAME}:latest
--set uiport=8080
--set uitermination=edge
--set httpvsport=8081
--set grpcvsport=5552
--set vstermination=passthrough"
helm --debug template ${HELM_ARGUMENTS}
helm --debug install --wait trafficparrot ${HELM_ARGUMENTS}
