#!/bin/bash added=0 copy() { local src=${TEMPLATE_ROOT}/$1 dest=$2 if [ ! -f $dest ];then cp "$src" "$dest" git add "$dest" added=$(($added+1)) fi } template() { local src=${TEMPLATE_ROOT}/$1 dest=$2 if [ ! -f $dest ];then envsubst '$ARTIFACTORY_URL,$DOMAIN,$DOMAIN_NAME,$ISSUER_NAME,$PROJECT_NAME,$PROJECT_PATH,$GIT_REPOSITORY_URL,$DEPLOY_URL,$STAGE,$ORG_NAME,$NAMESPACE' <"$src" >"$dest" git add "$dest" added=$(($added+1)) fi } git_push() { local message=$1 if [ $added -ne 0 ];then git commit -am "$message" git push fi } cleanup() { rm -rf .* * || true } del_resources() { local ress="$1" file="$2" yq -i 'del(.resources[]|select(.=="'"$ress"'"))' "$file" git add "$file" added=$(($added+1)) } add_resources() { local ress="$1" file="$2" del_resources "$@" yq -i '.resources += "'"$ress"'"' "$file" git add "$file" added=$(($added+1)) } git_prepare() { local url="$1" username="$2" email="$3" mkdir -p "${HOME}/.ssh" cp "${WORKSPACE_SSH_DIRECTORY_PATH}"/* "${HOME}/.ssh" chmod 700 "${HOME}/.ssh" chmod 400 "${HOME}/.ssh"/* git config --global user.name "$username" git config --global user.email "$email" git clone "$url" --branch main --depth 1 . } install_base() { mkdir -p bases/project bases/install bases/deploy bases/images bases/trigger-push bases/trigger-tag ci copy empty-kusto.yaml "ci/kustomization.yaml" copy yamllint.yaml .yamllint.yaml copy base-repo.yaml bases/images/repo.yaml copy images-kusto.yaml bases/images/kustomization.yaml copy base-deploy.yaml bases/project/deploy.yaml copy base-secret.yaml bases/project/secret.yaml copy base-config.yaml bases/project/config.yaml copy base-service.yaml bases/project/service.yaml copy base-kusto.yaml bases/project/kustomization.yaml copy install-install.yaml bases/install/install.yaml copy install-kusto.yaml bases/install/kustomization.yaml copy deploy-kusto.yaml bases/deploy/kustomization.yaml copy trigger-kusto.yaml bases/trigger-tag/kustomization.yaml copy trigger-tag.yaml bases/trigger-tag/trigger.yaml copy trigger-kusto.yaml bases/trigger-push/kustomization.yaml copy trigger-push.yaml bases/trigger-push/trigger.yaml template base-update.yaml.tmpl bases/images/update.yaml template base-cert.yaml.tmpl bases/project/cert.yaml template deploy-repo.yaml.tmpl bases/deploy/repo.yaml if [ -f README.md ] && [ $(wc -l < README.md) -eq 2 ];then rm README.md fi copy README.md README.md } create_prj() { export ORG_NAME=$(echo ${PROJECT_PATH}|sed 's#/.*##') mkdir -p "projects/${PROJECT_NAME}" "ci/${PROJECT_NAME}" template ci-kusto.yaml.tmpl "ci/${PROJECT_NAME}/kustomization.yaml" add_resources "${PROJECT_NAME}" "ci/kustomization.yaml" template project-kusto.yaml.tmpl "projects/${PROJECT_NAME}/kustomization.yaml" while [ $# -gt 0 ];do export STAGE=$1 mkdir -p "stages/${STAGE}/${PROJECT_NAME}" "stages/${STAGE}/deploy/${PROJECT_NAME}" template deploy-project-kusto.yaml.tmpl "stages/${STAGE}/deploy/${PROJECT_NAME}/kustomization.yaml" if [ "${STAGE}" == "prod" ] || [ "${STAGE}" == "production" ];then template deploy-policy-tag.yaml.tmpl "stages/${STAGE}/deploy/${PROJECT_NAME}/policy.yaml" else template deploy-policy-default.yaml.tmpl "stages/${STAGE}/deploy/${PROJECT_NAME}/policy.yaml" fi copy empty-kusto.yaml "stages/${STAGE}/deploy/kustomization.yaml" del_resources "../../../bases/deploy" "stages/${STAGE}/deploy/kustomization.yaml" template stage-kusto.yaml.tmpl "stages/${STAGE}/${PROJECT_NAME}/kustomization.yaml" template stage-ingress.yaml.tmpl "stages/${STAGE}/${PROJECT_NAME}/ingress.yaml" template stage-cert.yaml.tmpl "stages/${STAGE}/${PROJECT_NAME}/cert.yaml" template stage-config.yaml.tmpl "stages/${STAGE}/${PROJECT_NAME}/config.yaml" shift done } activate_prj() { export ORG_NAME=$(echo ${PROJECT_PATH}|sed 's#/.*##') while [ $# -gt 0 ];do export STAGE=$1 add_resources "${PROJECT_NAME}" "stages/${STAGE}/deploy/kustomization.yaml" shift done } delete_prj() { rm -rf "projects/${PROJECT_NAME}" while [ $# -gt 0 ];do export STAGE=$1 rm -rf "stages/${STAGE}/${PROJECT_NAME}" "stages/${STAGE}/deploy/${PROJECT_NAME}" del_resources "${PROJECT_NAME}" "stages/${STAGE}/deploy/kustomization.yaml" del_resources "${PROJECT_NAME}" "ci/kustomization.yaml" shift done }