EasyAuthForK8s/main.sh

192 строки
6.1 KiB
Bash
Исходник Обычный вид История

#!/bin/sh -x
echo ""
echo "BEGIN @ $(date +"%T"): START OF SCRIPT"
# Check Helm Installation
if ! [ -x "$(command -v helm)" ]; then
echo "Please install Helm."
exit
fi
# Check JQ Installation
if ! [ -x "$(command -v jq)" ]; then
echo "Please install JQ. You can install by typing:"
echo "sudo apt install jq"
exit
fi
# Check Kubernetes Installation
if ! [ -x "$(command -v kubectl)" ]; then
echo "Please install Kubernetes."
exit
fi
# Check Azure CLI Installation
if ! [ -x "$(command -v az)" ]; then
echo "Please install the Azure CLI."
exit
fi
echo ""
echo "BEGIN @ $(date +"%T"): Set variables..."
# Initialize Variables for flags
AD_APP_NAME=''
CLUSTER_NAME=''
CLUSTER_RG=''
EMAIL=''
LOCATION=''
INPUTIMAGE=''
2021-10-20 21:38:02 +03:00
ALT_TENANT_ID=''
SKIP_CLUSTER_CREATION=''
2021-10-28 00:50:19 +03:00
E2E_TEST_FLAG=''
SP=''
SP_SECRET=''
2021-10-28 00:50:19 +03:00
while getopts "a:c:r:e:l:i:t:s:z:pgh" OPTION
do
case $OPTION in
a)
AD_APP_NAME=$OPTARG ;;
c)
CLUSTER_NAME=$OPTARG ;;
r)
CLUSTER_RG=$OPTARG ;;
e)
EMAIL=$OPTARG ;;
l)
LOCATION=$OPTARG ;;
i)
INPUTIMAGE=$OPTARG ;;
2021-10-20 21:38:02 +03:00
t)
ALT_TENANT_ID=$OPTARG ;;
2021-10-28 00:50:19 +03:00
s)
SP=$OPTARG ;;
z)
SP_SECRET=$OPTARG ;;
p)
2021-10-15 07:18:47 +03:00
SKIP_CLUSTER_CREATION="True" ;;
2021-10-28 00:50:19 +03:00
g)
E2E_TEST_FLAG="True" ;;
h)
# Change to how others show it like az
echo "HELP: Here are the flags and their variables"
echo "REQUIRED: -a is for AD_APP_NAME"
echo "REQUIRED: -c is for CLUSTER_NAME *Note: Cluster Name must be unique*"
echo "REQUIRED: -r is for CLUSTER_RG"
echo "REQUIRED: -e is for EMAIL"
echo "REQUIRED: -l is for LOCATION"
echo "OPTOINAL: -i is for INPUTIMAGE"
2021-10-20 21:38:02 +03:00
echo "OPTOINAL: -t is for ALT_TENANT_ID"
echo "OPTOINAL: -s is for SERVICE_PRICIPAL"
echo "OPTOINAL: -z is for SP_SECRET"
echo "OPTOINAL: -p is for SKIP_CLUSTER_CREATION"
exit ;;
esac
done
# Force required flags.
if [ -z "$AD_APP_NAME" ] || [ -z "$CLUSTER_NAME" ] || [ -z "$CLUSTER_RG" ] || [ -z "$EMAIL" ] || [ -z "$LOCATION" ]; then
echo "*****ERROR. Please enter all required flags.*****"
exit
fi
echo ""
# Show the subscription we will be deploying to.
if [ -z "$E2E_TEST_FLAG" ]; then
echo "******We will be deploying to this subscription******"
az account show
fi
APP_HOSTNAME="$AD_APP_NAME.$LOCATION.cloudapp.azure.com"
HOMEPAGE=https://$APP_HOSTNAME
IDENTIFIER_URIS=$HOMEPAGE
2021-11-29 20:18:19 +03:00
REPLY_URLS=https://$APP_HOSTNAME/easyauth/signin-oidc
echo "The value of -a is $AD_APP_NAME - AD_APP_NAME"
echo "The value of -c is $CLUSTER_NAME - CLUSTER_NAME"
echo "The value of -r is $CLUSTER_RG - CLUSTER_RG"
echo "The value of -e is $EMAIL - EMAIL"
echo "The value of -l is $LOCATION - LOCATION"
echo "The value of -i is $INPUTIMAGE - INPUTIMAGE"
2021-10-20 21:38:02 +03:00
echo "The value of -t is $ALT_TENANT_ID - ALT_TENANT_ID"
echo "The value of -s is $SP - SERVICE_PRICIPAL"
2021-10-28 00:50:19 +03:00
echo "The value of -z is $SP_SECRET - SP_SECRET"
echo "The value of -p is $SKIP_CLUSTER_CREATION - SKIP_CLUSTER_CREATION"
2021-10-28 00:50:19 +03:00
echo "The value of -g is $E2E_TEST_FLAG - E2E_TEST_FLAG"
echo "COMPLETE @ $(date +"%T"): Setting variables"
echo "****BEGIN @ $(date +"%T"): Call AKS Cluster Creation script...****"
# If there is no flag set for SKIP_CLUSTER_CREATION, then create the AKS cluster.
if [ -z "$SKIP_CLUSTER_CREATION" ]; then
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/1-clusterCreation.sh
else
echo "CLUSTER CREATION WAS SKIPPED!"
fi
echo "****COMPLETE @ $(date +"%T"): Done cluster creation script.****"
# Get the credentials outside of cluster creation script.
echo "BEGIN @ $(date +"%T"): Getting cluster creds..."
az aks get-credentials -g $CLUSTER_RG -n $CLUSTER_NAME
echo "COMPLETE @ $(date +"%T"): Getting cluster creds"
# Add Helm
helm repo add stable https://charts.helm.sh/stable
echo "****BEGIN @ $(date +"%T"): Call Ingress Controller Creation script****"
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/2-ingressCreation.sh
echo "****COMPLETE @ $(date +"%T"): Ingress controller created script****"
echo "****BEGIN @ $(date +"%T"): Call ADD App Creation script****"
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/3-registerAADApp.sh
echo "****COMPLETE @ $(date +"%T"): AAD App created script****"
echo "****BEGIN @ $(date +"%T"): Call Deploy MSAL Proxy script****"
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/4-deployMSALProxy.sh
echo "****COMPLETE @ $(date +"%T"): Deployed MSAL Proxy script****"
echo "****BEGIN @ $(date +"%T"): Call Install Cert Manager script****"
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/5-installCertManager.sh
echo "****COMPLETE @ $(date +"%T"): Installed Cert Manager script****"
2021-06-10 22:16:39 +03:00
echo "BEGIN @ $(date +"%T"): Deploy sample app..."
# If we have a parameter for an image install a custom image. If not, then we install kuard.
if [ -z "$INPUTIMAGE" ]; then
echo "No image input, installing kuard."
2021-12-10 15:51:01 +03:00
kubectl run easyauth-sample-pod --image=docker.io/lesterjt/easyauthfork8s-sample:latest --expose --port=80
2021-06-10 22:16:39 +03:00
else
echo "Your custom image $INPUTIMAGE installed"
2021-12-10 15:51:01 +03:00
kubectl run custom-pod --image=$INPUTIMAGE --expose --port=80
2021-06-10 22:16:39 +03:00
fi
echo "COMPLETE @ $(date +"%T"): Deployed sample app"
echo "****BEGIN @ $(date +"%T"): Call Deploy New Ingress Resource script****"
2020-02-01 01:56:18 +03:00
. ./AutomationScripts/6-deployNewIngressResource.sh
echo "****COMPLETE @ $(date +"%T"): Deployed New Ingress Resource script****"
echo "BEGIN @ $(date +"%T"): Verify Production Certificate works..."
2021-10-14 23:08:48 +03:00
INPUT_STATUS=false
2021-10-14 23:35:04 +03:00
n=50
2021-10-14 23:08:48 +03:00
while [[ "$INPUT_STATUS" != "True" || "$INPUT_TYPE" != "Ready" ]]
do
echo ""
kubectl get certificate $TLS_SECRET_NAME
2021-10-14 23:08:48 +03:00
INPUT_STATUS=$(kubectl get certificate $TLS_SECRET_NAME -o=jsonpath='{.status.conditions[0].status}')
INPUT_TYPE=$(kubectl get certificate $TLS_SECRET_NAME -o=jsonpath='{.status.conditions[0].type}')
echo "status: " $INPUT_STATUS
echo "type: " $INPUT_TYPE
sleep 5
2021-10-14 23:35:04 +03:00
if [ "$n" == "0" ]; then
echo "ERROR. INFINITE LOOP in main.sh when calling kubectl get certificate."
exit 1
fi
n=$((n-1))
echo ""
done
echo "COMPLETE @ $(date +"%T"): Verify Production Certificate works"
echo "END OF SCRIPT"
echo ""
echo ""
echo "Visit the app in the browser. Good luck! " $HOMEPAGE
echo ""
echo ""