-
Notifications
You must be signed in to change notification settings - Fork 3
/
pantheon-db-cli
executable file
·51 lines (41 loc) · 2.29 KB
/
pantheon-db-cli
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
#!/bin/bash
# pantheon-db-cli
# Get a MySQL commandline on a Pantheon server that requires an SSH tunnel.
# Check if mysql is installed locally.
command -v mysql >/dev/null 2>&1 || { echo >&2 "FAILURE. MySQL is required. Please install mysql locally and run the command again."; exit 1; }
if [[ $# -lt 2 ]] ; then
>&2 echo -e 'Usage:'
>&2 echo -e " $(basename $0) [site] [environment]"
>&2 echo -e ' [site] - The name of a Pantheon site. E.g. aclu-d7'
>&2 echo -e ' [environment] - An environment for that site. E.g. dev'
exit 0
fi
connection_info=`terminus connection:info $1.$2 --fields=mysql_port,git_port,sftp_username,mysql_host,mysql_username,mysql_password,mysql_database --format=print-r`
if [ $? -ne 0 ]; then
>&2 echo -e 'FAILURE. Please ensure that you can run this command successfully:'
>&2 echo -e ' terminus site connection-info'
exit 1;
fi
mysql_port=`echo -e "$connection_info" | grep mysql_port | awk '{print $3}'`
git_port=`echo -e "$connection_info" | grep git_port | awk '{print $3}'`
if [ -z "$git_port" ]; then
git_port=2222
fi
sftp_username=`echo -e "$connection_info" | grep sftp_username | awk '{print $3}'`
mysql_host=`echo -e "$connection_info" | grep mysql_host | awk '{print $3}'`
mysql_username=`echo -e "$connection_info" | grep mysql_username | awk '{print $3}'`
mysql_password=`echo -e "$connection_info" | grep mysql_password | awk '{print $3}'`
mysql_database=`echo -e "$connection_info" | grep mysql_database | awk '{print $3}'`
if [ "$mysql_database" == '' ]; then
mysql_database="pantheon"
fi
>&2 echo -e "--Now waking up the server.--"
terminus env:wake $1.$2
>&2 echo -e "--Now opening the SSH connection.--"
echo "ssh -o 'StrictHostKeyChecking accept-new' -f -N -L $mysql_port:localhost:$mysql_port -p $git_port $sftp_username@$mysql_host"
ssh -o 'StrictHostKeyChecking accept-new' -f -N -L $mysql_port:localhost:$mysql_port -p $git_port $sftp_username@$mysql_host
>&2 echo -e "--Now opening MySQL CLI.--"
echo "mysql --user=$mysql_username --host=127.0.0.1 --port=$mysql_port --password=$mysql_password $mysql_database"
mysql --user=$mysql_username --host=127.0.0.1 --port=$mysql_port --password=$mysql_password $mysql_database
>&2 echo -e "--Now cleaning up the SSH connection.--"
ps -fU `whoami` | grep "ssh -o" | grep "@$mysql_host" | awk '{print $2}' | xargs kill