Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a port forwarding script for exposing deployed containers to local ports #729

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions infrastructure/port-forward.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
set -euo pipefail

# ANSI color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

if [ "$#" -ne 3 ]; then
echo -e "${RED}Usage: ./port-forward <target server address> <container:port> <local port>${NC}"
exit 1
fi

TARGET_SERVER="$1"
CONTAINER_PORT="$2"
LOCAL_PORT="$3"

TARGET_CONTAINER_NAME=$(echo $CONTAINER_PORT | cut -d: -f1)
PORT=$(echo $CONTAINER_PORT | cut -d: -f2)

# Generate the socat container's name
CONTAINER_NAME="tunnel_${TARGET_CONTAINER_NAME}_$(whoami)"

# Generate a random port between 30000 and 60000 for socat inside the host machine
SOCAT_PORT=$(( 30000 + RANDOM % 30001 ))

echo -e "${YELLOW}Setting up port forwarding...${NC}"
echo -e "Local Port: ${GREEN}$LOCAL_PORT${NC}"
echo -e "Target Server: ${GREEN}$TARGET_SERVER${NC}"
echo -e "Container and Port: ${GREEN}$TARGET_CONTAINER_NAME:$PORT${NC}"
echo -e "Internal socat Port on Host: ${GREEN}$SOCAT_PORT${NC}"
echo -e "Socat Container Name: ${GREEN}$CONTAINER_NAME${NC}"

ssh -tL $LOCAL_PORT:localhost:$SOCAT_PORT root@$TARGET_SERVER \
'docker run --rm --name '$CONTAINER_NAME' --network=opencrvs_overlay_net --publish '$SOCAT_PORT:$SOCAT_PORT' alpine/socat tcp-listen:'$SOCAT_PORT',fork,reuseaddr tcp-connect:'$TARGET_CONTAINER_NAME:$PORT''

echo -e "${GREEN}Port forwarding established and tunnel is online! Press Ctrl+C to close.${NC}"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"data-generator": "ts-node -r tsconfig-paths/register -T src/data-generator/index.ts",
"data-generator:generate-types": "graphql-codegen --config codegen.yml && yarn prettier --write src/data-generator/gateway.ts",
"deploy": "bash infrastructure/deploy.sh",
"port-forward": "bash infrastructure/port-forward.sh",
"validate-translations": "ts-node src/validate-translations.ts"
},
"devDependencies": {
Expand Down
Loading