diff --git a/src/machine.ts b/src/machine.ts index 9066f4c..49fbf99 100644 --- a/src/machine.ts +++ b/src/machine.ts @@ -28,12 +28,6 @@ export type SetContextEvent = value: ContextInput[K]; }; -function getHostLabel(context: Partial) { - const host = context?.destinationAlternatives - ?.[context?.alternativeIndex ?? 0]?.host; - return `[${host ?? "unknown"}]`; -} - export const machine = setup({ types: { context: {} as Context, @@ -85,7 +79,7 @@ export const machine = setup({ cleanup: { entry: async ({ context, self }) => { context.logger.log( - `${getHostLabel(context)}: Cleaning up...`, + `Cleaning up...`, ); const cleanedUp = Promise.allSettled([ context.stdin.close(), @@ -95,9 +89,7 @@ export const machine = setup({ ]); await kill(context.sshProcess, [["SIGINT", 1000], ["SIGTERM", 1000]]); await cleanedUp; - context.logger.log( - `${getHostLabel(context)}: Cleaned up.`, - ); + context.logger.log(`Cleaned up.`); self.send({ type: "cleanedUp" }); }, on: { @@ -106,14 +98,14 @@ export const machine = setup({ }, connecting: { entry: ({ context, self }) => { - context.logger.log(`${getHostLabel(context)}: Connecting...`); + context.logger.log(`Connecting...`); context.alternativeIndex = typeof context.alternativeIndex === "number" ? (context.alternativeIndex + 1) % context.sshCommands.length : 0; context.sshProcess = context.sshCommands[context.alternativeIndex] .spawn(); Object.assign(context, wrapProcess(context.sshProcess)); - context.logger.log(`${getHostLabel(context)}: Connected.`); + context.logger.log(`Connected.`); self.send({ type: "connectionSuccess" }); }, on: { @@ -126,7 +118,7 @@ export const machine = setup({ 5000: { target: "cleanup" }, }, entry: async ({ context, self }) => { - context.logger.log(`${getHostLabel(context)}: Reading output...`); + context.logger.log(`Reading output...`); const stdout: ReadableStream = context.stdout; const reader: ReadableStreamDefaultReader = stdout.getReader(); let done = false; @@ -135,39 +127,29 @@ export const machine = setup({ const result = await reader.read(); const burst = result.value; done = result.done; - context.logger.log(getHostLabel(context) + ":", { done, burst }); + context.logger.log(`done: ${done}, burst: ${burst}`); if (done || !burst) { - context.logger.log( - `${getHostLabel(context)}: Got no output (already done).`, - ); + context.logger.log(`Got no output (already done).`); continue; } if (isZfsUnlockPrompt(burst)) { - context.logger.log( - `${getHostLabel(context)}: Got zfs unlock prompt.`, - ); + context.logger.log(`Got zfs unlock prompt.`); self.send({ type: "zfsUnlockPromptDetected" }); break; } if (isCommandPrompt(burst)) { - context.logger.log( - `${getHostLabel(context)}: Got command prompt.`, - ); + context.logger.log(`Got command prompt.`); self.send({ type: "commandPromptDetected" }); break; } - context.logger.log(`${getHostLabel(context)}: Got other output.`); + context.logger.log(`Got other output.`); } } finally { - context.logger.log( - `${getHostLabel(context)}: Releasing stdout reader lock.`, - ); + context.logger.log(`Releasing stdout reader lock.`); reader.releaseLock(); - context.logger.log( - `${getHostLabel(context)}: Released stdout reader lock.`, - ); + context.logger.log(`Released stdout reader lock.`); } - context.logger.log(`${getHostLabel(context)}: Done reading output.`); + context.logger.log(`Done reading output.`); self.send({ type: "error", data: "Done reading output and no prompts detected.", @@ -190,9 +172,9 @@ export const machine = setup({ 5000: { target: "cleanup" }, }, entry: async ({ context }) => { - context.logger.log(`${getHostLabel(context)}: Entering passphrase...`); + context.logger.log(`Entering passphrase...`); await context.stdin.write(context.passphrase); - context.logger.log(`${getHostLabel(context)}: Entered passphrase.`); + context.logger.log(`Entered passphrase.`); }, description: "Detected a ZFS unlock prompt. Entering the decryption passphrase.", @@ -203,28 +185,20 @@ export const machine = setup({ error: { target: "cleanup" }, }, entry: ({ context, self }) => { - context.logger.log(`${getHostLabel(context)}: Checking ZFS status...`); - context.logger.log( - `${getHostLabel(context)}: Lol jk. Assuming zfs is unlocked.`, - ); - context.logger.log( - `${getHostLabel(context)}: ZFS filesystem is unlocked.`, - ); + context.logger.log(`Checking ZFS status...`); + context.logger.log(`Lol jk. Assuming zfs is unlocked.`); + context.logger.log(`ZFS filesystem is unlocked.`); self.send({ type: "zfsUnlocked" }); - context.logger.log( - `${getHostLabel(context)}: Done checking ZFS status.`, - ); + context.logger.log(`Done checking ZFS status.`); }, description: "Detected a normal command prompt. Checking if the ZFS filesystem is unlocked.", }, runningSleepInfinity: { entry: async ({ context, self }) => { - context.logger.log( - `${getHostLabel(context)}: Running sleep infinity...`, - ); + context.logger.log(`Running sleep infinity...`); await context.stdin.write("sleep infinity\n"); - context.logger.log(`${getHostLabel(context)}: Ran sleep infinity.`); + context.logger.log(`Ran sleep infinity.`); await context.sshProcess.status; self.send({ type: "serverRebootDetected" }); }, @@ -237,43 +211,33 @@ export const machine = setup({ exit: { type: "final", entry: async ({ context }) => { - context.logger.log( - `${getHostLabel(context)}: Exiting with final state...`, - ); - context.logger.log(`${getHostLabel(context)}: Releasing stdin lock...`); + context.logger.log(`Exiting with final state...`); + context.logger.log(`Releasing stdin lock...`); context.stdin.releaseLock(); - context.logger.log(`${getHostLabel(context)}: Released stdin lock.`); - context.logger.log( - `${getHostLabel(context)}: Pressing Ctrl-C, Ctrl-C, Ctrl-D...`, - ); + context.logger.log(`Released stdin lock.`); + context.logger.log(`Pressing Ctrl-C, Ctrl-C, Ctrl-D...`); const rawStdin: WritableStreamDefaultWriter = context .sshProcess.stdin.getWriter(); await rawStdin.write(new Uint8Array([0x03, 0x03, 0x04])); - context.logger.log( - `${getHostLabel(context)}: Pressed Ctrl-C, Ctrl-C, Ctrl-D.`, - ); - context.logger.log(`${getHostLabel(context)}: Closing stdin...`); + context.logger.log(`Pressed Ctrl-C, Ctrl-C, Ctrl-D.`); + context.logger.log(`Closing stdin...`); await rawStdin.close(); - context.logger.log(`${getHostLabel(context)}: Closed stdin.`); + context.logger.log(`Closed stdin.`); - context.logger.log(`${getHostLabel(context)}: Cancelling stdout...`); + context.logger.log(`Cancelling stdout...`); await context.stdout.cancel(); - context.logger.log(`${getHostLabel(context)}: Cancelled stdout.`); - context.logger.log(`${getHostLabel(context)}: Cancelling stderr...`); + context.logger.log(`Cancelled stdout.`); + context.logger.log(`Cancelling stderr...`); await context.stderr.cancel(); - context.logger.log(`${getHostLabel(context)}: Cancelled stderr.`); + context.logger.log(`Cancelled stderr.`); - context.logger.log(`${getHostLabel(context)}: Killing ssh process...`); + context.logger.log(`Killing ssh process...`); context.sshProcess.kill(); - context.logger.log(`${getHostLabel(context)}: Killed ssh process.`); - context.logger.log( - `${getHostLabel(context)}: Waiting for ssh process to exit...`, - ); + context.logger.log(`Killed ssh process.`); + context.logger.log(`Waiting for ssh process to exit...`); const status = await context.sshProcess.status; context.logger.log( - `${ - getHostLabel(context) - }: Ssh process exited with status code ${status.code}.`, + `Ssh process exited with status code ${status.code}.`, ); }, },