0

I am currently working on logging with the ELK (Elasticsearch, Logstash, Kibana) stack in an CoreOS cluster. I found out, you can use gelf to automatically send the logs from docker to logstash. I am using a sidekick to write a etcd variable of the logstash container IP address. Since I have to pass the IP of logstash to a flag in the docker run command I need to wait until the variable is set.

My question is how can I wait until etcdctl get /network/logstash returns the IP address.

This is how my unit-file looks at the moment:

[Unit]
Description=nginx with elk logging
After=logstash-discovery.service
Requires=docker.service

[Service]
TimeoutStartSec=0
ExecStartPre=/usr/bin/bash -c "/usr/bin/systemctl set-environment ADDR=$(etcdctl get /network/logstash)"
ExecStartPre=-/usr/bin/docker kill nginx1
ExecStartPre=-/usr/bin/docker rm nginx1
ExecStartPre=/usr/bin/docker pull 10.0.2.2:5000/nginx
ExecStart=/usr/bin/docker run -p 80:80 --name=nginx1 --net=host --log-driver=gelf --log-opt gelf-address=udp://${ADDR}:12201 --log-opt gelf-tag="fe" 10.0.2.2:5000/nginx
ExecStop=/usr/bin/docker stop nginx1

[X-Fleet]
Conflicts=logstash.service
Daniel Müssig
  • 1,582
  • 1
  • 14
  • 26

2 Answers2

1

The solution was to use etcdctl watch instead of etcdctl get but checking before if the variable isn't already set. So the full line looks like this:

ExecStartPre=/usr/bin/bash -c \
"if [ -z $(etcdctl get /network/logstash) ]; \
then /usr/bin/systemctl set-environment ADDR=$(etcdctl watch /network/logstash); \
else /usr/bin/systemctl set-environment ADDR=$(etcdctl get /network/logstash); \
fi"
Daniel Müssig
  • 1,582
  • 1
  • 14
  • 26
0

Your problem boils down to a question about bash programming: How do you wait until a variable is set before proceeding?

A variation of that question is already answered at How to create a loop in bash that is waiting for a webserver to respond?.

This takes a bit more than one line of code usually, so you may need to put the logic in a file and point ExecStartPre to that instead of using a one-liner.

Community
  • 1
  • 1
Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • 1
    I had already something like that in ExecStartPre but nothing changed. It was a while loop: while ${ADDR} == "" or is it an other value when ${ADDR} is empty – Daniel Müssig Jun 29 '16 at 18:57