Skip to content
static-git-discovery@.service 805 B
Newer Older
Pierre Ozoux's avatar
Pierre Ozoux committed
[Unit]
Description=%p for %i etcd registration

# Requirements
Requires=etcd.service

Pierre Ozoux's avatar
Pierre Ozoux committed
# Dependency ordering
Pierre Ozoux's avatar
Pierre Ozoux committed
After=etcd.service
After=static-git@%i.service
Pierre Ozoux's avatar
Pierre Ozoux committed

# Dependency binding
BindsTo=static-git@%i.service
Pierre Ozoux's avatar
Pierre Ozoux committed

[Service]
# Start
## Test whether service is accessible and then register useful information
ExecStart=/bin/bash -euxc ' \
Pierre Ozoux's avatar
Pierre Ozoux committed
  sleep 3; \
  while true; do \
    app=`echo %p | cut -d"-" -f1,2`; \
Pierre Ozoux's avatar
Pierre Ozoux committed
    ip=`docker inspect --format \'{{.NetworkSettings.IPAddress}}\' $app-%i`; \
    curl -f $ip; \
    if [ $? -eq 0 ]; then \
      etcdctl set /services/$app/%i \'{"ip":"\'$ip\'", "port":"80"}\' --ttl 60; \
Pierre Ozoux's avatar
Pierre Ozoux committed
    else \
      etcdctl rm /services/$app/%i; \
    fi; \
    sleep 50; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  done'

# Stop
ExecStop=/bin/bash -euxc ' \
Pierre Ozoux's avatar
Pierre Ozoux committed
  app=`echo %p | cut -d"-" -f1`; \
Pierre Ozoux's avatar
Pierre Ozoux committed
  /usr/bin/etcdctl rm /services/$app/%i