new new
This commit is contained in:
parent
9eae7c0951
commit
218609bffb
18
src/vm.ts
18
src/vm.ts
|
|
@ -5,6 +5,18 @@ const CONTAINER_NAME = "sandlot"
|
||||||
const USER = "ubuntu"
|
const USER = "ubuntu"
|
||||||
const CLAUDE_BIN = `/home/${USER}/.local/bin/claude`
|
const CLAUDE_BIN = `/home/${USER}/.local/bin/claude`
|
||||||
|
|
||||||
|
/** Translate a host path to its corresponding container path. */
|
||||||
|
function containerPath(hostPath: string): string {
|
||||||
|
const home = homedir()
|
||||||
|
if (hostPath.startsWith(`${home}/.sandlot`)) {
|
||||||
|
return "/sandlot" + hostPath.slice(`${home}/.sandlot`.length)
|
||||||
|
}
|
||||||
|
if (hostPath.startsWith(`${home}/dev`)) {
|
||||||
|
return "/dev-host" + hostPath.slice(`${home}/dev`.length)
|
||||||
|
}
|
||||||
|
return hostPath
|
||||||
|
}
|
||||||
|
|
||||||
function requireContainer(): void {
|
function requireContainer(): void {
|
||||||
if (!Bun.which("container")) {
|
if (!Bun.which("container")) {
|
||||||
console.error('Apple Container is not installed. Install it with: brew install container')
|
console.error('Apple Container is not installed. Install it with: brew install container')
|
||||||
|
|
@ -30,7 +42,7 @@ export async function ensure(log?: (msg: string) => void): Promise<void> {
|
||||||
// Create from scratch
|
// Create from scratch
|
||||||
const home = homedir()
|
const home = homedir()
|
||||||
log?.("Pulling image & creating container")
|
log?.("Pulling image & creating container")
|
||||||
await $`container run -d --name ${CONTAINER_NAME} -m 4G --mount type=bind,source=${home}/dev,target=${home}/dev,readonly -v ${home}/.sandlot:${home}/.sandlot ubuntu:24.04 sleep infinity`.quiet()
|
await $`container run -d --name ${CONTAINER_NAME} -m 4G --mount type=bind,source=${home}/dev,target=/dev-host,readonly -v ${home}/.sandlot:/sandlot ubuntu:24.04 sleep infinity`.quiet()
|
||||||
|
|
||||||
// Provision (as root)
|
// Provision (as root)
|
||||||
log?.("Installing packages")
|
log?.("Installing packages")
|
||||||
|
|
@ -97,7 +109,7 @@ export async function status(): Promise<"running" | "stopped" | "missing"> {
|
||||||
|
|
||||||
/** Launch claude in the container at the given workdir. */
|
/** Launch claude in the container at the given workdir. */
|
||||||
export async function claude(workdir: string, prompt?: string): Promise<void> {
|
export async function claude(workdir: string, prompt?: string): Promise<void> {
|
||||||
const args = ["container", "exec", "-it", "--user", USER, "--workdir", workdir, CONTAINER_NAME, CLAUDE_BIN, "--dangerously-skip-permissions"]
|
const args = ["container", "exec", "-it", "--user", USER, "--workdir", containerPath(workdir), CONTAINER_NAME, CLAUDE_BIN, "--dangerously-skip-permissions"]
|
||||||
if (prompt) args.push(prompt)
|
if (prompt) args.push(prompt)
|
||||||
const proc = Bun.spawn(args, { stdin: "inherit", stdout: "inherit", stderr: "inherit" })
|
const proc = Bun.spawn(args, { stdin: "inherit", stdout: "inherit", stderr: "inherit" })
|
||||||
await proc.exited
|
await proc.exited
|
||||||
|
|
@ -123,7 +135,7 @@ export async function info(): Promise<void> {
|
||||||
|
|
||||||
/** Run a bash command in the container at the given workdir, capturing output. */
|
/** Run a bash command in the container at the given workdir, capturing output. */
|
||||||
export async function exec(workdir: string, command: string): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
export async function exec(workdir: string, command: string): Promise<{ exitCode: number; stdout: string; stderr: string }> {
|
||||||
const result = await $`container exec --user ${USER} --workdir ${workdir} ${CONTAINER_NAME} bash -c ${"export PATH=$HOME/.local/bin:$PATH; " + command}`.nothrow().quiet()
|
const result = await $`container exec --user ${USER} --workdir ${containerPath(workdir)} ${CONTAINER_NAME} bash -c ${"export PATH=$HOME/.local/bin:$PATH; " + command}`.nothrow().quiet()
|
||||||
return {
|
return {
|
||||||
exitCode: result.exitCode,
|
exitCode: result.exitCode,
|
||||||
stdout: result.stdout.toString().trim(),
|
stdout: result.stdout.toString().trim(),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user