-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
206 lines (166 loc) · 5.37 KB
/
index.mjs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
// 1. https://nodejs.org/api/http.html
// 2. https://nodejs.org/api/single-executable-applications.html
`use strict`;
import assert from 'node:assert';
import {resolve} from 'node:path';
import {argv, env, cwd} from 'node:process';
import * as dotenv from 'dotenv';
dotenv.config({
path: resolve(cwd(), `.env`),
});
import {
parseBoolean,
request,
secureRequest,
shell,
} from './lib/utils.mjs';
import {
containerStatus,
containerUnlock,
containerLock,
containerFreeze,
containerThaw,
} from './lib/pct-api.mjs';
import {
virtualMachineStatus,
virtualMachineUnlock,
virtualMachineLock,
virtualMachineFreeze,
virtualMachineThaw,
} from './lib/qm-api.mjs';
const debugAssert = assert.strict.strictEqual;
const args = argv;
const argc = argv.length - 1;
const DEBUG = parseBoolean(env[`DEBUG`]);
const DRY_RUN = parseBoolean(env[`DRY_RUN`]);
const pveAuthKey = env[`PVE_AUTH_KEY`] || ``;
const RPORT = Math.abs(env[`REMOTE_PORT`]) || 8006;
const RHOST = env[`REMOTE_HOSTNAME`] || `scorpio-pve.lan`;
const useTLS = parseBoolean(env[`USE_TLS`]);
const proto = parseBoolean(useTLS) == true ? `HTTPS` : `HTTP`;
const TYPES_QEMU = `qemu`;
const TYPES_LXC = `lxc`;
const postData = JSON.stringify({
type: `vm`, // node, storage, ...
});
//https://scorpio-pve.lan:8006/api2/json/nodes/scorpio-pve/lxc/106/config
const fetchOptions = {
hostname: RHOST,
port: RPORT,
path: `/api2/json/cluster/resources?type=vm`,
method: `GET`,
headers: {
'Content-Type': `application/json`,
//'Content-Length': Buffer.byteLength(postData),
'Authorization': `PVEAPIToken=${pveAuthKey}`,
},
};
if(pveAuthKey && pveAuthKey.length > 0) {
console.debug(`An authorization key was found with the value of... ${pveAuthKey}\n`);
} else {
console.debug(`No authorization key was found...\n`);
}
console.info(`Beginning ${proto} request to ${fetchOptions.hostname}...\n`);
const fetch = parseBoolean(useTLS) == true ? secureRequest : request;
const main = async function(argc, argv) {
args.forEach((arg, pos) => {
if(arg) {
console.debug(`arg[${pos}: ${arg}`);
}
});
let lxcContainers = [];
let qemuContainers = [];
const req = await fetch(fetchOptions, async (res) => {
res.setEncoding('utf8');
res.on(`data`, (body) => {
if(DEBUG) {
console.info(`STATUS: ${res.statusCode}`);
console.info(`HEADERS: ${JSON.stringify(res.headers)}`);
console.info(`BODY: ${body}`);
}
if(body && body.length > 0) {
const jsonBody = JSON.parse(body);
const containers = jsonBody[`data`];
let runningContainers = [];
containers.forEach((container, index) => {
if(container) {
console.debug(`${container.name}: ${container.type} - ${container.vmid}...${container.status}`);
if(container.status === `running`) {
runningContainers.push(container);
}
}
});
runningContainers.forEach((container, index) => {
if(container) {
console.debug(`${container.name}: ${container.type} - ${container.vmid}...${container.status}`);
if(container.type === TYPES_QEMU) {
qemuContainers.push(container);
} else if(container.type === TYPES_LXC) {
lxcContainers.push(container);
}
}
});
const totalNumRunningContainers = runningContainers.length;
const numQemuContainers = qemuContainers.length;
const numLxcContainers = lxcContainers.length;
console.log(`totalNumRunningContainers: ${totalNumRunningContainers}`);
console.log(`numQemuContainers: ${numQemuContainers}`);
console.log(`numLxcContainers: ${numLxcContainers}`);
// shell(`echo "hi"`, {
// encoding: `utf8`,
// dryRun: DRY_RUN,
// });
// assert.isDeepStrictEqual
// debugAssert(containerLock(104, {
// dryRun: DRY_RUN,
// }), false);
}
qemuContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await virtualMachineLock(container.vmid);
}
});
qemuContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await virtualMachineFreeze(container.vmid);
}
});
qemuContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await virtualMachineThaw(container.vmid);
}
});
qemuContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await virtualMachineUnlock(container.vmid);
}
});
lxcContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await containerLock(container.vmid);
}
});
lxcContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await containerFreeze(container.vmid);
}
});
lxcContainers.forEach(async (container, pos) => {
if(container.status === "running") {
await containerUnlock(container.vmid);
}
});
});
res.on(`end`, () => {
console.debug(`EOF`);
});
});
req.on(`error`, (e) => {
console.error(`ERROR: ${e}`);
});
//req.write(`OK`);
//req.write(postData);
req.end();
}
await main(argc, argv);