Thanks for your kind help once again @stgraber.
I’ve been a little further and face some things I don’t quite understand.
I’ve setup a html page that execute some javascript code when the page loads.
Basically, it can be summed-up this way :
wsOp = new WebSocket (“wss://localhost:8443/ws/events?type=operation”);
wsOp.onmessage = function (evt) {
data = JSON.parse(evt.data)[‘metadata’];
switch (donnees[‘status_code’]) {
case 100: console.log (‘Operation ‘+data[‘id’]+’ created’); break;
case 101: console.log (‘Operation ‘+data[‘id’]+’ started’); break;
case 102: console.log (‘Operation ‘+data[‘id’]+’ stopped’); break;
case 103: console.log (‘Operation ‘+data[‘id’]+’ running’); break;
case 104: console.log (‘Operation ‘+data[‘id’]+’ cancelling’); break;
case 105: console.log (‘Operation ‘+data[‘id’]+’ pending’); break;
case 106: console.log (‘Operation ‘+data[‘id’]+’ starting’); break;
case 107: console.log (‘Operation ‘+data[‘id’]+’ stopping’); break;
case 108: console.log (‘Operation ‘+data[‘id’]+’ aborting’); break;
case 109: console.log (‘Operation ‘+data[‘id’]+’ freezing’); break;
case 110: console.log (‘Operation ‘+data[‘id’]+’ frozen’); break;
case 111: console.log (‘Operation ‘+data[‘id’]+’ thawed’); break;
case 200: console.log (‘Operation ‘+data[‘id’]+’ success’); break;
case 400: console.log (‘Operation ‘+data[‘id’]+’ failure’); break;
case 401: console.log (‘Operation ‘+data[‘id’]+’ cancelled’); break;
}
watchOp (‘1.0/’+data[‘id’]); // I’ll detail this function afterwards
}
Going only with this code snippet, when an operation is submitted on my local lxd server, I should see something in the javascript console log of my browser. Well, it does, but I have some few questions :
Why do I never see the codes 100, 101 ?
All I get is :
Operation [UUID] pending
Operation [UUID] running
Operation [UUID] running
Opeartion [UUID] success
For my tests, I use a XMLHttpRequest() object this way :
var http=new XMLHttpRequest();
var url=“/1.0/containers/”+container+“/exec”;
var params={“command”: [“systemctl”, “status”, App], “record_output”: false, “wait-for-websocket”: true, “interactive”: false, “environment”: { “HOME”: “/root”, “TERM”: “xterm-256color”, “USER”:“root”, “SYSTEMD_PAGER”: ‘’}, “width”: 192, “height”: 48};
http.open (“POST”, url, true);
http.setRequestHeader(“Content-type”, “application/json”);
http.send(JSON.stringify(params));
As for the WhachOp function, here is the relevant code :
function watchOp (url) {
var httpOp = new XMLHttpRequest();
httpOp.open (“GET”, url, true);
httpOp.onreadystatechange = function () {
if (httpOp.readyState == 4 && httpOp.status == 200) {
var opData = JSON.parse (httpOp.responseText)[‘metadata’];
var wsStdin = new Websocket (‘ws://localhost/ws/operations/’+opData[‘id’]+“/websocket?”+encodeURI(“secret=”+opData[‘metadata’][‘fds’][0]);
var wsStdOut = new Websocket (‘ws://localhost/ws/operations/’+opData[‘id’]+“/websocket?”+encodeURI(“secret=”+opData[‘metadata’][‘fds’][1]);
var wsStdErr = new WebSocket (‘ws://localhost/ws/operations/’+opData[‘id’]+“/websocket?”+encodeURI(“secret=”+opData[‘metadata’][‘fds’][3]);
var wsControl = new WebSocket (‘ws://localhost/ws/operations/’+opData[‘id’]+“/websocket?”+encodeURI(“secret=”+opData[‘metadata’][‘fds’][‘control’]);
var waitURI = ‘1.0/operations/’+opData[‘id’]+‘/wait?timeout=10’;
var httpWait = new XMLHttpRequest();
httpWait.open (“GET”, waitURI, true);
httpWait.onreadystatechange = function () {
if (httpWait.readyState == 4 && httpWait.status == 400) {
console.log (“Command terminated”);
}
};
httpWait.send();
}
}
};
httpOp.send();
}
I’ve thrown away all the code that makes tests upon the presence or not of each opData[‘metadata’][‘fds’][xxx] for the ease of reading. Each of the websockets have a onmessage function that displays the data received, if any, in the console.
What I don’t understand from this point are the following :
- following your answer, I’ve made up that the request to the /wait is made after the opening of all the websockets. However, If the params send are :
“wait-for-websocket”: true
“interactive”: false
All goes fine and the status = 200 is present. I get the apache2 status, but I don’t get the control chars (normally, there are couloured text in the output).
- if the params are :
“wait-for-websocket”: true
“interactive”: true
Then, I get the full output, from fds[0] (which is normal, according to the doc) and there is, of course, no fds[1] nor fds[2]. Yet, even if the output is better, the call to /wait?timeout=10 waits until the timeout is done.
And, I don’t get the -so waited- status = 200.
All I get is :
Operation [UUID] pending
Operation [UUID] running
I beleive I understand a little better how all this works, but there are still some things that are obscure to me.
So, if you somehow have some time to explain :
Why don’t I get the “couloured” output (I guess it has something to do with the interactive = false parameter which might have some impact on the tty allocated (or not)) ?
Why do I never get the 200 status (Success) in the second form ?
Thanks a lot in advance for having read all this long and detailed post.