mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[enh] Quickly implement logging in service.py and use it to show the diff
This commit is contained in:
parent
ea84f99461
commit
b7fcf9ffdb
2 changed files with 22 additions and 29 deletions
|
@ -120,7 +120,7 @@
|
||||||
"service_no_log" : "No log to display for service '{:s}'",
|
"service_no_log" : "No log to display for service '{:s}'",
|
||||||
"service_cmd_exec_failed" : "Unable to execute command '{:s}'",
|
"service_cmd_exec_failed" : "Unable to execute command '{:s}'",
|
||||||
"services_configured": "Configuration successfully generated",
|
"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",
|
"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}",
|
"service_add_configuration": "Adding the configuration file {file:s}",
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ import difflib
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
from moulinette.core import MoulinetteError
|
from moulinette.core import MoulinetteError
|
||||||
|
from moulinette.utils import log
|
||||||
|
|
||||||
template_dir = os.getenv(
|
template_dir = os.getenv(
|
||||||
'YUNOHOST_TEMPLATE_DIR',
|
'YUNOHOST_TEMPLATE_DIR',
|
||||||
|
@ -44,6 +45,9 @@ conf_backup_dir = os.getenv(
|
||||||
'/home/yunohost.backup/conffiles'
|
'/home/yunohost.backup/conffiles'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger = log.getActionLogger('yunohost.service')
|
||||||
|
|
||||||
|
|
||||||
def service_add(name, status=None, log=None, runlevel=None):
|
def service_add(name, status=None, log=None, runlevel=None):
|
||||||
"""
|
"""
|
||||||
Add a custom service
|
Add a custom service
|
||||||
|
@ -73,7 +77,7 @@ def service_add(name, status=None, log=None, runlevel=None):
|
||||||
except:
|
except:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('service_add_failed', name))
|
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):
|
def service_remove(name):
|
||||||
|
@ -96,7 +100,7 @@ def service_remove(name):
|
||||||
except:
|
except:
|
||||||
raise MoulinetteError(errno.EIO, m18n.n('service_remove_failed', name))
|
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):
|
def service_start(names):
|
||||||
|
@ -111,12 +115,12 @@ def service_start(names):
|
||||||
names = [names]
|
names = [names]
|
||||||
for name in names:
|
for name in names:
|
||||||
if _run_service_command('start', name):
|
if _run_service_command('start', name):
|
||||||
msignals.display(m18n.n('service_started', name), 'success')
|
logger.success(m18n.n('service_started', name))
|
||||||
else:
|
else:
|
||||||
if service_status(name)['status'] != 'running':
|
if service_status(name)['status'] != 'running':
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('service_start_failed', name))
|
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):
|
def service_stop(names):
|
||||||
|
@ -131,12 +135,12 @@ def service_stop(names):
|
||||||
names = [names]
|
names = [names]
|
||||||
for name in names:
|
for name in names:
|
||||||
if _run_service_command('stop', name):
|
if _run_service_command('stop', name):
|
||||||
msignals.display(m18n.n('service_stopped', name), 'success')
|
logger.success(m18n.n('service_stopped', name))
|
||||||
else:
|
else:
|
||||||
if service_status(name)['status'] != 'inactive':
|
if service_status(name)['status'] != 'inactive':
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('service_stop_failed', name))
|
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):
|
def service_enable(names):
|
||||||
|
@ -151,7 +155,7 @@ def service_enable(names):
|
||||||
names = [names]
|
names = [names]
|
||||||
for name in names:
|
for name in names:
|
||||||
if _run_service_command('enable', name):
|
if _run_service_command('enable', name):
|
||||||
msignals.display(m18n.n('service_enabled', name), 'success')
|
logger.success(m18n.n('service_enabled', name))
|
||||||
else:
|
else:
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('service_enable_failed', name))
|
m18n.n('service_enable_failed', name))
|
||||||
|
@ -169,7 +173,7 @@ def service_disable(names):
|
||||||
names = [names]
|
names = [names]
|
||||||
for name in names:
|
for name in names:
|
||||||
if _run_service_command('disable', name):
|
if _run_service_command('disable', name):
|
||||||
msignals.display(m18n.n('service_disabled', name), 'success')
|
logger.success(m18n.n('service_disabled', name))
|
||||||
else:
|
else:
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('service_disable_failed', name))
|
m18n.n('service_disable_failed', name))
|
||||||
|
@ -217,8 +221,7 @@ def service_status(names=[]):
|
||||||
shell=True)
|
shell=True)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
if 'usage:' in e.output.lower():
|
if 'usage:' in e.output.lower():
|
||||||
msignals.display(m18n.n('service_status_failed', name),
|
logger.warning(m18n.n('service_status_failed', name))
|
||||||
'warning')
|
|
||||||
else:
|
else:
|
||||||
result[name]['status'] = 'inactive'
|
result[name]['status'] = 'inactive'
|
||||||
else:
|
else:
|
||||||
|
@ -288,7 +291,7 @@ def service_regenconf(service=None, force=False):
|
||||||
hook_callback('conf_regen', [service], args=[force])
|
hook_callback('conf_regen', [service], args=[force])
|
||||||
else:
|
else:
|
||||||
hook_callback('conf_regen', args=[force])
|
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):
|
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)
|
ret = subprocess.check_output(cmd.split(), stderr=subprocess.STDOUT)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
# TODO: Log output?
|
# TODO: Log output?
|
||||||
msignals.display(m18n.n('service_cmd_exec_failed', ' '.join(e.cmd)),
|
logger.warning(m18n.n('service_cmd_exec_failed', ' '.join(e.cmd)))
|
||||||
'warning')
|
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -469,10 +471,8 @@ def service_saferemove(service, conf_file, force=False):
|
||||||
os.remove(conf_backup_file)
|
os.remove(conf_backup_file)
|
||||||
if os.isatty(1) and \
|
if os.isatty(1) and \
|
||||||
(len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
|
(len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
|
||||||
msignals.display(
|
logger.warning(m18n.n('service_configuration_conflict',
|
||||||
m18n.n('service_configuration_conflict', file=conf_file),
|
file=conf_file))
|
||||||
'warning'
|
|
||||||
)
|
|
||||||
|
|
||||||
_save_services(services)
|
_save_services(services)
|
||||||
|
|
||||||
|
@ -509,8 +509,7 @@ def service_safecopy(service, new_conf_file, conf_file, force=False):
|
||||||
)
|
)
|
||||||
process.wait()
|
process.wait()
|
||||||
else:
|
else:
|
||||||
msignals.display(m18n.n('service_add_configuration', file=conf_file),
|
logger.info(m18n.n('service_add_configuration', file=conf_file))
|
||||||
'info')
|
|
||||||
|
|
||||||
# Add the service if it does not exist
|
# Add the service if it does not exist
|
||||||
if service not in services.keys():
|
if service not in services.keys():
|
||||||
|
@ -539,15 +538,9 @@ def service_safecopy(service, new_conf_file, conf_file, force=False):
|
||||||
else:
|
else:
|
||||||
new_hash = previous_hash
|
new_hash = previous_hash
|
||||||
if (len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
|
if (len(previous_hash) == 32 or previous_hash[-32:] != current_hash):
|
||||||
msignals.display(
|
logger.warning(m18n.n('service_configuration_conflict',
|
||||||
m18n.n('service_configuration_conflict', file=conf_file),
|
file=conf_file, diff=''.join(diff)))
|
||||||
'warning'
|
|
||||||
)
|
|
||||||
print('\n' + conf_file)
|
|
||||||
for line in diff:
|
|
||||||
print(line.strip())
|
|
||||||
print('')
|
|
||||||
|
|
||||||
# Remove the backup file if the configuration has not changed
|
# Remove the backup file if the configuration has not changed
|
||||||
if new_hash == previous_hash:
|
if new_hash == previous_hash:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Reference in a new issue