-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild.js
66 lines (59 loc) · 1.49 KB
/
rebuild.js
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
const { exec } = require("node:child_process");
const net = require("node:net");
const retryTime = 10;//sec
const secret = process.env.SECRET;
let sudoPass = process.env.SUDOPASS;
const action = (data) => {
data = data.toString();
console.log(data);
if (data === "Rebuild")
exec(
`echo ${sudoPass} | sudo -S ./run.sh ${secret}`,
(err, stdout, stderr) => {
if (err) console.log(err);
if (stderr) console.log(stderr);
console.log(stdout);
}
);
};
let startingPort = 4331;
const makeClient = () => {
let localPort = startingPort++;
return () => {
const client = () => {
connection = net.connect(
{
host: "127.0.0.1",
port: localPort,
},
_handleConn
);
connection.on("data", (data) => {
action(data);
});
connection.on("close", _handleClose);
connection.on("error", _handleError);
function _handleConn() {
console.log("Connected to server on port " + localPort);
}
function _handleClose() {
setTimeout(() => {
console.log("Retrying connection on port " + localPort);
client();
}, retryTime*1000);
}
function _handleError() {
console.log("Connection Failed on port " + localPort);
}
};
client();
};
};
let client1 = makeClient();
let client2 = makeClient();
let client3 = makeClient();
let client4 = makeClient();
client1();
client2();
client3();
client4();