#!/bin/bash # # Prereqs: a running ocp 4 cluster, logged in as kubeadmin # MYDIR="$( cd "$(dirname "$0")" ; pwd -P )" function usage() { echo "usage: $(basename $0) [-c/--count usercount] -m/--module-type module_type" } # Create the log file file_name=logfile.txt current_time=$(date "+%Y.%m.%d-%H.%M.%S") new_fileName=$file_name.$current_time exec > $new_fileName # Defaults USERCOUNT=100 MODULE_TYPE=m1 REQUESTED_CPU=2 REQUESTED_MEMORY=4Gi GOGS_PWD=r3dh4t1! POSITIONAL=() while [[ $# -gt 0 ]] do key="$1" case $key in -c|--count) USERCOUNT="$2" shift # past argument shift # past value ;; -m|--module-type) MODULE_TYPE="$2" shift # past argument shift # past value ;; *) # unknown option echo "Unknown option: $key" usage exit 1 ;; esac done echo -e "Start with CCNRD Dev Track Environment Deployment... \n" start_time=$SECONDS set -- "${POSITIONAL[@]}" # restore positional parameters echo -e "USERCOUNT: $USERCOUNT" echo -e "MODULE_TYPE: $MODULE_TYPE\n" if [ ! "$(oc get clusterrolebindings)" ] ; then echo "not cluster-admin" exit 1 fi # Make the admin as cluster admin oc adm policy add-cluster-role-to-user cluster-admin $(oc whoami) # Add view role of default namespace to all userXX for i in $(eval echo "{0..$USERCOUNT}") ; do oc adm policy add-role-to-user view user$i -n default echo -n . sleep 2 done # create labs-infra project oc new-project labs-infra # adjust limits for admin oc get userquota/default RESULT=$? if [ $RESULT -eq 0 ]; then oc delete userquota/default else echo -e "userquota already is deleted...\n" fi oc delete limitrange --all -n labs-infra # get routing suffix TMP_PROJ="dummy-$RANDOM" oc new-project $TMP_PROJ oc create route edge dummy --service=dummy --port=8080 -n $TMP_PROJ ROUTE=$(oc get route dummy -o=go-template --template='{{ .spec.host }}' -n $TMP_PROJ) HOSTNAME_SUFFIX=$(echo $ROUTE | sed 's/^dummy-'${TMP_PROJ}'\.//g') MASTER_URL=$(oc whoami --show-server) CONSOLE_URL=$(oc whoami --show-console) echo -e "HOSTNAME_SUFFIX: $HOSTNAME_SUFFIX \n" oc project labs-infra # create templates for labs oc create -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/template-binary.json -n openshift oc create -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/template-prod.json -n openshift oc create -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/ccn-sso72-template.json -n openshift # deploy rhamt if [ -z "${MODULE_TYPE##*m1*}" ] ; then oc process -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/web-template-empty-dir-executor.json \ -p WEB_CONSOLE_REQUESTED_CPU=$REQUESTED_CPU \ -p WEB_CONSOLE_REQUESTED_MEMORY=$REQUESTED_MEMORY \ -p EXECUTOR_REQUESTED_CPU=$REQUESTED_CPU \ -p EXECUTOR_REQUESTED_MEMORY=2Gi | oc create -n labs-infra -f - fi # deploy gogs oc -n labs-infra new-app -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/gogs-template.yaml \ -p HOSTNAME=gogs-labs-infra.$HOSTNAME_SUFFIX \ -p GOGS_VERSION=0.11.34 \ -p SKIP_TLS_VERIFY=true \ -p APPLICATION_NAME=gogs # Wait for gogs postgresql to be running echo -e "Waiting for gogs postgresql to be running... \n" while [ 1 ]; do STAT=$(curl -s -w '%{http_code}' -o /dev/null http://gogs-labs-infra.$HOSTNAME_SUFFIX) if [ "$STAT" = 200 ] ; then break fi echo -n . sleep 10 done # Create gogs admin user STAT=$(curl -s -w '%{http_code}' -o /dev/null -X POST http://gogs-labs-infra.$HOSTNAME_SUFFIX/user/sign_up \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "user_name=adminuser&password=adminpwd&&retype=adminpwd&&email=adminuser@gogs.com") if [ "$STAT" = 302 ] || [ "$STAT" = 200 ] ; then echo "adminuser is created successfully..." else echo "Failure to create adminuser with $STAT" fi # Create gogs users echo -e "Creating $USERCOUNT gogs users.... \n" for i in $(eval echo "{0..$USERCOUNT}") ; do STAT=$(curl -s -w '%{http_code}' -o /dev/null -X POST http://gogs-labs-infra.$HOSTNAME_SUFFIX/api/v1/admin/users \ -H "Content-Type: application/json" \ -d '{"login_name": "user'"$i"'", "username": "user'"$i"'", "email": "user'"$i"'@gogs.com", "password": "'"$GOGS_PWD"'"}' \ -u adminuser:adminpwd) if [ "$STAT" = 200 ] || [ "$STAT" = 201 ] ; then echo "user$i is created successfully..." else echo "Failure to create user$i with $STAT" fi done # Create users' private repo echo -e "Creating $USERCOUNT users' private repo...." for MODULE in $(echo $MODULE_TYPE | sed "s/,/ /g") ; do MODULE_NO=$(echo $MODULE | cut -c 2) CLONE_ADDR=https://github.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2m$MODULE_NO-labs.git REPO_NAME=cloud-native-workshop-v2m$MODULE_NO-labs for i in $(eval echo "{0..$USERCOUNT}") ; do USER_ID=$(($i + 2)) STAT=$(curl -s -w '%{http_code}' -o /dev/null -X POST http://gogs-labs-infra.$HOSTNAME_SUFFIX/api/v1/repos/migrate \ -H "Content-Type: application/json" \ -d '{"clone_addr": "'"$CLONE_ADDR"'", "uid": '"$USER_ID"', "repo_name": "'"$REPO_NAME"'" }' \ -u "user${i}:${GOGS_PWD}") if [ "$STAT" = 201 ] ; then echo "user$i $MODULE repo is created successfully..." else echo "Failure to create user$i $MODULE repo with $STAT" fi done done # Setup Istio Service Mesh oc get project istio-operator RESULT=$? if [ $RESULT -eq 0 ]; then echo -e "istio-operator already exists..." elif [ -z "${MODULE_TYPE##*m3*}" || [ -z "${MODULE_TYPE##*m4*}" ] ; then echo -e "Installing istio-operator..." oc new-project istio-operator oc apply -n istio-operator -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/servicemesh-operator.yaml fi oc get project istio-system RESULT=$? if [ $RESULT -eq 0 ]; then echo -e "istio-system already exists..." elif [ -z "${MODULE_TYPE##*m3*}" || [ -z "${MODULE_TYPE##*m4*}" ] ; then echo -e "Deploying the Istio Control Plane with Single-Tenant..." oc new-project istio-system oc create -n istio-system -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/servicemeshcontrolplane.yaml # bash <(curl -L https://git.io/getLatestKialiOperator) --operator-image-version v1.0.0 --operator-watch-namespace '**' --accessible-namespaces '**' --operator-install-kiali false # oc apply -n istio-system -f https://raw.githubusercontent.com/kiali/kiali/v1.0.0/operator/deploy/kiali/kiali_cr.yaml fi # Create coolstore & bookinfo projects for each user echo -e "Creating coolstore & bookinfo projects for each user... \n" for i in $(eval echo "{0..$USERCOUNT}") ; do if [ -z "${MODULE_TYPE##*m1*}" ] || [ -z "${MODULE_TYPE##*m2*}" ] || [ -z "${MODULE_TYPE##*m3*}" ] ; then oc new-project user$i-inventory oc adm policy add-scc-to-user anyuid -z default -n user$i-inventory oc adm policy add-scc-to-user privileged -z default -n user$i-inventory oc adm policy add-role-to-user admin user$i -n user$i-inventory oc new-project user$i-catalog oc adm policy add-scc-to-user anyuid -z default -n user$i-catalog oc adm policy add-scc-to-user privileged -z default -n user$i-catalog oc adm policy add-role-to-user admin user$i -n user$i-catalog fi if [ -z "${MODULE_TYPE##*m3*}" ] ; then oc new-project user$i-bookinfo oc adm policy add-scc-to-user anyuid -z default -n user$i-bookinfo oc adm policy add-scc-to-user privileged -z default -n user$i-bookinfo oc adm policy add-role-to-user admin user$i -n user$i-bookinfo oc adm policy add-role-to-user view user$i -n istio-system fi if [ -z "${MODULE_TYPE##*m4*}" ] ; then oc new-project user$i-cloudnativeapps oc adm policy add-scc-to-user anyuid -z default -n user$i-cloudnativeapps oc adm policy add-scc-to-user privileged -z default -n user$i-cloudnativeapps oc adm policy add-role-to-user admin user$i -n user$i-cloudnativeapps oc adm policy add-role-to-user view user$i -n istio-system fi done # Install Custom Resource Definitions, Knative Serving, Knative Eventing if [ -z "${MODULE_TYPE##*m4*}" ] ; then echo -e "Installing Knative Subscriptions..." oc apply -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/catalog-sources.yaml echo -e "Installing Knative Serving..." oc apply -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/knative-serving-subscription.yaml echo -e "Installing Knative Eventing..." oc apply -f https://raw.githubusercontent.com/RedHat-Middleware-Workshops/cloud-native-workshop-v2-infra/ocp-4.1/files/knative-eventing-subscription.yaml for i in $(eval echo "{0..$USERCOUNT}") ; do oc adm policy add-role-to-user view user$i -n knative-serving done echo -e "Creating Role, Group, and assign Users" for i in $(eval echo "{0..$USERCOUNT}") ; do cat <Red Hat® Single Sign On", "loginTheme": "rh-sso", "adminTheme": "rh-sso", "accountTheme": "rh-sso", "emailTheme": "rh-sso", "accessTokenLifespan": 6000 }') if [ "$RES" = 204 ] ; then echo -e "Updated a master realm with RH-SSO theme successfully...\n" else echo -e "Failure to update a master realm with RH-SSO theme with $RES\n" fi echo -e "Creating RH-SSO users as many as gogs users \n" for i in $(eval echo "{0..$USERCOUNT}") ; do RES=$(curl -s -w '%{http_code}' -o /dev/null -k -X POST https://secure-rhamt-web-console-labs-infra.$HOSTNAME_SUFFIX/auth/admin/realms/rhamt/users \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -H "Authorization: Bearer $RESULT_TOKEN" \ -d '{ "username": "user'"$i"'", "enabled": true, "disableableCredentialTypes": [ "password" ] }') if [ "$RES" = 200 ] || [ "$RES" = 201 ] || [ "$RES" = 409 ] ; then echo -e "Created RH-SSO user$i successfully...\n" else echo -e "Failure to create RH-SSO user$i with $RES\n" fi done echo -e "Retrieving RH-SSO user's ID list \n" USER_ID_LIST=$(curl -k -X GET https://secure-rhamt-web-console-labs-infra.$HOSTNAME_SUFFIX/auth/admin/realms/rhamt/users/ \ -H "Accept: application/json" \ -H "Authorization: Bearer $RESULT_TOKEN") echo -e "USER_ID_LIST: $USER_ID_LIST \n" echo -e "Getting access token to reset passwords \n" export RESULT_TOKEN=$(curl -k -X POST https://secure-rhamt-web-console-labs-infra.$HOSTNAME_SUFFIX/auth/realms/master/protocol/openid-connect/token \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=admin" \ -d 'password=password' \ -d 'grant_type=password' \ -d 'client_id=admin-cli' | jq -r '.access_token') echo -e "RESULT_TOKEN: $RESULT_TOKEN \n" echo -e "Reset passwords for each RH-SSO user \n" for i in $(jq '. | keys | .[]' <<< "$USER_ID_LIST"); do USER_ID=$(jq -r ".[$i].id" <<< "$USER_ID_LIST") USER_NAME=$(jq -r ".[$i].username" <<< "$USER_ID_LIST") if [ "$USER_NAME" != "rhamt" ] ; then RES=$(curl -s -w '%{http_code}' -o /dev/null -k -X PUT https://secure-rhamt-web-console-labs-infra.$HOSTNAME_SUFFIX/auth/admin/realms/rhamt/users/$USER_ID/reset-password \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -H "Authorization: Bearer $RESULT_TOKEN" \ -d '{ "type": "password", "value": "'"$GOGS_PWD"'", "temporary": true}') if [ "$RES" = 204 ] ; then echo -e "user$i password is reset successfully...\n" else echo -e "Failure to reset user$i password with $RES\n" fi fi done fi oc delete project $TMP_PROJ # Install Che echo -e "Installing CodeReady Workspace...\n" cat <