Skip to content

Commit

Permalink
feat: add test db script (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Campos authored May 27, 2024
1 parent d19cd59 commit 41c07a1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tools/pg-test-portforward.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 41c07a1

Please sign in to comment.