mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-26 02:11:53 +00:00
* store, properties, manifest * interfaces * init and backups * fix init and backups * file models * more versions * dependencies * config except dynamic types * clean up config * remove disabled from non-dynamic vaues * actions * standardize example code block formats * wip: actions refactor Co-authored-by: Jade <Blu-J@users.noreply.github.com> * commit types * fix types * update types * update action request type * update apis * add description to actionrequest * clean up imports * revert package json * chore: Remove the recursive to the index * chore: Remove the other thing I was testing * flatten action requests * update container runtime with new config paradigm * new actions strategy * seems to be working * misc backend fixes * fix fe bugs * only show breakages if breakages * only show success modal if result * don't panic on failed removal * hide config from actions page * polyfill autoconfig * use metadata strategy for actions instead of prev * misc fixes * chore: split the sdk into 2 libs (#2736) * follow sideload progress (#2718) * follow sideload progress * small bugfix * shareReplay with no refcount false * don't wrap sideload progress in RPCResult * dont present toast --------- Co-authored-by: Aiden McClelland <me@drbonez.dev> * chore: Add the initial of the creation of the two sdk * chore: Add in the baseDist * chore: Add in the baseDist * chore: Get the web and the runtime-container running * chore: Remove the empty file * chore: Fix it so the container-runtime works --------- Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com> Co-authored-by: Aiden McClelland <me@drbonez.dev> * misc fixes * update todos * minor clean up * fix link script * update node version in CI test * fix node version syntax in ci build * wip: fixing callbacks * fix sdk makefile dependencies * add support for const outside of main * update apis * don't panic! * Chore: Capture weird case on rpc, and log that * fix procedure id issue * pass input value for dep auto config * handle disabled and warning for actions * chore: Fix for link not having node_modules * sdk fixes * fix build * fix build * fix build --------- Co-authored-by: Matt Hill <mattnine@protonmail.com> Co-authored-by: Jade <Blu-J@users.noreply.github.com> Co-authored-by: J H <dragondef@gmail.com> Co-authored-by: Jade <2364004+Blu-J@users.noreply.github.com> Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com>
145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
import { DEFAULT_SIGTERM_TIMEOUT } from "."
|
|
import { NO_TIMEOUT, SIGKILL, SIGTERM } from "../../../base/lib/types"
|
|
|
|
import * as T from "../../../base/lib/types"
|
|
import { asError } from "../../../base/lib/util/asError"
|
|
import {
|
|
ExecSpawnable,
|
|
MountOptions,
|
|
SubContainerHandle,
|
|
SubContainer,
|
|
} from "../util/SubContainer"
|
|
import { splitCommand } from "../util"
|
|
import * as cp from "child_process"
|
|
|
|
export class CommandController {
|
|
private constructor(
|
|
readonly runningAnswer: Promise<unknown>,
|
|
private state: { exited: boolean },
|
|
private readonly subcontainer: SubContainer,
|
|
private process: cp.ChildProcessWithoutNullStreams,
|
|
readonly sigtermTimeout: number = DEFAULT_SIGTERM_TIMEOUT,
|
|
) {}
|
|
static of<Manifest extends T.Manifest>() {
|
|
return async <A extends string>(
|
|
effects: T.Effects,
|
|
subcontainer:
|
|
| {
|
|
id: keyof Manifest["images"] & T.ImageId
|
|
sharedRun?: boolean
|
|
}
|
|
| SubContainer,
|
|
command: T.CommandType,
|
|
options: {
|
|
subcontainerName?: string
|
|
// Defaults to the DEFAULT_SIGTERM_TIMEOUT = 30_000ms
|
|
sigtermTimeout?: number
|
|
mounts?: { path: string; options: MountOptions }[]
|
|
runAsInit?: boolean
|
|
env?:
|
|
| {
|
|
[variable: string]: string
|
|
}
|
|
| undefined
|
|
cwd?: string | undefined
|
|
user?: string | undefined
|
|
onStdout?: (x: Buffer) => void
|
|
onStderr?: (x: Buffer) => void
|
|
},
|
|
) => {
|
|
const commands = splitCommand(command)
|
|
const subc =
|
|
subcontainer instanceof SubContainer
|
|
? subcontainer
|
|
: await (async () => {
|
|
const subc = await SubContainer.of(
|
|
effects,
|
|
subcontainer,
|
|
options?.subcontainerName || commands.join(" "),
|
|
)
|
|
for (let mount of options.mounts || []) {
|
|
await subc.mount(mount.options, mount.path)
|
|
}
|
|
return subc
|
|
})()
|
|
let childProcess: cp.ChildProcessWithoutNullStreams
|
|
if (options.runAsInit) {
|
|
childProcess = await subc.launch(commands, {
|
|
env: options.env,
|
|
})
|
|
} else {
|
|
childProcess = await subc.spawn(commands, {
|
|
env: options.env,
|
|
})
|
|
}
|
|
const state = { exited: false }
|
|
const answer = new Promise<null>((resolve, reject) => {
|
|
childProcess.on("exit", (code) => {
|
|
state.exited = true
|
|
if (
|
|
code === 0 ||
|
|
code === 143 ||
|
|
(code === null && childProcess.signalCode == "SIGTERM")
|
|
) {
|
|
return resolve(null)
|
|
}
|
|
if (code) {
|
|
return reject(new Error(`${commands[0]} exited with code ${code}`))
|
|
} else {
|
|
return reject(
|
|
new Error(
|
|
`${commands[0]} exited with signal ${childProcess.signalCode}`,
|
|
),
|
|
)
|
|
}
|
|
})
|
|
})
|
|
|
|
return new CommandController(
|
|
answer,
|
|
state,
|
|
subc,
|
|
childProcess,
|
|
options.sigtermTimeout,
|
|
)
|
|
}
|
|
}
|
|
get subContainerHandle() {
|
|
return new SubContainerHandle(this.subcontainer)
|
|
}
|
|
async wait({ timeout = NO_TIMEOUT } = {}) {
|
|
if (timeout > 0)
|
|
setTimeout(() => {
|
|
this.term()
|
|
}, timeout)
|
|
try {
|
|
return await this.runningAnswer
|
|
} finally {
|
|
if (!this.state.exited) {
|
|
this.process.kill("SIGKILL")
|
|
}
|
|
await this.subcontainer.destroy?.().catch((_) => {})
|
|
}
|
|
}
|
|
async term({ signal = SIGTERM, timeout = this.sigtermTimeout } = {}) {
|
|
try {
|
|
if (!this.state.exited) {
|
|
if (signal !== "SIGKILL") {
|
|
setTimeout(() => {
|
|
if (!this.state.exited) this.process.kill("SIGKILL")
|
|
}, timeout)
|
|
}
|
|
if (!this.process.kill(signal)) {
|
|
console.error(
|
|
`failed to send signal ${signal} to pid ${this.process.pid}`,
|
|
)
|
|
}
|
|
}
|
|
|
|
await this.runningAnswer
|
|
} finally {
|
|
await this.subcontainer.destroy?.()
|
|
}
|
|
}
|
|
}
|