Closed SD-10

This commit is contained in:
Difrex 2016-10-11 14:36:41 +03:00
parent f0a509049c
commit e57f08edbb

View File

@ -2,12 +2,18 @@ import dns.resolver
import dns.query import dns.query
from dns.exception import DNSException from dns.exception import DNSException
import logging import logging
import sys
# Configure logging # Logger configuration
FORMAT = '%(asctime) %(message)s' # This need to be moved
logging.basicConfig(format=FORMAT) def get_logger():
logger = logging.getLogger('discovery') # Configure logging
FORMAT = '%(asctime) %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
return logger
# Resolve service from mesos-dns SRV record # Resolve service from mesos-dns SRV record
# return dict {"servicename": [{"name": "service.f.q.d.n.", "port": 9999}]} # return dict {"servicename": [{"name": "service.f.q.d.n.", "port": 9999}]}
@ -15,22 +21,16 @@ def resolve(app, conf):
hosts = {} hosts = {}
services = app['services'] services = app['services']
domain = conf['domain'] domain = conf['domain']
group = None logger = get_logger()
# Groups hack
if app['env'].get('SUROK_DISCOVERY_GROUP') is not None:
group = app['env']['SUROK_DISCOVERY_GROUP']
for service in services: for service in services:
hosts[service['name']] = {} hosts[service['name']] = {}
# Check group configuration group = get_group(service, app)
if group is not None: if group is False:
pass logger.error('Group is not defined in config, SUROK_DISCOVERY_GROUP and MARATHON_APP_ID')
else: logger.error('Not in Mesos launch?')
# Load group from service config sys.exit(2)
# /etc/surok/conf.d/service_conf.json
group = service['group']
# Port name from app config # Port name from app config
ports = None ports = None
@ -56,6 +56,7 @@ def resolve(app, conf):
# Do SRV queries # Do SRV queries
# Return array: [{"name": "f.q.d.n", "port": 8876}] # Return array: [{"name": "f.q.d.n", "port": 8876}]
def do_query(fqdn, loglevel): def do_query(fqdn, loglevel):
logger = get_logger()
servers = [] servers = []
try: try:
query = dns.resolver.query(fqdn, 'SRV') query = dns.resolver.query(fqdn, 'SRV')
@ -75,5 +76,31 @@ def do_query(fqdn, loglevel):
# Groups switch # Groups switch
# Priority: config, environment, marathon environment # Priority: config, environment, marathon environment
def get_group(conf): def get_group(service, app):
pass # Check group in app conf
if 'group' in service:
return service['group']
# Check environment variable
elif app['env'].get('SUROK_DISCOVERY_GROUP'):
return app['env']['SUROK_DISCOVERY_GROUP']
# Check marathon environment variable
elif app['env'].get('MARATHON_APP_ID'):
group = parse_marathon_app_id(app['env']['MARATHON_APP_ID'])
return group
else:
return False
# Parse MARATHON_APP_ID
def parse_marathon_app_id(marathon_app_id):
marathon_app_id = marathon_app_id.split('/')
group = ''
counter = len(marathon_app_id) - 2
i = 0
while counter > i:
group = group + marathon_app_id[counter]
if counter != i + 1:
group += '.'
counter -= 1
return group