This commit is contained in:
Benjamin Toby
2025-02-16 17:12:40 +01:00
parent e9761cc971
commit e95f4d1087
628 changed files with 3091 additions and 1073 deletions
@@ -0,0 +1,50 @@
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
import { execSync, type ExecSyncOptions } from "child_process";
type Param = {
filePath: string;
debug?: boolean;
server?: any;
options?: ExecSyncOptions;
};
export default function bunExecSSH({
filePath,
debug,
server,
options,
}: Param): string | undefined {
try {
let cmdPrefix = grabCoderankSSHPrefix();
let finalCmd = `${cmdPrefix}`;
if (server) {
finalCmd += ` ${server.username}@${server.ip}`;
}
finalCmd += ` bun < ${filePath}`;
if (debug) {
console.log("DEBUG:::", finalCmd);
}
const str = execSync(finalCmd, {
stdio: "pipe",
...options,
encoding: "utf-8",
});
if (debug) {
console.log("DEBUG:::", str);
}
return str.trim();
} catch (error: any) {
if (debug) {
console.log(`DEBUG::: Raw SSh Error: ${error.message}`);
}
return undefined;
}
}
@@ -0,0 +1,22 @@
import { Client, type ConnectConfig } from "ssh2";
type Param = {
config: ConnectConfig;
};
export default async function connectSSH({
config,
}: Param): Promise<Client | undefined> {
return new Promise((resolve) => {
const ssh = new Client();
ssh.on("ready", () => {
resolve(ssh);
}).connect(config);
ssh.on("error", (err) => {
console.log(`SHH connect ERROR: ${err.message}`);
resolve(undefined);
});
});
}
@@ -0,0 +1,70 @@
import grabCoderankSSHPrefix from "./grab-ssh-prefix";
import { exec, execSync, type ExecSyncOptions } from "child_process";
import debugLog from "@moduletrace/datasquirel/dist/package-shared/utils/logging/debug-log";
type Param = {
cmd: string;
debug?: boolean;
server?: any;
options?: ExecSyncOptions;
detached?: boolean;
};
export default function execRawSSH({
cmd,
debug,
server,
options,
detached,
}: Param): string | undefined {
function debugFn(log: any, label?: string) {
debugLog({
log: log,
label: label,
title: "execRawSSH",
addTime: true,
});
}
try {
let cmdPrefix = grabCoderankSSHPrefix();
let finalCmd = `${cmdPrefix}`;
if (server) {
finalCmd += ` ${server.username}@${server.ip}`;
}
finalCmd += ` << 'CDREXEC' \n\
${cmd}\n\
CDREXEC`;
if (debug) {
debugFn(finalCmd, "finalCmd");
}
const str = detached
? ""
: execSync(finalCmd, {
stdio: "pipe",
...options,
encoding: "utf-8",
});
if (detached) {
exec(finalCmd);
}
if (debug) {
debugFn(str, "commandResult");
}
return str.trim();
} catch (error: any) {
if (debug) {
debugFn(error.message, "Raw SSh Error");
}
return undefined;
}
}
@@ -0,0 +1,51 @@
import { Client } from "ssh2";
type Param = {
ssh: Client;
cmd: string;
debug?: boolean;
};
export default async function execSSH({
ssh,
cmd,
debug,
}: Param): Promise<string | undefined> {
if (!ssh) {
console.log(`No SSH object passed into execSSH function`);
return undefined;
}
return new Promise((resolve) => {
let finalData = "";
let stdErrData = "";
ssh.exec(cmd, (err, stream) => {
if (err) throw err;
stream
.on("close", (code?: number, signal?: string) => {
if (code || signal) {
resolve(undefined);
} else {
resolve(finalData);
}
})
.on("data", (data: any) => {
finalData += data;
})
.stderr.on("data", (data) => {
stdErrData += data;
})
.on("close", () => {
if (stdErrData.match(/./)) {
if (debug) {
console.log("SSH Error code:", stdErrData);
}
resolve(undefined);
}
});
});
});
}
@@ -0,0 +1,3 @@
export default function grabCoderankSSHPrefix() {
return `ssh -i ${process.env.CODERANK_APP_DIR}/ssh/coderank -o StrictHostKeyChecking=no -C -c aes128-ctr`;
}