make python script configurable

This commit is contained in:
Kload 2013-06-17 08:08:26 +00:00
parent 78d2d134c8
commit 2deb68503e

View file

@ -5,19 +5,34 @@ import sys
import json import json
from urllib import urlopen from urllib import urlopen
urls = ['http://dynette-dev.herokuapp.com'] conf_file = '/etc/bind/named.conf.local' # Include this filename in '/etc/bind/named.conf'
lines = [] zone_dir = '/var/named/data/' # Do not forget the trailing '/'
subs_urls = ['http://dyndns.yunohost.org'] # 127.0.0.1 if you install subscribe server locally
ns1 = 'dynhost.yunohost.org' # Name servers
ns2 = 'hostmaster.yunohost.org'
for url in urls: allowed_operations = {
'.' : ['A', 'TXT', 'MX'],
'pubsub.' : ['A'],
'muc.' : ['A'],
'vjud' : ['A'],
'_xmpp-client._tcp.' : ['SRV'],
'_xmpp-server._tcp.' : ['SRV']
}
lines = []
for url in subs_urls:
domains = json.loads(str(urlopen(url +'/domains').read())) domains = json.loads(str(urlopen(url +'/domains').read()))
for domain in domains: for domain in domains:
result = json.loads(str(urlopen(url +'/all/'+ domain).read())) result = json.loads(str(urlopen(url +'/all/'+ domain).read()))
if not os.path.exists('/var/named/data/'+ domain +'.db'): if not os.path.exists(zone_dir + domain +'.db'):
db_lines = [ db_lines = [
'$ORIGIN .', '$ORIGIN .',
'$TTL 10 ; 10 seconds', '$TTL 10 ; 10 seconds',
domain+'. IN SOA dynhost.yunohost.org hostmaster.yunohost.org. (', domain+'. IN SOA '+ ns1 +'. '+ ns2 +'. (',
' 18 ; serial', ' 18 ; serial',
' 10800 ; refresh (3 hours)', ' 10800 ; refresh (3 hours)',
' 3600 ; retry (1 hour)', ' 3600 ; retry (1 hour)',
@ -25,31 +40,25 @@ for url in urls:
' 10 ; minimum (10 seconds)', ' 10 ; minimum (10 seconds)',
' )', ' )',
'$TTL 3600 ; 1 hour', '$TTL 3600 ; 1 hour',
' NS dynhost.yunohost.org.', ' NS '+ ns1 +'.',
' NS hostmaster.yunohost.org.', ' NS '+ ns2 +'.',
'', '',
'$ORIGIN '+ domain +'.', '$ORIGIN '+ domain +'.',
] ]
with open('/var/named/data/'+ domain +'.db', 'w') as zone: with open(zone_dir + domain +'.db', 'w') as zone:
for line in db_lines: for line in db_lines:
zone.write(line + '\n') zone.write(line + '\n')
lines.extend([ lines.extend([
'zone "'+ domain +'" {', 'zone "'+ domain +'" {',
' type master;', ' type master;',
' file "/var/named/data/'+ domain +'.db"; ', ' file "'+ zone_dir + domain +'.db"; ',
' update-policy {', ' update-policy {',
]) ])
for entry in result: for entry in result:
fqdn = entry['subdomain'] +'.' for subd, type in allowed_operations.items():
lines.extend([ if subd == '.': subd = ''
' grant '+ fqdn +' name '+ fqdn +' A TXT MX;', lines.append(' grant '+ entry['subdomain'] +'. name '+ subd + entry['subdomain'] +'. ' + ' '.join(type) +';')
' grant '+ fqdn +' name pubsub.'+ fqdn +' A;',
' grant '+ fqdn +' name muc.'+ fqdn +' A;',
' grant '+ fqdn +' name vjud.'+ fqdn +' A;',
' grant '+ fqdn +' name _xmpp-client._tcp.'+ fqdn +' SRV;',
' grant '+ fqdn +' name _xmpp-server._tcp.'+ fqdn +' SRV;',
])
lines.extend([ lines.extend([
' };', ' };',
@ -57,27 +66,26 @@ for url in urls:
]) ])
for entry in result: for entry in result:
fqdn = entry['subdomain'] +'.'
lines.extend([ lines.extend([
'key '+ fqdn +' {', 'key '+ entry['subdomain'] +'. {',
' algorithm hmac-md5;', ' algorithm hmac-md5;',
' secret "'+ entry['public_key'] +'";', ' secret "'+ entry['public_key'] +'";',
'};', '};',
]) ])
os.system('cp /etc/bind/named.conf.local /etc/bind/named.conf.local.back') os.system('cp '+ conf_file +' '+ conf_file +'.back')
with open('/etc/bind/named.conf.local', 'w') as zone: with open(conf_file, 'w') as zone:
for line in lines: for line in lines:
zone.write(line + '\n') zone.write(line + '\n')
os.system('chown -R bind:bind /var/named /etc/bind/named.conf.local') os.system('chown -R bind:bind '+ zone_dir +' '+ conf_file)
if os.system('rndc reload') == 0: if os.system('rndc reload') == 0:
exit(0) exit(0)
else: else:
os.system('cp /etc/bind/named.conf.local /etc/bind/named.conf.local.bad') os.system('cp '+ conf_file +' '+ conf_file +'.bad')
os.system('cp /etc/bind/named.conf.back /etc/bind/named.conf.local') os.system('cp '+ conf_file +'.back '+ conf_file)
os.system('rndc reload') os.system('rndc reload')
print("An error occured ! Please check daemon.log and your conf.bad") print("An error occured ! Please check daemon.log and your conf.bad")
exit(1) exit(1)