Skip to content

Commit

Permalink
Improve error handling for instance start failure
Browse files Browse the repository at this point in the history
Log file will not always be available.
  • Loading branch information
william-gr committed Dec 11, 2024
1 parent b14e7d9 commit 9127373
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/middlewared/middlewared/plugins/virt/instance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import aiohttp
import json
import os
import platform

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9127373

Please sign in to comment.