mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-13 10:30:16 +00:00
Improve exception handle when running actions
- Don't use depricated exception.message. Instead using exception.args - Pass stdout output along with stderr in exception - Handle possible exceptions such as ValueError, OSError in all cases - Log all command executions and their errors
This commit is contained in:
parent
b77c97e088
commit
642a4e10ff
12
actions.py
12
actions.py
@ -72,6 +72,7 @@ Actions run commands with this contract (version 1.1):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import pipes
|
import pipes
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -80,6 +81,9 @@ import cfg
|
|||||||
from errors import ActionError
|
from errors import ActionError
|
||||||
|
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def run(action, options=None, async=False):
|
def run(action, options=None, async=False):
|
||||||
"""Safely run a specific action as the current user.
|
"""Safely run a specific action as the current user.
|
||||||
See actions._run for more information.
|
See actions._run for more information.
|
||||||
@ -107,7 +111,6 @@ def _run(action, options=None, async=False, run_as_root=False):
|
|||||||
|
|
||||||
# contract 3A and 3B: don't call anything outside of the actions directory.
|
# contract 3A and 3B: don't call anything outside of the actions directory.
|
||||||
if os.sep in action:
|
if os.sep in action:
|
||||||
# TODO: perhaps we should raise an ActionError instead of ValueError
|
|
||||||
raise ValueError("Action can't contain:" + os.sep)
|
raise ValueError("Action can't contain:" + os.sep)
|
||||||
|
|
||||||
cmd = cfg.actions_dir + os.sep + action
|
cmd = cfg.actions_dir + os.sep + action
|
||||||
@ -131,6 +134,8 @@ def _run(action, options=None, async=False, run_as_root=False):
|
|||||||
if run_as_root:
|
if run_as_root:
|
||||||
cmd = ["sudo", "-n"] + cmd
|
cmd = ["sudo", "-n"] + cmd
|
||||||
|
|
||||||
|
LOGGER.info('Executing command - %s', cmd)
|
||||||
|
|
||||||
# contract 3C: don't interpret shell escape sequences.
|
# contract 3C: don't interpret shell escape sequences.
|
||||||
# contract 5 (and 6-ish).
|
# contract 5 (and 6-ish).
|
||||||
proc = subprocess.Popen(
|
proc = subprocess.Popen(
|
||||||
@ -142,5 +147,8 @@ def _run(action, options=None, async=False, run_as_root=False):
|
|||||||
if not async:
|
if not async:
|
||||||
output, error = proc.communicate()
|
output, error = proc.communicate()
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
raise ActionError('Running action %s failed: %s' % (action, error))
|
LOGGER.error('Error executing command - %s, %s, %s', cmd, output,
|
||||||
|
error)
|
||||||
|
raise ActionError(action, output, error)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|||||||
@ -32,7 +32,6 @@ import socket
|
|||||||
import actions
|
import actions
|
||||||
import cfg
|
import cfg
|
||||||
import util
|
import util
|
||||||
from errors import ActionError
|
|
||||||
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
@ -136,9 +135,9 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
if old_status['hostname'] != new_status['hostname']:
|
if old_status['hostname'] != new_status['hostname']:
|
||||||
try:
|
try:
|
||||||
set_hostname(new_status['hostname'])
|
set_hostname(new_status['hostname'])
|
||||||
except (ActionError, ValueError) as err:
|
except Exception as exception:
|
||||||
messages.error(request, _('Error setting hostname: %s') %
|
messages.error(request, _('Error setting hostname: %s') %
|
||||||
err.message)
|
str(exception))
|
||||||
else:
|
else:
|
||||||
messages.success(request, _('Hostname set'))
|
messages.success(request, _('Hostname set'))
|
||||||
else:
|
else:
|
||||||
@ -147,9 +146,9 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
if old_status['time_zone'] != new_status['time_zone']:
|
if old_status['time_zone'] != new_status['time_zone']:
|
||||||
try:
|
try:
|
||||||
actions.superuser_run('timezone-change', [new_status['time_zone']])
|
actions.superuser_run('timezone-change', [new_status['time_zone']])
|
||||||
except (ActionError, ValueError) as err:
|
except Exception as exception:
|
||||||
messages.error(request, _('Error setting time zone: %s') %
|
messages.error(request, _('Error setting time zone: %s') %
|
||||||
err.message)
|
str(exception))
|
||||||
else:
|
else:
|
||||||
messages.success(request, _('Time zone set'))
|
messages.success(request, _('Time zone set'))
|
||||||
else:
|
else:
|
||||||
@ -163,9 +162,6 @@ def set_hostname(hostname):
|
|||||||
hostname = str(hostname)
|
hostname = str(hostname)
|
||||||
|
|
||||||
LOGGER.info('Changing hostname to - %s', hostname)
|
LOGGER.info('Changing hostname to - %s', hostname)
|
||||||
try:
|
|
||||||
actions.superuser_run('xmpp-pre-hostname-change')
|
actions.superuser_run('xmpp-pre-hostname-change')
|
||||||
actions.superuser_run('hostname-change', hostname)
|
actions.superuser_run('hostname-change', hostname)
|
||||||
actions.superuser_run('xmpp-hostname-change', hostname, async=True)
|
actions.superuser_run('xmpp-hostname-change', hostname, async=True)
|
||||||
except OSError as err:
|
|
||||||
raise ActionError(err.message)
|
|
||||||
|
|||||||
@ -44,12 +44,15 @@ def index(request):
|
|||||||
@login_required
|
@login_required
|
||||||
def test(request):
|
def test(request):
|
||||||
"""Run diagnostics and the output page"""
|
"""Run diagnostics and the output page"""
|
||||||
output = ""
|
output = ''
|
||||||
error = ""
|
error = ''
|
||||||
try:
|
try:
|
||||||
output = actions.superuser_run("diagnostic-test")
|
output = actions.superuser_run("diagnostic-test")
|
||||||
except ActionError as err:
|
except ActionError as exception:
|
||||||
error = err.message
|
output, error = exception.args[1:]
|
||||||
|
except Exception as exception:
|
||||||
|
error = str(exception)
|
||||||
|
|
||||||
return TemplateResponse(request, 'diagnostics_test.html',
|
return TemplateResponse(request, 'diagnostics_test.html',
|
||||||
{'title': _('Diagnostic Test'),
|
{'title': _('Diagnostic Test'),
|
||||||
'diagnostics_output': output,
|
'diagnostics_output': output,
|
||||||
|
|||||||
@ -140,11 +140,8 @@ def on_service_enabled(sender, service_id, enabled, **kwargs):
|
|||||||
def _run(arguments, superuser=False):
|
def _run(arguments, superuser=False):
|
||||||
"""Run an given command and raise exception if there was an error"""
|
"""Run an given command and raise exception if there was an error"""
|
||||||
command = 'firewall'
|
command = 'firewall'
|
||||||
LOGGER.info('Running command - %s, %s, %s', command, arguments, superuser)
|
|
||||||
|
|
||||||
if superuser:
|
if superuser:
|
||||||
output = actions.superuser_run(command, arguments)
|
return actions.superuser_run(command, arguments)
|
||||||
else:
|
else:
|
||||||
output = actions.run(command, arguments)
|
return actions.run(command, arguments)
|
||||||
|
|
||||||
return output
|
|
||||||
|
|||||||
@ -6,7 +6,6 @@ from gettext import gettext as _
|
|||||||
|
|
||||||
import actions
|
import actions
|
||||||
import cfg
|
import cfg
|
||||||
from errors import ActionError
|
|
||||||
|
|
||||||
|
|
||||||
def get_modules_available():
|
def get_modules_available():
|
||||||
@ -86,7 +85,7 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
try:
|
try:
|
||||||
actions.superuser_run(
|
actions.superuser_run(
|
||||||
'module-manager', ['enable', cfg.python_root, module])
|
'module-manager', ['enable', cfg.python_root, module])
|
||||||
except ActionError:
|
except Exception:
|
||||||
# TODO: need to get plinth to load the module we just
|
# TODO: need to get plinth to load the module we just
|
||||||
# enabled
|
# enabled
|
||||||
messages.error(
|
messages.error(
|
||||||
@ -100,7 +99,7 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
try:
|
try:
|
||||||
actions.superuser_run(
|
actions.superuser_run(
|
||||||
'module-manager', ['disable', cfg.python_root, module])
|
'module-manager', ['disable', cfg.python_root, module])
|
||||||
except ActionError:
|
except Exception:
|
||||||
# TODO: need a smoother way for plinth to unload the
|
# TODO: need a smoother way for plinth to unload the
|
||||||
# module
|
# module
|
||||||
messages.error(
|
messages.error(
|
||||||
|
|||||||
@ -197,10 +197,8 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
def _run(arguments, superuser=True):
|
def _run(arguments, superuser=True):
|
||||||
"""Run an given command and raise exception if there was an error"""
|
"""Run an given command and raise exception if there was an error"""
|
||||||
command = 'pagekite-configure'
|
command = 'pagekite-configure'
|
||||||
LOGGER.info('Running command - %s, %s, %s', command, arguments, superuser)
|
|
||||||
|
|
||||||
if superuser:
|
if superuser:
|
||||||
output = actions.superuser_run(command, arguments)
|
return actions.superuser_run(command, arguments)
|
||||||
else:
|
else:
|
||||||
output = actions.run(command, arguments)
|
return actions.run(command, arguments)
|
||||||
return output
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user