diff --git a/src/test.ts b/src/test.ts index e61d5d6..e91a295 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,9 +1,7 @@ import { NS } from '@ns'; -import { nuke } from './utils/nuke'; import { scan } from './utils/scan'; -import { scriptPropagator } from './utils/script-propagator'; +import { startall } from './utils/startall'; export const main = (ns: NS) => { - ns.tprint(scan(ns, { rootAccess: true })); - scriptPropagator(ns); + ns.tprint(ns.purchaseServer('chungus', 128)); }; diff --git a/src/utils/killall.ts b/src/utils/killall.ts new file mode 100644 index 0000000..aa77371 --- /dev/null +++ b/src/utils/killall.ts @@ -0,0 +1,33 @@ +import { NS } from '@ns'; +import { scan } from './scan'; + +export const killall = (ns: NS): void => { + const allHosts = scan(ns); + let hostsWithKills = 0; + let killedProcesses = 0; + + allHosts.forEach((host) => { + // do not kill any scripts on home + if (host === 'home') return; + + // check if there are any running scripts on this host + const runningProcesses = ns.ps(host).length; + if (runningProcesses === 0) { + return; + } else { + // ns.killall returns true if it successfully stopped scripts + if (ns.killall(host)) { + hostsWithKills++; + killedProcesses += runningProcesses; + ns.tprint(`Killed ${runningProcesses} processes on ${host}`); + } + } + }); + + ns.tprint(`Processes killed: ${killedProcesses}`); + ns.tprint(`Hosts affected: ${hostsWithKills}`); +}; + +export const main = (ns: NS) => { + killall(ns); +}; diff --git a/src/utils/nuke.ts b/src/utils/nuke.ts index e20b452..a8bcc23 100644 --- a/src/utils/nuke.ts +++ b/src/utils/nuke.ts @@ -33,16 +33,19 @@ export const nuke = (ns: NS) => { skippedCount++; } } catch (error) { - ns.tprint(`Error nuking ${host}: ${error}`); + // ns.tprint(`Error nuking ${host}: ${error}`); skippedCount++; } }); // print a summary of the results - ns.tprint(`Already rooted: ${alreadyNukedCount}`); - ns.tprint(`Newly rooted: ${newlyNukedCount}`); + ns.tprint(`Already nuked: ${alreadyNukedCount}`); + ns.tprint(`Newly nuked: ${newlyNukedCount}`); ns.tprint(`Skipped: ${skippedCount}`); - // scan again to get the list of all hosts we have root access to return newlyNukedHosts; }; + +export const main = (ns: NS) => { + nuke(ns); +}; diff --git a/src/utils/scan.ts b/src/utils/scan.ts index e5115ab..7a158a3 100644 --- a/src/utils/scan.ts +++ b/src/utils/scan.ts @@ -36,3 +36,7 @@ export const scan = (ns: NS, options: ScanOptions = {}): string[] => { // convert the set to an array and return it return Array.from(filteredHosts); }; + +export const main = (ns: NS) => { + ns.tprint(scan(ns)); +}; diff --git a/src/utils/script-cleanup.ts b/src/utils/script-cleanup.ts new file mode 100644 index 0000000..559f625 --- /dev/null +++ b/src/utils/script-cleanup.ts @@ -0,0 +1,34 @@ +import { NS } from '@ns'; +import { scan } from './scan'; + +export const scriptCleanup = (ns: NS) => { + // get all hosts with root access + const rootedHosts: string[] = scan(ns, { rootAccess: true }); + + let totalScriptsDeleted = 0; + let hostsCleaned = 0; + + // iterate through all the rooted hosts and find .js files + rootedHosts.forEach((host) => { + let scriptsDeleted = 0; + + const remoteScripts: string[] = ns.ls(host, '.js'); + remoteScripts.forEach((script) => { + ns.rm(script, host); + scriptsDeleted++; + totalScriptsDeleted++; + }); + ns.tprint(`${host}: ${scriptsDeleted} scripts deleted`); + hostsCleaned++; + }); + + ns.tprint(`Total hosts cleaned: ${hostsCleaned}`); + ns.tprint(`Total scripts deleted: ${totalScriptsDeleted}`); + + // return the list of rooted hosts for potential further use + return rootedHosts; +}; + +export const main = (ns: NS) => { + scriptCleanup(ns); +}; diff --git a/src/utils/script-propagator.ts b/src/utils/script-propagator.ts index 85511d2..feca6fc 100644 --- a/src/utils/script-propagator.ts +++ b/src/utils/script-propagator.ts @@ -26,3 +26,7 @@ export const scriptPropagator = (ns: NS) => { // return the list of rooted hosts for potential further use return rootedHosts; }; + +export const main = (ns: NS) => { + scriptPropagator(ns); +}; diff --git a/src/utils/startall.ts b/src/utils/startall.ts new file mode 100644 index 0000000..106f266 --- /dev/null +++ b/src/utils/startall.ts @@ -0,0 +1,60 @@ +import { NS } from '@ns'; +import { scan } from './scan'; + +interface StartOptions { + threads?: number; // explicit thread count + ramPercentage?: number; // prcentage of max ram (0.0 to 1.0) + args?: string[]; // optional arguments to pass to the script +} + +export const startall = (ns: NS, scriptName: string, options: StartOptions = {}) => { + const { threads: threadCount, ramPercentage, args = [] } = options; + const rootedHosts = scan(ns, { rootAccess: true }); + let totalThreadsStarted = 0; + let hostsStartedOn = 0; + + // get the ram required to run one thread of the script + // assume that the script is already on all the hosts that we want to run it in + // should be ran after propagation + const ramPerThread = ns.getScriptRam(scriptName, 'home'); + + // iterate through all the rooted hosts and start the script + rootedHosts.forEach((host) => { + if (host === 'home') return; + + // calculate how many threads we can run based on the options provided + const availableRam = ns.getServerMaxRam(host) - ns.getServerUsedRam(host); + + let threadsToUse: number; + + if (threadCount) { + // priority 1 explicit thread count + threadsToUse = threadCount; + } else if (ramPercentage !== undefined) { + // priority 2 percentage of total ram + const targetRam = ns.getServerMaxRam(host) * ramPercentage; + threadsToUse = Math.floor(targetRam / ramPerThread); + } else { + // default use all available ram + threadsToUse = Math.floor(availableRam / ramPerThread); + } + + if (threadsToUse > 0) { + ns.exec(scriptName, host, threadsToUse, ...args); + ns.tprint(`Started ${scriptName} on ${host} with ${threadsToUse} threads`); + totalThreadsStarted += threadsToUse; + hostsStartedOn++; + } else { + ns.tprint(`Not enough RAM to run ${scriptName} on ${host}`); + } + }); + + ns.tprint(`Total threads started: ${totalThreadsStarted}`); + ns.tprint(`Hosts affected: ${hostsStartedOn}`); +}; + +export const main = (ns: NS) => { + // get the arguments from the command line + const args = ns.args as string[]; + startall(ns, 'super.js', { args: args }); +};