-
Notifications
You must be signed in to change notification settings - Fork 74
/
docker-entrypoint.sh
91 lines (75 loc) · 2.11 KB
/
docker-entrypoint.sh
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
86
87
88
89
90
#!/bin/sh
set -e
if [ "$1" = 'start' ]; then
CONTAINERS_FOLDER=/tmp/containers
NAMED_PIPE=/tmp/pipe
setConfiguration() {
KEY=$1
VALUE=$2
sed -i "s/{{$KEY}}/$VALUE/g" ${FILEBEAT_HOME}/filebeat.yml
}
getRunningContainers() {
curl --no-buffer -s -XGET --unix-socket /tmp/docker.sock http:/containers/json | python -c "
import json, sys
containers=json.loads(sys.stdin.readline())
for container in containers:
print(container['Id'])
"
}
getContainerName() {
curl --no-buffer -s -XGET --unix-socket /tmp/docker.sock http:/containers/$1/json | python -c "
import json, sys
container=json.loads(sys.stdin.readline())
print(container['Name'])
" | sed 's;/;;'
}
createContainerFile() {
touch "$CONTAINERS_FOLDER/$1"
}
removeContainerFile() {
rm "$CONTAINERS_FOLDER/$1"
}
collectContainerLogs() {
local CONTAINER=$1
echo "Processing $CONTAINER..."
createContainerFile $CONTAINER
CONTAINER_NAME=`getContainerName $CONTAINER`
curl -s --no-buffer -XGET --unix-socket /tmp/docker.sock "http:/containers/$CONTAINER/logs?stderr=1&stdout=1&tail=1&follow=1" | sed "s;^;[$CONTAINER_NAME] ;" > $NAMED_PIPE
echo "Disconnected from $CONTAINER."
removeContainerFile $CONTAINER
}
if [ -n "${LOGSTASH_HOST+1}" ]; then
setConfiguration "LOGSTASH_HOST" "$LOGSTASH_HOST"
else
echo "LOGSTASH_HOST is needed"
exit 1
fi
if [ -n "${LOGSTASH_PORT+1}" ]; then
setConfiguration "LOGSTASH_PORT" "$LOGSTASH_PORT"
else
echo "LOGSTASH_PORT is needed"
exit 1
fi
if [ -n "${SHIPPER_NAME+1}" ]; then
setConfiguration "SHIPPER_NAME" "$SHIPPER_NAME"
else
setConfiguration "SHIPPER_NAME" "`hostname`"
fi
rm -rf "$CONTAINERS_FOLDER"
rm -rf "$NAMED_PIPE"
mkdir "$CONTAINERS_FOLDER"
mkfifo -m a=rw "$NAMED_PIPE"
echo "Initializing Filebeat..."
cat $NAMED_PIPE | ${FILEBEAT_HOME}/filebeat -e -v &
while true; do
CONTAINERS=`getRunningContainers`
for CONTAINER in $CONTAINERS; do
if ! ls $CONTAINERS_FOLDER | grep -q $CONTAINER; then
collectContainerLogs $CONTAINER &
fi
done
sleep 5
done
else
exec "$@"
fi