[enh] Quickly implement logging in service.py and use it to show the diff

This commit is contained in:
Jérôme Lebleu 2015-11-14 23:38:44 +01:00
parent ea84f99461
commit b7fcf9ffdb
2 changed files with 22 additions and 29 deletions

View file

@ -120,7 +120,7 @@
"service_no_log" : "No log to display for service '{:s}'",
"service_cmd_exec_failed" : "Unable to execute command '{:s}'",
"services_configured": "Configuration successfully generated",
"service_configuration_conflict": "The file {file:s} has been changed since its last generation. Please apply the modifications manually or use the option --force (it will erase all the modifications previously done to the file)",
"service_configuration_conflict": "The file {file:s} has been changed since its last generation. Please apply the modifications manually or use the option --force (it will erase all the modifications previously done to the file). Here are the differences:\n{diff:s}",
"no_such_conf_file": "Unable to copy the file {file:s}: the file does not exist",
"service_add_configuration": "Adding the configuration file {file:s}",

View file

@ -34,6 +34,7 @@ import difflib
import hashlib
from moulinette.core import MoulinetteError
from moulinette.utils import log
template_dir = os.getenv(
'YUNOHOST_TEMPLATE_DIR',
@ -44,6 +45,9 @@ conf_backup_dir = os.getenv(
'/home/yunohost.backup/conffiles'
)
logger = log.getActionLogger('yunohost.service')
def service_add(name, status=None, log=None, runlevel=None):
"""
Add a custom service
@ -73,7 +77,7 @@ def service_add(name, status=None, log=None, runlevel=None):
except:
raise MoulinetteError(errno.EIO, m18n.n('service_add_failed', name))
msignals.display(m18n.n('service_added'), 'success')
logger.success(m18n.n('service_added'))
def service_remove(name):
@ -96,7 +100,7 @@ def service_remove(name):
except:
raise MoulinetteError(errno.EIO, m18n.n('service_remove_failed', name))
msignals.display(m18n.n('service_removed'), 'success')
logger.success(m18n.n('service_removed'))
def service_start(names):
@ -111,12 +115,12 @@ def service_start(names):
names = [names]
for name in names:
if _run_service_command('start', name):
msignals.display(m18n.n('service_started', name), 'success')
logger.success(m18n.n('service_started', name))
else:
if service_status(name)['status'] != 'running':
raise MoulinetteError(errno.EPERM,
m18n.n('service_start_failed', name))
msignals.display(m18n.n('service_already_started', name))
logger.info(m18n.n('service_already_started', name))
def service_stop(names):
@ -131,12 +135,12 @@ def service_stop(names):
names = [names]
for name in names:
if _run_service_command('stop', name):
msignals.display(m18n.n('service_stopped', name), 'success')
logger.success(m18n.n('service_stopped', name))
else:
if service_status(name)['status'] != 'inactive':
raise MoulinetteError(errno.EPERM,
m18n.n('service_stop_failed', name))
msignals.display(m18n.n('service_already_stopped', name))
logger.info(m18n.n('service_already_stopped', name))
def service_enable(names):
@ -151,7 +155,7 @@ def service_enable(names):
names = [names]
for name in names:
if _run_service_command('enable', name):
msignals.display(m18n.n('service_enabled', name), 'success')
logger.success(m18n.n('service_enabled', name))
else:
raise MoulinetteError(errno.EPERM,
m18n.n('service_enable_failed', name))
@ -169,7 +173,7 @@ def service_disable(names):
names = [names]
for name in names:
if _run_service_command('disable', name):
msignals.display(m18n.n('service_disabled', name), 'success')
logger.success(m18n.n('service_disabled', name))
else:
raise MoulinetteError(errno.EPERM,
m18n.n('service_disable_failed', name))
@ -217,8 +221,7 @@ def service_status(names=[]):
shell=True)
except subprocess.CalledProcessError as e:
if 'usage:' in e.output.lower():
msignals.display(m18n.n('service_status_failed', name),
'warning')
logger.warning(m18n.n('service_status_failed', name))
else:
result[name]['status'] = 'inactive'
else:
@ -288,7 +291,7 @@ def service_regenconf(service=None, force=False):
hook_callback('conf_regen', [service], args=[force])
else:
hook_callback('conf_regen', args=[force])
msignals.display(m18n.n('services_configured'), 'success')
logger.success(m18n.n('services_configured'))
def _run_service_command(action, service):
@ -317,8 +320,7 @@ def _run_service_command(action, service):
ret = subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# TODO: Log output?
msignals.display(m18n.n('service_cmd_exec_failed', ' '.join(e.cmd)),
'warning')
logger.warning(m18n.n('service_cmd_exec_failed', ' '.join(e.cmd)))
return False
return True
@ -469,10 +471,8 @@ def service_saferemove(service, conf_file, force=False):
os.remove(conf_backup_file)
if os.isatty(1) and \
(len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
msignals.display(
m18n.n('service_configuration_conflict', file=conf_file),
'warning'
)
logger.warning(m18n.n('service_configuration_conflict',
file=conf_file))
_save_services(services)
@ -509,8 +509,7 @@ def service_safecopy(service, new_conf_file, conf_file, force=False):
)
process.wait()
else:
msignals.display(m18n.n('service_add_configuration', file=conf_file),
'info')
logger.info(m18n.n('service_add_configuration', file=conf_file))
# Add the service if it does not exist
if service not in services.keys():
@ -539,15 +538,9 @@ def service_safecopy(service, new_conf_file, conf_file, force=False):
else:
new_hash = previous_hash
if (len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
msignals.display(
m18n.n('service_configuration_conflict', file=conf_file),
'warning'
)
print('\n' + conf_file)
for line in diff:
print(line.strip())
print('')
logger.warning(m18n.n('service_configuration_conflict',
file=conf_file, diff=''.join(diff)))
# Remove the backup file if the configuration has not changed
if new_hash == previous_hash:
try: