diff --git a/domain.py b/domain.py index 5faa37acb..2576dbd20 100644 --- a/domain.py +++ b/domain.py @@ -75,7 +75,10 @@ def domain_add(auth, domains, main=False, dyndns=False): """ attr_dict = { 'objectClass' : ['mailDomain', 'top'] } - ip = str(urlopen('http://ip.yunohost.org').read()) + try: + ip = str(urlopen('http://ip.yunohost.org').read()) + except IOError: + ip = "127.0.0.1" now = datetime.datetime.now() timestamp = str(now.year) + str(now.month) + str(now.day) result = [] @@ -94,17 +97,21 @@ def domain_add(auth, domains, main=False, dyndns=False): import requests from yunohost.dyndns import dyndns_subscribe - r = requests.get('http://dyndns.yunohost.org/domains') - 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) + try: + r = requests.get('http://dyndns.yunohost.org/domains') + except ConnectionError: + pass else: - raise MoulinetteError(errno.EINVAL, - m18n.n('domain_dyndns_root_unknown')) + 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')) # Commands ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA' diff --git a/dyndns.py b/dyndns.py index 7a37fa118..6a5dc9f06 100644 --- a/dyndns.py +++ b/dyndns.py @@ -49,8 +49,11 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None domain = f.readline().rstrip() # Verify if domain is available - if requests.get('http://%s/test/%s' % (subscribe_host, domain)).status_code != 200: - raise MoulinetteError(errno.EEXIST, m18n.n('dyndns_unavailable')) + try: + if requests.get('http://%s/test/%s' % (subscribe_host, domain)).status_code != 200: + raise MoulinetteError(errno.EEXIST, m18n.n('dyndns_unavailable')) + except ConnectionError: + raise MoulinetteError(errno.ENETUNREACH, m18n.n('no_internet_connection')) if key is None: if len(glob.glob('/etc/yunohost/dyndns/*.key')) == 0: @@ -64,7 +67,10 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None key = f.readline().strip().split(' ')[-1] # Send subscription - r = requests.post('http://%s/key/%s' % (subscribe_host, base64.b64encode(key)), data={ 'subdomain': domain }) + try: + r = requests.post('http://%s/key/%s' % (subscribe_host, base64.b64encode(key)), data={ 'subdomain': domain }) + except ConnectionError: + raise MoulinetteError(errno.ENETUNREACH, m18n.n('no_internet_connection')) if r.status_code != 201: try: error = json.loads(r.text)['error'] except: error = "Server error" @@ -92,7 +98,10 @@ def dyndns_update(dyn_host="dynhost.yunohost.org", domain=None, key=None, ip=Non domain = f.readline().rstrip() if ip is None: - new_ip = requests.get('http://ip.yunohost.org').text + try: + new_ip = requests.get('http://ip.yunohost.org').text + except ConnectionError: + raise MoulinetteError(errno.ENETUNREACH, m18n.n('no_internet_connection')) else: new_ip = ip diff --git a/locales/en.json b/locales/en.json index 161597835..fc61ac58f 100644 --- a/locales/en.json +++ b/locales/en.json @@ -49,6 +49,7 @@ "domain_uninstall_app_first" : "One or more apps are installed on this domain. Please uninstall them before proceed to domain removal.", "domain_deletion_failed" : "Unable to delete domain", "domain_deleted" : "Domain successfully deleted", + "no_internet_connection": "Server not connected to the Internet", "dyndns_key_generating" : "DNS key is being generated, it may take a while...", "dyndns_unavailable" : "Unavailable DynDNS subdomain", diff --git a/tools.py b/tools.py index 020f79534..9f69652bf 100644 --- a/tools.py +++ b/tools.py @@ -163,11 +163,15 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): if dyndns: dyndns_subscribe(domain=new_domain) elif len(new_domain.split('.')) >= 3: - r = requests.get('http://dyndns.yunohost.org/domains') - dyndomains = json.loads(r.text) - dyndomain = '.'.join(new_domain.split('.')[1:]) - if dyndomain in dyndomains: - dyndns_subscribe(domain=new_domain) + try: + r = requests.get('http://dyndns.yunohost.org/domains') + except ConnectionError: + pass + else: + dyndomains = json.loads(r.text) + dyndomain = '.'.join(new_domain.split('.')[1:]) + if dyndomain in dyndomains: + dyndns_subscribe(domain=new_domain) msignals.display(m18n.n('maindomain_changed'), 'success') @@ -196,14 +200,18 @@ def tools_postinstall(domain, password, dyndns=False): raise MoulinetteError(errno.EPERM, m18n.n('yunohost_already_installed')) if len(domain.split('.')) >= 3: - r = requests.get('http://dyndns.yunohost.org/domains') - dyndomains = json.loads(r.text) - dyndomain = '.'.join(domain.split('.')[1:]) - if dyndomain in dyndomains: - if requests.get('http://dyndns.yunohost.org/test/%s' % domain).status_code == 200: - dyndns=True - else: - raise MoulinetteError(errno.EEXIST, + try: + r = requests.get('http://dyndns.yunohost.org/domains') + except ConnectionError: + pass + else: + dyndomains = json.loads(r.text) + dyndomain = '.'.join(domain.split('.')[1:]) + if dyndomain in dyndomains: + if requests.get('http://dyndns.yunohost.org/test/%s' % domain).status_code == 200: + dyndns=True + else: + raise MoulinetteError(errno.EEXIST, m18n.n('dyndns_unavailable')) # Create required folders