[WIP] Configure SQL Proxy
This commit is contained in:
parent
566c56c423
commit
cfb82fd78a
4 changed files with 74 additions and 37 deletions
|
@ -10,10 +10,9 @@ services:
|
|||
- 3022:22
|
||||
volumes:
|
||||
- ./etc/ssh:/etc/ssh/
|
||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||
- ./script/hostman.sh:/hostman.sh:ro
|
||||
- ./script/sqlproxy.sh:/sqlproxy.sh:ro
|
||||
- ./script/sqlproxy_cli.sh:/sqlproxy_cli.sh:ro
|
||||
- ./config:/config
|
||||
environment:
|
||||
DISABLE_KEYGEN: true
|
||||
DISABLE_CONFIG_GEN: true
|
||||
|
|
72
script/myssh
72
script/myssh
|
@ -4,9 +4,9 @@ if [ -z "$SQL_CLI_TEMPLATE" ]
|
|||
then
|
||||
if [ $(uname -s) = "Linux" ]
|
||||
then
|
||||
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 3306'
|
||||
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
|
||||
else
|
||||
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:3306\" -a \"Sequel Ace\"'
|
||||
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:6033\" -a \"Sequel Ace\"'
|
||||
fi
|
||||
else
|
||||
echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE"
|
||||
|
@ -18,7 +18,6 @@ else
|
|||
fi
|
||||
|
||||
[ -z $SQL_PROXY_HOST ] && SQL_PROXY_HOST="localhost"
|
||||
[ -z $SQL_PROXY_DB_PORT ] && SQL_PROXY_DB_PORT="3306"
|
||||
CONNECTION_CACHE="$HOME/.cache/sqlproxy_$SQL_PROXY_HOST"
|
||||
|
||||
HELP="Usage: myssh [ls|connect]\n
|
||||
|
@ -54,20 +53,41 @@ disconnect() {
|
|||
|
||||
port_forward() {
|
||||
ACTIVE_HOST=$(cat $CONNECTION_CACHE)
|
||||
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$SQL_PROXY_DB_PORT ]
|
||||
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$2 ]
|
||||
then
|
||||
echo "Reconnect"
|
||||
if [ ! -z $ACTIVE_HOST ]
|
||||
then
|
||||
ssh -O cancel -L 3306:$ACTIVE_HOST -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
|
||||
ssh -O cancel -L 6033:$ACTIVE_HOST -S $HOME/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST} q
|
||||
fi
|
||||
ssh -O forward -L 3306:$1:$SQL_PROXY_DB_PORT -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST
|
||||
echo $ACTIVE_HOST
|
||||
echo $1 $2
|
||||
ssh -O forward -L 6033:$1:$2 -S ${HOME}/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST}
|
||||
fi
|
||||
echo $1:$SQL_PROXY_DB_PORT > $CONNECTION_CACHE
|
||||
echo $2:$3 > "${CONNECTION_CACHE}"
|
||||
}
|
||||
|
||||
ls_hosts() {
|
||||
ssh -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST ls
|
||||
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" ls
|
||||
}
|
||||
|
||||
get_host() {
|
||||
if [ $1 == '' ]
|
||||
then
|
||||
printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n'
|
||||
else
|
||||
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1"
|
||||
fi
|
||||
}
|
||||
|
||||
set_host_env() {
|
||||
if [ $1 == 'mysql' | $1 == 'psql' ]
|
||||
then
|
||||
TARGET_HOST_TYPE=$1
|
||||
fi
|
||||
if [ $2 != '' ]
|
||||
then
|
||||
|
||||
fi
|
||||
}
|
||||
|
||||
run_client() {
|
||||
|
@ -86,25 +106,21 @@ case $MAIN_OPTION in
|
|||
# check if host is valid
|
||||
TARGET_HOST=$1
|
||||
shift
|
||||
ls_hosts | grep -qe "^$TARGET_HOST$"
|
||||
GREP_EXIT_CODE=$?
|
||||
if [ $GREP_EXIT_CODE -eq 0 ]
|
||||
then
|
||||
while getopts "u:p:" o
|
||||
do
|
||||
case "$o" in
|
||||
u) MYSQL_USERNAME="$OPTARG" ;;
|
||||
p) MYSQL_PASSWORD="$OPTARG" ;;
|
||||
esac
|
||||
done
|
||||
port_forward $TARGET_HOST
|
||||
if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ]
|
||||
then
|
||||
run_client
|
||||
fi
|
||||
else
|
||||
echo "Invalid Hostname: $TARGET_HOST."
|
||||
fi
|
||||
|
||||
while getopts "u:p:" o
|
||||
do
|
||||
case "$o" in
|
||||
u) MYSQL_USERNAME="$OPTARG" ;;
|
||||
p) MYSQL_PASSWORD="$OPTARG" ;;
|
||||
esac
|
||||
done
|
||||
TARGET_HOST_DATA="$(get_host "${TARGET_HOST}")"
|
||||
port_forward $TARGET_HOST_DATA
|
||||
# port_forward $TARGET_HOST
|
||||
# if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ]
|
||||
# then
|
||||
# run_client
|
||||
# fi
|
||||
;;
|
||||
disconnect)
|
||||
disconnect;;
|
||||
|
|
|
@ -1,18 +1,27 @@
|
|||
#!/usr/bin/env sh
|
||||
DB_DATA_FILE="${DB_DATA_FILE:-/config/sqlproxy.json}"
|
||||
|
||||
ls_hosts() {
|
||||
# the containers version of grep does not support perl regex so "[^ ]*(?= # Added by hostman)" does not work
|
||||
grep -e "# Added by hostman" /etc/hosts | grep -oe "^[^ ]* [^ ]*" | grep -oe "[^ ]*$"
|
||||
jq -r '.[].host' < "${DB_DATA_FILE}"
|
||||
}
|
||||
|
||||
get_host() {
|
||||
export HOST=$(echo "${SSH_ORIGINAL_COMMAND}" | cut -d ' ' -f2)
|
||||
if [ "${HOST}" != 'get' ]
|
||||
then
|
||||
jq -r 'first(.[] | select(.host == $ENV.HOST)) | [ .type, .ip, .port, .user, .password ] | join(" ")' < "${DB_DATA_FILE}"
|
||||
fi
|
||||
}
|
||||
|
||||
idle() {
|
||||
echo "Press CTRL C to quit this connection"
|
||||
printf "Press CTRL C to quit this connection\n"
|
||||
sleep infinity
|
||||
}
|
||||
|
||||
case "$SSH_ORIGINAL_COMMAND" in
|
||||
case "${SSH_ORIGINAL_COMMAND}" in
|
||||
"") idle;;
|
||||
ls) ls_hosts;;
|
||||
get*) get_host;;
|
||||
q|quit) exit 0;;
|
||||
*) exit 1;;
|
||||
esac
|
||||
|
|
|
@ -4,16 +4,29 @@ set -e
|
|||
WRAP_START='[\n'
|
||||
if ${EXCLUDE_USERPASS}
|
||||
then
|
||||
TEMPLATE=' { "type": "${LOCAL_DB_TYPE}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
|
||||
TEMPLATE=' { "ip": "${LOCAL_IP}", "type": "${LOCAL_DB_TYPE}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
|
||||
else
|
||||
TEMPLATE=' { "type": "${LOCAL_DB_TYPE}", "user": "${LOCAL_DB_USER}", "password": "${LOCAL_DB_PASSWORD}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
|
||||
TEMPLATE=' { "ip": "${LOCAL_IP}", "type": "${LOCAL_DB_TYPE}", "user": "${LOCAL_DB_USER}", "password": "${LOCAL_DB_PASSWORD}", "host": "${LOCAL_DB_HOST}", "port": "${LOCAL_DB_PORT}" }'
|
||||
fi
|
||||
SEPARATOR=',\n'
|
||||
WRAP_END='\n]'
|
||||
OUT="/config/sqlproxy.json"
|
||||
|
||||
label_hook() {
|
||||
if [ -z "{LOCAL_DB_PORT}" ]
|
||||
then
|
||||
if [ "${LOCAL_DB_TYPE}" = "mysql" ]
|
||||
then
|
||||
LOCAL_DB_PORT='3306'
|
||||
elif [ "${LOCAL_DB_TYPE}" = "psql" ]
|
||||
then
|
||||
LOCAL_DB_PORT='5432'
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
template_hook() {
|
||||
if grep -q '"type": "(?mysql|psql)".*"host": ".+", port: "\d+"' <<< "${PARTIAL_RESULT}"
|
||||
if [ "$(jq '((.type == "mysql") or (.type == "psql")) and (.host != "")' <<< "${PARTIAL_RESULT}" 2> /dev/null)" = true ]
|
||||
then
|
||||
return 0
|
||||
fi
|
||||
|
|
Loading…
Reference in a new issue