UNPKG

4.84 kBJavaScriptView Raw
1'use strict';
2// @ts-check
3// ==================================================================================
4// virtualbox.js
5// ----------------------------------------------------------------------------------
6// Description: System Information - library
7// for Node.js
8// Copyright: (c) 2014 - 2020
9// Author: Sebastian Hildebrandt
10// ----------------------------------------------------------------------------------
11// License: MIT
12// ==================================================================================
13// 14. Docker
14// ----------------------------------------------------------------------------------
15
16const os = require('os');
17const exec = require('child_process').exec;
18const util = require('./util');
19
20function vboxInfo(callback) {
21
22 // fallback - if only callback is given
23 let result = [];
24 return new Promise((resolve) => {
25 process.nextTick(() => {
26 try {
27 exec(util.getVboxmanage() + ' list vms --long', function (error, stdout) {
28 let parts = (os.EOL + stdout.toString()).split(os.EOL + 'Name:');
29 parts.shift();
30 parts.forEach(part => {
31 const lines = ('Name:' + part).split(os.EOL);
32 const state = util.getValue(lines, 'State');
33 const running = state.startsWith('running');
34 const runningSinceString = running ? state.replace('running (since ', '').replace(')', '').trim() : '';
35 let runningSince = 0;
36 try {
37 if (running) {
38 const sinceDateObj = new Date(runningSinceString);
39 const offset = sinceDateObj.getTimezoneOffset();
40 runningSince = Math.round((Date.now() - Date.parse(sinceDateObj)) / 1000) + offset * 60;
41 }
42 } catch (e) {
43 util.noop();
44 }
45 const stoppedSinceString = !running ? state.replace('powered off (since', '').replace(')', '').trim() : '';
46 let stoppedSince = 0;
47 try {
48 if (!running) {
49 const sinceDateObj = new Date(stoppedSinceString);
50 const offset = sinceDateObj.getTimezoneOffset();
51 stoppedSince = Math.round((Date.now() - Date.parse(sinceDateObj)) / 1000) + offset * 60;
52 }
53 } catch (e) {
54 util.noop();
55 }
56 result.push({
57 id: util.getValue(lines, 'UUID'),
58 name: util.getValue(lines, 'Name'),
59 running,
60 started: runningSinceString,
61 runningSince,
62 stopped: stoppedSinceString,
63 stoppedSince,
64 guestOS: util.getValue(lines, 'Guest OS'),
65 hardwareUUID: util.getValue(lines, 'Hardware UUID'),
66 memory: parseInt(util.getValue(lines, 'Memory size', ' '), 10),
67 vram: parseInt(util.getValue(lines, 'VRAM size'), 10),
68 cpus: parseInt(util.getValue(lines, 'Number of CPUs'), 10),
69 cpuExepCap: util.getValue(lines, 'CPU exec cap'),
70 cpuProfile: util.getValue(lines, 'CPUProfile'),
71 chipset: util.getValue(lines, 'Chipset'),
72 firmware: util.getValue(lines, 'Firmware'),
73 pageFusion: util.getValue(lines, 'Page Fusion') === 'enabled',
74 configFile: util.getValue(lines, 'Config file'),
75 snapshotFolder: util.getValue(lines, 'Snapshot folder'),
76 logFolder: util.getValue(lines, 'Log folder'),
77 HPET: util.getValue(lines, 'HPET') === 'enabled',
78 PAE: util.getValue(lines, 'PAE') === 'enabled',
79 longMode: util.getValue(lines, 'Long Mode') === 'enabled',
80 tripleFaultReset: util.getValue(lines, 'Triple Fault Reset') === 'enabled',
81 APIC: util.getValue(lines, 'APIC') === 'enabled',
82 X2APIC: util.getValue(lines, 'X2APIC') === 'enabled',
83 ACPI: util.getValue(lines, 'ACPI') === 'enabled',
84 IOAPIC: util.getValue(lines, 'IOAPIC') === 'enabled',
85 biosAPICmode: util.getValue(lines, 'BIOS APIC mode'),
86 bootMenuMode: util.getValue(lines, 'Boot menu mode'),
87 bootDevice1: util.getValue(lines, 'Boot Device 1'),
88 bootDevice2: util.getValue(lines, 'Boot Device 2'),
89 bootDevice3: util.getValue(lines, 'Boot Device 3'),
90 bootDevice4: util.getValue(lines, 'Boot Device 4'),
91 timeOffset: util.getValue(lines, 'Time offset'),
92 RTC: util.getValue(lines, 'RTC'),
93 });
94 });
95
96 if (callback) { callback(result); }
97 resolve(result);
98 });
99 } catch (e) {
100 if (callback) { callback(result); }
101 resolve(result);
102 }
103 });
104 });
105}
106
107exports.vboxInfo = vboxInfo;