From 56513bbdb121aa654cff9bbb28241addbf0fb7ab Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:18:18 +0200 Subject: [PATCH 1/9] [mod] we never used those filtering/offset things --- data/actionsmap/yunohost.yml | 12 ------------ src/yunohost/domain.py | 17 ++++------------- 2 files changed, 4 insertions(+), 25 deletions(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 24b099451..b3df2aa58 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -241,18 +241,6 @@ domain: configuration: authenticate: all 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() add: diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 4665f9905..d3884ce46 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -43,7 +43,7 @@ from yunohost.service import service_regen_conf logger = getActionLogger('yunohost.domain') -def domain_list(auth, filter=None, limit=None, offset=None): +def domain_list(auth): """ List domains @@ -55,19 +55,10 @@ def domain_list(auth, filter=None, limit=None, offset=None): """ result_list = [] - # Set default arguments values - 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', 'virtualdomain=*', ['virtualdomain']) - result = auth.search('ou=domains,dc=yunohost,dc=org', filter, ['virtualdomain']) - - if len(result) > offset and limit > 0: - for domain in result[offset:offset + limit]: - result_list.append(domain['virtualdomain'][0]) + for domain in result: + result_list.append(domain['virtualdomain'][0]) return {'domains': result_list} From 286449386c366d49b3fae6228a73d2c8e52aca8b Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:25:11 +0200 Subject: [PATCH 2/9] [mod] clean code, this is what we indeed want to check --- src/yunohost/domain.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index d3884ce46..191a2e79a 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -114,13 +114,10 @@ def domain_add(auth, domain, dyndns=False): if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict): raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed')) - try: - with open('/etc/yunohost/installed', 'r') as f: - service_regen_conf(names=[ - 'nginx', 'metronome', 'dnsmasq', 'rmilter']) - os.system('yunohost app ssowatconf > /dev/null 2>&1') - except IOError: - pass + if os.path.exists('/etc/yunohost/installed'): + service_regen_conf(names=['nginx', 'metronome', 'dnsmasq', 'rmilter']) + os.system('yunohost app ssowatconf > /dev/null 2>&1') + except: # Force domain removal silently try: From 4c069c5c3f7dc2204e07974196d4fdf6f2cd206a Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:32:50 +0200 Subject: [PATCH 3/9] [mod] fails when registering dynette domain and that dynette can't be reached --- locales/en.json | 1 + src/yunohost/domain.py | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/locales/en.json b/locales/en.json index 7ce7d2980..d2f3d7137 100644 --- a/locales/en.json +++ b/locales/en.json @@ -143,6 +143,7 @@ "domain_deleted": "The domain has been deleted", "domain_deletion_failed": "Unable to delete 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_root_unknown": "Unknown DynDNS root domain", "domain_exists": "Domain already exists", diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 191a2e79a..fe4349e58 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -87,19 +87,20 @@ def domain_add(auth, domain, dyndns=False): try: r = requests.get('https://dyndns.yunohost.org/domains') - except requests.ConnectionError: - pass + except requests.ConnectionError as e: + raise MoulinetteError(errno.EHOSTUNREACH, + m18n.n('domain_dyndns_dynette_is_unreachable', error=str(e))) + + dyndomains = json.loads(r.text) + dyndomain = '.'.join(domain.split('.')[1:]) + if dyndomain in dyndomains: + if os.path.exists('/etc/cron.d/yunohost-dyndns'): + raise MoulinetteError(errno.EPERM, + m18n.n('domain_dyndns_already_subscribed')) + dyndns_subscribe(domain=domain) else: - dyndomains = json.loads(r.text) - dyndomain = '.'.join(domain.split('.')[1:]) - if dyndomain in dyndomains: - if os.path.exists('/etc/cron.d/yunohost-dyndns'): - raise MoulinetteError(errno.EPERM, - m18n.n('domain_dyndns_already_subscribed')) - dyndns_subscribe(domain=domain) - else: - raise MoulinetteError(errno.EINVAL, - m18n.n('domain_dyndns_root_unknown')) + raise MoulinetteError(errno.EINVAL, + m18n.n('domain_dyndns_root_unknown')) try: yunohost.certificate._certificate_install_selfsigned([domain], False) From 3f65543dc21729c4033b23853368c60b9fa73e8c Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:33:41 +0200 Subject: [PATCH 4/9] [mod] we need timeout everywhere to avoid blocking code --- src/yunohost/domain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index fe4349e58..5fb7ed313 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -86,7 +86,7 @@ def domain_add(auth, domain, dyndns=False): from yunohost.dyndns import dyndns_subscribe try: - r = requests.get('https://dyndns.yunohost.org/domains') + 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 a8e57d9c6a9e444563d0c14887f906151587b2a7 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:34:35 +0200 Subject: [PATCH 5/9] [mod] don't request the server if a dyndns domain is already subscribed --- src/yunohost/domain.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 5fb7ed313..a8142fe88 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -83,20 +83,21 @@ def domain_add(auth, domain, dyndns=False): if dyndns: if len(domain.split('.')) < 3: raise MoulinetteError(errno.EINVAL, m18n.n('domain_dyndns_invalid')) - from yunohost.dyndns import dyndns_subscribe + if os.path.exists('/etc/cron.d/yunohost-dyndns'): + raise MoulinetteError(errno.EPERM, + 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: - if os.path.exists('/etc/cron.d/yunohost-dyndns'): - raise MoulinetteError(errno.EPERM, - m18n.n('domain_dyndns_already_subscribed')) dyndns_subscribe(domain=domain) else: raise MoulinetteError(errno.EINVAL, From 555caa9c73ef0ef811ae51460d937341fe2c71e2 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:37:12 +0200 Subject: [PATCH 6/9] [mod] only check once if the domain exist --- src/yunohost/domain.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index a8142fe88..939ebb754 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -76,7 +76,9 @@ def domain_add(auth, domain, dyndns=False): attr_dict = {'objectClass': ['mailDomain', 'top']} - if domain in domain_list(auth)['domains']: + try: + auth.validate_uniqueness({'virtualdomain': domain}) + except MoulinetteError: raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists')) # DynDNS domain @@ -106,11 +108,6 @@ def domain_add(auth, domain, dyndns=False): try: yunohost.certificate._certificate_install_selfsigned([domain], False) - try: - auth.validate_uniqueness({'virtualdomain': domain}) - except MoulinetteError: - raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists')) - attr_dict['virtualdomain'] = domain if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict): From 1fe568a60d51bae1b84c3196e42650e6fff281e0 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:38:11 +0200 Subject: [PATCH 7/9] [mod] style, declare everything as once to make code more readable --- src/yunohost/domain.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 939ebb754..4690b2f3f 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -74,8 +74,6 @@ def domain_add(auth, domain, dyndns=False): """ from yunohost.hook import hook_callback - attr_dict = {'objectClass': ['mailDomain', 'top']} - try: auth.validate_uniqueness({'virtualdomain': domain}) except MoulinetteError: @@ -108,7 +106,10 @@ def domain_add(auth, domain, dyndns=False): try: yunohost.certificate._certificate_install_selfsigned([domain], False) - attr_dict['virtualdomain'] = domain + attr_dict = { + 'objectClass': ['mailDomain', 'top'], + 'virtualdomain': domain, + } if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict): raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed')) From 968259239dbd763bb3ed27c42f1e559a51ce491a Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Sun, 13 Aug 2017 23:40:44 +0200 Subject: [PATCH 8/9] [mod] call directly python code instead of using os.system --- src/yunohost/domain.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 4690b2f3f..457157160 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -141,6 +141,7 @@ def domain_remove(auth, domain, force=False): """ from yunohost.hook import hook_callback + from yunohost.app import app_ssowatconf if not force and domain not in domain_list(auth)['domains']: raise MoulinetteError(errno.EINVAL, m18n.n('domain_unknown')) @@ -167,7 +168,7 @@ def domain_remove(auth, domain, force=False): raise MoulinetteError(errno.EIO, m18n.n('domain_deletion_failed')) 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]) From 360ac66bc75f035217d61490967f6275698e6979 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sat, 19 Aug 2017 11:11:49 -0400 Subject: [PATCH 9/9] Use app_ssowatconf instead of os.system call --- src/yunohost/domain.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/yunohost/domain.py b/src/yunohost/domain.py index 457157160..d28e32d36 100644 --- a/src/yunohost/domain.py +++ b/src/yunohost/domain.py @@ -73,6 +73,7 @@ def domain_add(auth, domain, dyndns=False): """ from yunohost.hook import hook_callback + from yunohost.app import app_ssowatconf try: auth.validate_uniqueness({'virtualdomain': domain}) @@ -114,9 +115,10 @@ def domain_add(auth, domain, dyndns=False): if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict): raise MoulinetteError(errno.EIO, m18n.n('domain_creation_failed')) + # Don't regen these conf if we're still in postinstall if os.path.exists('/etc/yunohost/installed'): service_regen_conf(names=['nginx', 'metronome', 'dnsmasq', 'rmilter']) - os.system('yunohost app ssowatconf > /dev/null 2>&1') + app_ssowatconf(auth) except: # Force domain removal silently