From 41c07a1815b75d542eea8ff11fb15c76330c6356 Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Mon, 27 May 2024 13:30:54 -0300 Subject: [PATCH] feat: add test db script (#1184) --- tools/pg-test-portforward.sh | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 tools/pg-test-portforward.sh diff --git a/tools/pg-test-portforward.sh b/tools/pg-test-portforward.sh new file mode 100755 index 000000000..d1e3c5278 --- /dev/null +++ b/tools/pg-test-portforward.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# -*- coding: utf-8 -*- + +# Variables +TARGET_ENV="test" +PORT=15432 + +# Prod option: Just call the script with "./pg-test-portforward.sh prod" +if [ "$1" == "prod" ] || [ "$1" == "PROD" ]; then + echo "Switching to PROD instead of TEST!" + TARGET_ENV="prod" +else + echo "Starting port-forward script for TEST. You also can do './pg-test-portforward.sh prod'" +fi + +# Check for VPN +echo "Looking for VPN..." +if ping -c1 api.silver.devops.gov.bc.ca &> /dev/null; then + echo "VPN OK!" +else + echo "Oops. You need the VPN for this. Leaving!" + exit +fi + +# Check for oc login +echo "Looking for OC login status..." +OC_USER=$(oc whoami 2&> /dev/null && echo 'OK' || echo 'NO') +if [ "$OC_USER" == "OK" ]; then + echo "OC login OK!" +else + echo "Oops. You need to be logged in on the OpenShift CLI (oc)! Leaving!" + exit 1; +fi + +# 1. Set the project +PROJECT="b9d53b-$TARGET_ENV" +echo "Setting project..." +oc project $PROJECT + +# 2. Get the pod name +echo "Looking for database running pods on OpenShift..." +POD_NAME=$(oc get pods | grep nr-spar-"$TARGET_ENV"-database | grep Running | cut -d ' ' -f 1) +if [ "$POD_NAME" == "" ]; then + echo "Unable to find a Running pod on $TARGET_ENV! Leaving!" + exit 1; +else + echo "OK! Pod found! Name: $POD_NAME" +fi + +# 3. Get DB name, user and password +echo "Getting credentials..." +SECRETS=$(oc extract secret/nr-spar-$TARGET_ENV-database -n b9d53b-$TARGET_ENV --to=- 2> /dev/null) +DB_NAME=$(echo $SECRETS | cut -d ' ' -f 1) +DB_USER=$(echo $SECRETS | cut -d ' ' -f 3) +DB_PASS=$(echo $SECRETS | cut -d ' ' -f 2) +echo "Use this information to set up the database connection with '$TARGET_ENV' on your end:" +echo "- DB_HOST = localhost" +echo "- DB_PORT = $PORT" +echo "- DB_NAME = '$DB_NAME'" +echo "- DB_USER = '$DB_USER'" +echo "- DB_PASS = '$DB_PASS'" + +echo "Starting port forwarding... Press Ctrl+C to stop" +oc port-forward $POD_NAME $PORT:5432