mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
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:
parent
82fbbe1fec
commit
1f43be95a0
@ -24,6 +24,7 @@ import argparse
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
CONF_FILE = '/etc/apt/apt.conf.d/50unattended-upgrades'
|
CONF_FILE = '/etc/apt/apt.conf.d/50unattended-upgrades'
|
||||||
AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades'
|
AUTO_CONF_FILE = '/etc/apt/apt.conf.d/20auto-upgrades'
|
||||||
@ -55,24 +56,31 @@ def subcommand_run(_):
|
|||||||
try:
|
try:
|
||||||
setup()
|
setup()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('Error: Could not configure unattended-upgrades.')
|
print('Error: Could not configure unattended-upgrades.',
|
||||||
return
|
file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output(['unattended-upgrades', '-v'])
|
output = subprocess.check_output(['unattended-upgrades', '-v'])
|
||||||
except subprocess.CalledProcessError as error:
|
|
||||||
print('Error: %s', error)
|
|
||||||
except FileNotFoundError:
|
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:
|
else:
|
||||||
print('%s', output.decode())
|
print(output.decode())
|
||||||
|
|
||||||
|
|
||||||
def subcommand_check_auto(_):
|
def subcommand_check_auto(_):
|
||||||
"""Check if automatic upgrades are enabled"""
|
"""Check if automatic upgrades are enabled"""
|
||||||
arguments = ['apt-config', 'shell', 'UpdateInterval',
|
arguments = ['apt-config', 'shell', 'UpdateInterval',
|
||||||
'APT::Periodic::Update-Package-Lists']
|
'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
|
update_interval = 0
|
||||||
match = re.match(r"UpdateInterval='(.*)'", output)
|
match = re.match(r"UpdateInterval='(.*)'", output)
|
||||||
@ -87,8 +95,9 @@ def subcommand_enable_auto(_):
|
|||||||
try:
|
try:
|
||||||
setup()
|
setup()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('Error: Could not configure unattended-upgrades.')
|
print('Error: Could not configure unattended-upgrades.',
|
||||||
return
|
file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
with open(AUTO_CONF_FILE, 'w') as conffile:
|
with open(AUTO_CONF_FILE, 'w') as conffile:
|
||||||
conffile.write('APT::Periodic::Update-Package-Lists "1";\n')
|
conffile.write('APT::Periodic::Update-Package-Lists "1";\n')
|
||||||
|
|||||||
@ -24,8 +24,9 @@
|
|||||||
|
|
||||||
{% if upgrades_error %}
|
{% if upgrades_error %}
|
||||||
<div class="alert alert-danger" role="alert">
|
<div class="alert alert-danger" role="alert">
|
||||||
There was an error while upgrading:
|
There was an error while upgrading.
|
||||||
</div>
|
</div>
|
||||||
|
<h5>Output from unattended-upgrades:</h5>
|
||||||
<pre>{{ upgrades_error }}</pre>
|
<pre>{{ upgrades_error }}</pre>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|||||||
@ -124,13 +124,16 @@ def _apply_changes(request, old_status, new_status):
|
|||||||
else:
|
else:
|
||||||
option = 'disable-auto'
|
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:
|
if option == 'enable-auto':
|
||||||
messages.error(request,
|
|
||||||
_('Error when configuring unattended-upgrades: %s') %
|
|
||||||
output)
|
|
||||||
elif option == 'enable-auto':
|
|
||||||
messages.success(request, _('Automatic upgrades enabled'))
|
messages.success(request, _('Automatic upgrades enabled'))
|
||||||
else:
|
else:
|
||||||
messages.success(request, _('Automatic upgrades disabled'))
|
messages.success(request, _('Automatic upgrades disabled'))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user