Skip to content

Instantly share code, notes, and snippets.

@cgswong
Created January 21, 2015 23:10
Show Gist options
  • Save cgswong/4940c91559419c92b935 to your computer and use it in GitHub Desktop.
Save cgswong/4940c91559419c92b935 to your computer and use it in GitHub Desktop.
Consul Server bootstrap using Fleet and bash scripting
- name: [email protected]
content: |
[Unit]
Description=Consul Server
# Requirements
Requires=docker.service
Requires=fleet.service
Requires=consul-discovery@%i.service
# Dependency ordering and binding
After=docker.service
After=fleet.service
Before=consul-discovery@%i.service
[Service]
TimeoutStartSec=0
## TimeoutStopSec=90s
Restart=on-failure
RestartSec=10s
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill %p
ExecStartPre=-/usr/bin/docker rm %p
ExecStartPre=/usr/bin/docker pull progrium/consul
ExecStart=/usr/bin/bash -c '\
log_tag="%p"; \
if [ %i -eq 1 ]; then \
logger -t "$log_tag" "Starting initial consul server at ${COREOS_PRIVATE_IPV4}"; \
$(docker run --rm -e EXPECT=3 progrium/consul cmd:run ${COREOS_PRIVATE_IPV4} --name %p -v /mnt:/data); \
else \
primary_ip=$(fleetctl list-units --fields=unit,sub,machine | grep -E "%[email protected]" | sed -r "s/.*\/(.+?)/\1/"); \
logger -t "$log_tag" "Starting secondary consul server: $primary_ip"; \
$(docker run --rm progrium/consul cmd:run ${COREOS_PRIVATE_IPV4}::$primary_ip --name %p -v /mnt:/data); \
fi; '
ExecStop=-/usr/bin/docker exec %p consul leave
ExecStop=-/usr/bin/docker stop -t 30 %p
ExecStop=-/usr/bin/docker rm %p
[X-Fleet]
Conflicts=%p@*.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment