-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller1.js
More file actions
82 lines (75 loc) · 2.82 KB
/
controller1.js
File metadata and controls
82 lines (75 loc) · 2.82 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
const { Worker } = require("worker_threads")
const request = require('request-promise');
var WebSocket = new require('ws');
var websocket = new WebSocket.Server({
port:3000
});
var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});
websocket.on('connection', (websocket) => {
websocket.on('message', function message(data) {
var data = JSON.parse(data)
console.log(data["start"])
console.log(data["Id"]) // get id for get
const createdocker = new Worker("./dockercreate.js",{
workerData: data ,
})
const options = {
method: 'GET',
uri: `http://20.82.178.99:5240/Servers/Detailsapi/${data["Id"]}`,
json:true
}
if (data["stop"] === "dockerstop()"){
const dockerun = request(options)
.then((response) => {
console.log(response)
const ftpworker = new Worker("./dockerstop.js",{
workerData: response,
})
// const containerexec = new Worker("./containerexec.js",{
// workerData: data,
// })
});
}
if (data["run"] === "dockerun()"){
const dockerun = request(options)
.then((response) => {
console.log(response)
const ftpworker = new Worker("./dockerun.js",{
workerData: response,
})
// const containerexec = new Worker("./containerexec.js",{
// workerData: data,
// })
var container = docker.getContainer(`${response["containerId"]}`);
container.attach({stream: true, stdout: true, stderr: true}, function (err, stream) {
stream.pipe(process.stdout)
stream.on("data", info => websocket.send((info)))
})
});
}
const information = request(options)
.then((response) => {
console.log(response)
const ftpworker = new Worker("./ftpworker.js",{
workerData: response,
})
if(data["start"] === "dockercreate()"){
const dockercontainerinfo = request(options)
.then((response) => {
var container = docker.getContainer(`${response["containerId"]}`);
container.start()
container.attach({stream: true, stdout: true, stderr: true}, function (err, stream) {
stream.pipe(process.stdout)
stream.on("data", info => websocket.send((info)))
})
})
}
})
})
})
//If it will be error, node js wont crash!
process.on('uncaughtException', function (err) {
console.error(err);
console.log("Node NOT Exiting...");
});