Skip to content

Instantly share code, notes, and snippets.

@rohantmp
Last active May 12, 2021 13:52
Show Gist options
  • Save rohantmp/fc22dc42c40c2647239d69be5dd48910 to your computer and use it in GitHub Desktop.
Save rohantmp/fc22dc42c40c2647239d69be5dd48910 to your computer and use it in GitHub Desktop.
dev-scripts proxy automation.
# I use ~/.ssh/config to create ssh aliases for my machines.
# script requires passwordless ssh access
# script requires tmux
# After sourcing the script, just call rhhi_cluster_env_init, this will create a proxy to the host on 8053 in a tmux session and copy the kubeconfig from the host
# Once inited, other terminals can be brought into the env using rhhi_cluster_env which sets the env vars `https_proxy` and KUBECONFIG
export RHHI_REMOTE=root@bm9
# the ocp installer asset dir
OCP_LOCATION="/root/dev-scripts/ocp/ostest"
function rhhi_cluster_env {
export https_proxy=SOCKS5://localhost:8053
export KUBECONFIG=/home/${USER}/.kube/rhhi-config
}
function is_rhhi_proxy_running {
ps -ef|grep "ssh $RHHI_REMOTE -D 8053"|grep -v grep &&
echo "proxy is already running"
}
function is_rhhi_proxy_listening {
PROXY=$(netstat -tnlp 2>/dev/null |grep 8053|grep -v tcp6)
RUNNING=$(echo ${PROXY}|awk '{ split($4,a,":"); print a[2] " " $6;}')
test "${RUNNING}" = "8053 LISTEN" &&
echo "the proxy is listening at $(echo ${PROXY}|awk '{ print $4 }')"
}
function rhhi_run_proxy {
tmux kill-session -t rhhi-proxy 2>/dev/null
tmux new -s rhhi-proxy -d "ssh ${RHHI_REMOTE} -D 8053"
for i in {1..200}
do
echo -ne "[Proxy check $i]\r"
sleep 5
is_rhhi_proxy_listening && break
done
}
## The main function
function rhhi_cluster_env_init {
rhhi_cluster_env
(is_rhhi_proxy_running && is_rhhi_proxy_listening) ||
rhhi_run_proxy
oc get nodes ||
rsync ${RHHI_REMOTE}:${OCP_LOCATION}/auth/kubeconfig /home/${USER}/.kube/rhhi-config
}
@rohantmp
Copy link
Author

rohantmp commented Aug 7, 2019

I put this in ~/.bashrc.d and run rhhi_cluster_env_init when I want to start the proxy server and rhhi_cluster_env when I want to configure the current env to use an already existing one.

I use this to automatically proxy *.test.metalkube.org through this in firefox.

The only thing you'll have to change are the RHHI_REMOTE, DEV_SCRIPTS_LOCATION vars.

This assumes that dev-scripts has been launched for /root/dev-scripts on your remote machine and you have passwordless ssh.

@rohantmp
Copy link
Author

Updated, dev-scripts now uses ocp/ostest/auth/kubeconfig instead of ocp/auth/kubeconfig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment