-
Notifications
You must be signed in to change notification settings - Fork 9
/
mysql_bosh_lite
executable file
·58 lines (49 loc) · 1.34 KB
/
mysql_bosh_lite
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
set -u
set -e
set -o pipefail
get_database_password(){
set +u # $CREDHUB_PASSWORD may be unbound
if [[ -z $CREDHUB_PASSWORD ]]; then
credhub login --skip-tls-validation
else
credhub login -s "$CREDHUB_SERVER" -u "$CREDHUB_USERNAME" -p "$CREDHUB_PASSWORD" --skip-tls-validation
fi
set -u
cc_database_password=$(credhub get -n '/bosh-lite/cf/cc_database_password' --output-json | jq -r '.value')
}
ssh_tunnel(){
mysql_port=$(gshuf -i 10000-65535 -n 1)
mysql_ip=$(bosh is --column ips --column instance | grep database | cut -f 2)
test -z "$mysql_port" && exit 1
test -z "$mysql_ip" && exit 1
bosh ssh --opts="-L ${mysql_port}:${mysql_ip}:3306" -c 'while true; do continue; done' database &
ssh_pid=$!
}
connect_to_db(){
# Make sure the ssh tunnel didn't error and exit
if ! ps -p $ssh_pid > /dev/null; then
echo "Error: SSH tunnel not running"
exit 1
fi
mysql -ucloud_controller -p"${cc_database_password}" -P $mysql_port -h 127.0.0.1 cloud_controller
}
kill_tree() {
pid=$1
for child_pid in $(ps -axo pid,ppid | grep "${pid}$" | cut -d " " -f 1); do
kill_tree $child_pid
done
kill $pid
}
{
get_database_password &&
ssh_tunnel &&
sleep 3 &&
connect_to_db &&
kill_tree $ssh_pid
} || {
# If any of the previous functions failed, kill the ssh process tree
echo "Exiting due to error"
kill_tree $ssh_pid
exit 1
}