surok/surok.py

84 lines
1.8 KiB
Python
Raw Normal View History

2016-08-01 12:54:48 +03:00
#!/usr/bin/python3
from time import sleep
2016-08-09 15:40:07 +03:00
import os
2016-08-01 12:54:48 +03:00
from os import listdir
2016-08-09 14:13:20 +03:00
from os.path import isfile, join
2016-08-01 12:54:48 +03:00
import json
2016-08-01 14:56:35 +03:00
from surok.templates import gen
from surok.discovery import resolve
2016-08-01 15:24:13 +03:00
from surok.system import reload_conf
2016-08-01 12:54:48 +03:00
import argparse
2016-08-04 11:10:36 +03:00
2016-08-01 12:54:48 +03:00
# Load base configurations
2016-08-04 11:10:36 +03:00
surok_conf = '/etc/surok/conf/surok.json'
# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config')
args = parser.parse_args()
if args.config:
surok_conf = args.config
2016-08-05 10:32:18 +03:00
# Read config file
2016-08-04 11:10:36 +03:00
f = open(surok_conf, 'r')
2016-08-01 12:54:48 +03:00
conf = json.loads(f.read())
2016-08-01 14:56:35 +03:00
f.close()
2016-08-01 12:54:48 +03:00
2016-08-05 10:32:18 +03:00
# Get app configurations
# Return list of patches to app discovery configuration
2016-08-01 12:54:48 +03:00
def get_configs():
2016-08-09 14:13:20 +03:00
confs = [f for f in listdir(conf['confd']) if isfile(
join(conf['confd'], f))]
2016-08-01 12:54:48 +03:00
return confs
2016-08-01 14:56:35 +03:00
# Get Surok App configuration
2016-08-05 10:32:18 +03:00
# Read app conf from file and return dict
2016-08-01 14:56:35 +03:00
def load_app_conf(app):
2016-08-09 15:40:07 +03:00
# Load OS environment to app_conf
2016-08-09 14:13:20 +03:00
f = open(conf['confd'] + '/' + app)
c = json.loads(f.read())
2016-08-01 14:56:35 +03:00
f.close()
2016-08-09 15:40:07 +03:00
c['env'] = os.environ
2016-08-01 14:56:35 +03:00
return c
2016-08-01 12:54:48 +03:00
# Main loop
2016-08-05 10:32:18 +03:00
###########
# Bad hack for detect first run
# On host system set it to False
# TODO: put it to config
first = True
2016-08-01 12:54:48 +03:00
while 1:
confs = get_configs()
2016-08-01 14:56:35 +03:00
for app in confs:
app_conf = load_app_conf(app)
2016-08-17 12:13:02 +03:00
# Will be removed later
# For old configs
try:
loglevel = conf['loglevel']
except:
conf['loglevel'] = 'info'
2016-08-09 14:13:20 +03:00
# Resolve services
2016-10-05 11:46:51 +03:00
if resolve(app_conf, conf) != 404:
app_hosts = resolve(app_conf, conf)
2016-10-05 11:46:51 +03:00
# Populate my dictionary
my = {"services": app_hosts,
"conf_name": app_conf['conf_name']}
2016-08-09 14:13:20 +03:00
2016-10-05 11:46:51 +03:00
# Generate config from template
service_conf = gen(my, app_conf['template'])
2016-08-01 15:24:13 +03:00
2016-10-05 11:46:51 +03:00
first = reload_conf(service_conf, app_conf, first, conf)
2016-08-01 15:24:13 +03:00
2016-08-09 14:13:20 +03:00
sleep(conf['wait_time'])