diff --git a/bin/v2/postgres-setup.sh b/bin/v2/postgres-setup.sh new file mode 100755 index 00000000000..a2d9fc511d4 --- /dev/null +++ b/bin/v2/postgres-setup.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env bash +# set -ex + +NAMESPACE='${NAMESPACE}' +ENVIRONMENT='${ENVIRONMENT}' +NAMESPACE_NAME="$1" +APP_NAME="$2" +APPS_DIR="../../apps/" +CLUSTERS_DIR="../../clusters" + +PASSWORD=$(LC_ALL=C tr -dc 'A-Za-z0-9' ' +} + +if [ -z "${NAMESPACE_NAME}" ] || [ -z "${APP_NAME}" ]; then + usage + exit 1 +fi + +if [ ! -f "apps/$NAMESPACE_NAME/dev/aso" ]; then + echo "Creating aso" + mkdir -p apps/$NAMESPACE_NAME + mkdir -p apps/$NAMESPACE_NAME/dev + mkdir -p apps/$NAMESPACE_NAME/dev/aso + + ( + cat <"apps/$NAMESPACE_NAME/dev/aso/$NAMESPACE_NAME-postgres.yaml" +fi + +if [ ! -f "apps/$NAMESPACE_NAME/dev/sops-secrets" ]; then + echo "Creating sops-secrets" + mkdir -p apps/$NAMESPACE_NAME + mkdir -p apps/$NAMESPACE_NAME/dev + mkdir -p apps/$NAMESPACE_NAME/dev/sops-secrets +fi + +( + cat <"apps/$NAMESPACE_NAME/dev/sops-secrets/$APP_NAME-values.enc_temp.yaml" + +if ! [ -f /usr/local/bin/sops ]; then + echo "Sops is not installed... installing..." + curl -LO https://github.com/getsops/sops/releases/download/v3.9.1/sops-v3.9.1.darwin.amd64 + mv sops-v3.9.1.darwin.amd64 /usr/local/bin/sops + chmod +x /usr/local/bin/sops +fi + +sops --encrypt --azure-kv https://dtssharedservicesdevkv.vault.azure.net/keys/sops-key/2beba064ddfe454482ad0133af2cf0fd --encrypted-regex "^(data|stringData)$" --in-place apps/$NAMESPACE_NAME/dev/sops-secrets/$APP_NAME-values.enc_temp.yaml + +# making sure the indentation is with 2 spaces +yq eval --indent 2 -P "apps/$NAMESPACE_NAME/dev/sops-secrets/$APP_NAME-values.enc_temp.yaml" > "apps/$NAMESPACE_NAME/dev/sops-secrets/$APP_NAME-values.enc.yaml" + +# deleting the temp file +rm "./apps/$NAMESPACE_NAME/dev/sops-secrets/$APP_NAME-values.enc_temp.yaml" + +( + cat <"apps/$NAMESPACE_NAME/dev/sops-secrets/kustomization.yaml" + +if [ ! -f "apps/$NAMESPACE_NAME/dev/base/kustomization.yaml" ]; then + echo "Creating kustomization in dev/base" + mkdir -p apps/$NAMESPACE_NAME + mkdir -p apps/$NAMESPACE_NAME/dev + mkdir -p apps/$NAMESPACE_NAME/dev/base + + ( + cat <"apps/$NAMESPACE_NAME/dev/base/kustomization.yaml" +else +( + cat <"apps/$NAMESPACE_NAME/dev/base/kustomization_resources_temp.yaml" +( + cat <"apps/$NAMESPACE_NAME/dev/base/kustomization_patches_temp.yaml" + +# Path to the kustomization.yaml file +KUSTOMIZATION_FILE="apps/$NAMESPACE_NAME/dev/base/kustomization.yaml" + +# Path to the file to be included +FILE_RESOURCES_TO_INCLUDE="apps/$NAMESPACE_NAME/dev/base/kustomization_resources_temp.yaml" + +# Read the contents of the file to be included +FILE_RESOURCES_CONTENTS=$(cat "$FILE_RESOURCES_TO_INCLUDE") + +# Path to the file to be included +FILE_PATCHES_TO_INCLUDE="apps/$NAMESPACE_NAME/dev/base/kustomization_patches_temp.yaml" + +# Read the contents of the file to be included +FILE_PATCHES_CONTENTS=$(cat "$FILE_PATCHES_TO_INCLUDE") + +echo $FILE_RESOURCES_CONTENTS +echo $FILE_PATCHES_CONTENTS + +# Update the kustomization.yaml file +sed -i.bak "/namespace:/i\\ +$FILE_RESOURCES_CONTENTS +" "$KUSTOMIZATION_FILE" + +echo "Updated $KUSTOMIZATION_FILE with contents of $FILE_TO_RESOURCES_INCLUDE" + +# Update the kustomization.yaml file +sed -i.bak "/patches:/a\\ +$FILE_PATCHES_CONTENTS +" "$KUSTOMIZATION_FILE" + +echo "Updated $KUSTOMIZATION_FILE with contents of $FILE_TO_PATCHES_INCLUDE" + +# deleting the temp file +rm "./apps/$NAMESPACE_NAME/dev/base/kustomization_resources_temp.yaml" +rm "./apps/$NAMESPACE_NAME/dev/base/kustomization_patches_temp.yaml" +fi \ No newline at end of file