Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into vulcand-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a5huynh committed Feb 12, 2016
2 parents dda4138 + 87e8c1b commit 88fa080
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions sidekick.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# @Author: ahuynh
# @Date: 2015-06-10 16:51:36
# @Last Modified by: ahuynh
# @Last Modified time: 2016-02-11 14:23:46
# @Last Modified time: 2016-02-11 16:04:41
'''
The sidekick should essentially replace job of the following typical
bash script that is used to announce a service to ETCD.
Expand All @@ -30,6 +30,7 @@
import time
import socket
import sys
import json

from docker import Client
from docker.utils import kwargs_from_env
Expand Down Expand Up @@ -95,15 +96,24 @@ def announce_services( services, etcd_folder, etcd_client, timeout, ttl, vulcand
backend = "/vulcand/backends/{domain}/backend".format( domain=value['domain'] )
server = "/vulcand/backends/{domain}/servers/{uuid}".format( domain=value['domain'], uuid=key )
frontend = "/vulcand/frontends/{domain}/frontend".format( domain=value['domain'] )
announcement = {
backend: { 'Type': value[ 'type' ] },
server: { 'URL': 'http://{uri}'.format( **value ) },
frontend: {
'Type': value[ 'type' ],
'BackendId': value[ 'domain' ],
'Route': 'Host("{domain}")'.format( **value )
}
}
try:
if not healthy:
# Remove this server from ETCD if it exists
etcd_client.delete( backend )
etcd_client.delete( server )
etcd_client.delete( frontend )
else:
# Announce this server to ETCD
etcd_client.write( backend, json.dumps({ 'Type': value['type'] }), ttl=ttl)
etcd_client.write( server, json.dumps({ 'URL': 'http://{uri}'.format( **value ) }), ttl=ttl)
etcd_client.write( frontend, json.dumps({
'Type': value['type'],
'BackendId': value['domain'],
'Route': 'Host(`{0}`)'.format( value['domain'] )
}), ttl=ttl)

except etcd.EtcdException as e:
logging.error( e )
else:
announcement[ os.path.join( etcd_folder, key ) ] = value[ 'uri' ]

Expand Down Expand Up @@ -134,13 +144,13 @@ def check_health( service ):

try:
s = socket.socket()
s.connect( ( service['ip'], service['port'] ) )
s.connect( ( service['check_ip'], service['port'] ) )
except ConnectionRefusedError:
logger.error( 'tcp://{ip}:{port} health check FAILED'.format(**service) )
logger.error( 'tcp://{check_ip}:{port} health check FAILED'.format(**service) )
healthy = False
else:
s.close()
logger.info( 'tcp://{ip}:{port} health check SUCCEEDED'.format(**service) )
logger.info( 'tcp://{check_ip}:{port} health check SUCCEEDED'.format(**service) )
healthy = True
s.close()

Expand Down Expand Up @@ -203,7 +213,8 @@ def find_matching_container( containers, args ):
# Store the details
uri = '{}:{}'.format( args.ip, port )
matching[ uuid ] = {
'ip': args.check_ip,
'ip': args.ip,
'check_ip': args.check_ip,
'port': port,
'uri': uri,
'domain': args.domain,
Expand Down

0 comments on commit 88fa080

Please sign in to comment.