-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (88 loc) · 2.66 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
const core = require('@actions/core');
const fs = require('fs');
const { execSync, spawn } = require('child_process');
const VMMETER_PID = 'vmmeter-pid';
const VMMETER_PORT = 'vmmeter-port';
function run() {
const time = new Date().toTimeString();
core.setOutput("start-time", time);
// Check if arkade is installed
try {
fs.accessSync('/usr/local/bin/arkade', fs.constants.X_OK);
} catch (err) {
core.setFailed("arkade not found in /usr/local/bin, please install it to proceed.");
return;
}
// Check if vmmeter is installed
try {
fs.accessSync('/usr/local/bin/vmmeter', fs.constants.X_OK);
} catch (err) {
console.log("vmmeter not found. Attempting to install via arkade...");
try {
execSync('sudo -E /usr/local/bin/arkade oci install ghcr.io/openfaasltd/vmmeter:latest --path /usr/local/bin/', { stdio: 'inherit' });
} catch (installErr) {
core.setFailed(`Failed to install vmmeter: ${installErr.message}`);
return;
}
// Re-check if vmmeter is installed after installation attempt
try {
fs.accessSync('/usr/local/bin/vmmeter', fs.constants.X_OK);
} catch (finalErr) {
core.setFailed("vmmeter installation failed, still not found in /usr/local/bin.");
return;
}
}
// Start vmmeter as a detached process
const child = spawn(
'sudo',
['/usr/local/bin/vmmeter'],
{
detached: true,
stdio: 'ignore',
env: { ...process.env }
}
);
child.unref();
if (child.pid === undefined) {
core.warning("Unable to start vmmeter process");
try {
let data = fs.readFileSync('/tmp/vmmeter.log', 'utf8');
core.warning(`Error starting vmmeter, logs: ${data}`);
} catch {
core.warning("No logs found in /tmp/vmmeter.log");
}
return;
}
core.saveState(VMMETER_PID, child.pid.toString());
console.log(`vmmeter pid: ${child.pid}`);
let port = 0;
for (let i = 0; i < 100; i++) {
let found = false;
try {
const data = fs.readFileSync('/tmp/vmmeter.port', 'utf8');
port = parseInt(data.trim());
found = true;
} catch {
// Retry reading port file if not found
}
if (found) break;
execSync('sleep 0.1'); // Wait for 100ms
}
if (port > 0) {
console.log(`vmmeter port: ${port}`);
core.saveState(VMMETER_PORT, port);
} else {
core.warning("vmmeter port not found, process did not start correctly");
try {
let data = fs.readFileSync('/tmp/vmmeter.log', 'utf8');
core.warning(`Error starting vmmeter, logs: ${data}`);
} catch {
core.warning("No logs found in /tmp/vmmeter.log");
}
}
}
try {
run();
} catch (error) {
core.setFailed(error.message);
}