Seems similar! Mine is for my specific setup: I have different machines on home vs work, so I wanted it to Just Work. And it's one file:
#! /bin/sh -e
REMOTES="workserver:22 homeserver:22 bouncehost:2222"
# Figure out likely remotes by looking for ssh ControlPath vars, as
# per this cantrip in ssh_config:
# # Reuse ssh tunnels if possible.
# Host *
# ControlMaster auto
# ControlPath ~/.cache/ssh-%r@%h:%p
# ControlPersist 10m
find_remote()
{
for r in $REMOTES; do
if [ -S ~/.cache/ssh-`whoami`@$r ]; then
echo $r
return
fi
done
# Nothing cached, try ping.
for r in $REMOTES; do
host=$(echo $r | cut -d: -f1)
if ping -c 1 $host > /dev/null 2>&1; then
echo $r
return
fi
done
echo "No reachable remote" >&2
exit 1
}
# Make sure we're in a git dir!
[ -d .git ] || (echo "Not a git dir!"; exit 1)
PWD=$(pwd -P)
REMOTE=$(find_remote)
echo "rcargo: remote $REMOTE"
HOST=$(echo $REMOTE | cut -d: -f1)
PORT=$(echo $REMOTE | cut -d: -f2)
# Make sure directory exists
ssh "$HOST" -p $PORT "mkdir -p '$PWD'"
# Sync source (exclude junk)
rsync -az --delete \
-e "ssh -p $PORT" \
--exclude .git \
--exclude target \
"$PWD/" "$HOST:$PWD/"
ssh $HOST -p $PORT ". ~/.profile && cd '$PWD' && cargo $*"
rsync -az --delete \
-e "ssh -p $PORT" \
"$HOST:$PWD/target/" "$PWD/target/"