From 9127373444102e76a87f85f9fcc29c33bcaf30c6 Mon Sep 17 00:00:00 2001 From: William Grzybowski Date: Wed, 11 Dec 2024 13:07:59 -0300 Subject: [PATCH] Improve error handling for instance start failure Log file will not always be available. --- .../middlewared/plugins/virt/instance.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/middlewared/middlewared/plugins/virt/instance.py b/src/middlewared/middlewared/plugins/virt/instance.py index dd3a82a4b40df..f5e336da1aca3 100644 --- a/src/middlewared/middlewared/plugins/virt/instance.py +++ b/src/middlewared/middlewared/plugins/virt/instance.py @@ -1,4 +1,5 @@ import aiohttp +import json import os import platform @@ -287,15 +288,22 @@ async def start(self, job, id): await incus_call_and_wait(f'1.0/instances/{id}/state', 'put', {'json': { 'action': 'start', }}) - except CallError: + except CallError as e: log = 'lxc.log' if instance['type'] == 'CONTAINER' else 'qemu.log' content = await incus_call(f'1.0/instances/{id}/logs/{log}', 'get', json=False) output = [] while line := await content.readline(): output.append(line) output = output[-10:] - await job.logs_fd_write(b''.join(output).strip()) - raise CallError('Failed to start instance. Please check job logs.') + output = b''.join(output).strip() + errmsg = f'Failed to start instance: {e.errmsg}.' + try: + # If we get a json means there is no log file + json.loads(output.decode()) + except json.decoder.JSONDecodeError: + await job.logs_fd_write(output) + errmsg += ' Please check job logs.' + raise CallError(errmsg) return True