chore(coreos-device): better error handling

This commit is contained in:
Samuel Dolt 2023-02-06 11:14:36 +01:00
parent f086fe20de
commit 61781d6cd5
1 changed files with 15 additions and 5 deletions

View File

@ -46,6 +46,12 @@ def swupdate_www_push(image, device, port):
loop = asyncio.new_event_loop()
returncode = loop.run_until_complete(swupdate_www_push_async(image, device, port))
loop.close()
if returncode == 0:
click.secho("coreos-device: Update was successfull", bg="green")
else:
click.secho(f"coreos-device: Update has failed!", bg="red")
sys.exit(returncode)
@ -74,10 +80,10 @@ async def swupdate_www_push_data(session, url, image):
try:
async with session.post(f"{url}/upload", data=data) as resp:
if resp.status == 200:
click.secho("coreos-device: Update was successfull", bg="green")
click.secho(f"coreos-device: http status {resp.status}", bg="white", fg="black")
return 0
else:
click.secho(f"coreos-device: status {resp.status}, {await resp.text}", bg="red")
click.secho(f"coreos-device: http status {resp.status}, {await resp.text()}", bg="red")
return resp.status
except Exception as e:
click.secho(f"coreos-device: ERROR: {e}", bg="red")
@ -85,6 +91,7 @@ async def swupdate_www_push_data(session, url, image):
async def swupdate_www_websocket_logger(session, url):
return_code = 0
try:
async with session.ws_connect(f"{url}/ws") as ws:
async for msg in ws:
@ -94,15 +101,18 @@ async def swupdate_www_websocket_logger(session, url):
if "Waiting for requests" in msg:
await ws.close()
return 1
elif "ERROR" in msg:
elif "ERROR" in msg or "not successful" in msg:
click.secho("swupdate: " + data["text"], fg='red')
return_code = 1
elif "successful" in msg:
click.secho("swupdate: " + data["text"], fg="green")
else:
click.secho("swupdate: " + data["text"], fg='green')
click.secho("swupdate: " + data["text"])
if data["type"] == "status":
if data["status"] == "DONE":
await ws.close()
return 0
return return_code
except Exception as e:
click.secho(f"coreos-device: ERROR: {e}", bg="red")
return 1