-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToolExecuter.ts
More file actions
138 lines (121 loc) · 4.6 KB
/
Copy pathToolExecuter.ts
File metadata and controls
138 lines (121 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import CancellationToken from 'cancellationtoken';
import Log from './Log';
import * as Obj from './shared/Obj';
import JsonProxy from './shared/JsonProxy';
import { BackofficeStatus, ToolConfig, ToolExecuterStatus } from './shared/BackOfficeStatus';
import * as BackOfficeAPI from './shared/BackOfficeAPI';
import { AppContext } from './ModuleBase';
import * as SystemPromise from './SystemPromise';
import * as RequestHandler from "./RequestHandler";
import ConfigStore from './ConfigStore';
const logger = Log.logger(__filename);
// Allow to start a script from the UI.
// The script can produce output in the form of messages
// For now, messages goes into indi logs
type InstanciatedTool = {
id: string;
params: ToolConfig;
}
export default class ToolExecuter implements RequestHandler.APIAppProvider<BackOfficeAPI.ToolExecuterAPI>
{
private readonly jsonProxy: JsonProxy<BackofficeStatus>;
private readonly context: AppContext;
private readonly instanciatedTools: {[id:string]:InstanciatedTool};
private readonly status: ToolExecuterStatus;
constructor(jsonProxy:JsonProxy<BackofficeStatus>, context:AppContext) {
this.jsonProxy = jsonProxy;
this.instanciatedTools = {};
this.context = context;
jsonProxy.getTarget().toolExecuter = {
tools: {}
};
this.status = jsonProxy.getTarget().toolExecuter;
jsonProxy.addSynchronizer(['toolExecuter', 'tools'],
this.syncTools,
true);
new ConfigStore<ToolExecuterStatus["tools"]>(jsonProxy, 'toolExecuter', ['toolExecuter', 'tools'], {
}, {
"welcome": {
"desc": "Announce the startup of mobindi - not accessible through UI",
"cmd": ["touch", "/tmp/mobindi.started" ],
"hidden": true,
"trigger":"atstart"
},
"led_off": {
"desc":'Turn lights off',
"cmd": ["sudo", "-n", "/opt/local/lights.sh", "off"]
},
"reboot": {
"desc": "Reboot",
"confirm": "Do you really want to reboot ?",
"cmd": ["sudo", "-n", "/opt/local/reboot.sh"]
},
"shutdown": {
"desc": "Shutdown",
"confirm": "Do you really want to shutdown ?",
"cmd": ["sudo", "-n", "/opt/local/shutdown.sh"]
}
});
}
private initTool=(id:string, params:ToolConfig):InstanciatedTool=>
{
var result = ({
id: id,
params: params
})
if (params.trigger === "atstart") {
this.runTool(result);
}
return result;
}
// Given a tool object, returns a promise for its execution
private runTool=async (tool: InstanciatedTool)=>{
try {
logger.info('Will start tool', {id: tool.id, cmd: tool.params.cmd});
const ret = await SystemPromise.Exec(CancellationToken.CONTINUE, {
command: tool.params.cmd
});
logger.info('Tool terminated', {id: tool.id, cmd: tool.params.cmd, ret});
} catch(e) {
logger.warn('Tool error', {id: tool.id, cmd: tool.params.cmd}, e);
}
}
private syncTools=()=>
{
// At least a trigger def was updated.
for(const ikey of Object.keys(this.status.tools))
{
const wantedParams = this.status.tools[ikey];
if (!Obj.hasKey(this.instanciatedTools, ikey)) {
this.instanciatedTools[ikey] = this.initTool(ikey, Obj.deepCopy(wantedParams));
} else {
var existing = this.instanciatedTools[ikey];
if (!Obj.deepEqual(existing.params, wantedParams)) {
this.instanciatedTools[ikey] = this.initTool(ikey, Obj.deepCopy(wantedParams));
}
}
}
for(const ikey of Object.keys(this.instanciatedTools))
{
if (!Obj.hasKey(this.status.tools, ikey)) {
delete this.instanciatedTools[ikey];
}
}
}
public startTool = async (ct: CancellationToken, message: {uid: string}) => {
const which = message.uid;
if (!which) {
throw new Error("Invalid id");
}
if (!Obj.hasKey(this.instanciatedTools, which)) {
throw new Error("Unknown id");
}
const toStart = this.instanciatedTools[which];
this.runTool(toStart);
}
getAPI():RequestHandler.APIAppImplementor<BackOfficeAPI.ToolExecuterAPI> {
return {
startTool : this.startTool,
};
}
};