-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kevin Cui <[email protected]>
- Loading branch information
1 parent
38370e9
commit cd23f2c
Showing
5 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ words: | |
- vsock | ||
- btrfs | ||
- chrony | ||
- npipe | ||
dictionaries: | ||
- companies | ||
- softwareTerms | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
import type { OVMDarwinOptions } from "./type"; | ||
import type { OVMDarwinOptions, OVMWindowsOptions } from "./type"; | ||
import { DarwinOVM } from "./darwin"; | ||
import { WindowsOVM } from "./windows"; | ||
|
||
export const createDarwinOVM = (options: OVMDarwinOptions): Promise<DarwinOVM> => { | ||
return DarwinOVM.create(options); | ||
}; | ||
|
||
export const createWindowsOVM = (options: OVMWindowsOptions): WindowsOVM => { | ||
return WindowsOVM.create(options); | ||
}; | ||
|
||
export { | ||
OVMDarwinAppEventValue, | ||
OVMDarwinVzState, | ||
OVMWindowsSysEventValue, | ||
OVMWindowsAppEventValue, | ||
} from "./type"; | ||
|
||
export type { | ||
OVMDarwinEventData, | ||
OVMDarwinOptions, | ||
OVMDarwinInfo, | ||
OVMDarwinState, | ||
OVMWindowsOptions, | ||
OVMWindowsEventData, | ||
} from "./type"; | ||
export type { DarwinOVM }; | ||
export type { DarwinOVM, WindowsOVM }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import cp from "node:child_process"; | ||
import type { EventReceiver } from "remitter"; | ||
import { Remitter } from "remitter"; | ||
import type { OVMWindowsEventData, OVMWindowsOptions } from "./type"; | ||
import { Restful } from "./event_restful"; | ||
import { RequestWindows } from "./request"; | ||
|
||
export class WindowsOVM extends RequestWindows { | ||
public readonly events : EventReceiver<OVMWindowsEventData>; | ||
readonly #events: Remitter<OVMWindowsEventData>; | ||
private restful: Restful; | ||
private readonly restfulNPipeName: string; | ||
|
||
private constructor(private options: OVMWindowsOptions) { | ||
super(options.name); | ||
this.restfulNPipeName = `ovm-${options.name}-restful`; | ||
this.events = this.#events = new Remitter(); | ||
} | ||
|
||
public static create(options: OVMWindowsOptions): WindowsOVM { | ||
const ovm = new WindowsOVM(options); | ||
ovm.initEventRestful(); | ||
return ovm; | ||
} | ||
|
||
private initEventRestful(): void { | ||
this.restful = new Restful(); | ||
|
||
this.#events.remitAny((o) => { | ||
return this.restful.events.onAny((data) => { | ||
o.emit(data.event as keyof OVMWindowsEventData, data.data); | ||
}); | ||
}); | ||
|
||
this.restful.start(`//./pipe/${this.restfulNPipeName}`); | ||
} | ||
|
||
public start(): void { | ||
const versions = Object.keys(this.options.versions).map((key) => { | ||
return `${key}=${this.options.versions[key]}`; | ||
}).join(","); | ||
|
||
const launchTimeout = new Promise<void>((resolve, reject) => { | ||
const id = setTimeout(() => { | ||
disposer(); | ||
// eslint-disable-next-line prefer-promise-reject-errors | ||
reject(); | ||
}, 30 * 1000); | ||
|
||
const disposer = this.#events.onceAny(() => { | ||
clearTimeout(id); | ||
resolve(); | ||
}); | ||
}); | ||
|
||
const ovm = cp.spawn(this.options.ovmPath, [ | ||
"-name", this.options.name, | ||
"-log-path", this.options.logDir, | ||
"-image-dir", this.options.imageDir, | ||
"-rootfs-path", this.options.linuxPath.rootfs, | ||
"-versions", versions, | ||
"-event-npipe-name", this.restfulNPipeName, | ||
"-bind-pid", String(process.pid), | ||
], { | ||
timeout: 0, | ||
windowsHide: true, | ||
detached: true, | ||
stdio: "ignore", | ||
cwd: this.options.imageDir, | ||
}); | ||
|
||
ovm.unref(); | ||
|
||
launchTimeout | ||
.catch(() => { | ||
this.#events.emit("error", "OVM start timeout"); | ||
}); | ||
} | ||
} |