#!/bin/bash
# this command helper depends on key-based (passwordless) SSH access by the current user to each of the docker swarm nodes.
NODE_HOST=$1; shift;

nc -zvw3 $NODE_HOST 22 &> /dev/null;
PING_RESULT=$?

if [ $PING_RESULT != 0 ]; then
  echo
  echo "Host '$NODE_HOST' not reachable."
  exit 2
fi

# connect to the docker host through SSH
export DOCKER_HOST="ssh://$(whoami)@$NODE_HOST.$(hostname -d)"
# execute the command
docker $@