Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Commit 203d3ae

Browse files
committed
Add host-scripts/expose_ports.sh
1 parent 8355054 commit 203d3ae

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

host-scripts/expose_ports.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
# Exit immediately if a command exits with a non-zero status
4+
set -e
5+
6+
# echo fn that outputs to stderr http://stackoverflow.com/a/2990533/511069
7+
echoerr() {
8+
cat <<< "$@" 1>&2;
9+
}
10+
11+
# print error and exit
12+
die () {
13+
echoerr "ERROR: $1"
14+
# if $2 is defined AND NOT EMPTY, use $2; otherwise, set to "3"
15+
errnum=${2-3}
16+
exit $errnum
17+
}
18+
19+
# Required params
20+
[ -z "${1}" ] && die "Need at least 1 port number argument!"
21+
MYAPP_PORTS=$@
22+
23+
[ -z "${SSH_HOME}" ] && die "Need environment variable \$SSH_HOME set!"
24+
[ -z "${CONTAINER_IP}" ] && die "Need environment variable \$CONTAINER_IP set!"
25+
[ -z "${SSHD_PORT}" ] && die "Need environment variable \$SSHD_PORT set!"
26+
BASE_SSH_CMD="-p ${SSHD_PORT} -o StrictHostKeyChecking=no -N -fn application@${CONTAINER_IP}"
27+
28+
## convert space separated string into a bash array
29+
IFS=' ' read -a MYAPP_PORTS <<< "${MYAPP_PORTS}"
30+
declare -a MYAPP_PORTS=${MYAPP_PORTS}
31+
32+
# to setup tunnels first kill existing tunnels if any
33+
# then set them up in the background
34+
for i in "${MYAPP_PORTS[@]}"; do
35+
kill $(lsof -i tcp:${i} -F p | cut -b 2-) >/dev/null 2>&1 || true
36+
ssh-keygen -f "${SSH_HOME}/.ssh/known_hosts" -R [${CONTAINER_IP}]:${SSHD_PORT} >/dev/null 2>&1 || true
37+
ssh ${BASE_SSH_CMD} -R localhost:${i}:localhost:${i} &
38+
done

0 commit comments

Comments
 (0)