#!/bin/bash
set -e

while getopts "r:s" opt; do
    case $opt in
        r)
            roles=$OPTARG # coma seperated roles
        ;;
        s)
            subscription=$OPTARG #optional subscription ID
        ;;
    esac
done

[[ $# -eq 0 || -z $roles ]] && { echo "Usage: $0 -r <comma seperated role(s)>"; exit 1; }

# install jq if not installed
if ! command -v jq &> /dev/null
then
    echo "jq command not found. attempting to download jq binary"
    pth=$(pwd)
    export PATH=$PATH:$pth
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -o jq
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        curl -L https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64 -o jq
    else
        echo "Unsupported OS type. Exiting..."
        exit 1
    fi
    chmod +x jq
fi

IFS=','; rolesArr=($roles); unset IFS;
role1="${rolesArr[0]}"
subscriptionId=$( az account show -o json | jq -r '.id' )

# Create service principal with first
spJson=$(az ad sp create-for-rbac --role="$role1" --scopes="/subscriptions/$subscriptionId")
aadClientId=$(echo $spJson | jq -r '.appId')
aadClientSecret=$(echo $spJson | jq -r '.password')

# Get object object id
objectId=$( az ad sp list --filter "appId eq '$aadClientId'" | jq '.[0].objectId' -r )

# assign roles
for role in "${rolesArr[@]}"
do
    echo "Creating role $role"
    az role assignment create --role "$role" --assignee-object-id $objectId
done

echo " Creation Complete "

echo " - ClientID => $aadClientId"
echo " - ClientSecret => $aadClientSecret"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
    STORAGE_TYPE_KEY="storageVendor"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
    STORAGE_TYPE_KEY="storageVendor"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
    STORAGE_TYPE_KEY="storageClass"
fi
STORAGE_TYPE_VALUE=$STORAGEOPTION

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# Spark (Analytics Engine) subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-analyticsengine-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  labels:
    app.kubernetes.io/instance: ibm-cpd-ae-operator-subscription
    app.kubernetes.io/managed-by: ibm-cpd-ae-operator
    app.kubernetes.io/name: ibm-cpd-ae-operator-subscription
  name: ibm-cpd-ae-operator-subscription
  namespace: $OPERATORNAMESPACE
spec:
    channel: $CHANNEL
    installPlanApproval: Automatic
    name: analyticsengine-operator
    source: ibm-operator-catalog
    sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-analyticsengine-cr.yaml <<EOF
apiVersion: ae.cpd.ibm.com/v1
kind: AnalyticsEngine
metadata:
  name: analyticsengine-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  $STORAGE_TYPE_KEY: $STORAGE_TYPE_VALUE
  license:
    accept: true
    license: Enterprise
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-analyticsengine-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-ae-operator pod status

podname="ibm-cpd-ae-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-spark cr

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-analyticsengine-cr.yaml"

# Check CR Status

SERVICE="AnalyticsEngine"
CRNAME="analyticsengine-cr"
SERVICE_STATUS="analyticsengineStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# CCS subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-ccs-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  annotations: {}
  name: ibm-cpd-ccs-operator
  namespace: $OPERATORNAMESPACE
spec:
  channel: $CHANNEL
  config:
    resources: {}
  installPlanApproval: Automatic
  name: ibm-cpd-ccs
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-ccs-cr.yaml <<EOF
apiVersion: ccs.cpd.ibm.com/v1beta1
kind: CCS
metadata:
  name: ccs-cr
  namespace: $CPDNAMESPACE
spec:
  size: \"small\"
  REPLACE_VENDOR_OR_CLASS: REPLACE_SC
  license:
    accept: true
    license: Enterprise
  docker_registry_prefix: \"cp.icr.io/cp/cpd\"
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-ccs-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-ccs-operator pod status

podname="ibm-cpd-ccs-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-ccs cr
if [[ $STORAGEOPTION == "nfs" ]];then
runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_VENDOR_OR_CLASS#storageClass#g $CPDTEMPLATES/ibm-ccs-cr.yaml"
runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_SC#nfs#g $CPDTEMPLATES/ibm-ccs-cr.yaml"
elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then
runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_VENDOR_OR_CLASS#storageVendor#g $CPDTEMPLATES/ibm-ccs-cr.yaml"
runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_SC#$STORAGEVENDOR_VALUE#g $CPDTEMPLATES/ibm-ccs-cr.yaml"
fi

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-ccs-cr.yaml"

# Check CR Status

SERVICE="CCS"
CRNAME="ccs-cr"
SERVICE_STATUS="ccsStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# CDE operator and CR creation

# Download cde case package.
runuser -l $SUDOUSER -c "wget https://raw.githubusercontent.com/IBM/cloud-pak/master/repo/case/ibm-cde-2.0.0.tgz -P $CPDTEMPLATES -A 'ibm-cde-2.0.0.tgz'"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-cde-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  labels:
    app.kubernetes.io/instance: ibm-cde-operator-subscription
    app.kubernetes.io/managed-by: ibm-cde-operator
    app.kubernetes.io/name: ibm-cde-operator-subscription
  name: ibm-cde-operator-subscription
  namespace: ${OPERATORNAMESPACE}
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-cde-operator
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "oc project $OPERATORNAMESPACE; oc create -f $CPDTEMPLATES/ibm-cde-sub.yaml"

runuser -l $SUDOUSER -c "echo 'Sleeping 2m for operator to install'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-cpd-ae-operator pod status

podname="ibm-cde-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-cde cr

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-cde-cr.yaml <<EOF
apiVersion: cde.cpd.ibm.com/v1
kind: CdeProxyService
metadata:
  name: cde-cr
  namespace: $CPDNAMESPACE
spec:
  version: $VERSION
  size: \"small\"
  namespace: \"$CPDNAMESPACE\"
  storageClass: \"$STORAGECLASS_VALUE\"
  cert_manager_enabled: true
  license:
    accept: true
    license: Enterprise
EOF"

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-cde-cr.yaml"

# Check CR Status

SERVICE="CdeProxyService"
CRNAME="cde-cr"
SERVICE_STATUS="cdeStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# db2aaservice operator and CR creation

#runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-db2aaservice-catalogsource.yaml <<EOF
#apiVersion: operators.coreos.com/v1alpha1
#kind: CatalogSource
#metadata:
#  name: ibm-db2aaservice-cp4d-operator-catalog
#  namespace: openshift-marketplace
#spec:
#  displayName: IBM Db2aaservice CP4D Catalog
#  image: icr.io/cpopen/ibm-db2aaservice-cp4d-operator-catalog@sha256:a0d9b6c314193795ec1918e4227ede916743381285b719b3d8cfb05c35fec071
#  imagePullPolicy: Always
#  publisher: IBM
#  sourceType: grpc
#  updateStrategy:
#    registryPoll:
#      interval: 45m
#EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-db2aaservice-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: ibm-db2aaservice-cp4d-operator
  namespace: $OPERATORNAMESPACE
spec:
  channel: $CHANNEL
  name: ibm-db2aaservice-cp4d-operator
  installPlanApproval: Automatic
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-db2aaservice-cr.yaml <<EOF
apiVersion: databases.cpd.ibm.com/v1
kind: Db2aaserviceService
metadata:
  name: db2aaservice-cr
  namespace: $CPDNAMESPACE
spec:
  storageClass: $STORAGECLASS_VALUE
  version: \"$VERSION\"
  license:
    accept: true
    license: \"Enterprise\"
EOF"

# Create Subscription.

#runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-db2aaservice-catalogsource.yaml"
#runuser -l $SUDOUSER -c "echo 'Sleeping 2m for catalogsource to be created'"
#runuser -l $SUDOUSER -c "sleep 2m"

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-db2aaservice-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping 2m for sub to be created'"
runuser -l $SUDOUSER -c "sleep 2m"

runuser -l $SUDOUSER -c "echo 'Sleeping 2m for operator to install'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-db2aaservice-cp4d-operator-controller-manager pod status

podname="ibm-db2aaservice-cp4d-operator-controller-manager"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-db2aaservice cr

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-db2aaservice-cr.yaml"

# Check CR Status

SERVICE="Db2aaserviceService"
CRNAME="db2aaservice-cr"
SERVICE_STATUS="db2aaserviceStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# DB2u Catalogsource

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-db2u-cs.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
  name: ibm-db2uoperator-catalog
  namespace: openshift-marketplace
spec:
  sourceType: grpc
  image: docker.io/ibmcom/ibm-db2uoperator-catalog:latest
  imagePullPolicy: Always
  displayName: IBM Db2U Catalog
  publisher: IBM
  updateStrategy:
    registryPoll:
      interval: 45m
EOF"

## Creating CS and sub

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-db2u-cs.yaml"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# DMC Subscription

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-dmc-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: ibm-dmc-operator-subscription
  namespace: $OPERATORNAMESPACE
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-dmc-operator
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "oc project $OPERATORNAMESPACE; oc create -f $CPDTEMPLATES/ibm-dmc-sub.yaml"

runuser -l $SUDOUSER -c "echo 'Sleeping 2m for operator to install'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-cpd-ae-operator pod status

podname="ibm-dmc-controller-manager"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-dmc cr

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-dmc-cr.yaml <<EOF
apiVersion: dmc.databases.ibm.com/v1
kind: Dmcaddon
metadata:
  name: dmcaddon-cr
  namespace: $CPDNAMESPACE
spec:
  namespace: $CPDNAMESPACE
  storageClass: $STORAGECLASS_VALUE
  pullPrefix: cp.icr.io/cp/cpd
  version: \"$VERSION\"
  license:
    accept: true
    license: Standard
EOF"

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-dmc-cr.yaml"

# Check CR Status

SERVICE="dmcaddon"
CRNAME="dmcaddon-cr"
SERVICE_STATUS="dmcAddonStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

runuser -l $SUDOUSER -c "oc login \"https://api.${CLUSTERNAME}.${DOMAINNAME}:6443\" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true"

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# DV operator and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-dv-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: ibm-dv-operator-catalog-subscription
  namespace: $OPERATORNAMESPACE        # Pick the project that contains the Cloud Pak for Data operator
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-dv-operator
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "oc project $OPERATORNAMESPACE; oc create -f $CPDTEMPLATES/ibm-dv-sub.yaml"

runuser -l $SUDOUSER -c "echo 'Sleeping 2m for DV Subscription to install'"
runuser -l $SUDOUSER -c "sleep 2m"

runuser -l $SUDOUSER -c "echo 'Sleeping 2m for operator to install'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-dv-operator pod status

podname="ibm-dv-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-DV cr

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-dv-cr.yaml <<EOF
apiVersion: db2u.databases.ibm.com/v1
kind: DvService
metadata:
  name: dv-service-cr
  namespace: $CPDNAMESPACE
spec:
  license:
    accept: true
    license: Enterprise
  version: $VERSION
  size: "small"
  docker_registry_prefix: cp.icr.io/cp/cpd
EOF"

runuser -l $SUDOUSER -c "oc project $OPERATORNAMESPACE; oc create -f $CPDTEMPLATES/ibm-dv-cr.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping 2m for DV CR to be created'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check CR Status

SERVICE="DvService"
CRNAME="dv-service-cr"
SERVICE_STATUS="reconcileStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

runuser -l $SUDOUSER -c "mkdir -p $INSTALLERHOME"
runuser -l $SUDOUSER -c "mkdir -p $OCPTEMPLATES"
runuser -l $SUDOUSER -c "mkdir -p $CPDTEMPLATES"
runuser -l $SUDOUSER -c "cat > $OCPTEMPLATES/kubecredentials <<EOF
username: $OPENSHIFTUSER
password: $OPENSHIFTPASSWORD
EOF"

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

runuser -l $SUDOUSER -c "oc login \"https://api.${CLUSTERNAME}.${DOMAINNAME}:6443\" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true"

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

#CPD Config

#runuser -l $SUDOUSER -c "wget https://github.com/IBM/cloud-pak-cli/releases/download/v3.8.0/cloudctl-linux-amd64.tar.gz -O $CPDTEMPLATES/cloudctl-linux-amd64.tar.gz"
#runuser -l $SUDOUSER -c "https://github.com/IBM/cloud-pak-cli/releases/download/v3.8.0/cloudctl-linux-amd64.tar.gz.sig -O $CPDTEMPLATES/cloudctl-linux-amd64.tar.gz.sig"
#runuser -l $SUDOUSER -c "cd $CPDTEMPLATES && sudo tar -xvf cloudctl-linux-amd64.tar.gz -C /usr/local/bin"
#runuser -l $SUDOUSER -c "chmod +x /usr/local/bin/cloudctl-linux-amd64"
#runuser -l $SUDOUSER -c "sudo mv /usr/local/bin/cloudctl-linux-amd64 /usr/local/bin/cloudctl"

# Service Account Token for CPD installation
runuser -l $SUDOUSER -c "oc new-project $CPDNAMESPACE"
runuser -l $SUDOUSER -c "oc create serviceaccount cpdtoken"
runuser -l $SUDOUSER -c "oc policy add-role-to-user admin system:serviceaccount:$CPDNAMESPACE:cpdtoken"

# Service Account Token for CPD installation
runuser -l $SUDOUSER -c "oc new-project $OPERATORNAMESPACE"
runuser -l $SUDOUSER -c "oc create serviceaccount cpdtoken"
runuser -l $SUDOUSER -c "oc policy add-role-to-user admin system:serviceaccount:$OPERATORNAMESPACE:cpdtoken"

## Installing jq
runuser -l $SUDOUSER -c "wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O  $CPDTEMPLATES/jq"
runuser -l $SUDOUSER -c "sudo mv $CPDTEMPLATES/jq /usr/local/bin"
runuser -l $SUDOUSER -c "sudo chmod +x /usr/local/bin/jq"

# Update global pull secret and sysctl changes:

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/sysctl-worker.yaml <<EOF
apiVersion: machineconfiguration.openshift.io/v1
kind: KubeletConfig
metadata:
  name: db2u-kubelet
spec:
  machineConfigPoolSelector:
    matchLabels:
      db2u-kubelet: sysctl
  kubeletConfig:
    systemReserved:
      cpu: 1000m
      memory: 1Gi
    allowedUnsafeSysctls:
      - \"kernel.msg*\"
      - \"kernel.shm*\"
      - \"kernel.sem\"
EOF"

export ENTITLEMENT_USER=cp
export ENTITLEMENT_KEY=$APIKEY
pull_secret=$(echo -n "$ENTITLEMENT_USER:$ENTITLEMENT_KEY" | base64 -w0)
oc get secret/pull-secret -n openshift-config -o jsonpath='{.data.\.dockerconfigjson}' | base64 -d > $OCPTEMPLATES/dockerconfig.json
sed -i -e 's|:{|:{"cp.icr.io":{"auth":"'$pull_secret'"\},|' $OCPTEMPLATES/dockerconfig.json
oc set data secret/pull-secret -n openshift-config --from-file=.dockerconfigjson=$OCPTEMPLATES/dockerconfig.json

runuser -l $SUDOUSER -c "oc label machineconfigpool.machineconfiguration.openshift.io worker db2u-kubelet=sysctl"
runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/sysctl-worker.yaml"
runuser -l $SUDOUSER -c "sleep 3m"

# Check nodestatus if they are ready.

while true; do
    node_status=$(oc get nodes | grep -E "SchedulingDisabled|NotReady")
    if [[ -z $node_status ]]; then
        echo -e "\n******All nodes are running now.******"
        break
    fi
        echo -e "\n******Waiting for nodes to get ready.******"
        oc get nodes --no-headers | awk '{print $1 " " $2}'
        echo -e "\n******sleeping for 60Secs******"
        sleep 60
    done

# CPD Bedrock and Platform operator install:

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-operator-catalogsource.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
  name: ibm-operator-catalog
  namespace: openshift-marketplace
spec:
  displayName: \"IBM Operator Catalog\"
  publisher: IBM
  sourceType: grpc
  image: icr.io/cpopen/ibm-operator-catalog:latest
  updateStrategy:
    registryPoll:
      interval: 45m
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/db2u-catalogsource.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
  name: ibm-db2uoperator-catalog
  namespace: openshift-marketplace
spec:
  sourceType: grpc
  image: docker.io/ibmcom/ibm-db2uoperator-catalog:latest
  imagePullPolicy: Always
  displayName: IBM Db2U Catalog
  publisher: IBM
  updateStrategy:
    registryPoll:
      interval: 45m
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-operator-og.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha2
kind: OperatorGroup
metadata:
  name: operatorgroup
  namespace: ibm-common-services
spec:
  targetNamespaces:
  - ibm-common-services
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/cpd-platform-operator-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: cpd-operator
  namespace: ibm-common-services    # The project that contains the Cloud Pak for Data operator
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: cpd-platform-operator
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/cpd-platform-operator-operandrequest.yaml <<EOF
apiVersion: operator.ibm.com/v1alpha1
kind: OperandRequest
metadata:
  name: empty-request
  namespace: $CPDNAMESPACE        # Replace with the project where you will install Cloud Pak for Data
spec:
  requests: []
EOF"

# Run catalog source

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-operator-catalogsource.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 1m' "
runuser -l $SUDOUSER -c "sleep 1m"

# Create DB2U Operator catalog source

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/db2u-catalogsource.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 2m' "
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-operator-catalog pod status

podname="ibm-operator-catalog"
name_space="openshift-marketplace"
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

# Create IBM Operator Group

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-operator-og.yaml"

# Creating CPD Platform operator subscription:

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/cpd-platform-operator-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for ' "
runuser -l $SUDOUSER -c "sleep 2m"

# Check cpd-platform-operator-manager pod status

podname="cpd-platform-operator-manager"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

# Check ibm-namespace-scope-operator pod status

podname="ibm-namespace-scope-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

# Check ibm-common-service-operator pod status

podname="ibm-common-service-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

# Check operand-deployment-lifecycle-manager pod status

podname="operand-deployment-lifecycle-manager"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/cpd-platform-operator-operandrequest.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 2m' "
runuser -l $SUDOUSER -c "sleep 2m"

# Install Lite-cr
runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibmcpd-cr.yaml <<EOF
apiVersion: cpd.ibm.com/v1
kind: Ibmcpd
metadata:
  name: ibmcpd-cr                                         # This is the recommended name, but you can change it
  namespace: REPLACE_NAMESPACE                            # Replace with the project where you will install Cloud Pak for Data
spec:
  license:
    accept: true
    license: Enterprise                                   # Specify the Cloud Pak for Data license you purchased
  storageClass: \"REPLACE_STORAGECLASS\"                    # Replace with the name of a RWX storage class
  zenCoreMetadbStorageClass: \"REPLACE_STORAGECLASS\"       # (Recommended) Replace with the name of a RWO storage class
  version: \"$VERSION\"
EOF"

runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_STORAGECLASS#$STORAGECLASS_VALUE#g $CPDTEMPLATES/ibmcpd-cr.yaml"
runuser -l $SUDOUSER -c "sed -i -e s#REPLACE_NAMESPACE#$CPDNAMESPACE#g $CPDTEMPLATES/ibmcpd-cr.yaml"
runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibmcpd-cr.yaml"

# Check CR Status

SERVICE="ibmcpd"
CRNAME="ibmcpd-cr"
SERVICE_STATUS="controlPlaneStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

# Enable CSV injector patch

oc patch namespacescope common-service --type='json' -p='[{"op":"replace", "path": "/spec/csvInjector/enable", "value":true}]' -n $OPERATORNAMESPACE

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

## Creating IIS SC yaml

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wkc-iis-scc.yaml <<EOF
allowHostDirVolumePlugin: false
allowHostIPC: false
allowHostNetwork: false
allowHostPID: false
allowHostPorts: false
allowPrivilegeEscalation: true
allowPrivilegedContainer: false
allowedCapabilities: null
apiVersion: security.openshift.io/v1
defaultAddCapabilities: null
fsGroup:
  type: RunAsAny
kind: SecurityContextConstraints
metadata:
  annotations:
    kubernetes.io/description: WKC/IIS provides all features of the restricted SCC
      but runs as user 10032.
  name: wkc-iis-scc
readOnlyRootFilesystem: false
requiredDropCapabilities:
- KILL
- MKNOD
- SETUID
- SETGID
runAsUser:
  type: MustRunAs
  uid: 10032
seLinuxContext:
  type: MustRunAs
supplementalGroups:
  type: RunAsAny
volumes:
- configMap
- downwardAPI
- emptyDir
- persistentVolumeClaim
- projected
- secret
users:
- system:serviceaccount:$CPDNAMESPACE:wkc-iis-sa
EOF"

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wkc-iis-scc.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 1m' "
runuser -l $SUDOUSER -c "sleep 1m"

# WKC subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wkc-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  labels:
    app.kubernetes.io/instance:  ibm-cpd-wkc-operator-catalog-subscription
    app.kubernetes.io/managed-by: ibm-cpd-wkc-operator
    app.kubernetes.io/name:  ibm-cpd-wkc-operator-catalog-subscription
  name: ibm-cpd-wkc-operator
  namespace: ibm-common-services
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-cpd-wkc
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wkc-ocs-pwx-cr.yaml <<EOF
apiVersion: wkc.cpd.ibm.com/v1beta1
kind: WKC
metadata:
  name: wkc-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  storageVendor: \"$STORAGEVENDOR_VALUE\"
  license:
    accept: true
    license: Enterprise
  docker_registry_prefix: cp.icr.io/cp/cpd
  useODLM: true
  install_wkc_core_only: true
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wkc-nfs-cr.yaml <<EOF
apiVersion: wkc.cpd.ibm.com/v1beta1
kind: WKC
metadata:
  name: wkc-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  storageClass: \"$STORAGECLASS_VALUE\"
  license:
    accept: true
    license: Enterprise
  docker_registry_prefix: cp.icr.io/cp/cpd
  useODLM: true
  install_wkc_core_only: true
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wkc-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-wkc-operator pod status

podname="ibm-cpd-wkc-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-wkc cr

if [[ $STORAGEOPTION == "nfs" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wkc-nfs-cr.yaml"

elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wkc-ocs-pwx-cr.yaml"
fi

# Check CR Status

SERVICE="WKC"
CRNAME="wkc-cr"
SERVICE_STATUS="wkcStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# IIS CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-iis-ocs-pwx-cr.yaml <<EOF
apiVersion: iis.cpd.ibm.com/v1alpha1
kind: IIS
metadata:
  name: iis-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  storageVendor: \"$STORAGEVENDOR_VALUE\"
  license:
    accept: true
    license: Enterprise
  docker_registry_prefix: cp.icr.io/cp/cpd
  use_dynamic_provisioning: true
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-iis-nfs-cr.yaml <<EOF
apiVersion: iis.cpd.ibm.com/v1alpha1
kind: IIS
metadata:
  name: iis-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  storageClass: \"$STORAGECLASS_VALUE\"
  license:
    accept: true
    license: Enterprise
  docker_registry_prefix: cp.icr.io/cp/cpd
  use_dynamic_provisioning: true
EOF"

## Creating IIS SC yaml

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wkc-iis-scc.yaml <<EOF
allowHostDirVolumePlugin: false
allowHostIPC: false
allowHostNetwork: false
allowHostPID: false
allowHostPorts: false
allowPrivilegeEscalation: true
allowPrivilegedContainer: false
allowedCapabilities: null
apiVersion: security.openshift.io/v1
defaultAddCapabilities: null
fsGroup:
  type: RunAsAny
kind: SecurityContextConstraints
metadata:
  annotations:
    kubernetes.io/description: WKC/IIS provides all features of the restricted SCC
      but runs as user 10032.
  name: wkc-iis-scc
readOnlyRootFilesystem: false
requiredDropCapabilities:
- KILL
- MKNOD
- SETUID
- SETGID
runAsUser:
  type: MustRunAs
  uid: 10032
seLinuxContext:
  type: MustRunAs
supplementalGroups:
  type: RunAsAny
volumes:
- configMap
- downwardAPI
- emptyDir
- persistentVolumeClaim
- projected
- secret
users:
- system:serviceaccount:$CPDNAMESPACE:wkc-iis-sa
EOF"

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wkc-iis-scc.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 1m' "
runuser -l $SUDOUSER -c "sleep 1m"

# IIS Subscription
runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-iis-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  labels:
    app.kubernetes.io/instance: ibm-cpd-iis-operator-catalog-subscription
    app.kubernetes.io/managed-by: ibm-cpd-iis-operator
    app.kubernetes.io/name: ibm-cpd-iis-operator-catalog-subscription
  generation: 1
  name: ibm-cpd-iis-operator-catalog-subscription
  namespace: $OPERATORNAMESPACE
spec:
    channel: $CHANNEL
    installPlanApproval: Automatic
    name: ibm-cpd-iis
    source: ibm-operator-catalog
    sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-iis-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping 2m for operator to install'"
runuser -l $SUDOUSER -c "sleep 2m"

# Check ibm-cpd-iis-operator pod status

podname="ibm-cpd-iis-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-iis cr

if [[ $STORAGEOPTION == "nfs" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-iis-nfs-cr.yaml"

elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-iis-ocs-pwx-cr.yaml"
fi

# Check CR Status

SERVICE="IIS"
CRNAME="iis-cr"
SERVICE_STATUS="iisStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# UG CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-ug-ocs-pwx-cr.yaml <<EOF
apiVersion: wkc.cpd.ibm.com/v1beta1
kind: UG
metadata:
  name: ug-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  size: \"small\"
  storageVendor: \"$STORAGEVENDOR_VALUE\"
  license:
    accept: true
    license: \"Enterprise\"
  docker_registry_prefix: cp.icr.io/cp/cpd
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-ug-nfs-cr.yaml <<EOF
apiVersion: wkc.cpd.ibm.com/v1beta1
kind: UG
metadata:
  name: ug-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  size: \"small\"
  storageClass: \"$STORAGECLASS_VALUE\"
  license:
    accept: true
    license: \"Enterprise\"
  docker_registry_prefix: cp.icr.io/cp/cpd
EOF"

## Creating ibm-ug cr

if [[ $STORAGEOPTION == "nfs" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-ug-nfs-cr.yaml"

elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-ug-ocs-pwx-cr.yaml"
fi

# Check CR Status

SERVICE="UG"
CRNAME="ug-cr"
SERVICE_STATUS="ugStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# WML subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wml-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  labels:
    app.kubernetes.io/instance: ibm-cpd-wml-operator-subscription
    app.kubernetes.io/managed-by: ibm-cpd-wml-operator
    app.kubernetes.io/name: ibm-cpd-wml-operator-subscription
  name: ibm-cpd-wml-operator-subscription
  namespace: $OPERATORNAMESPACE
spec:
    channel: $CHANNEL
    installPlanApproval: Automatic
    name: ibm-cpd-wml-operator
    source: ibm-operator-catalog
    sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wml-ocs-pwx-cr.yaml <<EOF
apiVersion: wml.cpd.ibm.com/v1beta1
kind: WmlBase
metadata:
  name: wml-cr
  namespace: $CPDNAMESPACE
  labels:
    app.kubernetes.io/instance: wml-cr
    app.kubernetes.io/managed-by: ibm-cpd-wml-operator
    app.kubernetes.io/name: ibm-cpd-wml-operator
spec:
  scaleConfig: small
  is_35_upgrade: false
  ignoreForMaintenance: false
  docker_registry_prefix: \"cp.icr.io/cp/cpd\"
  storageClass: $STORAGECLASS_VALUE
  storageVendor: $STORAGEVENDOR_VALUE
  version: \"$VERSION\"
  license:
    accept: true
    license: \"Enterprise\"
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wml-nfs-cr.yaml <<EOF
apiVersion: wml.cpd.ibm.com/v1beta1
kind: WmlBase
metadata:
  name: wml-cr
  namespace: $CPDNAMESPACE
  labels:
    app.kubernetes.io/instance: wml-cr
    app.kubernetes.io/managed-by: ibm-cpd-wml-operator
    app.kubernetes.io/name: ibm-cpd-wml-operator
spec:
  scaleConfig: small
  is_35_upgrade: false
  ignoreForMaintenance: false
  docker_registry_prefix: \"cp.icr.io/cp/cpd\"
  storageClass: $STORAGECLASS_VALUE
  version: \"$VERSION\"
  license:
    accept: true
    license: \"Enterprise\"
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wml-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-wml-operator pod status

podname="ibm-cpd-wml-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-wml cr

if [[ $STORAGEOPTION == "nfs" ]];then
    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wml-nfs-cr.yaml"
elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then
    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wml-ocs-pwx-cr.yaml"
fi

# Check CR Status

SERVICE="WmlBase"
CRNAME="wml-cr"
SERVICE_STATUS="wmlStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# WOS subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wos-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: ibm-watson-openscale-operator-subscription
  labels:
    app.kubernetes.io/instance: ibm-watson-openscale-operator-subscription
    app.kubernetes.io/managed-by: ibm-watson-openscale-operator
    app.kubernetes.io/name: ibm-watson-openscale-operator-subscription
  namespace: $OPERATORNAMESPACE
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-cpd-wos
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wos-cr.yaml <<EOF
apiVersion: wos.cpd.ibm.com/v1
kind: WOService
metadata:
  name: aiopenscale-cr
  namespace: $CPDNAMESPACE
spec:
  scaleConfig: small
  storageClass: \"$STORAGECLASS_VALUE\"
  version: $VERSION
  type: service
  license:
    accept: true
    license: Enterprise
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wos-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-wos-operator pod status

podname="ibm-cpd-wos-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-wos cr

runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wos-cr.yaml"

# Check CR Status

SERVICE="WOService"
CRNAME="aiopenscale-cr"
SERVICE_STATUS="wosStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/sh

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export CPDNAMESPACE=$3
export STORAGEOPTION=$4
export ASSEMBLY=$5
export CLUSTERNAME=$6
export DOMAINNAME=$7
export OPENSHIFTUSER=$8
export APIKEY=$9
export CHANNEL=${10}
export VERSION=${11}

export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OPERATORNAMESPACE=ibm-common-services
export INSTALLERHOME=/home/$SUDOUSER/.ibm
export OCPTEMPLATES=/home/$SUDOUSER/.openshift/templates
export CPDTEMPLATES=/home/$SUDOUSER/.cpd/templates

# Set parameters
if [[ $STORAGEOPTION == "portworx" ]]; then
    STORAGECLASS_VALUE="portworx-shared-gp3"
    STORAGEVENDOR_VALUE="portworx"
elif [[ $STORAGEOPTION == "ocs" ]]; then
    STORAGECLASS_VALUE="ocs-storagecluster-cephfs"
    STORAGEVENDOR_VALUE="ocs"
elif [[ $STORAGEOPTION == "nfs" ]]; then
    STORAGECLASS_VALUE="nfs"
    STORAGEVENDOR_VALUE=""
fi

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# WSL subscription and CR creation

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wsl-sub.yaml <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  annotations: {}
  name: ibm-cpd-ws-operator-catalog-subscription
  namespace: $OPERATORNAMESPACE
spec:
  channel: $CHANNEL
  installPlanApproval: Automatic
  name: ibm-cpd-wsl
  source: ibm-operator-catalog
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wsl-ocs-pwx-cr.yaml <<EOF
apiVersion: ws.cpd.ibm.com/v1beta1
kind: WS
metadata:
  name: ws-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  size: \"small\"
  storageClass: \"$STORAGECLASS_VALUE\"
  storageVendor: \"$STORAGEVENDOR_VALUE\"
  license:
    accept: true
    license: Enterprise
EOF"

runuser -l $SUDOUSER -c "cat > $CPDTEMPLATES/ibm-wsl-nfs-cr.yaml <<EOF
apiVersion: ws.cpd.ibm.com/v1beta1
kind: WS
metadata:
  name: ws-cr
  namespace: $CPDNAMESPACE
spec:
  version: \"$VERSION\"
  size: \"small\"
  storageClass: \"$STORAGECLASS_VALUE\"
  license:
    accept: true
    license: Enterprise
EOF"

## Creating Subscription

runuser -l $SUDOUSER -c "oc create -f $CPDTEMPLATES/ibm-wsl-sub.yaml"
runuser -l $SUDOUSER -c "echo 'Sleeping for 5m' "
runuser -l $SUDOUSER -c "sleep 5m"

# Check ibm-cpd-ws-operator pod status

podname="ibm-cpd-ws-operator"
name_space=$OPERATORNAMESPACE
status="unknown"
while [ "$status" != "Running" ]
do
  pod_name=$(oc get pods -n $name_space | grep $podname | awk '{print $1}' )
  ready_status=$(oc get pods -n $name_space $pod_name  --no-headers | awk '{print $2}')
  pod_status=$(oc get pods -n $name_space $pod_name --no-headers | awk '{print $3}')
  echo $pod_name State - $ready_status, podstatus - $pod_status
  if [ "$ready_status" == "1/1" ] && [ "$pod_status" == "Running" ]
  then
  status="Running"
  else
  status="starting"
  sleep 10
  fi
  echo "$pod_name is $status"
done

## Creating ibm-wsl cr

if [[ $STORAGEOPTION == "nfs" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wsl-nfs-cr.yaml"

elif [[ $STORAGEOPTION == "ocs" || $STORAGEOPTION == "portworx" ]];then

    runuser -l $SUDOUSER -c "oc project $CPDNAMESPACE; oc create -f $CPDTEMPLATES/ibm-wsl-ocs-pwx-cr.yaml"
fi

# Check CR Status

SERVICE="WS"
CRNAME="ws-cr"
SERVICE_STATUS="wsStatus"

STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)

while  [[ ! $STATUS =~ ^(Completed|Complete)$ ]]; do
    echo "$CRNAME is Installing!!!!"
    sleep 60
    STATUS=$(oc get $SERVICE $CRNAME -n $CPDNAMESPACE -o json | jq .status.$SERVICE_STATUS | xargs)
    if [ "$STATUS" == "Failed" ]
    then
        echo "**********************************"
        echo "$CRNAME Installation Failed!!!!"
        echo "**********************************"
        exit
    fi
done
echo "*************************************"
echo "$CRNAME Installation Finished!!!!"
echo "*************************************"

echo "$(date) - ############### Script Complete #############"


#!/bin/bash

echo $(date) " - ############## Starting Script ####################"

set -e

export SUDOUSER=$1
export OPENSHIFTPASSWORD=$2
export SSHKEY=$3
export WORKERCOUNT=$4
export MASTERCOUNT=$5
export SUBSCRIPTIONID=$6
export TENANTID=$7
export AADCLIENTID=$8
export AADSECRET=$9
export RESOURCEGROUPNAME=${10}
export LOCATION=${11}
export VIRTUALNETWORKNAME=${12}
export PXSPECURL=${13}
export STORAGEOPTION=${14}
export NFSIPADDRESS=${15}
export SINGLEORMULTI=${16}
export ARTIFACTSLOCATION=${17::-1}
export ARTIFACTSTOKEN=\"${18}\"
export BASEDOMAIN=${19}
export MASTERINSTANCETYPE=${20}
export WORKERINSTANCETYPE=${21}
export CLUSTERNAME=${22}
export CLUSTERNETWORKCIDR=${23}
export HOSTADDRESSPREFIX=${24}
export VIRTUALNETWORKCIDR=${25}
export SERVICENETWORKCIDR=${26}
export BASEDOMAINRG=${27}
export NETWORKRG=${28}
export MASTERSUBNETNAME=${29}
export WORKERSUBNETNAME=${30}
export PULLSECRET=${31}
export FIPS=${32}
export PUBLISH=${33}
export OPENSHIFTUSER=${34}
export ENABLEAUTOSCALER=${35}
export OUTBOUNDTYPE=${36}
export EXISTING_RESOURCE_GROUP_NAME=${37}

#Var
export INSTALLERHOME=/home/$SUDOUSER/.openshift

echo $(date) " - Disable and enable repo starting"
sudo yum update -y --disablerepo=* --enablerepo="*microsoft*"
echo $(date) " - Disable and enable repo completed"

# Grow Root File System
yum -y install cloud-utils-growpart.noarch
echo $(date) " - Grow Root FS"

rootdev=`findmnt --target / -o SOURCE -n`
rootdrivename=`lsblk -no pkname $rootdev`
rootdrive="/dev/"$rootdrivename
name=`lsblk  $rootdev -o NAME | tail -1`
part_number=${name#*${rootdrivename}}

growpart $rootdrive $part_number -u on
xfs_growfs $rootdev

if [ $? -eq 0 ]
then
    echo $(date) " - Root File System successfully extended"
else
    echo $(date) " - Root File System failed to be grown"
	exit 20
fi

echo $(date) " - Install Podman"
yum install -y podman
echo $(date) " - Install Podman Complete"

echo $(date) " - Install httpd-tools"
yum install -y httpd-tools
echo $(date) " - Install httpd-tools Complete"

echo $(date) " - Download Binaries"
runuser -l $SUDOUSER -c "mkdir -p /home/$SUDOUSER/.openshift"

runuser -l $SUDOUSER -c "wget https://mirror.openshift.com/pub/openshift-v4/clients/ocp/4.8.11/openshift-install-linux-4.8.11.tar.gz"
runuser -l $SUDOUSER -c "wget https://mirror.openshift.com/pub/openshift-v4/clients/ocp/4.8.11/openshift-client-linux-4.8.11.tar.gz"
runuser -l $SUDOUSER -c "tar -xvf openshift-install-linux-4.8.11.tar.gz -C $INSTALLERHOME"
runuser -l $SUDOUSER -c "sudo tar -xvf openshift-client-linux-4.8.11.tar.gz -C /usr/bin"

chmod +x /usr/bin/kubectl
chmod +x /usr/bin/oc
chmod +x $INSTALLERHOME/openshift-install
echo $(date) " - Download Binaries Done."

echo $(date) " - Setup Azure Credentials for OCP"
runuser -l $SUDOUSER -c "mkdir -p /home/$SUDOUSER/.azure"
runuser -l $SUDOUSER -c "touch /home/$SUDOUSER/.azure/osServicePrincipal.json"
cat > /home/$SUDOUSER/.azure/osServicePrincipal.json <<EOF
{"subscriptionId":"$SUBSCRIPTIONID","clientId":"$AADCLIENTID","clientSecret":"$AADSECRET","tenantId":"$TENANTID"}
EOF
echo $(date) " - Setup Azure Credentials for OCP - Complete"

echo $(date) " - Setup Install config"
runuser -l $SUDOUSER -c "mkdir -p $INSTALLERHOME/openshiftfourx"
runuser -l $SUDOUSER -c "touch $INSTALLERHOME/openshiftfourx/install-config.yaml"
zones=""
if [[ $SINGLEORMULTI == "az" ]]; then
zones="zones:
      - '1'
      - '2'
      - '3'"
fi
cat > $INSTALLERHOME/openshiftfourx/install-config.yaml <<EOF
apiVersion: v1
baseDomain: $BASEDOMAIN
compute:
- hyperthreading: Enabled
  name: worker
  platform:
    azure:
      type: $WORKERINSTANCETYPE
      osDisk:
        diskSizeGB: 1024
      $zones
  replicas: $WORKERCOUNT
controlPlane:
  hyperthreading: Enabled
  name: master
  platform:
    azure:
      type: $MASTERINSTANCETYPE
      osDisk:
        diskSizeGB: 1024
      $zones
  replicas: $MASTERCOUNT
metadata:
  creationTimestamp: null
  name: $CLUSTERNAME
networking:
  clusterNetwork:
  - cidr: $CLUSTERNETWORKCIDR
    hostPrefix: $HOSTADDRESSPREFIX
  machineCIDR: $VIRTUALNETWORKCIDR
  networkType: OpenShiftSDN
  serviceNetwork:
  - $SERVICENETWORKCIDR
platform:
  azure:
    baseDomainResourceGroupName: $BASEDOMAINRG
    region: $LOCATION
    networkResourceGroupName: $NETWORKRG
    virtualNetwork: $VIRTUALNETWORKNAME
    controlPlaneSubnet: $MASTERSUBNETNAME
    computeSubnet: $WORKERSUBNETNAME
    outboundType: $OUTBOUNDTYPE
    resourceGroupName: $EXISTING_RESOURCE_GROUP_NAME
pullSecret: '$PULLSECRET'
fips: $FIPS
publish: $PUBLISH
sshKey: |
  $SSHKEY
EOF
echo $(date) " - Setup Install config - Complete"

runuser -l $SUDOUSER -c "cp $INSTALLERHOME/openshiftfourx/install-config.yaml $INSTALLERHOME/openshiftfourx/install-config-backup.yaml"

echo $(date) " - Install OCP"
runuser -l $SUDOUSER -c "export ARM_SKIP_PROVIDER_REGISTRATION=true"
runuser -l $SUDOUSER -c "$INSTALLERHOME/openshift-install create cluster --dir=$INSTALLERHOME/openshiftfourx --log-level=debug"
runuser -l $SUDOUSER -c "sleep 120"
echo $(date) " - OCP Install Complete"

echo $(date) " - Kube Config setup"
runuser -l $SUDOUSER -c "mkdir -p /home/$SUDOUSER/.kube"
runuser -l $SUDOUSER -c "cp $INSTALLERHOME/openshiftfourx/auth/kubeconfig /home/$SUDOUSER/.kube/config"
echo $(date) "Kube Config setup done"

#Switch to Machine API project
runuser -l $SUDOUSER -c "oc project openshift-machine-api"

echo $(date) " - Setting up Cluster Autoscaler"
runuser -l $SUDOUSER -c "cat > $INSTALLERHOME/openshiftfourx/cluster-autoscaler.yaml <<EOF
apiVersion: 'autoscaling.openshift.io/v1'
kind: 'ClusterAutoscaler'
metadata:
  name: 'default'
spec:
  podPriorityThreshold: -10
  resourceLimits:
    maxNodesTotal: 24
    cores:
      min: 48
      max: 128
    memory:
      min: 128
      max: 512
  scaleDown:
    enabled: true
    delayAfterAdd: '3m'
    delayAfterDelete: '2m'
    delayAfterFailure: '30s'
    unneededTime: '60s'
EOF"

echo $(date) " - Cluster Autoscaler setup complete"

echo $(date) " - Setting up Machine Autoscaler"
clusterid=$(oc get machineset -n openshift-machine-api -o jsonpath='{.items[0].metadata.labels.machine\.openshift\.io/cluster-api-cluster}' --kubeconfig /home/$SUDOUSER/.kube/config)
runuser -l $SUDOUSER -c "cat > $INSTALLERHOME/openshiftfourx/machine-autoscaler.yaml <<EOF
---
kind: MachineAutoscaler
apiVersion: autoscaling.openshift.io/v1beta1
metadata:
  name: ${clusterid}-worker-${LOCATION}1
  namespace: 'openshift-machine-api'
spec:
  minReplicas: 1
  maxReplicas: 12
  scaleTargetRef:
    apiVersion: machine.openshift.io/v1beta1
    kind: MachineSet
    name: ${clusterid}-worker-${LOCATION}1
---
kind: MachineAutoscaler
apiVersion: autoscaling.openshift.io/v1beta1
metadata:
  name: ${clusterid}-worker-${LOCATION}2
  namespace: openshift-machine-api
spec:
  minReplicas: 1
  maxReplicas: 12
  scaleTargetRef:
    apiVersion: machine.openshift.io/v1beta1
    kind: MachineSet
    name: ${clusterid}-worker-${LOCATION}2
---
kind: MachineAutoscaler
apiVersion: autoscaling.openshift.io/v1beta1
metadata:
  name: ${clusterid}-worker-${LOCATION}3
  namespace: openshift-machine-api
spec:
  minReplicas: 1
  maxReplicas: 12
  scaleTargetRef:
    apiVersion: machine.openshift.io/v1beta1
    kind: MachineSet
    name: ${clusterid}-worker-${LOCATION}3
EOF"

echo $(date) " - Machine Autoscaler setup complete"

echo $(date) " - Setting up Machine health checks"
clusterid=$(oc get machineset -n openshift-machine-api -o jsonpath='{.items[0].metadata.labels.machine\.openshift\.io/cluster-api-cluster}' --kubeconfig /home/$SUDOUSER/.kube/config)
runuser -l $SUDOUSER -c "cat > $INSTALLERHOME/openshiftfourx/machine-health-check.yaml <<EOF
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineHealthCheck
metadata:
  name: health-check-worker-${LOCATION}1
  namespace: openshift-machine-api
spec:
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-machine-role: worker
      machine.openshift.io/cluster-api-machine-type: worker
      machine.openshift.io/cluster-api-machineset: ${clusterid}-worker-${LOCATION}1
  unhealthyConditions:
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"False\"
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"Unknown\"
  maxUnhealthy: \"30%\"
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineHealthCheck
metadata:
  name: health-check-worker-${LOCATION}2
  namespace: openshift-machine-api
spec:
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-machine-role: worker
      machine.openshift.io/cluster-api-machine-type: worker
      machine.openshift.io/cluster-api-machineset: ${clusterid}-worker-${LOCATION}2
  unhealthyConditions:
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"False\"
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"Unknown\"
  maxUnhealthy: \"30%\"
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineHealthCheck
metadata:
  name: health-check-worker-${LOCATION}3
  namespace: openshift-machine-api
spec:
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-machine-role: worker
      machine.openshift.io/cluster-api-machine-type: worker
      machine.openshift.io/cluster-api-machineset: ${clusterid}-worker-${LOCATION}3
  unhealthyConditions:
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"False\"
  - type:    \"Ready\"
    timeout: \"300s\"
    status: \"Unknown\"
  maxUnhealthy: \"30%\"
EOF"

##Enable/Disable Autoscaler
if [[ $ENABLEAUTOSCALER == "true" || $ENABLEAUTOSCALER == "True" ]]; then
  runuser -l $SUDOUSER -c "oc create -f $INSTALLERHOME/openshiftfourx/cluster-autoscaler.yaml"
  runuser -l $SUDOUSER -c "oc create -f $INSTALLERHOME/openshiftfourx/machine-autoscaler.yaml"
  runuser -l $SUDOUSER -c "oc create -f $INSTALLERHOME/openshiftfourx/machine-health-check.yaml"
fi

echo $(date) " - Machine Health Check setup complete"

echo $(date) " - Setting up $STORAGEOPTION"
if [[ $STORAGEOPTION == "portworx" ]]; then
  runuser -l $SUDOUSER -c "wget $ARTIFACTSLOCATION/scripts/px-install.yaml$ARTIFACTSTOKEN -O $INSTALLERHOME/openshiftfourx/px-install.yaml"
  runuser -l $SUDOUSER -c "wget $ARTIFACTSLOCATION/scripts/px-storageclasses.yaml$ARTIFACTSTOKEN -O $INSTALLERHOME/openshiftfourx/px-storageclasses.yaml"
  runuser -l $SUDOUSER -c "oc create -f $INSTALLERHOME/openshiftfourx/px-install.yaml"
  runuser -l $SUDOUSER -c "sleep 30"
  runuser -l $SUDOUSER -c "oc apply -f '$PXSPECURL'"
  runuser -l $SUDOUSER -c "oc create -f $INSTALLERHOME/openshiftfourx/px-storageclasses.yaml"
fi

if [[ $STORAGEOPTION == "nfs" ]]; then
  runuser -l $SUDOUSER -c "oc adm policy add-scc-to-user hostmount-anyuid system:serviceaccount:kube-system:nfs-client-provisioner"
  runuser -l $SUDOUSER -c "wget $ARTIFACTSLOCATION/scripts/nfs-template.yaml$ARTIFACTSTOKEN -O  $INSTALLERHOME/openshiftfourx/nfs-template.yaml"
  runuser -l $SUDOUSER -c "oc process -f $INSTALLERHOME/openshiftfourx/nfs-template.yaml -p NFS_SERVER=$NFSIPADDRESS -p NFS_PATH=/exports/home | oc create -n kube-system -f -"
fi
echo $(date) " - Setting up $STORAGEOPTION - Done"

echo $(date) " - Creating $OPENSHIFTUSER user"
runuser -l $SUDOUSER -c "htpasswd -c -B -b /tmp/.htpasswd '$OPENSHIFTUSER' '$OPENSHIFTPASSWORD'"
runuser -l $SUDOUSER -c "sleep 3"
runuser -l $SUDOUSER -c "oc create secret generic htpass-secret --from-file=htpasswd=/tmp/.htpasswd -n openshift-config"
runuser -l $SUDOUSER -c "cat >  $INSTALLERHOME/openshiftfourx/auth.yaml <<EOF
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
  name: cluster
spec:
  tokenConfig:
    accessTokenMaxAgeSeconds: 172800
  identityProviders:
  - name: htpasswdProvider
    challenge: true
    login: true
    mappingMethod: claim
    type: HTPasswd
    htpasswd:
      fileData:
        name: htpass-secret
EOF"
runuser -l $SUDOUSER -c "oc apply -f $INSTALLERHOME/openshiftfourx/auth.yaml"
runuser -l $SUDOUSER -c "oc adm policy add-cluster-role-to-user cluster-admin '$OPENSHIFTUSER'"

echo $(date) " - ############## Script Complete ####################"


#!/bin/bash
export SUDOUSER=$1
export OPENSHIFTUSER=$2
export OPENSHIFTPASSWORD=$3
export CLUSTERNAME=$4
export DOMAINNAME=$5
export SINGLEORMULTI=$6
export REGION=$7
export RESOURCEGROUPNAME=$8
export VNETNAME=$9
export WORKERSUBNETNAME=${10}

export MULTIZONE1=1
export MULTIZONE2=2
export MULTIZONE3=3

export OCSTEMPLATES=/home/$SUDOUSER/.openshift/ocs/templates
runuser -l $SUDOUSER -c "mkdir -p $OCSTEMPLATES"

runuser -l $SUDOUSER -c "cat > $OCSTEMPLATES/toolbox.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rook-ceph-tools
  namespace: openshift-storage
  labels:
    app: rook-ceph-tools
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rook-ceph-tools
  template:
    metadata:
      labels:
        app: rook-ceph-tools
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: rook-ceph-tools
        image: rook/ceph:master
        command: [\"/tini\"]
        args: [\"-g\", \"--\", \"/usr/local/bin/toolbox.sh\"]
        imagePullPolicy: IfNotPresent
        env:
          - name: ROOK_CEPH_USERNAME
            valueFrom:
              secretKeyRef:
                name: rook-ceph-mon
                key: ceph-username
          - name: ROOK_CEPH_SECRET
            valueFrom:
              secretKeyRef:
                name: rook-ceph-mon
                key: ceph-secret
        volumeMounts:
          - mountPath: /etc/ceph
            name: ceph-config
          - name: mon-endpoint-volume
            mountPath: /etc/rook
      volumes:
        - name: mon-endpoint-volume
          configMap:
            name: rook-ceph-mon-endpoints
            items:
            - key: data
              path: mon-endpoints
        - name: ceph-config
          emptyDir: {}
      tolerations:
        - key: \"node.kubernetes.io/unreachable\"
          operator: \"Exists\"
          effect: \"NoExecute\"
          tolerationSeconds: 5
EOF"

runuser -l $SUDOUSER -c "cat > $OCSTEMPLATES/ocs-olm.yaml <<EOF
---
apiVersion: v1
kind: Namespace
metadata:
  labels:
    openshift.io/cluster-monitoring: \"true\"
  name: openshift-storage
spec: {}
---
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
  name: openshift-storage-operatorgroup
  namespace: openshift-storage
spec:
  targetNamespaces:
  - openshift-storage
---
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: ocs-operator
  namespace: openshift-storage
  labels:
    operators.coreos.com/ocs-operator.openshift-storage: ''
spec:
  channel: stable-4.8
  installPlanApproval: Automatic
  name: ocs-operator
  source: redhat-operators
  sourceNamespace: openshift-marketplace
EOF"

runuser -l $SUDOUSER -c "cat > $OCSTEMPLATES/ocs-storagecluster.yaml <<EOF
apiVersion: ocs.openshift.io/v1
kind: StorageCluster
metadata:
  annotations:
    uninstall.ocs.openshift.io/cleanup-policy: delete
    uninstall.ocs.openshift.io/mode: graceful
  name: ocs-storagecluster
  namespace: openshift-storage
  finalizers:
    - storagecluster.ocs.openshift.io
spec:
  encryption:
    enable: true
  externalStorage: {}
  managedResources:
    cephBlockPools: {}
    cephFilesystems: {}
    cephObjectStoreUsers: {}
    cephObjectStores: {}
  storageDeviceSets:
    - config: {}
      count: 1
      dataPVCTemplate:
        metadata:
          creationTimestamp: null
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Ti
          storageClassName: managed-premium
          volumeMode: Block
        status: {}
      name: ocs-deviceset
      placement: {}
      portable: true
      replica: 3
      resources: {}
  version: 4.8.0
EOF"

runuser -l $SUDOUSER -c "cat > $OCSTEMPLATES/ocs-machineset-singlezone.yaml <<EOF
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
  labels:
    machine.openshift.io/cluster-api-cluster: CLUSTERID
  name: CLUSTERID-workerocs-$REGION
  namespace: openshift-machine-api
spec:
  replicas: 3
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-cluster: CLUSTERID
      machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-cluster: CLUSTERID
        machine.openshift.io/cluster-api-machine-role: worker
        machine.openshift.io/cluster-api-machine-type: worker
        machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION
    spec:
      taints:
      - effect: NoSchedule
        key: node.ocs.openshift.io/storage
        value: \"true\"
      metadata:
        labels:
          cluster.ocs.openshift.io/openshift-storage: \"\"
          node-role.kubernetes.io/infra: \"\"
          node-role.kubernetes.io/worker: \"\"
          role: storage-node
      providerSpec:
        value:
          apiVersion: azureproviderconfig.openshift.io/v1beta1
          credentialsSecret:
            name: azure-cloud-credentials
            namespace: openshift-machine-api
          image:
            offer: \"\"
            publisher: \"\"
            resourceID: /resourceGroups/CLUSTERID-rg/providers/Microsoft.Compute/images/CLUSTERID
            sku: \"\"
            version: \"\"
          kind: AzureMachineProviderSpec
          location: $REGION
          managedIdentity: CLUSTERID-identity
          metadata:
            creationTimestamp: null
          networkResourceGroup: $RESOURCEGROUPNAME
          osDisk:
            diskSizeGB: 512
            managedDisk:
              storageAccountType: Premium_LRS
            osType: Linux
          publicIP: false
          publicLoadBalancer: CLUSTERID
          resourceGroup: CLUSTERID-rg
          subnet: $WORKERSUBNETNAME
          userDataSecret:
            name: worker-user-data
          vmSize: Standard_D16s_v3
          vnet: $VNETNAME
          zone: \"\"
EOF"

runuser -l $SUDOUSER -c "cat > $OCSTEMPLATES/ocs-machineset-multizone.yaml <<EOF
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
  labels:
    machine.openshift.io/cluster-api-cluster: CLUSTERID
  name: CLUSTERID-workerocs-$REGION$MULTIZONE1
  namespace: openshift-machine-api
spec:
  replicas: 1
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-cluster: CLUSTERID
      machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE1
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-cluster: CLUSTERID
        machine.openshift.io/cluster-api-machine-role: worker
        machine.openshift.io/cluster-api-machine-type: worker
        machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE1
    spec:
      taints:
      - effect: NoSchedule
        key: node.ocs.openshift.io/storage
        value: \"true\"
      metadata:
        labels:
          cluster.ocs.openshift.io/openshift-storage: \"\"
          node-role.kubernetes.io/infra: \"\"
          node-role.kubernetes.io/worker: \"\"
          role: storage-node
      providerSpec:
        value:
          apiVersion: azureproviderconfig.openshift.io/v1beta1
          credentialsSecret:
            name: azure-cloud-credentials
            namespace: openshift-machine-api
          image:
            offer: \"\"
            publisher: \"\"
            resourceID: /resourceGroups/CLUSTERID-rg/providers/Microsoft.Compute/images/CLUSTERID
            sku: \"\"
            version: \"\"
          kind: AzureMachineProviderSpec
          location: $REGION
          managedIdentity: CLUSTERID-identity
          metadata:
            creationTimestamp: null
          networkResourceGroup: $RESOURCEGROUPNAME
          osDisk:
            diskSizeGB: 512
            managedDisk:
              storageAccountType: Premium_LRS
            osType: Linux
          publicIP: false
          publicLoadBalancer: CLUSTERID
          resourceGroup: CLUSTERID-rg
          subnet: $WORKERSUBNETNAME
          userDataSecret:
            name: worker-user-data
          vmSize: Standard_D16s_v3
          vnet: $VNETNAME
          zone: \"1\"
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
  labels:
    machine.openshift.io/cluster-api-cluster: CLUSTERID
  name: CLUSTERID-workerocs-$REGION$MULTIZONE2
  namespace: openshift-machine-api
spec:
  replicas: 1
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-cluster: CLUSTERID
      machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE2
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-cluster: CLUSTERID
        machine.openshift.io/cluster-api-machine-role: worker
        machine.openshift.io/cluster-api-machine-type: worker
        machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE2
    spec:
      taints:
      - effect: NoSchedule
        key: node.ocs.openshift.io/storage
        value: \"true\"
      metadata:
        labels:
          cluster.ocs.openshift.io/openshift-storage: \"\"
          node-role.kubernetes.io/infra: \"\"
          node-role.kubernetes.io/worker: \"\"
          role: storage-node
      providerSpec:
        value:
          apiVersion: azureproviderconfig.openshift.io/v1beta1
          credentialsSecret:
            name: azure-cloud-credentials
            namespace: openshift-machine-api
          image:
            offer: \"\"
            publisher: \"\"
            resourceID: /resourceGroups/CLUSTERID-rg/providers/Microsoft.Compute/images/CLUSTERID
            sku: \"\"
            version: \"\"
          kind: AzureMachineProviderSpec
          location: $REGION
          managedIdentity: CLUSTERID-identity
          metadata:
            creationTimestamp: null
          networkResourceGroup: $RESOURCEGROUPNAME
          osDisk:
            diskSizeGB: 512
            managedDisk:
              storageAccountType: Premium_LRS
            osType: Linux
          publicIP: false
          publicLoadBalancer: CLUSTERID
          resourceGroup: CLUSTERID-rg
          subnet: $WORKERSUBNETNAME
          userDataSecret:
            name: worker-user-data
          vmSize: Standard_D16s_v3
          vnet: $VNETNAME
          zone: \"2\"
---
apiVersion: machine.openshift.io/v1beta1
kind: MachineSet
metadata:
  labels:
    machine.openshift.io/cluster-api-cluster: CLUSTERID
  name: CLUSTERID-workerocs-$REGION$MULTIZONE3
  namespace: openshift-machine-api
spec:
  replicas: 1
  selector:
    matchLabels:
      machine.openshift.io/cluster-api-cluster: CLUSTERID
      machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE3
  template:
    metadata:
      labels:
        machine.openshift.io/cluster-api-cluster: CLUSTERID
        machine.openshift.io/cluster-api-machine-role: worker
        machine.openshift.io/cluster-api-machine-type: worker
        machine.openshift.io/cluster-api-machineset: CLUSTERID-workerocs-$REGION$MULTIZONE3
    spec:
      taints:
      - effect: NoSchedule
        key: node.ocs.openshift.io/storage
        value: \"true\"
      metadata:
        labels:
          cluster.ocs.openshift.io/openshift-storage: \"\"
          node-role.kubernetes.io/infra: \"\"
          node-role.kubernetes.io/worker: \"\"
          role: storage-node
      providerSpec:
        value:
          apiVersion: azureproviderconfig.openshift.io/v1beta1
          credentialsSecret:
            name: azure-cloud-credentials
            namespace: openshift-machine-api
          image:
            offer: \"\"
            publisher: \"\"
            resourceID: /resourceGroups/CLUSTERID-rg/providers/Microsoft.Compute/images/CLUSTERID
            sku: \"\"
            version: \"\"
          kind: AzureMachineProviderSpec
          location: $REGION
          managedIdentity: CLUSTERID-identity
          metadata:
            creationTimestamp: null
          networkResourceGroup: $RESOURCEGROUPNAME
          osDisk:
            diskSizeGB: 512
            managedDisk:
              storageAccountType: Premium_LRS
            osType: Linux
          publicIP: false
          publicLoadBalancer: CLUSTERID
          resourceGroup: CLUSTERID-rg
          subnet: $WORKERSUBNETNAME
          userDataSecret:
            name: worker-user-data
          vmSize: Standard_D16s_v3
          vnet: $VNETNAME
          zone: \"3\"
EOF"

#Login
var=1
while [ $var -ne 0 ]; do
echo "Attempting to login $OPENSHIFTUSER to https://api.${CLUSTERNAME}.${DOMAINNAME}:6443 "
oc login "https://api.${CLUSTERNAME}.${DOMAINNAME}:6443" -u $OPENSHIFTUSER -p $OPENSHIFTPASSWORD --insecure-skip-tls-verify=true
var=$?
echo "exit code: $var"
done

# #OCS Operator will install its components only on nodes labelled for OCS with the key
# OCS_NODES=$(oc get nodes --show-labels | grep node-role.kubernetes.io/worker= |cut -d' ' -f1)
# for ocsnode in ${OCS_NODES[@]}; do
# oc label nodes $ocsnode cluster.ocs.openshift.io/openshift-storage=''
# done
## Creating ibm-ccs cr
if [[ $SINGLEORMULTI == "noha" ]];then

CLUSTERID=$(oc get machineset -n openshift-machine-api -o jsonpath='{.items[0].metadata.labels.machine\.openshift\.io/cluster-api-cluster}')
runuser -l $SUDOUSER -c "sed -i -e s/CLUSTERID/$CLUSTERID/g $OCSTEMPLATES/ocs-machineset-singlezone.yaml"
runuser -l $SUDOUSER -c "oc create -f $OCSTEMPLATES/ocs-machineset-singlezone.yaml"
runuser -l $SUDOUSER -c "echo sleeping for 10mins untill the node comes up"
runuser -l $SUDOUSER -c "sleep 600"

elif [[ $SINGLEORMULTI == "az" ]];then

CLUSTERID=$(oc get machineset -n openshift-machine-api -o jsonpath='{.items[0].metadata.labels.machine\.openshift\.io/cluster-api-cluster}')
runuser -l $SUDOUSER -c "sed -i -e s/CLUSTERID/$CLUSTERID/g $OCSTEMPLATES/ocs-machineset-multizone.yaml"
runuser -l $SUDOUSER -c "oc create -f $OCSTEMPLATES/ocs-machineset-multizone.yaml"
runuser -l $SUDOUSER -c "echo sleeping for 10mins untill the node comes up"
runuser -l $SUDOUSER -c "sleep 600"

fi

runuser -l $SUDOUSER -c "oc create -f $OCSTEMPLATES/ocs-olm.yaml"
runuser -l $SUDOUSER -c "echo sleeping for 5mins"
runuser -l $SUDOUSER -c "sleep 300"
runuser -l $SUDOUSER -c "oc apply -f $OCSTEMPLATES/ocs-storagecluster.yaml"
runuser -l $SUDOUSER -c "echo sleeping for 10mins"
runuser -l $SUDOUSER -c "sleep 600"
runuser -l $SUDOUSER -c "oc apply -f $OCSTEMPLATES/toolbox.yaml"
runuser -l $SUDOUSER -c "echo sleeping for 1min"
runuser -l $SUDOUSER -c "sleep 60"

echo $(date) " - ############## Script Complete ####################"