Skip to content

Commit

Permalink
ci: add cleanup dps
Browse files Browse the repository at this point in the history
  • Loading branch information
pratapaprasanna committed Apr 12, 2023
1 parent eaf3fde commit 0b1156e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/cleanup-dp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Cleanup Deploy Previews

on:
# This line enables manual triggering of this workflow.
workflow_dispatch:

jobs:
execute:
runs-on: ubuntu-latest
steps:
# Checkout the code
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Install mongosh
run: |
sudo apt-get update
sudo apt-get install -y wget gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt-get update
sudo apt-get install -y mongodb-mongosh
- name: Install relevant packages
run: |
which aws
sudo apt update -q
sudo apt install -y curl unzip less jq
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.23.6/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
- name: Delete Helm chart
env:
AWS_ROLE_ARN: ${{ secrets.AWS_ROLE_ARN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_URL: ${{ secrets.DB_URL }}

run: /bin/bash ./runbooks/tasks/cleanup_dp.sh
38 changes: 38 additions & 0 deletions scripts/cleanup_dp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# Configure the AWS & kubectl environment

set -o errexit
set -o nounset

export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY

export AWS_REGION=ap-south-1
export AWS_DEFAULT_OUTPUT=json

export cluster_name=uat-cluster

sts_output=$(aws sts assume-role --role-arn "$AWS_ROLE_ARN" --role-session-name dp-session-script)

export AWS_ACCESS_KEY_ID="$(echo "$sts_output" | jq -r '.Credentials.AccessKeyId')"
export AWS_SECRET_ACCESS_KEY="$(echo "$sts_output" | jq -r '.Credentials.SecretAccessKey')"
export AWS_SESSION_TOKEN="$(echo "$sts_output" | jq -r '.Credentials.SessionToken')"

export KUBECONFIG="$PWD/kubeconfig"
aws eks update-kubeconfig --name "$cluster_name" --role-arn "$AWS_ROLE_ARN"

### Get list of helm charts
deployed_charts="$(helm ls -A --filter 'ce[0-9]+' --output json | jq -r '.[].namespace')"

for i in $deployed_charts
do
pr=$(echo $i | cut -c 3-);
pr_status="$(gh pr view "$pr" --json state --jq .state)"
echo $pr_state
if [[ $pr_state == "MERGED" ]]
then
echo "helm uninstall $i -n $i"
kubectl delete ns $NAMESPACE || echo "true"
echo mongosh "mongodb+srv://$DB_USERNAME:$DB_PASSWORD@$DB_URL/$i?retryWrites=true&minPoolSize=1&maxPoolSize=10&maxIdleTimeMS=900000&authSource=admin" --eval 'db.dropDatabase()'
fi
done

0 comments on commit 0b1156e

Please sign in to comment.