Skip to content
Merged
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
10 changes: 5 additions & 5 deletions apps/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bun
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { parseArgs } from "node:util";
import { compiled, dashboardKey, dataDir, defaultPort, start, version } from "@vmbridge/server";
import { pidFile, restartSignal, runningPid, stopProcess } from "./process.ts";
import { logPath, protectLog, service, serviceAddresses } from "./service.ts";
import { protectLog, service, serviceAddresses } from "./service.ts";
import { update, updateFromDashboard } from "./update.ts";

const help = `VM Bridge ${version}
Expand Down Expand Up @@ -168,9 +168,9 @@ async function run(options: { host?: string; port: number }) {
process.on(restartSignal, () => stop(true));
}

function backgroundNotice(pid: number) {
async function backgroundNotice(pid: number) {
console.log(`VM Bridge is already running in the background (process ${pid}), and keeps running after this terminal closes.\n`);
const addresses = existsSync(logPath()) ? serviceAddresses(readFileSync(logPath(), "utf8")) : [];
const addresses = serviceAddresses(await service.output());
for (const address of addresses) console.log(address);
if (addresses.length) console.log("");
console.log("To run it in this terminal instead, stop the background service first with `vmbridge stop`.");
Expand Down Expand Up @@ -216,7 +216,7 @@ async function main() {
if (existing) throw new Error(`VM Bridge is already running in a terminal (process ${existing}). Stop it first.`);
const options = serverOptions(values);
await service.install(options);
return console.log(`VM Bridge now runs in the background on port ${options.port}. Logs: ${logPath()}`);
return console.log(`VM Bridge now runs in the background on port ${options.port}. Logs: ${service.logLocation()}`);
}
if (subcommand === "uninstall") {
await service.uninstall();
Expand Down
20 changes: 18 additions & 2 deletions apps/cli/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { spawn } from "node:child_process";
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, rmSync, writeFileSync } from "node:fs";
import { chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, join } from "node:path";
import { childEnv, compiled, dataDir } from "@vmbridge/server";

const label = "dev.vmbridge";

/** The background service's output on macOS. It can hold provider errors and addresses, so only the owner reads it. */
/** The background service's output on macOS (Linux uses the journal). It can hold provider errors and addresses, so only the owner reads it. */
export const logPath = () => join(dataDir(), "vmbridge.log");

/** launchd appends to the log with whatever mode it already has, and makes a new one world-readable. */
Expand Down Expand Up @@ -151,6 +151,13 @@ ${env}
await this.stop();
rmSync(this.plist(), { force: true });
},

/** Where to read the service's output. */
logLocation: () => logPath(),

async output() {
return existsSync(logPath()) ? readFileSync(logPath(), "utf8") : "";
},
};

const systemdQuote = (value: string) => `"${value.replaceAll("\\", "\\\\").replaceAll('"', '\\"').replaceAll("%", "%%")}"`;
Expand Down Expand Up @@ -224,6 +231,13 @@ WantedBy=default.target
rmSync(this.unit(), { force: true });
await exec(["systemctl", "--user", "daemon-reload"]);
},

/** systemd keeps the service's output in the journal, not in a file. */
logLocation: () => "journalctl --user -u vmbridge",

async output() {
return (await exec(["journalctl", "--user", "--unit", "vmbridge.service", "--output", "cat", "--lines", "200", "--no-pager"])).stdout;
},
};

const unsupported = {
Expand All @@ -235,6 +249,8 @@ const unsupported = {
start: async () => {},
detached: async (_: string[], __: string) => 1,
uninstall: async () => {},
logLocation: () => "",
output: async () => "",
};

export const service = process.platform === "darwin" ? launchd : process.platform === "linux" ? systemd : unsupported;