2024-01-01 11:21:22 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
2024-01-02 07:33:24 +01:00
|
|
|
function with_set_x() {
|
|
|
|
set -x
|
|
|
|
"$@"
|
|
|
|
{
|
|
|
|
ec=$?
|
|
|
|
set +x
|
|
|
|
return $ec
|
|
|
|
} 2>/dev/null
|
|
|
|
}
|
|
|
|
|
2024-01-10 11:54:33 +01:00
|
|
|
function log() {
|
|
|
|
echo "$*" >&2
|
|
|
|
}
|
|
|
|
|
2024-01-02 07:33:24 +01:00
|
|
|
ssh_cmd='ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
|
2024-01-10 11:54:33 +01:00
|
|
|
ssh_copy_id=false
|
2024-01-02 07:33:24 +01:00
|
|
|
args=()
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
arg=$1
|
|
|
|
shift
|
|
|
|
case "$arg" in
|
|
|
|
-v)
|
|
|
|
ssh_cmd+='-v'
|
|
|
|
;;
|
2024-01-10 11:54:33 +01:00
|
|
|
-r)
|
|
|
|
args+=("$arg" "$1")
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
;;
|
2024-01-02 07:33:24 +01:00
|
|
|
--copy-id)
|
|
|
|
ssh_copy_id=true
|
|
|
|
continue
|
2024-01-02 18:43:06 +01:00
|
|
|
;;
|
2024-01-10 11:54:33 +01:00
|
|
|
--server-py=*)
|
|
|
|
server_pyenv_ver="${arg#*=}"
|
|
|
|
continue
|
|
|
|
;;
|
|
|
|
--client-py=*)
|
|
|
|
client_pyenv_ver="${arg#*=}"
|
|
|
|
continue
|
|
|
|
;;
|
2024-01-02 18:43:06 +01:00
|
|
|
-6)
|
|
|
|
ipv6_only=true
|
|
|
|
continue
|
|
|
|
;;
|
2024-01-02 07:33:24 +01:00
|
|
|
--sshuttle-bin=*)
|
|
|
|
sshuttle_bin="${arg#*=}"
|
|
|
|
continue
|
|
|
|
;;
|
2024-01-02 18:43:06 +01:00
|
|
|
-*) ;;
|
2024-01-02 07:33:24 +01:00
|
|
|
*)
|
|
|
|
if [[ -z "$node" ]]; then
|
|
|
|
node=$arg
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
args+=("$arg")
|
|
|
|
done
|
2024-01-01 11:21:22 +01:00
|
|
|
|
2024-01-02 05:16:22 +01:00
|
|
|
port="2222"
|
2024-01-01 11:21:22 +01:00
|
|
|
user="test:test"
|
|
|
|
|
2024-01-02 07:33:24 +01:00
|
|
|
if [[ $node == node-* ]]; then
|
2024-01-10 11:54:33 +01:00
|
|
|
pycmd="/pyenv/shims/python"
|
|
|
|
ssh_cmd+=" -o SetEnv=PYENV_VERSION=${server_pyenv_ver:-'3'}"
|
|
|
|
args=("--python=$pycmd" "${args[@]}")
|
2024-01-02 07:33:24 +01:00
|
|
|
host=$("$(dirname "$0")/test-bed" get-ip "$node")
|
|
|
|
index=${node#node-}
|
2024-01-02 18:43:06 +01:00
|
|
|
if [[ $ipv6_only == true ]]; then
|
|
|
|
args+=("2001:0DB8::/112")
|
|
|
|
else
|
|
|
|
args+=("10.55.$index.0/24")
|
|
|
|
fi
|
2024-01-02 07:33:24 +01:00
|
|
|
else
|
|
|
|
host=$node
|
|
|
|
fi
|
|
|
|
|
2024-01-01 11:21:22 +01:00
|
|
|
if ! command -v sshpass >/dev/null; then
|
2024-01-10 11:54:33 +01:00
|
|
|
log "sshpass is not found. You might have to manually enter ssh password: 'test'"
|
2024-01-02 07:33:24 +01:00
|
|
|
user=${user%:*}
|
2024-01-01 11:21:22 +01:00
|
|
|
fi
|
|
|
|
|
2024-01-02 07:33:24 +01:00
|
|
|
if [[ $ssh_copy_id == true ]]; then
|
2024-01-10 11:54:33 +01:00
|
|
|
log "Trying to make it passwordless"
|
2024-01-02 07:33:24 +01:00
|
|
|
with_set_x ssh-copy-id -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p "$port" "$user@$host"
|
2024-01-01 11:21:22 +01:00
|
|
|
fi
|
|
|
|
|
2024-01-02 18:43:06 +01:00
|
|
|
if [[ -z $sshuttle_bin || "$sshuttle_bin" == dev ]]; then
|
2024-01-02 13:23:20 +01:00
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
export PYTHONPATH="."
|
2024-01-10 11:54:33 +01:00
|
|
|
if [[ -n $client_pyenv_ver ]]; then
|
|
|
|
log "Using pyenv version: $client_pyenv_ver"
|
|
|
|
command -v pyenv &>/dev/null || log "You have to install pyenv to use --client-py" && exit 1
|
|
|
|
sshuttle_cmd=(/usr/bin/env PYENV_VERSION="$client_pyenv_ver" pyenv exec python -m sshuttle)
|
|
|
|
else
|
|
|
|
log "Using best python version availble"
|
|
|
|
if [ -x "$(command -v python3)" ] &&
|
|
|
|
python3 -c "import sys; sys.exit(not sys.version_info > (3, 5))"; then
|
|
|
|
sshuttle_cmd=(python3 -m sshuttle)
|
|
|
|
else
|
|
|
|
sshuttle_cmd=(python -m sshuttle)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
[[ -n $client_pyenv_ver ]] && log "Can't specify --client-py when --sshuttle-bin is specified" && exit 1
|
|
|
|
sshuttle_cmd=("$sshuttle_bin")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ " ${args[*]} " != *" --ssh-cmd "* ]]; then
|
|
|
|
args=("--ssh-cmd" "$ssh_cmd" "${args[@]}")
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ " ${args[*]} " != *" -r "* ]]; then
|
|
|
|
args=("-r" "$user@$host:$port" "${args[@]}")
|
2024-01-02 13:23:20 +01:00
|
|
|
fi
|
|
|
|
|
2024-01-01 11:21:22 +01:00
|
|
|
set -x
|
2024-01-10 11:54:33 +01:00
|
|
|
"${sshuttle_cmd[@]}" --version
|
|
|
|
exec "${sshuttle_cmd[@]}" "${args[@]}"
|