2023-06-01 12:24:34 +02:00
|
|
|
class BackendJob extends AntOSDKBaseJob {
|
2021-05-11 15:05:28 +02:00
|
|
|
constructor(data)
|
|
|
|
{
|
|
|
|
super(data);
|
|
|
|
}
|
|
|
|
execute()
|
|
|
|
{
|
|
|
|
switch (this.job.cmd) {
|
2023-06-01 12:24:34 +02:00
|
|
|
case 'cmd-exec':
|
2021-05-11 15:05:28 +02:00
|
|
|
/**
|
|
|
|
* Execute a linux command with the
|
|
|
|
* help of a server side lua API
|
|
|
|
* script
|
|
|
|
*/
|
|
|
|
this.exec();
|
|
|
|
break;
|
2023-06-01 12:24:34 +02:00
|
|
|
case 'lua-exec':
|
|
|
|
this.xlua(this.job.data.path, this.job.data.params);
|
|
|
|
break;
|
2021-05-11 15:05:28 +02:00
|
|
|
default:
|
|
|
|
const err_msg = `Unkown job ${this.job.cmd}`;
|
|
|
|
this.log_error(err_msg);
|
|
|
|
return this.error(err_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec()
|
|
|
|
{
|
|
|
|
const path = "pkg://libantosdk/core/lua/api.lua".abspath(this.job.root);
|
2023-06-01 12:24:34 +02:00
|
|
|
if(!this.job.data.pwd)
|
|
|
|
{
|
|
|
|
this.job.data.pwd = this.job.root;
|
|
|
|
}
|
|
|
|
const params = {
|
|
|
|
action: "exec",
|
|
|
|
args: this.job.data
|
|
|
|
};
|
|
|
|
this.xlua(path, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xlua(path, params)
|
|
|
|
{
|
2021-05-11 15:05:28 +02:00
|
|
|
const url = API.REST.replace("http","ws") + "/system/apigateway?ws=1";
|
|
|
|
try{
|
|
|
|
const socket = new WebSocket(url);
|
|
|
|
socket.onerror = (e)=> {
|
|
|
|
this.log_error(e.toString());
|
|
|
|
this.error(e);
|
|
|
|
socket.close();
|
|
|
|
};
|
|
|
|
socket.onclose = (e) => {
|
|
|
|
this.log_info("Connection closed");
|
|
|
|
this.result("Done");
|
|
|
|
};
|
|
|
|
socket.onopen = (e) => {
|
|
|
|
// send the command
|
|
|
|
const cmd = {
|
|
|
|
path: path,
|
2023-06-01 12:24:34 +02:00
|
|
|
parameters: params
|
2021-05-11 15:05:28 +02:00
|
|
|
};
|
|
|
|
socket.send(JSON.stringify(cmd));
|
|
|
|
};
|
|
|
|
socket.onmessage = (e) => {
|
|
|
|
const json = JSON.parse(e.data);
|
|
|
|
if(json.error)
|
|
|
|
{
|
|
|
|
this.log_error(json.error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.log_print(json.result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(error)
|
|
|
|
{
|
|
|
|
this.log_error(error.toString());
|
|
|
|
return this.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 12:24:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
API.jobhandle["cmd-exec"] = BackendJob;
|
|
|
|
API.jobhandle["lua-exec"] = BackendJob;
|