Connect to console websocket on container start/stop/restart

Hi,
For my personal purposes, I’d like to get what is displayed on the console when one type in :
root@server$ lxc {start|restart|stop} [instance] --console via javascript to display all these informations in a webapp I’m developing.

So far, here is what I did (in pseudo code) :
1 - PUT the desired state on 1.0/instances/instance/state, for example {‘action’: ‘start’, ‘record-output’:true}
2 - when readystate==4 and status==202, POST {} to 1.0/instances/instance/console to get the websocket’s console ids.
3 - open a websocket with the fetched parameters on wss://server/1.0/operations/[operation_id]/websocket?encodeURI(‘secret=’+fds[0])
4 - on event on this socket display what has been received.

However, this doesn’t seem to work fine and I must be missing something.

  1. when starting an instance, the websocket can’t be opened at this state, lxd answers “400 bad request” because the container is not in an state where the console is available. So the questions here is : when should I launch the websocket’s connection ? And when can I detect that the container is completely started so that I can release the websocket’s connexion ?
  2. when restarting an instance, we get only the end of the console’s messages :
    “Sending SIGTEM to all remaining processes…
    Sending SIGKILL to all remaining processes…
    Halting system”
    All that is before what happened (the stopping of all daemons is not fetched, the connection to the websocket must happen too late. Is there a way to get all messages ?
    And, as you might have anticipated, the start of the instance don’t display anything since the whole have been stopped, the connection to the websocket is destroyed…
  3. when stopping an instance, we fall with the same problems as the first part of the restarting process. I guess that this could be solved by connecting to the websocket before putting the “stop” action.

Any clue to have this working as I expect would be kind, mainly for the start process.

Regards.

For stop and restart, the CLI connects to the websocket prior to sending the state change request, that way you get all the output.

For start, you need to wait for the operation to succeed. The CLI does that pretty easily as it’s connected to /1.0/events and so gets instantly notified of operation changes.
An alternative for simpler use cases is to hit /1.0/operations/UUID/wait, then as soon as that returns, hit the console endpoint.

Thank you so much for your answer @stgraber.

I’ve successfully achieved the STOP part.
For the START part, I’ve successfully done the following :

  1. PUT the start action on the container start
  2. get /1.0/operations/UUID/wait
  3. POST on the console uri
    3.1) when the above gets ready (4 && 202) open the websocket on fds[0] and fds[control]

This kinda works, but here are the things I haven’t solved yet for the START part :

  • the first messages send by the console are lost. I guess that this is caused by the time taken to POST on the console uri and open the websockets.
  • I haven’t found any clue on when to close the websocket opened on the console. Idealy, it should be closed when the starting process is completely done.

I’ll try the restart tomorow.

If you have any clue for this…

Regards.