Add postgres cli support.

This commit is contained in:
Kevin Baensch 2023-01-05 09:48:19 +01:00
parent 9083bfc5ad
commit 264cf17de5
3 changed files with 28 additions and 23 deletions

View file

@ -72,7 +72,6 @@ do
then
LOCAL_IP=$(jq -r '.NetworkSettings.Networks | if has($ENV.NETWORK_NAME) then .[$ENV.NETWORK_NAME].IPAddress else first(.[].IPAddress) end' <<< "${CONTAINER_DATA}");
fi
# export LOCAL_IP
# Read Label values
CONTAINER_LABELS=$(jq '.Labels' <<< "${CONTAINER_DATA}");

View file

@ -15,7 +15,7 @@ SYNTAX connect host [-u user] [-p password] [-c client]
get_template_string() {
if [ -z "$SQL_CLI_TEMPLATE" ]
then
if [ $(uname -s) = "Linux" ]
if [ "$(uname -s)" = "Linux" ]
then
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
else
@ -33,24 +33,23 @@ get_template_string() {
}
ssh_status() {
ssh -O check -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST > /dev/null 2>&1
echo $?
ssh -O check -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" > /dev/null 2>&1
}
connect() {
mkdir -p $HOME/.ssh/controlmasters
mkdir -p "${HOME}/.ssh/controlmasters"
if [ $(ssh_status) -ne 0 ]
if ! ssh_status
then
echo "" > $CONNECTION_CACHE
ssh -o "ControlPersist=10m" -M -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
echo "" > "${CONNECTION_CACHE}"
ssh -o "ControlPersist=10m" -M -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" q
fi
}
disconnect() {
if [ $(ssh_status) -eq 0 ]
if ssh_status
then
ssh -O stop -S $HOME/.ssh/controlmasters/%r@%h:%p $SQL_PROXY_HOST q
ssh -O stop -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" q
fi
}
@ -59,32 +58,32 @@ disconnect() {
# $1 - target ip
# $2 - target port
port_forward() {
ACTIVE_HOST=$(cat $CONNECTION_CACHE)
ACTIVE_HOST=$(cat "${CONNECTION_CACHE}")
if [ -z "${ACTIVE_HOST}" ] || [ "${ACTIVE_HOST}" != "$1:$2" ]
then
if [ -n "${ACTIVE_HOST}" ]
then
ssh -O cancel -L 6033:${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 6033:$1:$2 -S ${HOME}/.ssh/controlmasters/%r@%h:%p ${SQL_PROXY_HOST}
ssh -O forward -L "6033:$1:$2" -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}"
fi
echo "$1:$2" > "${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 = '' ]
if [ "$1" = '' ]
then
printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n'
exit 1
else
TARGET_HOST_DATA=$(ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1")
TARGET_HOST_DATA=$(ssh -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" "get $1")
if [ "${TARGET_HOST_DATA}" = '' ]
then
printf 'No such host: "%s"\n' $1
printf 'No such host: "%s"\n' "$1"
exit 1
fi
fi
@ -98,7 +97,7 @@ get_host() {
# $4 - username (optional)
# $5 - password (optional)
set_host_env() {
if [ "$1" = 'mysql' ] || [ "$1" = 'psql' ]
if [ "$1" = 'mysql' ] || [ "$1" = 'postgres' ]
then
TARGET_HOST_TYPE="$1"
else
@ -147,14 +146,20 @@ run_client() {
else
SHOW_CLI_HELP=true
fi
elif [ "${$1}" = 'psql' ]
elif [ "$1" = 'postgres' ]
then
if which psql >/dev/null 2>&1
then
psql "postgresql://$2:$3@${SQL_PROXY_HOST}:6033/postgres"
else
SHOW_CLI_HELP=true
fi
fi
if [ "${SHOW_CLI_HELP}" = true ]
then
printf 'No %s client binary found.\nYou can maually establish a connection using the following data.\nhost:\t%s\nport:\t6033\nuser:\t%s\npassword:\t%s\n' "$1" "${SQL_PROXY_HOST}" '3306' "$2" "$3"
printf 'No %s client binary found.\nYou can maually establish a connection using the following data.\n' "$1"
printf 'host:\t%s\nport:\t%s\nuser:\t%s\npassword:\t%s\n' "${SQL_PROXY_HOST}" '6033' "$2" "$3"
fi
}
@ -188,6 +193,7 @@ case "${MAIN_OPTION}" in
esac
done
get_host "${TARGET_HOST}"
# Do not quote this.
set_host_env ${TARGET_HOST_DATA}
port_forward "${TARGET_HOST_IP}" "$TARGET_HOST_PORT"
if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ]

View file

@ -18,7 +18,7 @@ label_hook() {
if [ "${LOCAL_DB_TYPE}" = "mysql" ]
then
LOCAL_DB_PORT='3306'
elif [ "${LOCAL_DB_TYPE}" = "psql" ]
elif [ "${LOCAL_DB_TYPE}" = "postgres" ]
then
LOCAL_DB_PORT='5432'
fi
@ -26,7 +26,7 @@ label_hook() {
}
template_hook() {
if [ "$(jq '((.type == "mysql") or (.type == "psql")) and (.host != "")' <<< "${PARTIAL_RESULT}" 2> /dev/null)" = true ]
if [ "$(jq '((.type == "mysql") or (.type == "postgres")) and (.host != "")' <<< "${PARTIAL_RESULT}" 2> /dev/null)" = true ]
then
return 0
fi