Skip to content

Update repo #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json']
},
plugins: [
'@typescript-eslint',
'unicorn'
],
extends: [
'xo',
'xo-typescript',
'plugin:@typescript-eslint/recommended-requiring-type-checking'
],
rules: {
'object-curly-spacing': ['error', 'always'],
'@typescript-eslint/object-curly-spacing': ['error', 'always'],
'@typescript-eslint/restrict-template-expressions': ['warn', { allowNumber: true, allowBoolean: true }]
},
ignorePatterns: [
'styleguide',
'dist',
'examples'
]
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

# misc
**/.DS_Store
*.tgz
14 changes: 8 additions & 6 deletions examples/builder.ts → examples/builder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import libvirt, {
const {
DomainBuilder,
domainDescToXml,
domainDescFromXml,
} from "../dist";

(async () => {
Hypervisor
} = require("../dist");

const main = async () => {
const uri = process.env.LIBVIRT_URI || "qemu:///system";
const hypervisor = new libvirt.Hypervisor({ uri });
const hypervisor = new Hypervisor({ uri });

await hypervisor.connectOpen();

Expand Down Expand Up @@ -52,4 +52,6 @@ import libvirt, {
const xml = domainDescToXml(domain);
await hypervisor.domainDefineXML(xml);

})();
};

main().catch(console.error);
27 changes: 12 additions & 15 deletions examples/list.ts → examples/list.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import chalk from "chalk";
import process from "process";
const chalk = require ("chalk");
const process = require ("process");

import libvirt from "../";

(async () => {
const { Hypervisor, ConnectListAllDomainsFlags } = require("../dist");

const main = async () => {
const uri = process.env.LIBVIRT_URI || "qemu:///system";
const hypervisor = new libvirt.Hypervisor({ uri });
const hypervisor = new Hypervisor({ uri });

await hypervisor.connectOpen();

const hostname = await hypervisor.connectGetHostname();
process.stdout.write(`Connected to ${hostname}!\n\n`);

const activeDomains = await hypervisor.connectListAllDomains(
libvirt.ConnectListAllDomainsFlags.ACTIVE);
const inactiveDomains = await hypervisor.connectListAllDomains(
libvirt.ConnectListAllDomainsFlags.INACTIVE);
const activeDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.ACTIVE);
const inactiveDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.INACTIVE);

const activeDomainNames = await Promise.all(activeDomains
.map((domain) => hypervisor.domainGetName(domain)));
const activeDomainNames = await Promise.all(activeDomains.map((domain) => hypervisor.domainGetName(domain)));

const inactiveDomainNames = await Promise.all(inactiveDomains
.map((domain) => hypervisor.domainGetName(domain)));
const inactiveDomainNames = await Promise.all(inactiveDomains.map((domain) => hypervisor.domainGetName(domain)));
await hypervisor.connectClose();

process.stdout.write("Active Domains\n");
Expand All @@ -39,4 +34,6 @@ import libvirt from "../";
}
process.stdout.write("\n");

})();
};

main().catch(console.error);
16 changes: 8 additions & 8 deletions examples/shutdown.ts → examples/shutdown.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import chalk from "chalk";
import process from "process";
const chalk = require ("chalk");
const process = require ("process");

import libvirt from "../";

(async () => {
const { ConnectListAllDomainsFlags } = require("../dist");

const main = async () => {
const uri = process.env.LIBVIRT_URI || "qemu:///system";
const hypervisor = new libvirt.Hypervisor({ uri });

// Connecting to our hypervisor
await hypervisor.connectOpen();

const activeDomains = await hypervisor.connectListAllDomains(
libvirt.ConnectListAllDomainsFlags.ACTIVE);
const activeDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.ACTIVE);

if (activeDomains.length === 0) {
process.stdout.write("No domains for shutdown :(");
Expand All @@ -34,4 +32,6 @@ import libvirt from "../";
});
}

})();
};

main().catch(console.error);
25 changes: 11 additions & 14 deletions examples/start.ts → examples/start.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import chalk from "chalk";
import process from "process";
const chalk = require ("chalk");

import libvirt from "../";

