Guest User

Untitled

a guest
Nov 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Default settings block, change here if you need or put other defaults to your .bashrc
  5. #
  6.  
  7. DSFC_USER="${DSFC_USER:-root}" # defaults to root
  8. DSFC_HOSTS_GREP="${DSFC_HOSTS_GREP:-"swarm-"}" # what to grep in /etc/hosts
  9. DSFC_HOSTS_LIST="${DSFC_HOSTS_LIST:-`(cat /etc/hosts | grep ${DSFC_HOSTS_GREP} | cut -f1 | xargs -L1 echo | uniq)`}" # change the default inside backticks to your way to get hosts list
  10. DSFC_METHOD_SSH="${DSFC_METHOD_SSH:-1}" # whether to use the ssh and docker ps method
  11. DSFC_METHOD_SSH_CONNECT_TIMEOUT="${DSFC_METHOD_SSH_CONNECT_TIMEOUT:-5}" # connection timeout for the ssh method
  12. DSFC_METHOD_DSM="${DSFC_METHOD_DSM:-1}" # whether to fallback to the local docker swarm manager capabilities
  13. DSCF_METHOD_DSM_SERVICE="${DSCF_METHOD_DSM_SERVICE:-.}" # sets a grep pattern for service name too look the container among - speeds up docker swarm manager method.
  14.  
  15. #
  16. # Usage printing block
  17. #
  18.  
  19. read -r -d '' usage << EOM
  20. Finds container by id on a predefined hosts list, since swarm does not make it easy. Assuming you have ssh access to the swarm nodes list or running on a manager node.
  21.  
  22. Usage:
  23. $(basename "$0") [-h|--help] - this message
  24. $(basename "$0") CONTAINER_ID_OR_NAME [SERVICE_NAME] - this is basically strings passed to grep on each host. Set SERVICE_NAME if known to speed up search in Docker Swarm Manager node mode.
  25.  
  26. Environment variables:
  27. DSFC_HOSTS_LIST=see-explanation
  28. list of hosts to connect to for the "ssh docker ps" method. Defaults to all records from "/etc/hosts" having substring defined in DSFC_HOSTS_GREP.
  29. DSFC_HOSTS_GREP=swarm-
  30. if DSFC_HOSTS_LIST is not set, this variable will be used as the grep pattern to filter hosts from "/etc/hosts"
  31. DSFC_USER=root
  32. user to connect to the hosts on the list.
  33. DSFC_METHOD_SSH_CONNECT_TIMEOUT=5
  34. ssh connect timeout when using "ssh docker ps" method
  35. DSFC_METHOD_SSH=1
  36. whether to use the "ssh docker ps" method at all
  37. DSFC_METHOD_DSM=1
  38. whether to use the "docker swarm manager node" method at all
  39. DSCF_METHOD_DSM_SERVICE=.
  40. if defined, SERVICE_NAME is set to this var value if not implicitly set on command line
  41.  
  42. Workflow:
  43. Tries the "ssh docker ps" method first, since it's way faster. If the first method failed or disabled, tries "docker swarm manager node" method, that may take a while.
  44.  
  45.  
  46. EOM
  47.  
  48. if [[ $# -eq 0 ]] ; then
  49. echo "$usage"
  50. exit 1
  51. fi
  52.  
  53. case "$1" in
  54. -h|--help) printf "$usage"
  55. exit 0
  56. ;;
  57. esac
  58.  
  59. if [ ! -z "$2" ]; then
  60. DSCF_METHOD_DSM_SERVICE="$2"
  61. fi
  62.  
  63. #
  64. # Trying using the "ssh and docker" ps method
  65. #
  66.  
  67. DSFC_METHOD_SSH_RESULT=-1 # using unique simple numbers
  68.  
  69. if (( DSFC_METHOD_SSH == 1 )) ; then
  70.  
  71. DSFC_METHOD_SSH_RESULT=0
  72.  
  73. for DSFC_HOST in $DSFC_HOSTS_LIST ; do ssh -o ConnectTimeout=$DSFC_METHOD_SSH_CONNECT_TIMEOUT $DSFC_USER'@'$DSFC_HOST 'docker ps | grep '$1' | grep '$DSCF_METHOD_DSM_SERVICE' |sed "s/.*/$(hostname)/"|sort|uniq' || DSFC_METHOD_SSH_RESULT=$? ; done
  74.  
  75. if [ -z "$DSFC_HOSTS_LIST" ]; then # if no hosts were found, consider error
  76. DSFC_METHOD_SSH_RESULT=-3 # using unique simple numbers
  77. fi
  78.  
  79. fi
  80.  
  81. #
  82. # Trying using the slower "swarm manager method" if allowed and ssh method didn't work
  83. #
  84.  
  85. DSFC_METHOD_DSM_RESULT=1 # if not trying - do not interfere with previous result
  86. if ((DSFC_METHOD_DSM = 1 && DSFC_METHOD_SSH_RESULT != 0)) ; then
  87.  
  88. RW_DSM="$(docker node ls)"
  89. if [[ $? -eq 0 ]] ; then
  90.  
  91. SEARCHFOR="$(docker service list --format '{{.Name}}' | tr '\n' ' ' | grep $DSCF_METHOD_DSM_SERVICE )"; ((for f in $(docker service ps -q $SEARCHFOR); do docker inspect --format '{{.Status.ContainerStatus.ContainerID}} {{.NodeID}}' $f; done) | grep $1 | cut -f2 -d' ' | xargs docker node inspect --format "{{.Description.Hostname}}") | sort | uniq
  92.  
  93. DSFC_METHOD_DSM_RESULT=$?
  94.  
  95. else
  96. DSFC_METHOD_DSM_RESULT=1 # if not on a docker swarm node, use previous exit codes
  97. fi
  98.  
  99. fi
  100.  
  101. exit $(( DSFC_METHOD_DSM_RESULT * DSFC_METHOD_SSH_RESULT )) # we have simple numbers, so unique exit code, but maybe negative
Add Comment
Please, Sign In to add comment