mirror of
https://github.com/unraid/api.git
synced 2026-04-26 01:08:53 -05:00
fix: switch libvirt libs
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
* Written by: Alexis Tyler
|
||||
*/
|
||||
|
||||
import { ConnectListAllDomainsFlags } from '@vmngr/libvirt';
|
||||
import { log } from '../../log';
|
||||
import { CoreResult, CoreContext } from '../../types';
|
||||
import { parseDomains, getHypervisor, ensurePermission } from '../../utils';
|
||||
import { getHypervisor, ensurePermission } from '../../utils';
|
||||
|
||||
/**
|
||||
* Get vm domains.
|
||||
@@ -20,14 +22,19 @@ export const getDomains = async (context: CoreContext): Promise<CoreResult> => {
|
||||
});
|
||||
|
||||
const hypervisor = await getHypervisor();
|
||||
const defined = await parseDomains('name', await hypervisor.listDefinedDomainsAsync());
|
||||
const active = await parseDomains('id', await hypervisor.listActiveDomainsAsync());
|
||||
const activeDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.ACTIVE);
|
||||
const inactiveDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.INACTIVE);
|
||||
const activeDomainNames = await Promise.all(activeDomains.map(async domain => hypervisor.domainGetName(domain)));
|
||||
const inactiveDomainNames = await Promise.all(inactiveDomains.map(async domain => hypervisor.domainGetName(domain)));
|
||||
|
||||
log.debug('Active: "%s"', activeDomains);
|
||||
log.debug('Inactive: "%s"', inactiveDomains);
|
||||
|
||||
return {
|
||||
text: `Defined domains: ${JSON.stringify(defined, null, 2)}\nActive domains: ${JSON.stringify(active, null, 2)}`,
|
||||
text: `Defined domains: ${JSON.stringify(activeDomainNames, null, 2)}\nActive domains: ${JSON.stringify(inactiveDomainNames, null, 2)}`,
|
||||
json: [
|
||||
...defined,
|
||||
...active
|
||||
...activeDomains,
|
||||
...inactiveDomains
|
||||
]
|
||||
};
|
||||
};
|
||||
|
||||
@@ -5,33 +5,26 @@
|
||||
|
||||
import fs from 'fs';
|
||||
import { AppError } from '../../errors';
|
||||
import { Hypervisor } from '@vmngr/libvirt';
|
||||
|
||||
// Libvirt is an optional dependency
|
||||
let libvirt;
|
||||
let client;
|
||||
const uri = process.env.LIBVIRT_URI ?? 'qemu:///system';
|
||||
|
||||
let hypervisor: Hypervisor;
|
||||
|
||||
export const getHypervisor = async () => {
|
||||
// Return client if it's already connected
|
||||
if (client) {
|
||||
return client;
|
||||
// Return hypervisor if it's already connected
|
||||
if (hypervisor) {
|
||||
return hypervisor;
|
||||
}
|
||||
|
||||
// Check if libvirt service is running and then connect
|
||||
const running = fs.existsSync('/var/run/libvirt/libvirtd.pid');
|
||||
|
||||
if (!running) {
|
||||
throw new AppError('Libvirt service is not running');
|
||||
}
|
||||
|
||||
// Try and get dep loaded or throw error
|
||||
try {
|
||||
libvirt = require('libvirt');
|
||||
} catch {
|
||||
throw new AppError('Libvirt dep is missing.');
|
||||
}
|
||||
hypervisor = new Hypervisor({ uri });
|
||||
await hypervisor.connectOpen();
|
||||
|
||||
// Connect to local socket
|
||||
const { Hypervisor } = libvirt;
|
||||
client = new Hypervisor('qemu:///system');
|
||||
await client.connectAsync();
|
||||
return hypervisor;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user