Understanding of lxc exec REST API

(BOURIAUD David) #1

Hi !
I try to understand how the lxc exec REST API can be used and, so far I fall into something that don"t work as I expect it, so I guess that there is something I misunderstand. Here is the context :

A POST to https://lxd-server:8443/1.0/containers/[container]/exec request with, for example, these parameters :
{“command”: [“systemctl”, “status”, “apache2”], “record_output”: false, “wait-for-websocket”: true, “interactive”: true, “environment”: { “HOME”: “/root”, “TERM”: “xterm-256color”, “USER”:“root”, “SYSTEMD_PAGER”: ‘’}, “width”: 192, “height”: 48};

The idea behind this is to get the apache2 status running inside the container.

This works fine. But I want the output of the ran command. So, here are the things I issue thereafter :

  1. launch a GET request to 1.0/operations/[UUID]/wait?timeout=10 (with the UUID I got from the POST request).
  2. open a websocket on wss://lxd-server:8443/1.0/ws/operations/[UUID]/websocket?secret=[secret] (I guess that this is expected to answer to the “wait-for-websocket” parameter set to true in the initial POST request). Here, secret is made from the metadata.fds.control.
  3. open a websocket on wss://lxc-server:8443/1.0/ws/operations/[UUID]/websocket?secret=[secret] (secret is build here from metadata.fds.0, to get the output of the command).

As a matter of fact, this works fine. However, what I don’t understand is that I have to wait 10 seconds (the timeout parameter) to get the output. If I don’t give the timeout parameter, things wait for ever… Ideally, I would expect to have an output as soon as the launched command is running. Here, it don’t provide much information, but with more verbose and long-run commands, the timeout can lead to a kill (or an end-of-output) that I don’t want.

What do I do wrong ? What did I misunderstand ? Is there a way to achieve what I want ?
Thanks all for your kind help.

(Stéphane Graber) #2

/wait will wait until the command is done running, /wait?timeout=10 will wait until the command is done running OR timeout after 10s.

It’s unclear above if you’re attempting 1) before doing 2) and 3) or if you’re doing this in parallel.
If in parallel, then wait should return once the command is done completing, if doing it before 2) and 3) then it would indeed hang since the command hasn’t started yet (will only start once all websockets are connected).