surok/surok.py
Васильев Евгений Владимирович 12b868ad0b Add Marathon discovery. Remade discovery and logger module
2017-01-25 13:11:51 +03:00

85 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python3
from time import sleep
import os
from os import listdir
from os.path import isfile, join
import json
import argparse
from surok.templates import gen
from surok.discovery import Discovery
from surok.system import reload_conf
from surok.logger import Logger
logger=Logger()
# Load base configurations
surok_conf = '/etc/surok/conf/surok.json'
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help='surok.json path')
args = parser.parse_args()
if args.config:
surok_conf = args.config
# Read config file
f = open(surok_conf, 'r')
conf = json.loads(f.read())
f.close()
# Get app configurations
# Return list of patches to app discovery configuration
def get_configs():
confs = [f for f in listdir(conf['confd']) if isfile(
join(conf['confd'], f))]
return sorted(confs)
# Get Surok App configuration
# Read app conf from file and return dict
def load_app_conf(app):
# Load OS environment to app_conf
f = open(conf['confd'] + '/' + app)
c = json.loads(f.read())
f.close()
c['env'] = os.environ
return c
logger.set_level(conf.get('loglevel','info'))
# Main loop
###########
discovery=Discovery()
while 1:
confs = get_configs()
# Update config from discovery object
discovery.set_config(conf)
# Update discovery data
discovery.update_data()
for app in confs:
app_conf = load_app_conf(app)
# Resolve services
app_hosts = discovery.resolve(app_conf)
# Populate my dictionary
my = {"services": app_hosts,
"conf_name": app_conf['conf_name']}
logger.debug('my=',my)
# Generate config from template
service_conf = gen(my, app_conf['template'])
reload_conf(service_conf, app_conf, conf, app_hosts)
sleep(conf['wait_time'])