Merge pull request #348 from YunoHost/clean_domains.py

Clean domains.py
This commit is contained in:
Laurent Peuch 2017-08-19 20:23:31 +02:00 committed by GitHub
commit 66524213cd
3 changed files with 36 additions and 56 deletions

View file

@ -229,18 +229,6 @@ domain:
configuration: configuration:
authenticate: all authenticate: all
authenticator: ldap-anonymous authenticator: ldap-anonymous
arguments:
-f:
full: --filter
help: LDAP filter used to search
-l:
full: --limit
help: Maximum number of domain fetched
type: int
-o:
full: --offset
help: Starting number for domain fetching
type: int
### domain_add() ### domain_add()
add: add:

View file

@ -144,6 +144,7 @@
"domain_deletion_failed": "Unable to delete domain", "domain_deletion_failed": "Unable to delete domain",
"domain_dns_conf_is_just_a_recommendation": "This command shows you what is the *recommended* configuration. It does not actually set up the DNS configuration for you. It is your responsability to configure your DNS zone in your registrar according to this recommendation.", "domain_dns_conf_is_just_a_recommendation": "This command shows you what is the *recommended* configuration. It does not actually set up the DNS configuration for you. It is your responsability to configure your DNS zone in your registrar according to this recommendation.",
"domain_dyndns_already_subscribed": "You've already subscribed to a DynDNS domain", "domain_dyndns_already_subscribed": "You've already subscribed to a DynDNS domain",
"domain_dyndns_dynette_is_unreachable": "Unable to reach YunoHost dynette, either your YunoHost is not correctly connected to the internet or the dynette server is down. Error: {error}",
"domain_dyndns_invalid": "Invalid domain to use with DynDNS", "domain_dyndns_invalid": "Invalid domain to use with DynDNS",
"domain_dyndns_root_unknown": "Unknown DynDNS root domain", "domain_dyndns_root_unknown": "Unknown DynDNS root domain",
"domain_exists": "Domain already exists", "domain_exists": "Domain already exists",

View file

@ -43,7 +43,7 @@ from yunohost.service import service_regen_conf
logger = getActionLogger('yunohost.domain') logger = getActionLogger('yunohost.domain')
def domain_list(auth, filter=None, limit=None, offset=None): def domain_list(auth):
""" """
List domains List domains
@ -55,18 +55,9 @@ def domain_list(auth, filter=None, limit=None, offset=None):
""" """
result_list = [] result_list = []
# Set default arguments values result = auth.search('ou=domains,dc=yunohost,dc=org', 'virtualdomain=*', ['virtualdomain'])
if offset is None:
offset = 0
if limit is None:
limit = 1000
if filter is None:
filter = 'virtualdomain=*'
result = auth.search('ou=domains,dc=yunohost,dc=org', filter, ['virtualdomain']) for domain in result:
if len(result) > offset and limit > 0:
for domain in result[offset:offset + limit]:
result_list.append(domain['virtualdomain'][0]) result_list.append(domain['virtualdomain'][0])
return {'domains': result_list} return {'domains': result_list}
@ -82,29 +73,32 @@ def domain_add(auth, domain, dyndns=False):
""" """
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.app import app_ssowatconf
attr_dict = {'objectClass': ['mailDomain', 'top']} try:
auth.validate_uniqueness({'virtualdomain': domain})
if domain in domain_list(auth)['domains']: except MoulinetteError:
raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists')) raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists'))
# DynDNS domain # DynDNS domain
if dyndns: if dyndns:
if len(domain.split('.')) < 3: if len(domain.split('.')) < 3:
raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid')) raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid'))
from yunohost.dyndns import dyndns_subscribe
try:
r = requests.get('https://dyndns.yunohost.org/domains')
except requests.ConnectionError:
pass
else:
dyndomains = json.loads(r.text)
dyndomain = '.'.join(domain.split('.')[1:])
if dyndomain in dyndomains:
if os.path.exists('/etc/cron.d/yunohost-dyndns'): if os.path.exists('/etc/cron.d/yunohost-dyndns'):
raise MoulinetteError(errno.EPERM, raise MoulinetteError(errno.EPERM,
m18n.n('domain_dyndns_already_subscribed')) m18n.n('domain_dyndns_already_subscribed'))
try:
r = requests.get('https://dyndns.yunohost.org/domains', timeout=30)
except requests.ConnectionError as e:
raise MoulinetteError(errno.EHOSTUNREACH,
m18n.n('domain_dyndns_dynette_is_unreachable', error=str(e)))
from yunohost.dyndns import dyndns_subscribe
dyndomains = json.loads(r.text)
dyndomain = '.'.join(domain.split('.')[1:])
if dyndomain in dyndomains:
dyndns_subscribe(domain=domain) dyndns_subscribe(domain=domain)
else: else:
raise MoulinetteError(errno.EINVAL, raise MoulinetteError(errno.EINVAL,
@ -113,23 +107,19 @@ def domain_add(auth, domain, dyndns=False):
try: try:
yunohost.certificate._certificate_install_selfsigned([domain], False) yunohost.certificate._certificate_install_selfsigned([domain], False)
try: attr_dict = {
auth.validate_uniqueness({'virtualdomain': domain}) 'objectClass': ['mailDomain', 'top'],
except MoulinetteError: 'virtualdomain': domain,
raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists')) }
attr_dict['virtualdomain'] = domain
if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict): if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict):
raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed')) raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed'))
try: # Don't regen these conf if we're still in postinstall
with open('/etc/yunohost/installed', 'r') as f: if os.path.exists('/etc/yunohost/installed'):
service_regen_conf(names=[ service_regen_conf(names=['nginx', 'metronome', 'dnsmasq', 'rmilter'])
'nginx', 'metronome', 'dnsmasq', 'rmilter']) app_ssowatconf(auth)
os.system('yunohost app ssowatconf > /dev/null 2>&1')
except IOError:
pass
except: except:
# Force domain removal silently # Force domain removal silently
try: try:
@ -153,6 +143,7 @@ def domain_remove(auth, domain, force=False):
""" """
from yunohost.hook import hook_callback from yunohost.hook import hook_callback
from yunohost.app import app_ssowatconf
if not force and domain not in domain_list(auth)['domains']: if not force and domain not in domain_list(auth)['domains']:
raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown')) raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown'))
@ -179,7 +170,7 @@ def domain_remove(auth, domain, force=False):
raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed'))
service_regen_conf(names=['nginx', 'metronome', 'dnsmasq']) service_regen_conf(names=['nginx', 'metronome', 'dnsmasq'])
os.system('yunohost app ssowatconf > /dev/null 2>&1') app_ssowatconf(auth)
hook_callback('post_domain_remove', args=[domain]) hook_callback('post_domain_remove', args=[domain])