Skip to content

Commit

Permalink
add proxy/bastion for connection
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyLuLiu committed Sep 27, 2024
1 parent 5a38b3a commit 5c648f3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/common/remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,24 @@ check_connection() {
# Define remote connection
uri () {
local remote="${TARGET_HOST_USERNAME}@${TARGET_HOST}"
if [[ ! -z "${TARGET_HOST_DOMAIN+x}" ]]; then
if [[ -n "${TARGET_HOST_DOMAIN}" ]]; then
remote="${TARGET_HOST_USERNAME}@${TARGET_HOST_DOMAIN}@${TARGET_HOST}"
fi
echo "${remote}"
}
# Define the host bastion
proxy () {
local proxy="${PROXY_USERNAME}@${PROXY_HOST}"
echo "${remote}"
}

# Generate SCP command
# $1 local path
# $2 remote path
scp_to_cmd () {
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
if [[ -n "${PROXY_HOST}" && -n "${PROXY_USERNAME}" ]]; then
echo "scp -r $(connect_options) -J $(proxy) ${1} $(uri):${2}"
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
echo "scp -r $(connect_options) -i ${TARGET_HOST_KEY_PATH} ${1} $(uri):${2}"
else
echo "sshpass -p ${TARGET_HOST_PASSWORD} scp -r $(connect_options) ${1} $(uri):${2}"
Expand All @@ -84,7 +91,9 @@ scp_to_cmd () {
# $1 remote path
# $2 local path
scp_from_cmd () {
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
if [[ -n "${PROXY_HOST}" && -n "${PROXY_USERNAME}" ]]; then
echo "scp -r $(connect_options) -J $(proxy) $(uri):${1} ${2}"
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
echo "scp -r $(connect_options) -i ${TARGET_HOST_KEY_PATH} $(uri):${1} ${2}"
else
echo "sshpass -p ${TARGET_HOST_PASSWORD} scp -r $(connect_options) $(uri):${1} ${2}"
Expand All @@ -94,16 +103,19 @@ scp_from_cmd () {
# Generate SSH command
ssh_cmd () {
cmd=""
if [[ ! -z "${TARGET_HOST_KEY_PATH+x}" ]]; then
if [[ -n "${PROXY_HOST}" && -n "${PROXY_USERNAME}" ]]; then
cmd="ssh $(connect_options) -J $(proxy) $(uri) "
elif [[ -n "${TARGET_HOST_KEY_PATH}" ]]; then
cmd="ssh $(connect_options) -i ${TARGET_HOST_KEY_PATH} $(uri) "
else
cmd="sshpass -p ${TARGET_HOST_PASSWORD} ssh $(connect_options) $(uri) "
fi

# On AWS MacOS ssh session is not recognized as expected
if [[ ${OS} == 'darwin' ]]; then
cmd+="sudo su - ${TARGET_HOST_USERNAME} -c \"PATH=\$PATH:/usr/local/bin && $@\""
else
cmd+="$@"
fi
echo "${cmd}"
}
}

0 comments on commit 5c648f3

Please sign in to comment.