Skip to content

Commit

Permalink
refactor(darwin)!: use new version ovm for x64 (#144)
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Cui <[email protected]>
  • Loading branch information
BlackHole1 authored Dec 25, 2024
1 parent c76cd09 commit 32a119d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 318 deletions.
39 changes: 16 additions & 23 deletions scripts/resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,32 @@
"darwin-x64": [
{
"name": "ovm",
"version": "v2.0.0-RC8",
"download": "https://static.oomol.com/ovm-resources/ovm/{version}/ovm-amd64",
"sha256": "7f576fe11b62a8af8e932401c4749d26b3745d46cefd65bd3091ebf010ba837c",
"out": "ovm"
},
{
"name": "rootfs",
"version": "v1.0.3",
"download": "https://static.oomol.com/ovm-resources/ovm-core/{version}/applehv-rootfs-amd64.rootfs.erofs",
"sha256": "7436bb01151154bd967af286435e65194da66570760cdc50e26a3702ac7ff87a",
"out": "rootfs.erofs"
"version": "v1.0.0-RC7",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-amd64",
"sha256": "40bb26e0859b1246c7e862568ec3d46c9975931559288f9449cbf091431fe637",
"out": "bin/ovm"
},
{
"name": "initrd",
"version": "v1.0.3",
"download": "https://static.oomol.com/ovm-resources/ovm-core/{version}/initrd-amd64.initrd.gz",
"sha256": "064eaf482a0ed8b8b7071d4b85f37ff26e201ef844566cac6b6406b8724cb698",
"out": "initrd.gz"
"name": "bootable",
"version": "v0.0.1",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/bootable-darwin-amd64.img.zst",
"sha256": "8d1da14847745cd6f76927eed39d8fa4fa81027149261e58a3c9aca0d1d985f7",
"out": "bootable.img.zst"
},
{
"name": "kernel",
"version": "v1.0.3",
"download": "https://static.oomol.com/ovm-resources/ovm-core/{version}/kernel-amd64.bzImage",
"sha256": "fba25a5e0019f25cb43cb18571eb1e4d816324716de1cd98d14defca397ca158",
"out": "bzImage"
"name": "libexec",
"version": "v0.0.1",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/libexec-darwin-amd64.tar.gz",
"sha256": "632de5605647b19d2c50211592cb2b9b9b4ab918f828540425069657526e7baf",
"out": "libexec.tar.gz"
}
],
"darwin-arm64": [
{
"name": "ovm",
"version": "v1.0.0-RC6",
"version": "v1.0.0-RC7",
"download": "https://static.oomol.com/ovm-resources/Bauklotze/{version}/ovm-arm64",
"sha256": "8b658e81dc7ec73a6f45dd33a397db01758ea70d7f8687fd600aaba536a39137",
"sha256": "362a8983e805fb9efd2caaea4b95be06b4a0e3b547d7c52044950f1c8b1c406e",
"out": "bin/ovm"
},
{
Expand Down
32 changes: 16 additions & 16 deletions src/darwin_arm64.ts → src/darwin.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { RequestDarwinArm64 } from "./request";
import { RequestDarwin } from "./request";
import cp from "node:child_process";
import type { EventReceiver } from "remitter";
import { Remitter } from "remitter";
import type { OVMDarwinArm64InitEvent, OVMDarwinArm64Options, OVMDarwinArm64StartEvent } from "./type";
import type { OVMDarwinInitEvent, OVMDarwinOptions, OVMDarwinStartEvent } from "./type";
import { Restful } from "./event_restful";
import fs from "node:fs/promises";
import path from "node:path";
import { tmpdir } from "node:os";
import { enableDebug, resourceArm64 } from "./utils";
import { enableDebug, resource } from "./utils";

export class DarwinOVMArm64 extends RequestDarwinArm64 {
export class DarwinOVM extends RequestDarwin {
public readonly events: {
init: EventReceiver<OVMDarwinArm64InitEvent>;
start: EventReceiver<OVMDarwinArm64StartEvent>;
init: EventReceiver<OVMDarwinInitEvent>;
start: EventReceiver<OVMDarwinStartEvent>;
};
readonly #events: {
init: Remitter<OVMDarwinArm64InitEvent>;
start: Remitter<OVMDarwinArm64StartEvent>;
init: Remitter<OVMDarwinInitEvent>;
start: Remitter<OVMDarwinStartEvent>;
};

private restful: Record<"init" | "start", Restful>;
private restfulWithInit: string;
private restfulWithStart: string;

private constructor(private options: OVMDarwinArm64Options) {
private constructor(private options: OVMDarwinOptions) {
super(options.workspace);
this.events = this.#events = {
init: new Remitter(),
start: new Remitter(),
};
}

public static async create(options: OVMDarwinArm64Options): Promise<DarwinOVMArm64> {
public static async create(options: OVMDarwinOptions): Promise<DarwinOVM> {
await fs.mkdir(options.workspace, {
recursive: true,
});
const ovm = new DarwinOVMArm64(options);
const ovm = new DarwinOVM(options);
await Promise.all([
ovm.initEventRestful(),
]);
Expand All @@ -50,13 +50,13 @@ export class DarwinOVMArm64 extends RequestDarwinArm64 {

this.#events.init.remitAny((o) => {
return this.restful.init.events.onAny((data) => {
o.emit(data.event as keyof OVMDarwinArm64InitEvent, data.data);
o.emit(data.event as keyof OVMDarwinInitEvent, data.data);
});
});

this.#events.start.remitAny((o) => {
return this.restful.start.events.onAny((data) => {
o.emit(data.event as keyof OVMDarwinArm64StartEvent, data.data);
o.emit(data.event as keyof OVMDarwinStartEvent, data.data);
});
});

Expand All @@ -82,13 +82,13 @@ export class DarwinOVMArm64 extends RequestDarwinArm64 {
});
});

