|
| 1 | +#!/bin/bash |
| 2 | +# Exit if any command fails. |
| 3 | +set -e |
| 4 | + |
| 5 | +# --- Parse Input Arguments --- |
| 6 | +# Initialize variables |
| 7 | +CONTEXT="" |
| 8 | +NAMESPACE="" |
| 9 | +APP="" |
| 10 | + |
| 11 | +# Process each parameter using flags |
| 12 | +while [[ "$#" -gt 0 ]]; do |
| 13 | + case $1 in |
| 14 | + --context) |
| 15 | + CONTEXT="$2" |
| 16 | + shift 2 |
| 17 | + ;; |
| 18 | + --namespace) |
| 19 | + NAMESPACE="$2" |
| 20 | + shift 2 |
| 21 | + ;; |
| 22 | + --app) |
| 23 | + APP="$2" |
| 24 | + shift 2 |
| 25 | + ;; |
| 26 | + *) |
| 27 | + echo "Unknown parameter passed: $1" |
| 28 | + echo "Usage: $0 --context <context> --namespace <namespace> --app <app>" |
| 29 | + exit 1 |
| 30 | + ;; |
| 31 | + esac |
| 32 | +done |
| 33 | + |
| 34 | +# Check if all required parameters are provided |
| 35 | +if [ -z "$CONTEXT" ] || [ -z "$NAMESPACE" ] || [ -z "$APP" ]; then |
| 36 | + echo "Missing required parameter." |
| 37 | + echo "Usage: $0 --context <context> --namespace <namespace> --app <app>" |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +# --- Step 1: nais login --- |
| 42 | +echo "Starting nais login. This command will open a browser for authentication..." |
| 43 | +nais login |
| 44 | +# The command waits until login is complete. |
| 45 | + |
| 46 | +# --- Step 2: nais postgres prepare --- |
| 47 | +echo -e "\nRunning 'nais postgres prepare' for:" |
| 48 | +echo " Context: $CONTEXT" |
| 49 | +echo " Namespace: $NAMESPACE" |
| 50 | +echo " App: $APP" |
| 51 | +echo -e "\nPlease follow the instructions provided by the command below." |
| 52 | +# Run prepare; it will prompt for confirmation (y/N) only once. |
| 53 | +nais postgres prepare --context "$CONTEXT" --namespace "$NAMESPACE" "$APP" |
| 54 | + |
| 55 | +# --- Step 3: nais postgres grant --- |
| 56 | +echo -e "\nGranting PostgreSQL access with 'nais postgres grant'..." |
| 57 | +nais postgres grant --context "$CONTEXT" --namespace "$NAMESPACE" "$APP" |
| 58 | + |
| 59 | +# --- Step 4: nais postgres proxy --- |
| 60 | +echo -e "\nStarting PostgreSQL proxy with 'nais postgres proxy'..." |
| 61 | +nais postgres proxy --context "$CONTEXT" --namespace "$NAMESPACE" "$APP" |
| 62 | + |
| 63 | +echo -e "\nAll commands executed successfully." |
0 commit comments