diff --git a/solutions/setup.sh b/solutions/setup.sh new file mode 100644 index 000000000..a8e1434f4 --- /dev/null +++ b/solutions/setup.sh @@ -0,0 +1,317 @@ +#!/bin/bash +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +usage() { + cat < /dev/null 1>&1 + #else + # echo "Role $i already set on $USER" + #fi + done + + + # fetch the LZ + #cd ../../../ + # check for existing landing-zone + + #kpt pkg get https://github.com/GoogleCloudPlatform/pubsec-declarative-toolkit.git/solutions/landing-zone landing-zone + # cp the setters.yaml + #cp pubsec-declarative-toolkit/solutions/landing-zone/setters.yaml landing-zone/ + #cp pubsec-declarative-toolkit/solutions/landing-zone/.krmignore landing-zone/ + + echo "kpt live init" + kpt live init core-landing-zone --namespace config-control --force + echo "kpt fn render" + kpt fn render core-landing-zone + echo "kpt live apply" + kpt live apply core-landing-zone --reconcile-timeout=2m --output=table + echo "Wait 2 min" + count=$(kubectl get gcp | grep UpdateFailed | wc -l) + echo "UpdateFailed: $count" + count=$(kubectl get gcp | grep UpToDate | wc -l) + echo "UpToDate: $count" + kubectl get gcp + +fi + +if [[ "$REMOVE_LZ" != false ]]; then + echo "deleting lz on ${CLUSTER} in region ${REGION}" + # stay in current dir + # will take up to 15-45 min and may hang unless liens are removed + # 3 problematic projects + #gcloud config set project audit-prj-id-oldv1 + #AUDIT_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $AUDIT_LIEN + + #gcloud config set project net-host-prj-prod-oldv1 + #PROD_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $PROD_LIEN + + #gcloud config set project net-host-prj-nonprod-oldv1 + #NONPROD_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $NONPROD_LIEN + + #kpt live destroy core-landing-zone +fi + + # delete +if [[ "$DELETE_KCC" != false ]]; then + echo "Deleting cluster ${CLUSTER} in region ${REGION}" + # stay in current dir + # will take up to 15-45 min and may hang unless liens are removed + # 3 problematic projects + #gcloud config set project audit-prj-id-oldv1 + #AUDIT_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $AUDIT_LIEN + + #gcloud config set project net-host-prj-prod-oldv1 + #PROD_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $PROD_LIEN + + #gcloud config set project net-host-prj-nonprod-oldv1 + #NONPROD_LIEN=$(gcloud alpha resource-manager liens list) + #gcloud alpha resource-manager liens delete $NONPROD_LIEN + + #kpt live destroy core-landing-zone + + # delete kpt pkg get + #rm -rf landing-zone + # https://cloud.google.com/sdk/gcloud/reference/anthos/config/controller/delete + echo "Delete Cluster ${CLUSTER} in region ${REGION}" + startd=`date +%s` + # note: cluster name is krmapihost-$CLUSTER + gcloud anthos config controller delete --location $REGION $CLUSTER --quiet + endd=`date +%s` + runtimed=$((endd-startd)) + echo "Cluster delete time: ${runtimed} sec" + + # delete VPC (routes and firewalls will be deleted as well) + #echo "deleting subnet ${SUBNET}" + #gcloud compute networks subnets delete ${SUBNET} --region=$REGION -q + #echo "deleting vpc ${NETWORK}" + #gcloud compute networks delete ${NETWORK} -q + + # disable billing before deletion - to preserve the project/billing quota + #gcloud alpha billing projects unlink ${CC_PROJECT_ID} + # delete cc project + #gcloud projects delete $CC_PROJECT_ID --quiet +fi + +end=`date +%s` +runtime=$((end-start)) +echo "Total Duration: ${runtime} sec" + echo "Date: $(date)" + echo "Timestamp: $(date +%s)" + + gcloud config set project "${BOOT_PROJECT_ID}" + echo "Switched back to boot project ${BOOT_PROJECT_ID}" + # go back to the script dir + #cd pubsec-declarative-toolkit/solutions/landing-zone +} + +UNIQUE= +DEPLOY_LZ=false +CREATE_KCC=false +DELETE_KCC=false +REMOVE_LZ=false +BOOT_PROJECT_ID= + +while getopts ":b:u:c:l:d:r:p:" PARAM; do + case $PARAM in + b) + BOOT_PROJECT_ID=${OPTARG} + ;; + u) + UNIQUE=${OPTARG} + ;; + c) + CREATE_KCC=${OPTARG} + ;; + l) + DEPLOY_LZ=${OPTARG} + ;; + r) + REMOVE_LZ=${OPTARG} + ;; + d) + DELETE_KCC=${OPTARG} + ;; + p) + KCC_PROJECT_ID=${OPTARG} + ;; + ?) + usage + exit + ;; + esac +done + +# echo "Options are: -c true/false (create kcc), -l true/false (deploy landing zone) -r (remove lz) -d true/false (delete kcc) -p kcc-project-id" + + +if [[ -z $UNIQUE ]]; then + usage + exit 1 +fi + +deployment $BOOT_PROJECT_ID $UNIQUE $CREATE_KCC $DEPLOY_LZ $REMOVE_LZ $DELETE_KCC $KCC_PROJECT_ID +printf "**** Done ****\n"