const ovmBin = resourceArm64("ovm", this.options.resource);
const ovmBin = resource("ovm", this.options.resource);
const ovmArgs = [
"machine",
"init",
"--cpus", String(this.options.cpu),
"--memory", String(this.options.memory),
"--boot", resourceArm64("image", this.options.resource),
"--boot", resource("image", this.options.resource),
"--boot-version", this.options.versions.image,
"--data-version", this.options.versions.data,
"--report-url", `unix://${this.restfulWithInit}`,
Expand Down Expand Up @@ -137,7 +137,7 @@ export class DarwinOVMArm64 extends RequestDarwinArm64 {
});
});

const ovmBin = resourceArm64("ovm", this.options.resource);
const ovmBin = resource("ovm", this.options.resource);
const ovmArgs = [
"machine",
"start",
Expand Down
116 changes: 0 additions & 116 deletions src/darwin_x64.ts

This file was deleted.

21 changes: 6 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import type { OVMDarwinArm64Options, OVMDarwinOptions, OVMWindowsOptions } from "./type";
import { DarwinOVM } from "./darwin_x64";
import type { OVMDarwinOptions, OVMWindowsOptions } from "./type";
import { DarwinOVM } from "./darwin";
import { WindowsOVM } from "./windows";
import { DarwinOVMArm64 } from "./darwin_arm64";

export const createDarwinOVM = (options: OVMDarwinOptions): Promise<DarwinOVM> => {
return DarwinOVM.create(options);
};

export const createDarwinArm64OVM = (options: OVMDarwinArm64Options): Promise<DarwinOVMArm64> => {
return DarwinOVMArm64.create(options);
};

export const createWindowsOVM = (options: OVMWindowsOptions): WindowsOVM => {
return WindowsOVM.create(options);
};

export { OVMDarwinAppEventValue, OVMDarwinVzState, OVMWindowsRunEventValue, OVMWindowsPrepareEventValue } from "./type";
export { OVMWindowsRunEventValue, OVMWindowsPrepareEventValue } from "./type";

export type {
OVMDarwinEventData,
OVMDarwinOptions,
OVMDarwinInitEvent,
OVMDarwinStartEvent,
OVMDarwinInfo,
OVMDarwinState,
OVMDarwinArm64Options,
OVMDarwinArm64InitEvent,
OVMDarwinArm64StartEvent,
OVMDarwinArm64Info,
OVMDarwinArm64State,
OVMWindowsOptions,
OVMWindowsEventData,
OVMWindowsInfo,
} from "./type";
export type { DarwinOVM, DarwinOVMArm64, WindowsOVM };
export type { DarwinOVM, WindowsOVM };
50 changes: 8 additions & 42 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import path from "node:path";
import http from "node:http";
import { Readable } from "node:stream";
import type {
OVMDarwinArm64Info,
OVMDarwinArm64State,
OVMDarwinInfo,
OVMDarwinState,
OVMWindowsInfo,
} from "./type";
import type { OVMDarwinInfo, OVMDarwinState, OVMWindowsInfo } from "./type";
import { createEventSource } from "eventsource-client";
import fetch from "node-fetch";
import { enableDebug } from "./utils";
Expand All @@ -22,7 +16,7 @@ const DEFAULT_TIMEOUT = 200;
const NEVER_TIMEOUT = 0;

abstract class Request {
public abstract info(): Promise<OVMDarwinInfo | OVMWindowsInfo | OVMDarwinArm64Info>;
public abstract info(): Promise<OVMDarwinInfo | OVMWindowsInfo>;

protected readonly socketPath: string;

Expand Down Expand Up @@ -122,35 +116,7 @@ abstract class Request {
}
}

export class RequestDarwin extends Request {
public constructor(socketDir: string, name: string) {
super(path.join(socketDir, `${name}-restful.sock`));
}

public async info(): Promise<OVMDarwinInfo> {
return JSON.parse(await this.do("info", Method.GET)) as Promise<OVMDarwinInfo>;
}

public async state(): Promise<OVMDarwinState> {
return JSON.parse(await this.do("state", Method.GET)) as Promise<OVMDarwinState>;
}

public async pause(): Promise<void> {
await this.do("pause", Method.POST);
}

public async resume(): Promise<void> {
await this.do("resume", Method.POST);
}

public async powerSaveMode(enable: boolean): Promise<void> {
await this.do("power-save-mode", Method.PUT, DEFAULT_TIMEOUT, {
enable,
});
}
}

type RequestDarwinArm64RawInfoResp = {
type RequestDarwinRawInfoResp = {
GvProxy: {
HostSocks: [string];
};
Expand All @@ -161,13 +127,13 @@ type RequestDarwinArm64RawInfoResp = {
}
}

export class RequestDarwinArm64 extends Request {
export class RequestDarwin extends Request {
public constructor(workspace: string) {
super(path.join(workspace, "tmp", "ovm_restapi.socks"), "http://ovm/default/");
}

public async info(): Promise<OVMDarwinArm64Info> {
const result = JSON.parse(await this.do("info", Method.GET)) as RequestDarwinArm64RawInfoResp;
public async info(): Promise<OVMDarwinInfo> {
const result = JSON.parse(await this.do("info", Method.GET)) as RequestDarwinRawInfoResp;
return {
podmanSocketPath: result.GvProxy.HostSocks[0],
sshPort: result.SSH.Port,
Expand All @@ -177,8 +143,8 @@ export class RequestDarwinArm64 extends Request {
};
}

public async state(): Promise<OVMDarwinArm64State> {
const result = JSON.parse(await this.do("vmstat", Method.GET)) as { CurrentStat: OVMDarwinArm64State["state"] };
public async state(): Promise<OVMDarwinState> {
const result = JSON.parse(await this.do("vmstat", Method.GET)) as { CurrentStat: OVMDarwinState["state"] };
return {
state: result.CurrentStat,
canStop: true,
Expand Down
Loading

0 comments on commit 32a119d

Please sign in to comment.