I am using CoreOS to schedule systemd units with fleet. I have two units (firehose.service
and firehose-announce.service
. I am trying to get the firehose-announce.service
to start and stop along with the firehose.service
. Here is the unit file for firehose-announce.service
:
[Unit]
Description=Firehose etcd announcer
BindsTo=firehose@%i.service
After=firehose@%i.service
Requires=firehose@%i.service
[Service]
EnvironmentFile=/etc/environment
TimeoutStartSec=30s
ExecStartPre=/bin/sh -c 'sleep 1'
ExecStart=/bin/sh -c "port=$(docker inspect -f '{{range $i, $e := .NetworkSettings.Ports }}{{$p := index $e 0}}{{$p.HostPort}}{{end}}' firehose-%i); echo -n \"Adding socket $COREOS_PRIVATE_IPV4:$port/tcp to /firehose/upstream/firehose-%i\"; while netstat -lnt | grep :$port >/dev/null; do etcdctl set /firehose/upstream/firehose-%i $COREOS_PRIVATE_IPV4:$port --ttl 300 >/dev/null; sleep 200; done"
RestartSec=30s
Restart=on-failure
[X-Fleet]
X-ConditionMachineOf=firehose@%i.service
I am trying to use BindsTo
with the notion that start and stop of firehose.service
will also start or stop firehose-announce.service
. But this never happens correctly. If firehose.service
is stopped, then firehose-announce.service
goes to failed state. But when I start firehose.service
, the firehose-announce.service
doesn't start up.
What am I doing wrong here?
I seem to have finally stumbled on the correct combination to get this working as desired.
In my
firehose-announce.service
unit I only set aBindsTo
. The entire unit is:This will cause the
firehose-announce.service
unit to stop whenfirehose.service
does. Great. But how do we start it up again?I reverse the dependency to be in my
firehose.service
unit like so:This is saying that
firehose.service
wantsfirehose-announce.service
to start up when it does (but don't fail iffirehose-announce.service
can't start). It also makes surefirehose.service
starts beforefirehose-announce.service
.I tested this and the units now seem to stop and start together as desired.