mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 07:43:12 +09:00
Merge pull request #1010 from bbassie/feature/plugin-browser
Plugin management through web interface
This commit is contained in:
1 parent
e36a76acc5
commit
ffe82f81d8
48 files changed
+4275
-536
No files matched your search
+79
-24
@@ -4,6 +4,9 @@ import { exec } from "child_process";
|
||||
import { logger } from "./logging.js";
|
||||
import { RequestError } from "./errors.js";
|
||||
import type { PluginNodeEnvInfo } from "./plugin.js";
|
||||
import { isNonRegistryPackage } from "./load_plugin_list.js";
|
||||
import { LockFile, LockFileExistsError, LockFileRecoveryError } from "./LockFile.js";
|
||||
import { checkRegistry, parsePackageSpec } from "./npm_registry.js";
|
||||
const execAsync = util.promisify(exec);
|
||||
|
||||
function isDev() {
|
||||
@@ -12,10 +15,55 @@ function isDev() {
|
||||
return import.meta.dirname.split(path.sep).at(-5) === "packages"; // opposed to "@clusterio"
|
||||
}
|
||||
|
||||
async function logExec(cmd: string) {
|
||||
logger.audit(`RCE | ${cmd}`);
|
||||
if (!isDev()) {
|
||||
await execAsync(cmd);
|
||||
const operationInProgressMessage = "Another package operation is already in progress on this machine";
|
||||
|
||||
let operationInProgress = false;
|
||||
|
||||
/**
|
||||
* Run an npm command, at most one at a time per installation. npm runs in the working directory,
|
||||
* which a standalone controller and host share, so a lock file there guards against operations
|
||||
* from other processes and other copies of lib. The flag makes concurrent calls through this copy
|
||||
* reject without racing a release of the lock.
|
||||
*
|
||||
* @param cmd - Command to run
|
||||
* @param check - Called while holding the lock before running the command, may throw to cancel it
|
||||
*/
|
||||
async function logExec(cmd: string, check?: () => Promise<void>) {
|
||||
// Checked and set before the first await so concurrent calls in this process can't interleave
|
||||
if (operationInProgress) {
|
||||
throw new RequestError(operationInProgressMessage);
|
||||
}
|
||||
operationInProgress = true;
|
||||
const lockPath = path.resolve("npm-operation.lock");
|
||||
// Signal listeners would change how the process exits. If only this process is killed npm keeps
|
||||
// running unlocked, Ctrl+C, systemd and containers stop it too.
|
||||
const lock = new LockFile(lockPath, false);
|
||||
try {
|
||||
try {
|
||||
await lock.acquire();
|
||||
} catch (err) {
|
||||
if (err instanceof LockFileRecoveryError) {
|
||||
throw new RequestError(err.message);
|
||||
}
|
||||
if (!(err instanceof LockFileExistsError)) {
|
||||
throw err;
|
||||
}
|
||||
const holder = await lock.holderPid();
|
||||
throw new RequestError(
|
||||
`${operationInProgressMessage}${holder ? ` (${lockPath} is held by process ${holder})` : ""}`
|
||||
);
|
||||
}
|
||||
try {
|
||||
await check?.();
|
||||
logger.audit(`RCE | ${cmd}`);
|
||||
if (!isDev()) {
|
||||
await execAsync(cmd);
|
||||
}
|
||||
} finally {
|
||||
await lock.release();
|
||||
}
|
||||
} finally {
|
||||
operationInProgress = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,16 +71,13 @@ export async function updatePackage(...names: string[]) {
|
||||
return logExec(`npm update --save ${names.join(" ")}`);
|
||||
}
|
||||
|
||||
export async function installPackage(name: string) {
|
||||
return logExec(`npm install --save ${name}`);
|
||||
export async function installPackage(name: string, check?: () => Promise<void>) {
|
||||
return logExec(`npm install --save ${name}`, check);
|
||||
}
|
||||
|
||||
export async function handlePluginUpdate(pluginName: string, pluginInfos: PluginNodeEnvInfo[]) {
|
||||
if (!pluginInfos.some(plugin => plugin.npmPackage === pluginName)) {
|
||||
throw new RequestError(`Plugin ${pluginName} is not installed on this machine`);
|
||||
}
|
||||
|
||||
return await updatePackage(pluginName);
|
||||
/** Whether an npm operation is running in this installation, from this or another process */
|
||||
export async function packageOperationRunning() {
|
||||
return operationInProgress || !await new LockFile(path.resolve("npm-operation.lock")).isStale();
|
||||
}
|
||||
|
||||
export async function handleUpdateAll(corePackage: string, pluginInfos: PluginNodeEnvInfo[]) {
|
||||
@@ -42,20 +87,30 @@ export async function handleUpdateAll(corePackage: string, pluginInfos: PluginNo
|
||||
return await updatePackage(corePackage, ...pluginPackages);
|
||||
}
|
||||
|
||||
export async function handlePluginInstall(pluginName: string) {
|
||||
if (pluginName.length > 214 || /[^a-zA-Z0-9\-_.+@\/]/.test(pluginName)) {
|
||||
// https://docs.npmjs.com/cli/v11/configuring-npm/package-json#name
|
||||
// https://www.npmjs.com/package/validate-npm-package-name
|
||||
throw new RequestError(`Invalid plugin name: ${pluginName}`);
|
||||
/** Throws if the dependency by this name is a path, git, url or alias that installing by name would replace */
|
||||
async function checkNotLocalDependency(name: string) {
|
||||
if (await isNonRegistryPackage(name)) {
|
||||
throw new RequestError(`${name} is not installed from npm on this machine`);
|
||||
}
|
||||
}
|
||||
|
||||
const packageName = encodeURI(pluginName);
|
||||
const npmRequest = await fetch(`https://registry.npmjs.com/${packageName}`, {
|
||||
method: "HEAD",
|
||||
});
|
||||
if (!npmRequest.ok) {
|
||||
throw new RequestError(`Unknown plugin: ${packageName}`);
|
||||
export async function handlePluginUpdate(pluginName: string, pluginInfos: PluginNodeEnvInfo[]) {
|
||||
// Optionally pinned to a version as name@version to update past the range in package.json
|
||||
const { name, version } = parsePackageSpec(pluginName);
|
||||
if (!pluginInfos.some(plugin => plugin.npmPackage === name)) {
|
||||
throw new RequestError(`Plugin ${name} is not installed on this machine`);
|
||||
}
|
||||
|
||||
return await installPackage(packageName);
|
||||
if (version === undefined) {
|
||||
return await updatePackage(name);
|
||||
}
|
||||
// Checked under the lock as another operation may change where it's installed from
|
||||
return await installPackage(await checkRegistry(name, version), () => checkNotLocalDependency(name));
|
||||
}
|
||||
|
||||
/** Install a plugin, optionally pinned to a version as name@version, returns the package name */
|
||||
export async function handlePluginInstall(pluginName: string) {
|
||||
const { name, version } = parsePackageSpec(pluginName);
|
||||
await installPackage(await checkRegistry(name, version), () => checkNotLocalDependency(name));
|
||||
return name;
|
||||
}
|
||||
Reference in new issue
Block a user