Multiple service discovery
This commit is contained in:
parent
651ed3420c
commit
d62a424cf2
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
surok/__pycache__
|
surok/__pycache__
|
||||||
__pycache__
|
__pycache__
|
||||||
|
*.pyc
|
||||||
|
*.swp
|
||||||
|
@ -1 +1,12 @@
|
|||||||
{}
|
{
|
||||||
|
"services": [
|
||||||
|
{
|
||||||
|
"name": "kioskservice",
|
||||||
|
"group": "production.romania"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"conf_name": "test",
|
||||||
|
"template": "templates/nginx_proxy.jj2",
|
||||||
|
"dest": "emailsender",
|
||||||
|
"reload_cmd": "/bin/echo reload nginx"
|
||||||
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
"marathon": "10.0.1.199:8080",
|
"marathon": "10.0.1.199:8080",
|
||||||
"confd": "conf.d"
|
"confd": "conf.d",
|
||||||
|
"domain": "marathon.mesos",
|
||||||
|
"wait_time": 20,
|
||||||
|
"lock_dir": "/var/tmp"
|
||||||
}
|
}
|
||||||
|
4
surok.py
4
surok.py
@ -9,7 +9,7 @@ from surok.discovery import resolve
|
|||||||
from surok.system import reload_conf
|
from surok.system import reload_conf
|
||||||
|
|
||||||
# Load base configurations
|
# Load base configurations
|
||||||
f = open('/etc/surok/conf/surok.json', 'r')
|
f = open('conf/surok.json', 'r')
|
||||||
conf = json.loads(f.read())
|
conf = json.loads(f.read())
|
||||||
print(conf)
|
print(conf)
|
||||||
f.close()
|
f.close()
|
||||||
@ -36,7 +36,7 @@ while 1:
|
|||||||
for app in confs:
|
for app in confs:
|
||||||
app_conf = load_app_conf(app)
|
app_conf = load_app_conf(app)
|
||||||
app_hosts = resolve(app_conf, conf)
|
app_hosts = resolve(app_conf, conf)
|
||||||
my = {'app': app_conf['name'], 'hosts': app_hosts}
|
my = { 'hosts': app_hosts }
|
||||||
service_conf = gen(my, app_conf['template'])
|
service_conf = gen(my, app_conf['template'])
|
||||||
|
|
||||||
print(reload_conf(service_conf, app_conf))
|
print(reload_conf(service_conf, app_conf))
|
||||||
|
@ -2,10 +2,12 @@ import dns.resolver
|
|||||||
|
|
||||||
def resolve(app, conf):
|
def resolve(app, conf):
|
||||||
hosts = []
|
hosts = []
|
||||||
|
services = app['services']
|
||||||
domain = conf['domain']
|
domain = conf['domain']
|
||||||
for rdata in dns.resolver.query('_' + app['name'] + '.' + app['group'] + '._tcp.' + domain, 'SRV'):
|
for service in services:
|
||||||
info = str(rdata).split()
|
for rdata in dns.resolver.query('_' + service['name'] + '.' + service['group'] + '._tcp.' + domain, 'SRV'):
|
||||||
server = { 'name': info[3], 'port': info[2] }
|
info = str(rdata).split()
|
||||||
hosts.append(server)
|
server = { service['name']: { 'name': info[3], 'port': info[2] } }
|
||||||
|
hosts.append(server)
|
||||||
|
|
||||||
return hosts
|
return hosts
|
||||||
|
@ -28,14 +28,14 @@ def write_lock(name, service_conf):
|
|||||||
def reload_conf(service_conf, app_conf):
|
def reload_conf(service_conf, app_conf):
|
||||||
|
|
||||||
# Check old config
|
# Check old config
|
||||||
if get_old(app_conf['name'], service_conf) != 1:
|
if get_old(app_conf['conf_name'], service_conf) != 1:
|
||||||
print('Write new configuration')
|
print('Write new configuration')
|
||||||
|
|
||||||
f = open(app_conf['dest'], 'w')
|
f = open(app_conf['dest'], 'w')
|
||||||
f.write(service_conf)
|
f.write(service_conf)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
write_lock(app_conf['name'], service_conf)
|
write_lock(app_conf['conf_name'], service_conf)
|
||||||
|
|
||||||
# Reload conf
|
# Reload conf
|
||||||
stdout = os.popen(app_conf['reload_cmd']).read()
|
stdout = os.popen(app_conf['reload_cmd']).read()
|
||||||
|
11
templates/nginx_proxy.jj2
Normal file
11
templates/nginx_proxy.jj2
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{% for host in my['hosts'] %}
|
||||||
|
server {{host['kioskservice']['name']}}:{{host['kioskservice']['port']}};
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 0.0.0.0:80;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user