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

# Download the release zip file and your trafficparrot.usage.license key file
rm -f ./licenseserver-image/*.zip
rm -f ./licenseserver-image/*.license
wget https://.../trafficparrot.usage.license -P ./licenseserver-image
wget https://.../trafficparrot-license-usage-linux-x64-jre-1.14.10.zip -P ./licenseserver-image

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

# Login to oc
export OC_LOGIN_USERNAME=developer
export 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-license-usage
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') \
./licenseserver-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 License Server
# set localdev=false if you are not working with a localhost cluster
# set storageclass to the name of a storage provider that can be used for a PersistentVolumeClaim
helm --debug uninstall --wait trafficparrot-license-usage
export HELM_ARGUMENTS="./licenseserver-chart
--namespace ${APP_NAME}
--create-namespace
--set appid=${APP_NAME}
--set image=${APP_NAME}:latest
--set uiport=8050
--set uitermination=edge
--set usageport=8040
--set usagetermination=passthrough
--set storage=1Gi
--set storageclass=example
--set localdev=true"
helm --debug template ${HELM_ARGUMENTS}
helm --debug install --wait trafficparrot-license-usage ${HELM_ARGUMENTS}
