-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.sh
43 lines (29 loc) · 873 Bytes
/
cli.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
OPENSHIFT_URL="<YOUR_OPENSHIFT_CLUSTER_URL_HERE>" # e.g. https://api.ocp-prod-0.k8s.it.helsinki.fi:6443
API_TOKEN=$(cat production-token)
oc login --server=$OPENSHIFT_URL --token=$API_TOKEN
DEPLOYMENT_NAME="db-tools"
POD_NAME=$(oc get pods -l deployment=$DEPLOYMENT_NAME -o jsonpath='{.items[0].metadata.name}')
db_names=()
db_urls=()
while read line; do
name=$(echo $line | cut -d ' ' -f 1)
url=$(echo $line | cut -d ' ' -f 2)
db_names+=($name)
db_urls+=($url)
done < databases.txt
echo "Database to connect to:"
read -r database_name
database_url=""
for i in ${!db_names[@]}; do
if [ "$database_name" == "${db_names[$i]}" ]; then
database_url="${db_urls[$i]}"
break
fi
done
if [ -z "$database_url" ]; then
echo "Database not found"
exit 1
fi
echo "Connecting to $database_url"
oc exec -it $POD_NAME -- psql $database_url