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 then
LOCAL_IP=$(jq -r '.NetworkSettings.Networks | if has($ENV.NETWORK_NAME) then .[$ENV.NETWORK_NAME].IPAddress else first(.[].IPAddress) end' <<< "${CONTAINER_DATA}"); LOCAL_IP=$(jq -r '.NetworkSettings.Networks | if has($ENV.NETWORK_NAME) then .[$ENV.NETWORK_NAME].IPAddress else first(.[].IPAddress) end' <<< "${CONTAINER_DATA}");
fi fi
# export LOCAL_IP
# Read Label values # Read Label values
CONTAINER_LABELS=$(jq '.Labels' <<< "${CONTAINER_DATA}"); CONTAINER_LABELS=$(jq '.Labels' <<< "${CONTAINER_DATA}");

View file

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

View file

@ -18,7 +18,7 @@ label_hook() {
if [ "${LOCAL_DB_TYPE}" = "mysql" ] if [ "${LOCAL_DB_TYPE}" = "mysql" ]
then then
LOCAL_DB_PORT='3306' LOCAL_DB_PORT='3306'
elif [ "${LOCAL_DB_TYPE}" = "psql" ] elif [ "${LOCAL_DB_TYPE}" = "postgres" ]
then then
LOCAL_DB_PORT='5432' LOCAL_DB_PORT='5432'
fi fi
@ -26,7 +26,7 @@ label_hook() {
} }
template_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 then
return 0 return 0
fi fi