Skip to content

Instantly share code, notes, and snippets.

@mwhooker
Last active August 29, 2015 14:10
Show Gist options
  • Save mwhooker/57ef93bef080bdf2c1df to your computer and use it in GitHub Desktop.
Save mwhooker/57ef93bef080bdf2c1df to your computer and use it in GitHub Desktop.
[Unit]
Description=Mongodb service on port %i
# Requirements
Requires=etcd.service
Requires=docker.service
Requires=mongodb-discovery@%i.service
# Dependency ordering
After=etcd.service
After=docker.service
Before=mongodb-discovery@%i.service
[Service]
# Let processes take awhile to start up (for first run Docker containers)
TimeoutStartSec=0
# Change killmode from "control-group" to "none" to let Docker remove
# work correctly.
KillMode=none
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment
# Pre-start and Start
## Directives with "=-" are allowed to fail without consequence
ExecStartPre=-/usr/bin/docker kill mongodb.%i
ExecStartPre=-/usr/bin/docker rm mongodb.%i
# I don't trust the "dockerfile" repository
ExecStartPre=/usr/bin/docker pull dockerfile/mongodb
ExecStart=/usr/bin/docker run --name mongodb.%i -p ${COREOS_PRIVATE_IPV4}:%i:27017 \
dockerfile/mongodb
# Stop
ExecStop=/usr/bin/docker stop mongodb.%i
[X-Fleet]
# Don't schedule on the same machine as other Mongo instances
X-Conflicts=mongodb@*.service
[Unit]
Description=Mongodb on port %i etcd registration
# Requirements
Requires=etcd.service
Requires=mongodb@%i.service
# Dependency ordering and binding
After=etcd.service
After=mongodb@%i.service
BindsTo=mongodb@%i.service
[Service]
# Get CoreOS environmental variables
EnvironmentFile=/etc/environment
# Start
## Test whether service is accessible and then register useful information
ExecStart=/bin/bash -c '\
while true; do \
netstat -lnt | grep ${COREOS_PRIVATE_IPV4}:%i; \
if [ $? -eq 0 ]; then \
etcdctl set /services/mongodb/${COREOS_PRIVATE_IPV4} \'${COREOS_PRIVATE_IPV4}:%i\' --ttl 30; \
else \
etcdctl rm /services/mongodb/${COREOS_PRIVATE_IPV4}; \
fi; \
sleep 20; \
done'
# Stop
ExecStop=/usr/bin/etcdctl rm /services/mongodb/${COREOS_PRIVATE_IPV4}
[X-Fleet]
# Schedule on the same machine as the associated Mongodb service
X-ConditionMachineOf=mongodb@%i.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment