Skip to content

Instantly share code, notes, and snippets.

@mhamrah
Forked from skippy/boot.sh
Last active August 29, 2015 14:14
Show Gist options
  • Save mhamrah/edcb553695665c7918b0 to your computer and use it in GitHub Desktop.
Save mhamrah/edcb553695665c7918b0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# set -eo pipefail
export CONSUL_NUM_INSTANCES=${CONSUL_NUM_INSTANCES:-3}
echo " => stopping consul"
fleetctl stop -block-attempts=10 \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
# FIXME: sometimes it takes awhile for systemd to stop things...
# if we go right to destroy this can leave services running...
echo " => destroying consul"
fleetctl destroy \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
echo " => sleeping and then submitting consul"
SCRIPT=`readlink -f "$0"`
SCRIPTPATH=`dirname "$SCRIPT"`
sleep 5
fleetctl load \
$(eval echo "$SCRIPTPATH/consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
"$SCRIPTPATH/consul-agent.service"
echo " => starting consul"
fleetctl start \
$(eval echo "consul-server@{1..$CONSUL_NUM_INSTANCES}.service") \
consul-agent.service
[Unit]
Description=Consul Agent
Requires=docker.service
Requires=fleet.service
After=docker.service
After=fleet.service
[Service]
TimeoutStartSec=10m
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=/bin/bash -c '\
log_tag="%p"; \
if $(systemctl list-units | grep consul-server 2>&1 >/dev/null); then \
logger -t "$log_tag" "skipping host as it already has a consul server running"; \
exit 0; \
else \
primary_ip=$(fleetctl list-units --fields=unit,sub,machine | grep -E "[email protected]" | sed -r "s/.*\/(.+?)/\1/"); \
logger -t "$log_tag" "starting consul client. Connecting to $primary_ip"; \
$(docker run --rm progrium/consul cmd:run ${COREOS_PRIVATE_IPV4}::$primary_ip::client --name %p); \
fi; '
ExecStop=-/usr/bin/docker stop -t 30 %p
ExecStop=-/usr/bin/docker rm %p
[X-Fleet]
Global=true
[Unit]
Description=Consul Server
Requires=docker.service
Requires=fleet.service
After=docker.service
After=fleet.service
[Service]
TimeoutStartSec=10m
TimeoutStopSec=90s
Restart=always
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=/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); \
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); \
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