upgrades: Handle action errors using error codes

- This is more reliable than string parsing in the output.

- We will be using pythonic try/catches instead of if conditions.
This commit is contained in:
Sunil Mohan Adapa 2015-02-10 14:54:53 +05:30 committed by James Valleroy
parent 82fbbe1fec
commit 1f43be95a0
3 changed files with 29 additions and 16 deletions

View File

@ -24,6 +24,7 @@ import argparse
import os
import re
import subprocess
import sys
CONF_FILE = '/etc/apt/apt.conf.d/50unattended-upgrades'
AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades'
@ -55,24 +56,31 @@ def subcommand_run(_):
try:
setup()
except FileNotFoundError:
print('Error: Could not configure unattended-upgrades.')
return
print('Error: Could not configure unattended-upgrades.',
file=sys.stderr)
sys.exit(1)
try:
output = subprocess.check_output(['unattended-upgrades', '-v'])
except subprocess.CalledProcessError as error:
print('Error: %s', error)
except FileNotFoundError:
print('Error: unattended-upgrades is not available.')
print('Error: unattended-upgrades is not available.', file=sys.stderr)
sys.exit(2)
except Exception as error:
print('Error: {0}'.format(error), file=sys.stderr)
sys.exit(3)
else:
print('%s', output.decode())
print(output.decode())
def subcommand_check_auto(_):
"""Check if automatic upgrades are enabled"""
arguments = ['apt-config', 'shell', 'UpdateInterval',
'APT::Periodic::Update-Package-Lists']
output = subprocess.check_output(arguments).decode()
try:
output = subprocess.check_output(arguments).decode()
except subprocess.CalledProcessError as error:
print('Error: {0}'.format(error), file=sys.stderr)
sys.exit(1)
update_interval = 0
match = re.match(r"UpdateInterval='(.*)'", output)
@ -87,8 +95,9 @@ def subcommand_enable_auto(_):
try:
setup()
except FileNotFoundError:
print('Error: Could not configure unattended-upgrades.')
return
print('Error: Could not configure unattended-upgrades.',
file=sys.stderr)
sys.exit(1)
with open(AUTO_CONF_FILE, 'w') as conffile:
conffile.write('APT::Periodic::Update-Package-Lists "1";\n')

View File

@ -24,8 +24,9 @@
{% if upgrades_error %}
<div class="alert alert-danger" role="alert">
There was an error while upgrading:
There was an error while upgrading.
</div>
<h5>Output from unattended-upgrades:</h5>
<pre>{{ upgrades_error }}</pre>
{% endif %}

View File

@ -124,13 +124,16 @@ def _apply_changes(request, old_status, new_status):
else:
option = 'disable-auto'
output = actions.superuser_run('upgrades', [option])
try:
actions.superuser_run('upgrades', [option])
except ActionError as exception:
error = exception.args[2]
messages.error(
request, _('Error when configuring unattended-upgrades: %s') %
error)
return
if 'Error' in output:
messages.error(request,
_('Error when configuring unattended-upgrades: %s') %
output)
elif option == 'enable-auto':
if option == 'enable-auto':
messages.success(request, _('Automatic upgrades enabled'))
else:
messages.success(request, _('Automatic upgrades disabled'))