-
Notifications
You must be signed in to change notification settings - Fork 6
/
dockernodeps
executable file
·87 lines (71 loc) · 2.1 KB
/
dockernodeps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env bash
# {{ ansible_managed }}
set -u
set -e
set -o pipefail
ALL=false
CONTAINERFOUND=false
# --------------------------------------------------------------------------------------
# parse parameters
if [[ $# = 0 ]] ; then
echo ""
echo "Usage:"
echo "$0 [-a] <SERVICE_NAME>"
echo ""
echo "Example:"
echo "$0 stack1_influxdb"
echo "$0 -a stack1_influxdb"
echo ""
echo "Returns the docker node-id and the container-id in format NODE-ID:CONTAINER-ID running the given service as parameter."
echo "If the option '-a' is not used, the search exits with the first pair found."
exit 1
fi
for PARAMETER in $@ ; do
case $1 in
-a)
ALL=true
shift
;;
*_*)
SERVICENAME=$1
shift
;;
*)
echo "$1 is not a valid parameter. Run $0 without parameters to get usage."
exit 1
;;
esac
done
# --------------------------------------------------------------------------------------
# check if ssh-agent contains key
if ! ssh-add -l >/dev/null ; then
echo ssh-agent does not contain key. Be sure to use key-forwarding with ssh.
exit 1
fi
# --------------------------------------------------------------------------------------
# --------------------------------------------------------------------------------------
# parse docker nodes running the service
for NODEID in $(docker node ls -q) ; do
if [[ $(docker service ps -f "node=$NODEID" --format "{{.CurrentState}}" "$SERVICENAME" 2>/dev/null | grep -cw 'Running') > 0 ]] ; then
NODENAME=$(docker node ls --format {{.Hostname}} -f id=$NODEID)
CONTAINERIDCOMMAND="docker ps -q -f status=running --filter name=$SERVICENAME 2>/dev/null"
CONTAINERID=$(ssh "$NODENAME" "$CONTAINERIDCOMMAND")
CONTAINERFOUND=true
if $ALL ; then
echo "${NODENAME}:${CONTAINERID}"
else
RETURN="${NODENAME}:${CONTAINERID}"
fi
fi
done
# --------------------------------------------------------------------------------------
# OUTPUT
if $CONTAINERFOUND ; then
if ! $ALL ; then
echo $RETURN
fi
exit 0
else
echo "no container with running $SERVICENAME found in this swarm."
exit 1
fi