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

Commit

Permalink
vulcan
Browse files Browse the repository at this point in the history
  • Loading branch information
rwillard committed Feb 10, 2016
1 parent a6ba564 commit de378a6
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions sidekick.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
parser.add_argument( '--etcd-host', action='store', default='localhost',
help='ETCD host' )

parser.add_argument( '--etcd-port', action='store', type=int, default=4001,
parser.add_argument( '--etcd-port', action='store', type=int, default=2379,
help='ETCD port' )

parser.add_argument( '--prefix', action='store', default='/services',
Expand All @@ -74,24 +74,46 @@
parser.add_argument( '--ttl', action='store', type=int, default=60,
help='ETCD ttl for the service announcement' )

parser.add_argument( '--vulcand', action='store', type=bool, default=False,
help='Selector for LB')

def announce_services( services, etcd_folder, etcd_client, timeout , ttl ):
parser.add_argument( '--type', action='store', default='http',
help='type for Vulcand')

def announce_services( services, etcd_folder, etcd_client, timeout , ttl):
for key, value in services:
logger.info( 'Health check for {}'.format( key ) )

full_key = os.path.join( etcd_folder, key )

healthy = check_health( value )

try:
if not healthy:
# Remove this server from ETCD if it exists
etcd_client.delete( full_key )
else:
# Announce this server to ETCD
etcd_client.write( full_key, value['uri'], ttl=ttl )
except etcd.EtcdException as e:
logging.error( e )
if value['vulcand']:
backend = "/vulcand/backends/"+key+"/backend"
server = "/vulcand/backends/"+key+"/servers/srv1"
frontend = "/vulcand/frontends/"+key+"/frontend"
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, {"Type": value['type']}, ttl=ttl)
etcd_client.write( server, {"URL": "http://"+str(value['ip'])+":"+str(value['port'])}, ttl=ttl)
etcd_client.write( frontend, {"Type": value['type'], "BackendId": key, "Route": "Host(`"+value['domain']+"`)"}, ttl=ttl)
except etcd.EtcdException as e:
logging.error( e )

else:
full_key = os.path.join( etcd_folder, key )
try:
if not healthy:
# Remove this server from ETCD if it exists
etcd_client.delete( full_key )
else:
# Announce this server to ETCD
etcd_client.write( full_key, value['uri'], ttl=ttl )
except etcd.EtcdException as e:
logging.error( e )

logger.info( 'Sleeping for {} seconds'.format( timeout ) )
time.sleep( timeout )
Expand Down Expand Up @@ -166,16 +188,19 @@ def find_matching_container( containers, args ):
for port in ports:
port = port[ 'PublicPort' ]

# Create a UUID
m = hashlib.md5()
m.update( args.name.encode('utf-8') )
m.update( args.ip.encode('utf-8') )
m.update( str( port ).encode('utf-8') )
uuid = m.hexdigest()
if args.vulcand:
matching[args.name] = { 'ip': args.ip, 'port': port, 'domain': args.domain, 'vulcand': args.vulcand, 'type': args.type}
else:
# Create a UUID
m = hashlib.md5()
m.update( args.name.encode('utf-8') )
m.update( args.ip.encode('utf-8') )
m.update( str( port ).encode('utf-8') )
uuid = m.hexdigest()

# Store the details
uri = '{}:{}'.format( args.ip, port )
matching[ uuid ] = { 'ip': args.check_ip, 'port': port, 'uri': uri }
# Store the details
uri = '{}:{}'.format( args.ip, port )
matching[ uuid ] = { 'ip': args.check_ip, 'port': port, 'uri': uri }

return matching

Expand All @@ -197,7 +222,7 @@ def main():

# Connect to ECTD
etcd_client = etcd.Client( host=args.etcd_host, port=args.etcd_port )
etcd_folder = os.path.join( args.prefix, args.domain )
etcd_folder = os.path.join( args.prefix, args.name )
logger.debug( 'Announcing to {}'.format( etcd_folder ) )

# Find the matching container
Expand All @@ -218,7 +243,7 @@ def main():
etcd_folder,
etcd_client,
args.timeout,
args.ttl )
args.ttl, )

if __name__ == '__main__':
main()

0 comments on commit de378a6

Please sign in to comment.