Created
September 13, 2015 16:09
-
-
Save ketzacoatl/1a25503445a684e31dbd to your computer and use it in GitHub Desktop.
Proof of Concept - Docker service orchestration via python consul-lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from time import sleep | |
import consul | |
import consul_lock | |
from docker import Client | |
''' | |
This is a PoC which uses consul's locking mechanism to ensure only | |
one instance of the named docker container is running. Requires: | |
a) pip install python-consul consul-lock | |
b) docker create --name my_container my/app | |
try it: python only-one.py container_name token | |
''' | |
pause = 3 | |
container_name = sys.argv[1] | |
token = sys.argv[2] | |
cli = Client(base_url='unix://var/run/docker.sock') | |
consul_client = consul.Consul(token=token) | |
consul_lock.defaults.consul_client = consul_client | |
print 'starting up!' | |
while True: | |
ephemeral_lock = consul_lock.EphemeralLock(('only-one/%s' % container_name), | |
lock_timeout_seconds=3600, | |
acquire_timeout_ms=500) | |
try: | |
print 'acquiring distributed lock in consul..' | |
was_acquired = ephemeral_lock.acquire(fail_hard=False) | |
if was_acquired: | |
# do dangerous stuff here | |
print 'lock acquired!' | |
print ('starting container %s' % container_name) | |
response = cli.start(container=container_name) | |
print(response) | |
print 'will now wait/block until the container exits..' | |
cli.wait(container=container_name) | |
else: | |
print 'someone else has the lock :\ try again later' | |
finally: | |
print 'be sure there is no lock in consul..' | |
ephemeral_lock.release() | |
print ('sleeping for %d seconds' % pause) | |
sleep(pause) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment