-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
114 lines (98 loc) · 4.03 KB
/
index.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
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
//I am writing a script wich starts a Hyper-V VM using Powershell after receiving a HTTP request.
//I am using the following code to start the VM:
var config = require('./config.json');
const { exec } = require('child_process');
//Importing variables from config.json
var verbose = config.verbose;
var vmName = config.vmName;
//Setup a HTTP server using Express
const express = require('express');
const app = express();
const port = config.port;
app.listen(port, () => { //Start the server
console.log(`Server running at port ${port}`);
});
app.get('/', (req, res) => { //Handle GET requests
res.statusCode = 200; //OK
res.send('Received your request!');
console.log("Received a request!");
//if the header contains the key "vmname" print the value of the key and save it to the variable vmName
if (req.headers.hasOwnProperty("vmname")) {
vmName = config.vmName; //reset the vmName to the default value
vmName = req.headers.vmname;
console.log("Requested VM-Name: " + req.headers.vmname);
}
//if the header contains the key "action" start or stop the VM depending on the value of the key
if (req.headers.hasOwnProperty("action")) {
if (req.headers.action == "start") {
startVM();
}
else if (req.headers.action == "stop") {
stopVM();
}
else {
console.log("Invalid action: " + req.headers.action);
}
}
else {
console.log("No action specified!");
}
});
//start listening for GET requests on the path /ismyvmactive
//these GET requests are responded with 200 OK if the requested vmname inside the header is found in the output of the command "Get-VM -ComputerName ${config.hostName} | Where-Object {$_.State -eq 'Running'}" and 404 Not Found if the vmname is not found
app.get('/ismyvmactive', (req, res) => {
exec(`Get-VM -ComputerName ${config.hostName} | Where-Object {$_.State -eq \'Running\'}`, {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
if (error !== null) {
printError(error, stderr, stdout);
}
else {
if (stdout.includes(req.headers.vmname)) {
res.status(200).send('Your requested VM is active!');
console.log("Requested VM is active!");
console.log("Requested VM-Name: " + req.headers.vmname);
}
else {
console.log(stdout);
res.status(404).send('Your requested VM is not active!');
console.log("Requested VM is not active!");
console.log("Requested VM-Name: " + req.headers.vmname);
}
}
})
});
//The following code is used to start the VM
function startVM() {
exec('Start-VM -Name ' + vmName, {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
if (error !== null) {
printError(error, stderr, stdout);
}
else {
console.log (`VM (${vmName}) started successfully!`)
}
console.log(stdout); //does only print the output of the command if the VM is already running
})
}
//The following code is used to stop the VM
function stopVM() {
exec('Stop-VM -Name ' + vmName, {'shell':'powershell.exe'}, (error, stdout, stderr)=> {
if (error !== null) {
printError(error, stderr, stdout);
}
else {
console.log (`VM (${vmName}) stopped successfully!`)
}
console.log(stdout); //does only print the output of the command if the VM is already stopped
})
}
//Method overloading workaround:
function printError(error, stderr, stdout) {
if (verbose) {
console.log('exec error: \n' + error);
console.log('stderr: \n' + stderr);
console.log('stdout: \n' + stdout);
} else {
console.log("An error occured. Are u sure u are running this script as an administrator?")
console.log("If u are sure, try to run the script with verbose set to true!")
console.log("If the error remains, please open an issue on GitHub.")
}
}