forked from vmware-tanzu-learning/pal-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-databases.sh
More file actions
executable file
·87 lines (59 loc) · 1.9 KB
/
migrate-databases.sh
File metadata and controls
executable file
·87 lines (59 loc) · 1.9 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env bash
set -e
app_name="${1:-pal-tracker}"
script_dir="${2:-.}"
service_name="${3:-tracker-database}"
service_key="${4:-flyway-migration-key}"
function main() {
echo "Creating service key, if necessary ..."
cf create-service-key $service_name $service_key > /dev/null
echo "Retrieving target database parameters ..."
credentials=$(cf service-key $service_name $service_key | sed -ne '/{/,$p')
db_host=$(echo $credentials | jq -r '.hostname')
db_name=$(echo $credentials | jq -r '.name')
db_username=$(echo $credentials | jq -r '.username')
db_password=$(echo $credentials | jq -r '.password')
db_port=$(echo $credentials | jq -r '.port')
test -n "$db_host" || exit 1
echo "Opening ssh tunnel to $db_host:$db_port ..."
cf ssh -N -L 63306:$db_host:$db_port $app_name &
cf_ssh_pid=$!
echo "Waiting for tunnel ..."
wait_for_tunnel
echo "Running migration ..."
flyway-*/flyway -url="jdbc:mysql://127.0.0.1:63306/$db_name" \
-locations=filesystem:"$script_dir"/databases/tracker \
-user="$db_username" \
-password="$db_password" \
migrate
echo "Migration complete"
}
function pre_tunnel_exit() {
echo "ERROR: No suitable credentials found for application '$app_name' and service '$service_name'" >&2
}
trap pre_tunnel_exit EXIT
function close_tunnel_at_exit() {
echo "Closing tunnel"
kill -KILL $cf_ssh_pid
}
trap close_tunnel_at_exit EXIT
function wait_for_tunnel() {
set +e
for x in {1..10}; do
$(nc -z localhost 63306)
nc_status=$?
if [[ "$nc_status" == "0" ]]; then
echo "Ssh tunnel success."
break
else
echo "Ssh tunnel not detected yet..."
sleep 3
fi
done
if [[ "$nc_status" != "0" ]] ; then
echo "Failed to detect ssh tunnel. Exiting."
exit 1
fi
set -e
}
main