(async () => {
const { Hypervisor, ConnectListAllDomainsFlags } = require("../dist");

const main = async () => {
const uri = process.env.LIBVIRT_URI || "qemu:///system";
const hypervisor = new libvirt.Hypervisor({ uri });
const hypervisor = new Hypervisor({ uri });

// Connecting to our hypervisor
await hypervisor.connectOpen();

const inactiveDomains = await hypervisor.connectListAllDomains(
libvirt.ConnectListAllDomainsFlags.INACTIVE);
const inactiveDomains = await hypervisor.connectListAllDomains(ConnectListAllDomainsFlags.INACTIVE);

if (inactiveDomains.length === 0) {
process.stdout.write("No domains to start :(");
Expand All @@ -25,12 +22,12 @@ import libvirt from "../";
process.stdout.write(`Starting domain: ${chalk.blue(domainName)} ... `);

await hypervisor.domainCreate(inactiveDomain).then(() => {
process.stdout.write(
`domain ${chalk.green(domainName)} has been started!\n\n`);
}).catch((err: Error) => {
process.stderr.write(
`domain ${chalk.red(domainName)} shutdown ERROR: ${err} \n\n`);
process.stdout.write(`domain ${chalk.green(domainName)} has been started!\n\n`);
}).catch(err => {
process.stderr.write(`domain ${chalk.red(domainName)} shutdown ERROR: ${err} \n\n`);
});
}

})();
};

main().catch(console.error);
63 changes: 0 additions & 63 deletions examples/tsconfig.json

This file was deleted.

93 changes: 93 additions & 0 deletions lib/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
export declare interface HypervisorOptions {
uri: string;
}

export declare class Hypervisor {
constructor(options: HypervisorOptions);

connectOpen(): Promise<void>;
connectClose(): Promise<void>;
connectListAllDomains(flags?: ConnectListAllDomainsFlags): Promise<Domain[]>;
connectListDomains(): Promise<number[]>;
connectListDefinedDomains(): Promise<string[]>;
connectGetMaxVcpus(type?: string): Promise<number>;
connectGetHostname(): Promise<string>;

domainCreateXML(xml: string): Promise<Domain>;
domainDefineXML(xml: string): Promise<Domain>;
domainGetInfo(domain: Domain): Promise<DomainInfo>;
domainGetID(domain: Domain): Promise<number | null>;
domainGetName(domain: Domain): Promise<string>;
domainGetUUIDString(domain: Domain): Promise<string>;
domainLookupByID(id: number): Promise<Domain>;
domainLookupByName(name: string): Promise<Domain>;
domainLookupByUUIDString(uuid: string): Promise<Domain>;
domainSave(domain: Domain, filename: string): Promise<void>;
domainRestore(filename: string): Promise<void>;
domainCreate(domain: Domain): Promise<void>;
domainShutdown(domain: Domain): Promise<void>;
domainGetXMLDesc(domain: Domain, flags?: DomainGetXMLDescFlags): Promise<string>;

nodeGetInfo(): Promise<NodeInfo>;
}

export declare const enum ConnectListAllDomainsFlags {
ACTIVE = 1,
INACTIVE = 2,
PERSISTENT = 4,
TRANSIENT = 8,
RUNNING = 16,
PAUSED = 32,
SHUTOFF = 64,
OTHER = 128,
MANAGEDSAVE = 256,
NO_MANAGEDSAVE = 512,
AUTOSTART = 1024,
NO_AUTOSTART = 2048,
HAS_SNAPSHOT = 4096,
NO_SNAPSHOT = 8192,
HAS_CHECKPOINT = 16384,
NO_CHECKPOINT = 32768,
}

export declare const enum DomainGetXMLDescFlags {
SECURE = 1,
INACTIVE = 2,
UPDATE_CPU = 4,
MIGRATABLE = 8,
}

export declare class Domain {}

// https://libvirt.org/manpages/virsh.html#list
export declare const enum DomainState {
NOSTATE = 0,
RUNNING = 1,
// Docs are wrong this is not IDLE this is BLOCKED
// libvirt-domain.h can prove this
BLOCKED = 2,
PAUSED = 3,
SHUTDOWN = 4,
SHUTOFF = 5,
CRASHED = 6,
PMSUSPENDED = 7,
}

export declare interface DomainInfo {
state: DomainState;
maxMem: number;
memory: number;
nrVirtCpu: number;
cpuTime: number;
}

export declare interface NodeInfo {
model: string;
memory: number;
cpus: number;
mhz: number;
nodes: number;
sockets: number;
cores: number;
threads: number;
}
Loading