From dfbfc0cfc7ab35e8a1188b647c7f8c3a3915e62a Mon Sep 17 00:00:00 2001 From: ljf Date: Thu, 24 Nov 2016 02:55:59 +0100 Subject: [PATCH 1/4] [fix] Can't get mailbos used space if dovecot is down --- locales/en.json | 1 + src/yunohost/user.py | 46 +++++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/locales/en.json b/locales/en.json index e939b26f..a87a459b 100644 --- a/locales/en.json +++ b/locales/en.json @@ -118,6 +118,7 @@ "mail_alias_remove_failed": "Unable to remove mail alias '{mail:s}'", "mail_domain_unknown": "Unknown mail address domain '{domain:s}'", "mail_forward_remove_failed": "Unable to remove mail forward '{mail:s}'", + "mailbox_used_space_dovecot_down": "Dovecot mailbox service need to be up, if you want to get mailbox used space", "maindomain_change_failed": "Unable to change the main domain", "maindomain_changed": "The main domain has been changed", "monitor_disabled": "The server monitoring has been disabled", diff --git a/src/yunohost/user.py b/src/yunohost/user.py index ec7dd539..348a23d3 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -92,7 +92,7 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None): def user_create(auth, username, firstname, lastname, mail, password, - mailbox_quota=0): + mailbox_quota="0"): """ Create user @@ -398,20 +398,36 @@ def user_info(auth, username): result_dict['mail-forward'] = user['maildrop'][1:] if 'mailuserquota' in user: - if user['mailuserquota'][0] != '0': - cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] - userquota = subprocess.check_output(cmd,stderr=subprocess.STDOUT, - shell=True) - quotavalue = re.findall(r'\d+', userquota) - result = '%s (%s%s)' % ( _convertSize(eval(quotavalue[0])), - quotavalue[2], '%') - result_dict['mailbox-quota'] = { - 'limit' : user['mailuserquota'][0], - 'use' : result - } - else: - result_dict['mailbox-quota'] = m18n.n('unlimit') - + userquota = user['mailuserquota'][0] + if isinstance( userquota, int ): + userquota = str(userquota) + + # Test if userquota is '0' or '0M' ( quota pattern is ^(\d+[bkMGT])|0$ ) + is_limited = not re.match('0[bkMGT]?', userquota) + storage_use = '?' + cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] + try: + cmd_result = subprocess.check_output(cmd,stderr=subprocess.STDOUT, + shell=True) + # Exemple of return value for cmd: + # """Quota name=User quota Type=STORAGE Value=0 Limit=- %=0 + # Quota name=User quota Type=MESSAGE Value=0 Limit=- %=0""" + has_value=re.search(r'Value=(\d+)', cmd_result) + if has_value: + storage_use = int(has_value.group(1)) + storage_use = _convertSize(storage_use) + if is_limited: + has_percent=re.search(r'%=(\d+)', cmd_result) + if has_percent: + percentage = int(has_percent.group(1)) + storage_use += ' (%s%s)' % (percentage, '%') + except subprocess.CalledProcessError: + logger.warning(m18n.n('mailbox_used_space_dovecot_down')) + result_dict['mailbox-quota'] = { + 'limit' : userquota if is_limited else m18n.n('unlimit'), + 'use' : storage_use + } + if result: return result_dict else: From d85657a5c60afc8c9387e830d8250dd0e950dd18 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Wed, 7 Dec 2016 21:54:13 +0100 Subject: [PATCH 2/4] [mod] style --- src/yunohost/user.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/yunohost/user.py b/src/yunohost/user.py index 348a23d3..6c73c5ba 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -399,30 +399,38 @@ def user_info(auth, username): if 'mailuserquota' in user: userquota = user['mailuserquota'][0] - if isinstance( userquota, int ): + + if isinstance(userquota, int): userquota = str(userquota) # Test if userquota is '0' or '0M' ( quota pattern is ^(\d+[bkMGT])|0$ ) is_limited = not re.match('0[bkMGT]?', userquota) storage_use = '?' + cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] + try: - cmd_result = subprocess.check_output(cmd,stderr=subprocess.STDOUT, + cmd_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) # Exemple of return value for cmd: # """Quota name=User quota Type=STORAGE Value=0 Limit=- %=0 # Quota name=User quota Type=MESSAGE Value=0 Limit=- %=0""" - has_value=re.search(r'Value=(\d+)', cmd_result) + has_value = re.search(r'Value=(\d+)', cmd_result) + if has_value: storage_use = int(has_value.group(1)) storage_use = _convertSize(storage_use) + if is_limited: - has_percent=re.search(r'%=(\d+)', cmd_result) + has_percent = re.search(r'%=(\d+)', cmd_result) + if has_percent: percentage = int(has_percent.group(1)) - storage_use += ' (%s%s)' % (percentage, '%') + storage_use += ' (%s%%)' % percentage + except subprocess.CalledProcessError: logger.warning(m18n.n('mailbox_used_space_dovecot_down')) + result_dict['mailbox-quota'] = { 'limit' : userquota if is_limited else m18n.n('unlimit'), 'use' : storage_use From 0e2cae4f1b473b6734fda28066b80ce38c884c7c Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 11 Dec 2016 17:59:39 -0500 Subject: [PATCH 3/4] Use service_status to check if dovecot is running --- src/yunohost/user.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/yunohost/user.py b/src/yunohost/user.py index 6c73c5ba..65cfddae 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -35,6 +35,7 @@ import re from moulinette.core import MoulinetteError from moulinette.utils.log import getActionLogger +from yunohost.service import service_status logger = getActionLogger('yunohost.user') @@ -407,9 +408,10 @@ def user_info(auth, username): is_limited = not re.match('0[bkMGT]?', userquota) storage_use = '?' - cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] - - try: + if (service_status("dovecot")["status"] != "running"): + logger.warning(m18n.n('mailbox_used_space_dovecot_down')) + else: + cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0] cmd_result = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True) # Exemple of return value for cmd: @@ -428,9 +430,6 @@ def user_info(auth, username): percentage = int(has_percent.group(1)) storage_use += ' (%s%%)' % percentage - except subprocess.CalledProcessError: - logger.warning(m18n.n('mailbox_used_space_dovecot_down')) - result_dict['mailbox-quota'] = { 'limit' : userquota if is_limited else m18n.n('unlimit'), 'use' : storage_use From 6f313d943bb7ce47598b9c61336f3bc34d967966 Mon Sep 17 00:00:00 2001 From: Laurent Peuch Date: Mon, 12 Dec 2016 00:14:22 +0100 Subject: [PATCH 4/4] [mod] pep8 --- src/yunohost/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/user.py b/src/yunohost/user.py index 65cfddae..08ab4c64 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -408,7 +408,7 @@ def user_info(auth, username): is_limited = not re.match('0[bkMGT]?', userquota) storage_use = '?' - if (service_status("dovecot")["status"] != "running"): + if service_status("dovecot")["status"] != "running": logger.warning(m18n.n('mailbox_used_space_dovecot_down')) else: cmd = 'doveadm -f flow quota get -u %s' % user['uid'][0]