mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 07:30:41 +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
@@ -1,5 +1,5 @@
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { Button, Checkbox, Form, FormInstance, Input, Modal, Radio, Select } from "antd";
|
||||
import { Button, Checkbox, Modal, Tooltip } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
@@ -7,92 +7,7 @@ import { useAccount } from "../model/account";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import ControlContext from "./ControlContext";
|
||||
|
||||
type NpmActionProps = {
|
||||
setApplyAction(func: () => Promise<void>): void,
|
||||
form: FormInstance,
|
||||
target: lib.AddressShorthand,
|
||||
}
|
||||
|
||||
function NpmRemoteUpdate({ setApplyAction, form, target }: NpmActionProps) {
|
||||
const control = useContext(ControlContext);
|
||||
|
||||
setApplyAction(async () => {
|
||||
if (target === "controller") {
|
||||
return await control.send(new lib.ControllerUpdateRequest());
|
||||
}
|
||||
return await control.sendTo(target, new lib.HostUpdateRequest());
|
||||
});
|
||||
|
||||
return <>
|
||||
</>;
|
||||
}
|
||||
|
||||
function NpmUpdateAll({ setApplyAction, form, target }: NpmActionProps) {
|
||||
const control = useContext(ControlContext);
|
||||
|
||||
setApplyAction(async () => {
|
||||
await control.sendTo(target, new lib.UpdateAllRequest());
|
||||
});
|
||||
|
||||
return <>
|
||||
</>;
|
||||
}
|
||||
|
||||
function NpmPluginUpdate({ setApplyAction, form, target }: NpmActionProps) {
|
||||
const control = useContext(ControlContext);
|
||||
const [plugins, setPlugins] = useState<lib.PluginDetails[]>([]);
|
||||
|
||||
setApplyAction(async () => {
|
||||
const values = form.getFieldsValue();
|
||||
if (!values.plugin || values.plugin === "") {
|
||||
throw new Error("Plugin not provided");
|
||||
}
|
||||
await control.sendTo(target, new lib.PluginUpdateRequest(values.plugin));
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
control.sendTo(target, new lib.PluginListRequest()).then(newPlugins => {
|
||||
setPlugins(newPlugins);
|
||||
}).catch(notifyErrorHandler("Error fetching role list"));
|
||||
}, [target]);
|
||||
|
||||
return <>
|
||||
<Form.Item label="Plugin" name="plugin">
|
||||
<Select
|
||||
showSearch
|
||||
placeholder="Select a plugin"
|
||||
optionLabelProp="label"
|
||||
options={plugins
|
||||
.filter(p => p.npmPackage !== undefined)
|
||||
.map(plugin => ({
|
||||
value: plugin.npmPackage,
|
||||
label: `${plugin.title} (v${plugin.version})`,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</Form.Item>
|
||||
</>;
|
||||
}
|
||||
|
||||
function NpmPluginInstall({ setApplyAction, form, target }: NpmActionProps) {
|
||||
const control = useContext(ControlContext);
|
||||
|
||||
setApplyAction(async () => {
|
||||
const values = form.getFieldsValue();
|
||||
if (!values.newPlugin || values.newPlugin === "") {
|
||||
throw new Error("Plugin not provided");
|
||||
}
|
||||
await control.sendTo(target, new lib.PluginInstallRequest(values.newPlugin));
|
||||
});
|
||||
|
||||
// TODO custom npm search based on tags
|
||||
return <>
|
||||
<Form.Item label="Plugin" name="newPlugin">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
</>;
|
||||
}
|
||||
|
||||
/** Updates Clusterio on a machine, plugins are installed and updated from the plugins page */
|
||||
export function NpmButton(props: {
|
||||
target: "controller" | { hostId: number },
|
||||
canRestart?: boolean,
|
||||
@@ -101,138 +16,72 @@ export function NpmButton(props: {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [open, setOpen] = useState(false);
|
||||
const [formAction, setFormAction] = useState<string | undefined>(undefined);
|
||||
const [form] = Form.useForm();
|
||||
|
||||
const [allows, setAllows] = useState({
|
||||
remoteUpdates: true, pluginUpdates: true, pluginInstall: true,
|
||||
});
|
||||
const [restart, setRestart] = useState(false);
|
||||
const [allowed, setAllowed] = useState(true);
|
||||
|
||||
const isController = props.target === "controller";
|
||||
const hostId = props.target === "controller" ? undefined : props.target.hostId;
|
||||
useEffect(() => {
|
||||
// Without config permission assume it's allowed so the request can still be sent
|
||||
if (!account.hasPermission(isController ? "core.controller.get_config" : "core.host.get_config")) {
|
||||
setAllowed(true);
|
||||
return;
|
||||
}
|
||||
if (props.target === "controller") {
|
||||
if (!account.hasPermission("core.controller.get_config")) {
|
||||
setAllows({ remoteUpdates: true, pluginUpdates: true, pluginInstall: true });
|
||||
return; // If a user doesn't have config perms, then assume true to allow the sending the request
|
||||
}
|
||||
control.send(new lib.ControllerConfigGetRequest()).then(async (serializedConfig) => {
|
||||
control.send(new lib.ControllerConfigGetRequest()).then(serializedConfig => {
|
||||
const config = lib.ControllerConfig.fromJSON(serializedConfig, "control");
|
||||
setAllows({
|
||||
remoteUpdates: config.get("controller.allow_remote_updates"),
|
||||
pluginUpdates: config.get("controller.allow_plugin_updates"),
|
||||
pluginInstall: config.get("controller.allow_plugin_install"),
|
||||
});
|
||||
setAllowed(config.get("controller.allow_remote_updates"));
|
||||
}).catch(notifyErrorHandler("Failed to fetch controller config for remote updates"));
|
||||
} else {
|
||||
if (!account.hasPermission("core.host.get_config")) {
|
||||
setAllows({ remoteUpdates: true, pluginUpdates: true, pluginInstall: true });
|
||||
return; // If a user doesn't have config perms, then assume true to allow the sending the request
|
||||
}
|
||||
control.sendTo(props.target, new lib.HostConfigGetRequest()).then(async (serializedConfig) => {
|
||||
control.sendTo(props.target, new lib.HostConfigGetRequest()).then(serializedConfig => {
|
||||
const config = lib.HostConfig.fromJSON(serializedConfig, "control");
|
||||
setAllows({
|
||||
remoteUpdates: config.get("host.allow_remote_updates"),
|
||||
pluginUpdates: config.get("host.allow_plugin_updates"),
|
||||
pluginInstall: config.get("host.allow_plugin_install"),
|
||||
});
|
||||
setAllowed(config.get("host.allow_remote_updates"));
|
||||
}).catch(notifyErrorHandler("Failed to fetch host config for remote updates"));
|
||||
}
|
||||
}, [hostId]);
|
||||
|
||||
const coreUpdatePermission = `core.${props.target === "controller" ? "controller" : "host"}.update` as const;
|
||||
|
||||
function onValuesChange({ action } : { action?: string }) {
|
||||
if (action) {
|
||||
setFormAction(action);
|
||||
}
|
||||
}
|
||||
|
||||
let applyAction: () => Promise<void>;
|
||||
const setApplyAction = (func: () => Promise<void>) => { applyAction = func; };
|
||||
const canRestart = account.hasPermission(isController ? "core.controller.restart" : "core.host.restart");
|
||||
async function onOk() {
|
||||
if (!applyAction) {
|
||||
form.setFields([{ name: "action", errors: ["Action is required"] }]);
|
||||
return;
|
||||
if (props.target === "controller") {
|
||||
await control.send(new lib.ControllerUpdateRequest());
|
||||
} else {
|
||||
await control.sendTo(props.target, new lib.HostUpdateRequest());
|
||||
}
|
||||
await applyAction();
|
||||
|
||||
const values = form.getFieldsValue();
|
||||
if (values.restart) {
|
||||
// Ternary not used because TS disliked it
|
||||
if (restart) {
|
||||
if (props.target === "controller") {
|
||||
await control.sendTo(props.target, new lib.ControllerRestartRequest());
|
||||
await control.send(new lib.ControllerRestartRequest());
|
||||
} else {
|
||||
await control.sendTo(props.target, new lib.HostRestartRequest());
|
||||
}
|
||||
}
|
||||
|
||||
setFormAction(undefined);
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
return <>
|
||||
<Button
|
||||
type="default"
|
||||
disabled={props.disabled}
|
||||
onClick={() => { setOpen(true); }}
|
||||
>Updates</Button>
|
||||
<Tooltip title={allowed ? undefined : "Remote updates are disabled on this machine"}>
|
||||
<Button disabled={props.disabled || !allowed} onClick={() => { setOpen(true); }}>
|
||||
Update Clusterio
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Modal
|
||||
title="Updates"
|
||||
okText="Apply"
|
||||
title="Update Clusterio"
|
||||
okText="Update"
|
||||
open={open}
|
||||
okButtonProps={{disabled: formAction === undefined}}
|
||||
onOk={() => { onOk().catch(notifyErrorHandler(`Error running ${formAction}`)); }}
|
||||
onOk={() => { onOk().catch(notifyErrorHandler("Error updating Clusterio")); }}
|
||||
onCancel={() => { setOpen(false); }}
|
||||
destroyOnHidden
|
||||
>
|
||||
<Form form={form} onValuesChange={onValuesChange} clearOnDestroy>
|
||||
<Form.Item label="Action" name="action">
|
||||
<Radio.Group value={formAction}>
|
||||
{account.hasPermission(coreUpdatePermission)
|
||||
? <Radio.Button value="remote_update" disabled={!allows.remoteUpdates}>
|
||||
Update Clusterio
|
||||
</Radio.Button>: undefined}
|
||||
{account.hasPermission(coreUpdatePermission) && account.hasPermission("core.plugin.update")
|
||||
? <Radio.Button
|
||||
value="update_all"
|
||||
disabled={!allows.remoteUpdates || !allows.pluginUpdates}
|
||||
>
|
||||
Update All
|
||||
</Radio.Button> : undefined}
|
||||
{account.hasPermission("core.plugin.update")
|
||||
? <Radio.Button value="plugin_update" disabled={!allows.pluginUpdates}>
|
||||
Update Plugin
|
||||
</Radio.Button> : undefined}
|
||||
{account.hasPermission("core.plugin.install")
|
||||
? <Radio.Button value="plugin_install" disabled={!allows.pluginInstall}>
|
||||
Install Plugin
|
||||
</Radio.Button> : undefined}
|
||||
</Radio.Group>
|
||||
</Form.Item>
|
||||
{
|
||||
formAction !== undefined
|
||||
&& account.hasPermission(`core.${props.target === "controller" ? "controller" : "host"}.restart`)
|
||||
&& <Form.Item label="Restart on completion" name="restart" valuePropName="checked">
|
||||
<Checkbox disabled={props.canRestart === false}/>
|
||||
</Form.Item>
|
||||
}
|
||||
{formAction === "remote_update"
|
||||
? <NpmRemoteUpdate {...{setApplyAction, form, target: props.target}}/> : undefined}
|
||||
{formAction === "update_all"
|
||||
? <NpmUpdateAll {...{setApplyAction, form, target: props.target}}/> : undefined}
|
||||
{formAction === "plugin_update"
|
||||
? <NpmPluginUpdate {...{setApplyAction, form, target: props.target}}/> : undefined}
|
||||
{formAction === "plugin_install"
|
||||
? <NpmPluginInstall {...{setApplyAction, form, target: props.target}}/> : undefined}
|
||||
</Form>
|
||||
<p>Updates Clusterio within the version range in the machine's package.json.</p>
|
||||
{canRestart && <Checkbox
|
||||
checked={restart}
|
||||
disabled={props.canRestart === false}
|
||||
onChange={event => { setRestart(event.target.checked); }}
|
||||
>Restart on completion</Checkbox>}
|
||||
</Modal>
|
||||
</>;
|
||||
}
|
||||
|
||||
export function hasNpmButtonPermission(controller: boolean) {
|
||||
const account = useAccount();
|
||||
return account.hasAnyPermission(
|
||||
controller ? "core.controller.update" : "core.host.update",
|
||||
"core.plugin.update", "core.plugin.install"
|
||||
);
|
||||
return account.hasPermission(controller ? "core.controller.update" : "core.host.update");
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { Button, Descriptions, Input, Modal, Select, Space, Table, Typography } from "antd";
|
||||
import SearchOutlined from "@ant-design/icons/SearchOutlined";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useHosts } from "../model/host";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import ControlContext from "./ControlContext";
|
||||
import { InstallTarget, PluginInstaller, RemoteInstallDisabledNotice, addressKey } from "./PluginInstall";
|
||||
import { PluginInstalledTag, PluginUpdateButton, PluginVersions } from "./PluginClusterStatus";
|
||||
import type { PluginStatus } from "../util/plugin_status";
|
||||
|
||||
const { Text, Link } = Typography;
|
||||
|
||||
/** Statuses are undefined until the plugin lists of the machines have loaded, or without core.plugin.list */
|
||||
type BrowserProps = { statuses?: Map<string, PluginStatus>, installer: PluginInstaller };
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
// Installs on the chosen machines, defaulting to every connected machine that allows installs.
|
||||
function InstallControls(props: { packageSpec: string, installer: PluginInstaller }) {
|
||||
const account = useAccount();
|
||||
const [hosts] = useHosts();
|
||||
// Tracks what was deselected so machines connecting later, or allowing installs, start selected
|
||||
const [deselected, setDeselected] = useState(new Set<string>());
|
||||
const { access, installing, restarting } = props.installer;
|
||||
|
||||
const machines: (InstallTarget & { key: string, allowed: boolean })[] = [
|
||||
{ name: "Controller", address: "controller" as lib.AddressShorthand },
|
||||
...[...hosts.values()]
|
||||
.filter(host => host.connected)
|
||||
.sort((a, b) => strcmp(a.name, b.name))
|
||||
.map(host => ({ name: host.name, address: { hostId: host.id } as lib.AddressShorthand })),
|
||||
].map(target => ({ ...target, key: addressKey(target.address), allowed: access.canInstall(target.address) }));
|
||||
const selected = machines.filter(machine => machine.allowed && !deselected.has(machine.key));
|
||||
const blocked = machines.filter(machine => !machine.allowed);
|
||||
|
||||
return <Space direction="vertical" style={{ width: "100%" }}>
|
||||
<Space wrap>
|
||||
<Text>Install on</Text>
|
||||
<Select
|
||||
mode="multiple"
|
||||
style={{ minWidth: 260 }}
|
||||
placeholder="Select machines"
|
||||
optionFilterProp="label"
|
||||
value={selected.map(machine => machine.key)}
|
||||
onChange={keys => {
|
||||
setDeselected(new Set(machines.filter(machine => !keys.includes(machine.key)).map(m => m.key)));
|
||||
}}
|
||||
options={machines.map(machine => ({
|
||||
value: machine.key,
|
||||
label: machine.allowed ? machine.name : `${machine.name} (remote installs disabled)`,
|
||||
disabled: !machine.allowed,
|
||||
}))}
|
||||
/>
|
||||
<Button
|
||||
type="primary"
|
||||
loading={installing}
|
||||
disabled={!account.hasPermission("core.plugin.install") || !selected.length || restarting}
|
||||
onClick={() => { props.installer.install(props.packageSpec, selected); }}
|
||||
>Install</Button>
|
||||
</Space>
|
||||
{blocked.length > 0 && <RemoteInstallDisabledNotice
|
||||
controller={blocked.some(machine => machine.address === "controller")}
|
||||
hosts={blocked.filter(machine => machine.address !== "controller").map(machine => machine.name)}
|
||||
/>}
|
||||
</Space>;
|
||||
}
|
||||
|
||||
function PluginDetails(props: { plugin: lib.PluginSearchResult } & BrowserProps) {
|
||||
const account = useAccount();
|
||||
const { plugin } = props;
|
||||
const status = props.statuses?.get(plugin.name);
|
||||
// Without list permission the statuses never load, so offer installs without them
|
||||
const statusesKnown = props.statuses !== undefined || !account.hasPermission("core.plugin.list");
|
||||
// Links come from package metadata on npm, only http(s) links are shown so they can't run scripts
|
||||
const links = [
|
||||
{ label: "npm", url: lib.safeExternalUrl(plugin.npm) },
|
||||
{ label: "Homepage", url: lib.safeExternalUrl(plugin.homepage) },
|
||||
{ label: "Repository", url: lib.safeExternalUrl(plugin.repository) },
|
||||
].filter(link => link.url !== undefined);
|
||||
return <>
|
||||
<Descriptions size="small" column={1} bordered style={{ marginBottom: 16 }}>
|
||||
<Descriptions.Item label="Package">{plugin.name}</Descriptions.Item>
|
||||
<Descriptions.Item label="Version">{plugin.version}</Descriptions.Item>
|
||||
{status && <Descriptions.Item label="Installed">
|
||||
<PluginVersions status={status} latest={plugin.version} />
|
||||
<div style={{ marginTop: 4 }}>
|
||||
<PluginUpdateButton status={status} latest={plugin.version} installer={props.installer} />
|
||||
</div>
|
||||
</Descriptions.Item>}
|
||||
{plugin.description && <Descriptions.Item label="Description">{plugin.description}</Descriptions.Item>}
|
||||
{plugin.publisher && <Descriptions.Item label="Publisher">{plugin.publisher}</Descriptions.Item>}
|
||||
{plugin.date && <Descriptions.Item label="Published">
|
||||
{new Date(plugin.date).toLocaleString()}
|
||||
</Descriptions.Item>}
|
||||
{links.length > 0 && <Descriptions.Item label="Links">
|
||||
<Space wrap>
|
||||
{links.map(link => <Link key={link.label} href={link.url} target="_blank" rel="noopener noreferrer">
|
||||
{link.label}
|
||||
</Link>)}
|
||||
</Space>
|
||||
</Descriptions.Item>}
|
||||
</Descriptions>
|
||||
{/* Installed plugins are brought in line by the update action above instead */}
|
||||
{statusesKnown && !status && <InstallControls packageSpec={plugin.name} installer={props.installer} />}
|
||||
</>;
|
||||
}
|
||||
|
||||
export function PluginBrowserButton(props: BrowserProps) {
|
||||
const control = useContext(ControlContext);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [query, setQuery] = useState("");
|
||||
const [page, setPage] = useState(1);
|
||||
const [pageSize, setPageSize] = useState(10);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [results, setResults] = useState<lib.PluginSearchResult[]>([]);
|
||||
const [selected, setSelected] = useState<lib.PluginSearchResult | undefined>();
|
||||
// Package entered by name, for plugins the search doesn't find
|
||||
const [manual, setManual] = useState<string>();
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
return;
|
||||
}
|
||||
setLoading(true);
|
||||
let canceled = false;
|
||||
control.send(new lib.PluginSearchRequest(query, page, pageSize)).then(response => {
|
||||
if (canceled) { return; }
|
||||
setTotal(response.total);
|
||||
setResults(response.results);
|
||||
setSelected(current => response.results.find(r => r.name === current?.name));
|
||||
}).catch(notifyErrorHandler("Error searching for plugins")).finally(() => {
|
||||
if (!canceled) { setLoading(false); }
|
||||
});
|
||||
// eslint-disable-next-line consistent-return
|
||||
return () => { canceled = true; };
|
||||
}, [open, query, page, pageSize]);
|
||||
|
||||
return <>
|
||||
<Button icon={<SearchOutlined />} onClick={() => { setOpen(true); }}>Browse plugins</Button>
|
||||
<Modal
|
||||
title="Browse plugins"
|
||||
open={open}
|
||||
onCancel={() => { setOpen(false); }}
|
||||
width={1000}
|
||||
footer={<Button onClick={() => { setOpen(false); }}>Close</Button>}
|
||||
>
|
||||
<Input.Search
|
||||
placeholder="Search npm for plugins"
|
||||
allowClear
|
||||
enterButton
|
||||
style={{ marginBottom: 16 }}
|
||||
onSearch={value => { setQuery(value); setPage(1); }}
|
||||
/>
|
||||
<Input.Search
|
||||
placeholder="Or install a package by name, as name or name@version"
|
||||
allowClear
|
||||
enterButton="Select"
|
||||
style={{ marginBottom: 16 }}
|
||||
onSearch={value => { setManual(value.trim() || undefined); setSelected(undefined); }}
|
||||
/>
|
||||
<Table
|
||||
size="small"
|
||||
loading={loading}
|
||||
dataSource={results}
|
||||
rowKey="name"
|
||||
rowClassName={plugin => (plugin.name === selected?.name ? "ant-table-row-selected" : "")}
|
||||
onRow={plugin => ({
|
||||
onClick: () => { setSelected(plugin); setManual(undefined); },
|
||||
style: { cursor: "pointer" },
|
||||
})}
|
||||
pagination={{
|
||||
current: page,
|
||||
pageSize,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
onChange: (newPage, newPageSize) => { setPage(newPage); setPageSize(newPageSize); },
|
||||
}}
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
render: (_, plugin) => {
|
||||
const status = props.statuses?.get(plugin.name);
|
||||
return <>
|
||||
{plugin.name}
|
||||
{status && <span style={{ marginLeft: 8 }}>
|
||||
<PluginInstalledTag status={status} latest={plugin.version} />
|
||||
</span>}
|
||||
</>;
|
||||
},
|
||||
},
|
||||
{ title: "Version", dataIndex: "version", key: "version", width: 120 },
|
||||
{ title: "Publisher", dataIndex: "publisher", key: "publisher", width: 160, responsive: ["md"] },
|
||||
{
|
||||
title: "Description",
|
||||
dataIndex: "description",
|
||||
key: "description",
|
||||
ellipsis: true,
|
||||
responsive: ["lg"],
|
||||
},
|
||||
]}
|
||||
/>
|
||||
{selected && <PluginDetails plugin={selected} statuses={props.statuses} installer={props.installer} />}
|
||||
{manual && <>
|
||||
<Descriptions size="small" column={1} bordered style={{ marginBottom: 16 }}>
|
||||
<Descriptions.Item label="Package">{manual}</Descriptions.Item>
|
||||
</Descriptions>
|
||||
<InstallControls packageSpec={manual} installer={props.installer} />
|
||||
</>}
|
||||
{!selected && !manual && <Text type="secondary">Select a plugin to see details and install it.</Text>}
|
||||
</Modal>
|
||||
</>;
|
||||
}
|
||||
@@ -0,0 +1,593 @@
|
||||
import { ReactNode, useContext, useEffect, useState } from "react";
|
||||
import { Button, Checkbox, Modal, Popconfirm, Space, Table, Tag, Tooltip, Typography } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useHosts } from "../model/host";
|
||||
import { useInstances } from "../model/instance";
|
||||
import { useSystems } from "../model/system";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import { poll } from "../util/poll";
|
||||
import ControlContext from "./ControlContext";
|
||||
import Link from "./Link";
|
||||
import { PackageOperation, PluginInstaller, addressKey } from "./PluginInstall";
|
||||
import {
|
||||
Machine, MachinePlugin, MachinePlugins, PluginStatus, SkippedOperation, UpdatePlan,
|
||||
compareVersions, machineStatusLabels, planMachineUpdate, planUpdate,
|
||||
} from "../util/plugin_status";
|
||||
|
||||
const { Text } = Typography;
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
const latestVersionsRefreshMs = 60e3;
|
||||
|
||||
function plural(count: number, word: string) {
|
||||
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
||||
}
|
||||
|
||||
function machinePath(machine: Machine) {
|
||||
return machine.isController ? "/controller" : `/hosts/${(machine.address as { hostId: number }).hostId}/view`;
|
||||
}
|
||||
|
||||
type ListedPlugins = { plugins: lib.PluginDetails[], systemUpdatedAtMs?: number };
|
||||
type MachinePluginsState = {
|
||||
machines?: MachinePlugins[],
|
||||
unknown: Machine[],
|
||||
/** Fetch the plugin lists of connected machines now, instead of waiting for the next system metrics */
|
||||
refresh: (addresses?: lib.AddressShorthand[]) => Promise<void>,
|
||||
};
|
||||
|
||||
/**
|
||||
* Plugins of the controller and every host, as last collected with the system metrics. Offline
|
||||
* hosts keep the plugins they had when last seen.
|
||||
*/
|
||||
export function useMachinePlugins(): MachinePluginsState {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [hosts, hostsSynced] = useHosts();
|
||||
const [systems, systemsSynced] = useSystems();
|
||||
// Lists fetched directly, used until the system metrics of the machine update
|
||||
const [listed, setListed] = useState(new Map<string, ListedPlugins>());
|
||||
const canList = account.hasPermission("core.plugin.list");
|
||||
|
||||
const allMachines: Machine[] = [
|
||||
{ name: "Controller", isController: true, address: "controller" },
|
||||
...[...hosts.values()]
|
||||
.filter(host => !host.isDeleted)
|
||||
.sort((a, b) => strcmp(a.name, b.name))
|
||||
.map(host => ({
|
||||
name: host.name, isController: false, address: { hostId: host.id }, offline: !host.connected,
|
||||
})),
|
||||
];
|
||||
const systemOf = (machine: Machine) => systems.get(
|
||||
machine.isController ? "controller" : (machine.address as { hostId: number }).hostId
|
||||
);
|
||||
|
||||
async function refresh(addresses?: lib.AddressShorthand[]) {
|
||||
const keys = addresses && new Set(addresses.map(addressKey));
|
||||
const targets = allMachines.filter(
|
||||
machine => !machine.offline && (!keys || keys.has(addressKey(machine.address)))
|
||||
);
|
||||
const results = await Promise.all(targets.map(async machine => {
|
||||
try {
|
||||
const plugins = await control.sendTo(machine.address, new lib.PluginListRequest());
|
||||
const systemUpdatedAtMs = systemOf(machine)?.updatedAtMs;
|
||||
return [addressKey(machine.address), { plugins, systemUpdatedAtMs }] as const;
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
}));
|
||||
setListed(current => new Map([...current, ...results.filter(result => result !== undefined)]));
|
||||
}
|
||||
|
||||
// Covers machines that don't report plugins in their metrics, and metrics being turned off
|
||||
const connectedKey = allMachines.filter(machine => !machine.offline).map(m => addressKey(m.address)).join(",");
|
||||
useEffect(() => {
|
||||
if (canList && hostsSynced) {
|
||||
refresh();
|
||||
}
|
||||
}, [canList, hostsSynced, connectedKey]);
|
||||
|
||||
if (!canList || !hostsSynced || (!systemsSynced && !listed.size)) {
|
||||
return { machines: undefined, unknown: [], refresh };
|
||||
}
|
||||
const machines: MachinePlugins[] = [];
|
||||
const unknown: Machine[] = [];
|
||||
for (const machine of allMachines) {
|
||||
const system = systemOf(machine);
|
||||
const fresh = listed.get(addressKey(machine.address));
|
||||
const plugins = fresh && (!system?.plugins || fresh.systemUpdatedAtMs === system.updatedAtMs)
|
||||
? fresh.plugins
|
||||
: system?.plugins;
|
||||
if (plugins) {
|
||||
machines.push({ ...machine, plugins });
|
||||
} else {
|
||||
unknown.push(machine);
|
||||
}
|
||||
}
|
||||
return { machines, unknown, refresh };
|
||||
}
|
||||
|
||||
/** Latest version published on npm of each package */
|
||||
export function useLatestVersions(packages: string[]) {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [versions, setVersions] = useState<Record<string, string>>({});
|
||||
const packagesKey = [...new Set(packages)].sort().join(",");
|
||||
const canList = account.hasPermission("core.plugin.list");
|
||||
|
||||
useEffect(() => {
|
||||
if (!packagesKey || !canList) {
|
||||
return undefined;
|
||||
}
|
||||
let canceled = false;
|
||||
let notified = false;
|
||||
// Packages the registry couldn't be reached for are left out, so look them up again later.
|
||||
// The controller caches the ones it found.
|
||||
const stop = poll(async () => {
|
||||
try {
|
||||
const response = await control.send(new lib.PluginLatestVersionsRequest(packagesKey.split(",")));
|
||||
if (!canceled) {
|
||||
// Merged so a lookup failing on a later refresh doesn't hide what was found before
|
||||
setVersions(current => ({ ...current, ...response.versions }));
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (!canceled && !notified) {
|
||||
notified = true;
|
||||
notifyErrorHandler("Failed to look up latest plugin versions")(err);
|
||||
}
|
||||
}
|
||||
}, latestVersionsRefreshMs);
|
||||
return () => { canceled = true; stop(); };
|
||||
}, [packagesKey, canList]);
|
||||
|
||||
return versions;
|
||||
}
|
||||
|
||||
function isPending(machine: MachinePlugin) {
|
||||
return machine.running !== machine.afterRestart;
|
||||
}
|
||||
|
||||
function CountLine(props: { label: string, count: number, type?: "warning" | "secondary" }) {
|
||||
return <div><Text type={props.type}>{props.label} {props.count}</Text></div>;
|
||||
}
|
||||
|
||||
/** One line per version running now or after a restart, plus restart pending, disabled and missing counts */
|
||||
export function PluginVersions(props: { status: PluginStatus, latest?: string }) {
|
||||
const { machines, unknown } = props.status;
|
||||
const versions = [...new Set(machines.flatMap(machine => [machine.running, machine.afterRestart]))]
|
||||
.filter((version): version is string => version !== undefined)
|
||||
.sort(compareVersions)
|
||||
.reverse();
|
||||
const count = (filter: (machine: MachinePlugin) => boolean) => machines.filter(filter).length;
|
||||
const missing = count(machine => (!machine.plugin || Boolean(machine.plugin.removed)) && !machine.nameTaken);
|
||||
|
||||
return <>
|
||||
{versions.map(version => {
|
||||
const now = count(machine => machine.running === version);
|
||||
const after = count(machine => machine.afterRestart === version);
|
||||
return <div key={version}>
|
||||
<Text>{version}</Text>
|
||||
{props.latest === version && <Tag color="success" style={{ marginLeft: 6 }}>latest</Tag>}
|
||||
<Text type="secondary" style={{ marginLeft: 12 }}>
|
||||
{plural(now, "machine")}{now !== after && ` → ${after} after restart`}
|
||||
</Text>
|
||||
</div>;
|
||||
})}
|
||||
{count(isPending) > 0 && <CountLine label="Restart pending on" count={count(isPending)} />}
|
||||
{count(machine => machine.disabled) > 0 && <CountLine
|
||||
label="Disabled on" count={count(machine => machine.disabled)} type="secondary"
|
||||
/>}
|
||||
{count(machine => machine.noEntrypoint) > 0 && <CountLine
|
||||
label="No entrypoint on" count={count(machine => machine.noEntrypoint)} type="secondary"
|
||||
/>}
|
||||
{count(machine => machine.nameTaken) > 0 && <CountLine
|
||||
label="From another package on" count={count(machine => machine.nameTaken)} type="secondary"
|
||||
/>}
|
||||
{missing > 0 && <CountLine label="Not installed on" count={missing} type="warning" />}
|
||||
{unknown.length > 0 && <CountLine label="Plugins not reported by" count={unknown.length} type="warning" />}
|
||||
</>;
|
||||
}
|
||||
|
||||
type ChecklistItem = { key: string, label: ReactNode, disabledReason?: string };
|
||||
type ChecklistModalProps = {
|
||||
open: boolean,
|
||||
title: string,
|
||||
okText: string,
|
||||
danger?: boolean,
|
||||
items: ChecklistItem[],
|
||||
extra?: ReactNode,
|
||||
onCancel: () => void,
|
||||
onOk: (keys: string[]) => void,
|
||||
};
|
||||
|
||||
/** Confirms a list of actions, all ticked by default. Actions that can't be done are disabled with the reason. */
|
||||
function ChecklistModal(props: ChecklistModalProps) {
|
||||
const [unchecked, setUnchecked] = useState(new Set<string>());
|
||||
useEffect(() => {
|
||||
if (props.open) {
|
||||
setUnchecked(new Set());
|
||||
}
|
||||
}, [props.open]);
|
||||
const possible = props.items.filter(item => !item.disabledReason);
|
||||
const selected = possible.filter(item => !unchecked.has(item.key));
|
||||
const toggle = (key: string, checked: boolean) => setUnchecked(current => {
|
||||
const next = new Set(current);
|
||||
if (checked) {
|
||||
next.delete(key);
|
||||
} else {
|
||||
next.add(key);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
|
||||
return <Modal
|
||||
title={props.title}
|
||||
open={props.open}
|
||||
okText={props.okText}
|
||||
okButtonProps={{ danger: props.danger, disabled: !selected.length }}
|
||||
onOk={() => props.onOk(selected.map(item => item.key))}
|
||||
onCancel={props.onCancel}
|
||||
destroyOnHidden
|
||||
>
|
||||
{props.extra}
|
||||
{possible.length > 1 && <Checkbox
|
||||
style={{ margin: "8px 0" }}
|
||||
checked={selected.length === possible.length}
|
||||
indeterminate={selected.length > 0 && selected.length < possible.length}
|
||||
onChange={event => setUnchecked(new Set(event.target.checked ? [] : possible.map(item => item.key)))}
|
||||
>All ({selected.length}/{possible.length})</Checkbox>}
|
||||
<div style={{ maxHeight: 320, overflowY: "auto" }}>
|
||||
{props.items.map(item => <div key={item.key}>
|
||||
<Checkbox
|
||||
checked={!item.disabledReason && !unchecked.has(item.key)}
|
||||
disabled={Boolean(item.disabledReason)}
|
||||
onChange={event => toggle(item.key, event.target.checked)}
|
||||
>
|
||||
{item.label}
|
||||
{item.disabledReason && <Text type="secondary"> ({item.disabledReason})</Text>}
|
||||
</Checkbox>
|
||||
</div>)}
|
||||
</div>
|
||||
</Modal>;
|
||||
}
|
||||
|
||||
type Account = ReturnType<typeof useAccount>;
|
||||
|
||||
function canChangePlugins(account: Account) {
|
||||
return account.hasPermission("core.plugin.install") || account.hasPermission("core.plugin.update");
|
||||
}
|
||||
|
||||
function operationKey(change: PackageOperation | SkippedOperation, title: string) {
|
||||
return `${title}:${change.kind}:${addressKey(change.target.address)}`;
|
||||
}
|
||||
|
||||
type PlannedUpdate = { title: string, latest: string, plan: UpdatePlan };
|
||||
|
||||
/** One checklist item per operation and skipped operation */
|
||||
function planItems(updates: PlannedUpdate[], withTitle: boolean) {
|
||||
const operations = new Map<string, PackageOperation>();
|
||||
const items: ChecklistItem[] = [];
|
||||
for (const { title, latest, plan } of updates) {
|
||||
const label = (change: PackageOperation | SkippedOperation) => <>
|
||||
{withTitle && <Text strong>{title}: </Text>}
|
||||
{change.kind === "install" ? "Install" : "Update to"} {latest} on {change.target.name}
|
||||
</>;
|
||||
for (const operation of plan.operations) {
|
||||
const key = operationKey(operation, title);
|
||||
operations.set(key, operation);
|
||||
items.push({ key, label: label(operation) });
|
||||
}
|
||||
for (const skipped of plan.skipped) {
|
||||
items.push({ key: operationKey(skipped, title), label: label(skipped), disabledReason: skipped.reason });
|
||||
}
|
||||
}
|
||||
return { items, operations };
|
||||
}
|
||||
|
||||
type UpdateButtonProps = { status: PluginStatus, title?: string, latest?: string, installer: PluginInstaller };
|
||||
|
||||
/** Updates a plugin to the latest version on every machine */
|
||||
export function PluginUpdateButton(props: UpdateButtonProps) {
|
||||
const account = useAccount();
|
||||
const [open, setOpen] = useState(false);
|
||||
const { status, latest, installer } = props;
|
||||
if (!latest || !canChangePlugins(account)) {
|
||||
return null;
|
||||
}
|
||||
const plan = planUpdate(status, latest, installer.access, account);
|
||||
const changes = [...plan.operations, ...plan.skipped];
|
||||
if (!changes.length) {
|
||||
return <Tooltip title="Up to date on every machine">
|
||||
<Button size="small" disabled>Update to {latest}</Button>
|
||||
</Tooltip>;
|
||||
}
|
||||
const onlyInstalls = changes.every(change => change.kind === "install");
|
||||
const label = onlyInstalls ? `Install on missing (${changes.length})` : `Update to ${latest}`;
|
||||
const title = props.title ?? status.title;
|
||||
const { items, operations } = planItems([{ title, latest, plan }], false);
|
||||
|
||||
return <>
|
||||
<Button
|
||||
size="small"
|
||||
loading={installer.installing}
|
||||
disabled={installer.restarting}
|
||||
onClick={() => setOpen(true)}
|
||||
>{label}</Button>
|
||||
<ChecklistModal
|
||||
open={open}
|
||||
title={`${onlyInstalls ? "Install" : "Update"} ${title} ${latest}`}
|
||||
okText={onlyInstalls ? "Install" : "Update"}
|
||||
items={items}
|
||||
extra={<p>Machines need a restart afterwards.</p>}
|
||||
onCancel={() => setOpen(false)}
|
||||
onOk={keys => {
|
||||
setOpen(false);
|
||||
installer.run(keys.map(key => operations.get(key)!));
|
||||
}}
|
||||
/>
|
||||
</>;
|
||||
}
|
||||
|
||||
type UpdateAllProps = {
|
||||
statuses: PluginStatus[],
|
||||
/** Display names of plugins by status key, falling back to the title reported by the machines */
|
||||
titles?: Map<string, string>,
|
||||
latestVersions: Record<string, string>,
|
||||
installer: PluginInstaller,
|
||||
};
|
||||
|
||||
/** Updates every plugin with a newer version to the latest version on every machine */
|
||||
export function UpdateAllButton(props: UpdateAllProps) {
|
||||
const account = useAccount();
|
||||
const [open, setOpen] = useState(false);
|
||||
if (!canChangePlugins(account)) {
|
||||
return null;
|
||||
}
|
||||
const updates = props.statuses.flatMap(status => {
|
||||
const latest = status.npmPackage && props.latestVersions[status.npmPackage];
|
||||
if (!latest) {
|
||||
return [];
|
||||
}
|
||||
const plan = planUpdate(status, latest, props.installer.access, account);
|
||||
const title = props.titles?.get(status.key) ?? status.title;
|
||||
return plan.operations.length || plan.skipped.length ? [{ title, latest, plan }] : [];
|
||||
});
|
||||
const pluginCount = updates.filter(update => update.plan.operations.length).length;
|
||||
const { items, operations } = planItems(updates, true);
|
||||
|
||||
return <>
|
||||
<Tooltip title={pluginCount ? undefined : "No plugin updates can be done"}>
|
||||
<Button
|
||||
loading={props.installer.installing}
|
||||
disabled={!pluginCount || props.installer.restarting}
|
||||
onClick={() => setOpen(true)}
|
||||
>Update all ({pluginCount})</Button>
|
||||
</Tooltip>
|
||||
<ChecklistModal
|
||||
open={open}
|
||||
title={`Update ${plural(pluginCount, "plugin")} to the latest version`}
|
||||
okText="Update"
|
||||
items={items}
|
||||
extra={<p>Machines need a restart afterwards.</p>}
|
||||
onCancel={() => setOpen(false)}
|
||||
onOk={keys => {
|
||||
setOpen(false);
|
||||
props.installer.run(keys.map(key => operations.get(key)!));
|
||||
}}
|
||||
/>
|
||||
</>;
|
||||
}
|
||||
|
||||
type RestartPendingProps = {
|
||||
statuses: PluginStatus[],
|
||||
machines: MachinePlugins[],
|
||||
installer: PluginInstaller,
|
||||
};
|
||||
|
||||
/** Restarts the chosen machines with a pending restart, hosts first and the controller last */
|
||||
export function RestartPendingButton(props: RestartPendingProps) {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [systems] = useSystems();
|
||||
const [instances] = useInstances();
|
||||
const [open, setOpen] = useState(false);
|
||||
const { installer } = props;
|
||||
const permitted = (machine: Machine) => account.hasPermission(
|
||||
machine.isController ? "core.controller.restart" : "core.host.restart"
|
||||
);
|
||||
if (!account.hasPermission("core.controller.restart") && !account.hasPermission("core.host.restart")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pendingKeys = new Set(props.statuses.flatMap(
|
||||
status => status.machines.filter(isPending).map(machine => addressKey(machine.address))
|
||||
));
|
||||
const systemOf = (machine: Machine) => systems.get(
|
||||
machine.isController ? "controller" : (machine.address as { hostId: number }).hostId
|
||||
);
|
||||
const pending = props.machines.filter(
|
||||
machine => pendingKeys.has(addressKey(machine.address)) || systemOf(machine)?.restartRequired
|
||||
);
|
||||
const cannotRestart = (machine: Machine) => {
|
||||
if (machine.offline) {
|
||||
return "offline";
|
||||
}
|
||||
if (!permitted(machine)) {
|
||||
return "no restart permission";
|
||||
}
|
||||
if (!systemOf(machine)?.canRestart) {
|
||||
return "not running under a process monitor";
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
const runningInstances = (machine: Machine) => [...instances.values()].filter(instance => (
|
||||
!machine.isController
|
||||
&& instance.assignedHost === (machine.address as { hostId: number }).hostId
|
||||
&& !["unknown", "unassigned", "stopped", "deleted"].includes(instance.status)
|
||||
)).length;
|
||||
const items = pending.map(machine => {
|
||||
const count = runningInstances(machine);
|
||||
return {
|
||||
key: addressKey(machine.address),
|
||||
label: <>
|
||||
{machine.name}
|
||||
{count > 0 && <Text type="warning"> stops {plural(count, "running instance")}</Text>}
|
||||
</>,
|
||||
disabledReason: cannotRestart(machine),
|
||||
};
|
||||
});
|
||||
const blocked = items.filter(item => item.disabledReason).length;
|
||||
|
||||
async function restartMachines(restart: MachinePlugins[]) {
|
||||
const hosts = restart.filter(machine => !machine.isController);
|
||||
const results = await Promise.allSettled(hosts.map(
|
||||
machine => control.sendTo(machine.address, new lib.HostRestartRequest())
|
||||
));
|
||||
const started: Machine[] = [];
|
||||
results.forEach((result, i) => {
|
||||
if (result.status === "rejected") {
|
||||
notifyErrorHandler(`Failed to restart ${hosts[i].name}`)(result.reason);
|
||||
} else {
|
||||
started.push(hosts[i]);
|
||||
}
|
||||
});
|
||||
// The controller goes last as restarting it disconnects this page until it's back
|
||||
const controller = restart.find(machine => machine.isController);
|
||||
if (controller) {
|
||||
try {
|
||||
await control.send(new lib.ControllerRestartRequest());
|
||||
started.push(controller);
|
||||
} catch (err: any) {
|
||||
notifyErrorHandler("Failed to restart the controller")(err);
|
||||
}
|
||||
}
|
||||
if (started.length) {
|
||||
notify(`Restarting ${plural(started.length, "machine")}`, "info");
|
||||
}
|
||||
}
|
||||
|
||||
return <>
|
||||
<Tooltip title={pending.length
|
||||
? `${plural(pending.length, "machine")} need a restart`
|
||||
: "No machine needs a restart"
|
||||
}>
|
||||
<Button
|
||||
loading={installer.restarting}
|
||||
disabled={!pending.length || installer.installing}
|
||||
onClick={() => setOpen(true)}
|
||||
>Restart pending ({pending.length})</Button>
|
||||
</Tooltip>
|
||||
<ChecklistModal
|
||||
open={open}
|
||||
title="Restart machines with a pending restart"
|
||||
okText="Restart"
|
||||
danger
|
||||
items={items}
|
||||
extra={blocked > 0 && <p>
|
||||
{plural(blocked, "machine")} can't be restarted from here and must be restarted by hand.
|
||||
The <Link to="/hosts">hosts list</Link> will show which need to be restarted.
|
||||
</p>}
|
||||
onCancel={() => setOpen(false)}
|
||||
onOk={keys => {
|
||||
setOpen(false);
|
||||
const restart = pending.filter(machine => keys.includes(addressKey(machine.address)));
|
||||
installer.restart(() => restartMachines(restart));
|
||||
}}
|
||||
/>
|
||||
</>;
|
||||
}
|
||||
|
||||
type MachineRow = MachinePlugin | (Machine & { unknown: true });
|
||||
|
||||
type MachineActionProps = { status: PluginStatus, machine: MachinePlugin, latest: string, installer: PluginInstaller };
|
||||
|
||||
/** Install or update action for one machine, disabled with the reason when it can't be done */
|
||||
function MachineAction(props: MachineActionProps) {
|
||||
const account = useAccount();
|
||||
const { status, machine, latest, installer } = props;
|
||||
const plan = planMachineUpdate(status, machine, latest, installer.access, account);
|
||||
const [operation] = plan.operations;
|
||||
const [skipped] = plan.skipped;
|
||||
const change = operation ?? skipped;
|
||||
if (!change) {
|
||||
return null;
|
||||
}
|
||||
const label = `${change.kind === "install" ? "Install" : "Update to"} ${latest}`;
|
||||
if (!operation) {
|
||||
return <Tooltip title={skipped.reason}><Button size="small" disabled>{label}</Button></Tooltip>;
|
||||
}
|
||||
return <Popconfirm
|
||||
title={`${label} on ${machine.name}?`}
|
||||
description="The machine needs a restart afterwards."
|
||||
onConfirm={() => installer.run([operation])}
|
||||
>
|
||||
<Button size="small" loading={installer.installing} disabled={installer.restarting}>{label}</Button>
|
||||
</Popconfirm>;
|
||||
}
|
||||
|
||||
/** Version and status of a plugin on each machine */
|
||||
export function PluginMachinesTable(props: { status: PluginStatus, latest?: string, installer: PluginInstaller }) {
|
||||
const account = useAccount();
|
||||
const { status, latest, installer } = props;
|
||||
const rows: MachineRow[] = [
|
||||
...status.machines,
|
||||
...status.unknown.map(machine => ({ ...machine, unknown: true as const })),
|
||||
];
|
||||
return <Table
|
||||
size="small"
|
||||
dataSource={rows}
|
||||
rowKey={machine => addressKey(machine.address)}
|
||||
pagination={{ defaultPageSize: 20, hideOnSinglePage: true }}
|
||||
columns={[
|
||||
{
|
||||
title: "Machine",
|
||||
key: "name",
|
||||
render: (_, machine) => <Link to={machinePath(machine)}>{machine.name}</Link>,
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
title: "Running",
|
||||
key: "running",
|
||||
render: (_, machine) => ("unknown" in machine ? null : <>
|
||||
{machine.running ?? <Text type="secondary">None</Text>}
|
||||
{machine.running !== undefined && machine.running === latest
|
||||
&& <Tag color="success" style={{ marginLeft: 6 }}>latest</Tag>}
|
||||
</>),
|
||||
},
|
||||
{
|
||||
title: "After restart",
|
||||
key: "afterRestart",
|
||||
render: (_, machine) => ("unknown" in machine || !isPending(machine)
|
||||
? null
|
||||
: machine.afterRestart ?? <Text type="secondary">None</Text>),
|
||||
},
|
||||
{
|
||||
title: "Status",
|
||||
key: "status",
|
||||
render: (_, machine) => <Space size={[0, 4]} wrap>
|
||||
{("unknown" in machine ? ["Plugins not reported"] : machineStatusLabels(machine))
|
||||
.map(label => <Tag key={label}>{label}</Tag>)}
|
||||
</Space>,
|
||||
},
|
||||
{
|
||||
key: "action",
|
||||
align: "right",
|
||||
render: (_, machine) => (latest && !("unknown" in machine) && canChangePlugins(account)
|
||||
? <MachineAction status={status} machine={machine} latest={latest} installer={installer} />
|
||||
: null),
|
||||
},
|
||||
]}
|
||||
/>;
|
||||
}
|
||||
|
||||
/** Compact summary for search results: how many machines have the plugin installed */
|
||||
export function PluginInstalledTag(props: { status: PluginStatus, latest?: string }) {
|
||||
const { machines } = props.status;
|
||||
const installed = machines.filter(machine => machine.plugin && !machine.plugin.removed);
|
||||
const current = machines.filter(machine => machine.afterRestart === props.latest);
|
||||
const color = current.length === machines.length ? "success" : "warning";
|
||||
return <Tooltip title={<PluginVersions status={props.status} latest={props.latest} />}>
|
||||
<Tag color={color}>Installed on {installed.length}</Tag>
|
||||
</Tooltip>;
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
import { useContext, useEffect, useRef, useState } from "react";
|
||||
import { Alert, Typography } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useHosts } from "../model/host";
|
||||
import { ExclusiveRunner } from "../util/exclusive";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import ControlContext from "./ControlContext";
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
export type InstallTarget = { name: string, address: lib.AddressShorthand };
|
||||
export type PackageOperation = { kind: "install" | "update", target: InstallTarget, packageSpec: string };
|
||||
export type RemoteAccess = {
|
||||
canInstall: (address: lib.AddressShorthand) => boolean,
|
||||
canUpdate: (address: lib.AddressShorthand) => boolean,
|
||||
};
|
||||
export type PluginInstaller = {
|
||||
access: RemoteAccess,
|
||||
installing: boolean,
|
||||
restarting: boolean,
|
||||
/** Install a package, optionally pinned as name@version, on every target */
|
||||
install: (packageSpec: string, targets: InstallTarget[]) => Promise<void>,
|
||||
/** Run package operations one at a time, a standalone controller and host share one installation */
|
||||
run: (operations: PackageOperation[]) => Promise<void>,
|
||||
/** Run a restart, never at the same time as package operations */
|
||||
restart: (restartMachines: () => Promise<void>) => Promise<void>,
|
||||
};
|
||||
|
||||
type MachineAccess = { install: boolean, update: boolean };
|
||||
const defaultAccess: MachineAccess = { install: true, update: true };
|
||||
|
||||
export function addressKey(address: lib.AddressShorthand) {
|
||||
return address === "controller" ? "controller" : String((address as { hostId: number }).hostId);
|
||||
}
|
||||
|
||||
// Which machines accept remote plugin installs and updates, assumed true when the config can't be read.
|
||||
function useRemoteAccess(): RemoteAccess {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
const [hosts] = useHosts();
|
||||
const [controllerAccess, setControllerAccess] = useState(defaultAccess);
|
||||
const [hostsAccess, setHostsAccess] = useState(new Map<number, MachineAccess>());
|
||||
const connectedHostIds = [...hosts.values()].filter(host => host.connected).map(host => host.id);
|
||||
const canGetControllerConfig = account.hasPermission("core.controller.get_config");
|
||||
const canGetHostConfig = account.hasPermission("core.host.get_config");
|
||||
|
||||
useEffect(() => {
|
||||
if (!canGetControllerConfig) {
|
||||
setControllerAccess(defaultAccess);
|
||||
return undefined;
|
||||
}
|
||||
let canceled = false;
|
||||
control.send(new lib.ControllerConfigGetRequest()).then(serializedConfig => {
|
||||
if (canceled) {
|
||||
return;
|
||||
}
|
||||
const config = lib.ControllerConfig.fromJSON(serializedConfig, "control");
|
||||
setControllerAccess({
|
||||
install: config.get("controller.allow_plugin_install"),
|
||||
update: config.get("controller.allow_plugin_updates"),
|
||||
});
|
||||
}).catch(notifyErrorHandler("Failed to fetch controller config"));
|
||||
return () => { canceled = true; };
|
||||
}, [canGetControllerConfig]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!canGetHostConfig) {
|
||||
setHostsAccess(new Map());
|
||||
return undefined;
|
||||
}
|
||||
let canceled = false;
|
||||
Promise.all(connectedHostIds.map(async hostId => {
|
||||
try {
|
||||
const serializedConfig = await control.sendTo({ hostId }, new lib.HostConfigGetRequest());
|
||||
const config = lib.HostConfig.fromJSON(serializedConfig, "control");
|
||||
return [hostId, {
|
||||
install: config.get("host.allow_plugin_install"),
|
||||
update: config.get("host.allow_plugin_updates"),
|
||||
}] as const;
|
||||
} catch (err) {
|
||||
return [hostId, defaultAccess] as const;
|
||||
}
|
||||
})).then(entries => {
|
||||
if (!canceled) {
|
||||
setHostsAccess(new Map(entries));
|
||||
}
|
||||
});
|
||||
return () => { canceled = true; };
|
||||
}, [connectedHostIds.join(","), canGetHostConfig]);
|
||||
|
||||
const accessOf = (address: lib.AddressShorthand) => (address === "controller"
|
||||
? controllerAccess
|
||||
: hostsAccess.get((address as { hostId: number }).hostId) ?? defaultAccess
|
||||
);
|
||||
return {
|
||||
canInstall: address => accessOf(address).install,
|
||||
canUpdate: address => accessOf(address).update,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs and updates plugins and reports the outcome. Owned by the plugins page so the busy
|
||||
* state is shared between the browser and update actions and survives browsing.
|
||||
*/
|
||||
export function usePluginInstaller(onDone?: (addresses: lib.AddressShorthand[]) => void): PluginInstaller {
|
||||
const control = useContext(ControlContext);
|
||||
const access = useRemoteAccess();
|
||||
const [busy, setBusy] = useState<"installing" | "restarting">();
|
||||
// A confirm opened before a run started can still be clicked while it runs
|
||||
const runner = useRef(new ExclusiveRunner(setBusy)).current;
|
||||
|
||||
async function exclusive(state: "installing" | "restarting", action: () => Promise<void>) {
|
||||
if (!await runner.run(state, action)) {
|
||||
notify("Another plugin change or restart is still in progress", "warning");
|
||||
}
|
||||
}
|
||||
|
||||
async function runOperations(operations: PackageOperation[]) {
|
||||
const done: string[] = [];
|
||||
try {
|
||||
for (const { kind, target, packageSpec } of operations) {
|
||||
const request = kind === "install"
|
||||
? new lib.PluginInstallRequest(packageSpec)
|
||||
: new lib.PluginUpdateRequest(packageSpec);
|
||||
try {
|
||||
await control.sendTo(target.address, request);
|
||||
done.push(`${kind === "install" ? "Installed" : "Updated"} ${packageSpec} on ${target.name}`);
|
||||
} catch (err: any) {
|
||||
notifyErrorHandler(`Failed to ${kind} ${packageSpec} on ${target.name}`)(err);
|
||||
}
|
||||
}
|
||||
if (done.length) {
|
||||
notify(
|
||||
done.length === 1 ? done[0] : `Completed ${done.length} plugin changes`, "success",
|
||||
<>
|
||||
{done.length > 1 && done.map(line => <div key={line}>{line}</div>)}
|
||||
<div>A restart is required to apply the changes.</div>
|
||||
</>,
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
onDone?.(operations.map(operation => operation.target.address));
|
||||
}
|
||||
}
|
||||
|
||||
async function run(operations: PackageOperation[]) {
|
||||
await exclusive("installing", () => runOperations(operations));
|
||||
}
|
||||
|
||||
async function restart(restartMachines: () => Promise<void>) {
|
||||
await exclusive("restarting", restartMachines);
|
||||
}
|
||||
|
||||
async function install(packageSpec: string, targets: InstallTarget[]) {
|
||||
await run(targets.map(target => ({ kind: "install", target, packageSpec })));
|
||||
}
|
||||
|
||||
return {
|
||||
access, installing: busy === "installing", restarting: busy === "restarting", install, run, restart,
|
||||
};
|
||||
}
|
||||
|
||||
// Remote installs can only be enabled locally on each machine, so tell the user how.
|
||||
export function RemoteInstallDisabledNotice(props: { controller: boolean, hosts: string[] }) {
|
||||
const names = [...(props.controller ? ["the controller"] : []), ...props.hosts];
|
||||
return <Alert
|
||||
type="info"
|
||||
showIcon
|
||||
message={`Remote plugin installs are disabled on ${names.join(", ")}`}
|
||||
description={<>
|
||||
For security reasons this can't be changed from the web UI. To allow installs, stop the machine
|
||||
and run the following in its installation directory, then start it again:
|
||||
{props.controller && <div>
|
||||
<Text type="secondary">Controller: </Text>
|
||||
<Text code copyable>npx clusteriocontroller config set controller.allow_plugin_install true</Text>
|
||||
</div>}
|
||||
{props.hosts.length > 0 && <div>
|
||||
<Text type="secondary">Host: </Text>
|
||||
<Text code copyable>npx clusteriohost config set host.allow_plugin_install true</Text>
|
||||
</div>}
|
||||
</>}
|
||||
/>;
|
||||
}
|
||||
@@ -2,62 +2,68 @@ import React, { useEffect, useContext, useState } from "react";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { Alert, Descriptions, Spin } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type { PluginWebApi } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PageHeader from "./PageHeader";
|
||||
import SectionHeader from "./SectionHeader";
|
||||
import notify from "../util/notify";
|
||||
import { usePluginInstaller } from "./PluginInstall";
|
||||
import { PluginMachinesTable, PluginUpdateButton, useLatestVersions, useMachinePlugins } from "./PluginClusterStatus";
|
||||
import { PluginStatus, computePluginStatuses, pluginStatusKey } from "../util/plugin_status";
|
||||
|
||||
export default function PluginViewPage() {
|
||||
let params = useParams();
|
||||
const control = useContext(ControlContext);
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>();
|
||||
let pluginName = String(params.name);
|
||||
type PluginMachinesProps = {
|
||||
status: PluginStatus,
|
||||
title: string,
|
||||
refresh: (addresses: lib.AddressShorthand[]) => void,
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
let response = await fetch(`${webRoot}api/plugins`);
|
||||
if (response.ok) {
|
||||
setPluginList(await response.json());
|
||||
} else {
|
||||
notify("Failed to load plugin list");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
function PluginMachines(props: PluginMachinesProps) {
|
||||
const installer = usePluginInstaller(props.refresh);
|
||||
const { npmPackage } = props.status;
|
||||
const latestVersions = useLatestVersions(npmPackage ? [npmPackage] : []);
|
||||
const latest = npmPackage ? latestVersions[npmPackage] : undefined;
|
||||
|
||||
const loadedPlugin = control.loadedPlugins.get(pluginName);
|
||||
const pluginInfo = control.pluginInfos.get(pluginName);
|
||||
const pluginTitle = pluginInfo ? pluginInfo.title : pluginName;
|
||||
let nav = [{ name: "Plugins", path: "/plugins" }, { name: pluginTitle }];
|
||||
if (!pluginList) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle} />
|
||||
<Spin size="large" />
|
||||
</PageLayout>;
|
||||
}
|
||||
return <>
|
||||
<Descriptions bordered size="small" style={{ marginTop: "1em" }}>
|
||||
<Descriptions.Item label="Package" span={npmPackage ? 1 : 3}>
|
||||
{npmPackage ?? "Not published to npm"}
|
||||
</Descriptions.Item>
|
||||
{npmPackage && <Descriptions.Item label="Latest version" span={2}>
|
||||
{latest ?? "Unknown"}
|
||||
</Descriptions.Item>}
|
||||
</Descriptions>
|
||||
<SectionHeader
|
||||
title="Machines"
|
||||
extra={<PluginUpdateButton
|
||||
status={props.status} title={props.title} latest={latest} installer={installer}
|
||||
/>}
|
||||
/>
|
||||
<PluginMachinesTable status={props.status} latest={latest} installer={installer} />
|
||||
</>;
|
||||
}
|
||||
|
||||
let pluginMeta = pluginList.find(p => p.name === pluginName);
|
||||
if (!pluginMeta) {
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader title="Plugin not found" />
|
||||
<p>Plugin with name {pluginName} was not found on the controller.</p>
|
||||
</PageLayout>;
|
||||
}
|
||||
type ControllerPluginProps = {
|
||||
pluginMeta: PluginWebApi,
|
||||
pluginInfo?: lib.PluginWebpackEnvInfo,
|
||||
loadedPlugin?: lib.PluginWebpackEnvInfo,
|
||||
title: string,
|
||||
};
|
||||
|
||||
/** What the controller and web UI have loaded of the plugin */
|
||||
function ControllerPlugin(props: ControllerPluginProps) {
|
||||
const { pluginMeta, pluginInfo, loadedPlugin } = props;
|
||||
const pluginError = pluginMeta.web.error ?? pluginInfo?.error;
|
||||
const hasWebModule = Boolean(pluginMeta.web.main || pluginMeta.web.error);
|
||||
if (!loadedPlugin) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle}>
|
||||
return <>
|
||||
<Descriptions bordered size="small" title={props.title}>
|
||||
<Descriptions.Item label="Version">{pluginMeta.version}</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded in Web UI" span={2}>
|
||||
{hasWebModule ? "No" : "No web module"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded on controller">{pluginMeta.loaded ? "Yes" : "No"}</Descriptions.Item>
|
||||
<Descriptions.Item label="Enabled on controller" span={2}>
|
||||
{pluginMeta.enabled ? "Yes" : "No"}
|
||||
</Descriptions.Item>
|
||||
</Descriptions>
|
||||
{pluginError && <Alert
|
||||
style={{
|
||||
@@ -73,20 +79,19 @@ export default function PluginViewPage() {
|
||||
type="error"
|
||||
showIcon
|
||||
/>}
|
||||
</PageLayout>;
|
||||
</>;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle}>
|
||||
const homepageUrl = lib.safeExternalUrl(loadedPlugin.package.homepage);
|
||||
return <>
|
||||
<Descriptions bordered size="small" title={props.title}>
|
||||
<Descriptions.Item label="Version">{pluginMeta.version}</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded in Web UI" span={2}>Yes</Descriptions.Item>
|
||||
<Descriptions.Item label="Loaded on controller">{pluginMeta.loaded ? "Yes" : "No"}</Descriptions.Item>
|
||||
<Descriptions.Item label="Enabled on controller" span={2}>
|
||||
{pluginMeta.enabled ? "Yes" : "No"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Description" span={3}>{loadedPlugin.description}</Descriptions.Item>
|
||||
{loadedPlugin.package.homepage ? <Descriptions.Item label="Homepage" span={3}>
|
||||
<a href={loadedPlugin.package.homepage}>{loadedPlugin.package.homepage}</a>
|
||||
{homepageUrl
|
||||
? <a href={homepageUrl}>{loadedPlugin.package.homepage}</a>
|
||||
: loadedPlugin.package.homepage}
|
||||
</Descriptions.Item> : null}
|
||||
{loadedPlugin.package.author ? <Descriptions.Item label="Author" span={3}>
|
||||
{loadedPlugin.package.author}
|
||||
@@ -107,5 +112,60 @@ export default function PluginViewPage() {
|
||||
type="warning"
|
||||
showIcon
|
||||
/> : null}
|
||||
</>;
|
||||
}
|
||||
|
||||
export default function PluginViewPage() {
|
||||
let params = useParams();
|
||||
const control = useContext(ControlContext);
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>();
|
||||
let pluginName = String(params.name);
|
||||
const { machines, unknown, refresh } = useMachinePlugins();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
let response = await fetch(`${webRoot}api/plugins`);
|
||||
if (response.ok) {
|
||||
setPluginList(await response.json());
|
||||
} else {
|
||||
notify("Failed to load plugin list");
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const pluginMeta = pluginList?.find(p => p.name === pluginName);
|
||||
// Plugins the controller hasn't loaded are linked to by their npm package or name on the hosts
|
||||
const status = machines
|
||||
? computePluginStatuses(machines, unknown).get(pluginMeta ? pluginStatusKey(pluginMeta) : pluginName)
|
||||
: undefined;
|
||||
const pluginInfo = control.pluginInfos.get(pluginName);
|
||||
const pluginTitle = pluginInfo?.title ?? status?.title ?? pluginName;
|
||||
let nav = [{ name: "Plugins", path: "/plugins" }, { name: pluginTitle }];
|
||||
if (!pluginList || (!pluginMeta && !machines)) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle} />
|
||||
<Spin size="large" />
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
if (!pluginMeta && !status) {
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader title="Plugin not found" />
|
||||
<p>Plugin with name {pluginName} was not found on the controller or any host.</p>
|
||||
</PageLayout>;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
{pluginMeta
|
||||
? <ControllerPlugin
|
||||
pluginMeta={pluginMeta}
|
||||
pluginInfo={pluginInfo}
|
||||
loadedPlugin={control.loadedPlugins.get(pluginName)}
|
||||
title={pluginTitle}
|
||||
/>
|
||||
: <Descriptions bordered size="small" title={pluginTitle}>
|
||||
<Descriptions.Item label="Loaded on controller">No</Descriptions.Item>
|
||||
</Descriptions>}
|
||||
{status && <PluginMachines status={status} title={pluginTitle} refresh={refresh} />}
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { Table } from "antd";
|
||||
import { Space, Table, Typography } from "antd";
|
||||
import CloseCircleFilled from "@ant-design/icons/CloseCircleFilled";
|
||||
import InfoCircleFilled from "@ant-design/icons/InfoCircleFilled";
|
||||
|
||||
import type { PluginWebApi, PluginWebpackEnvInfo } from "@clusterio/lib";
|
||||
|
||||
import notify from "../util/notify";
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
import { PluginBrowserButton } from "./PluginBrowser";
|
||||
import { usePluginInstaller } from "./PluginInstall";
|
||||
import {
|
||||
PluginUpdateButton, PluginVersions, RestartPendingButton, UpdateAllButton, useLatestVersions, useMachinePlugins,
|
||||
} from "./PluginClusterStatus";
|
||||
import { PluginStatus, computePluginStatuses, pluginStatusKey } from "../util/plugin_status";
|
||||
import PageLayout from "./PageLayout";
|
||||
import PageHeader from "./PageHeader";
|
||||
import useTableQueryState from "../util/useTableQueryState";
|
||||
@@ -17,20 +24,60 @@ import Link from "./Link";
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
type PluginRow = {
|
||||
meta: PluginWebApi;
|
||||
key: string;
|
||||
title: string;
|
||||
/** Not present for plugins the controller hasn't loaded */
|
||||
meta?: PluginWebApi;
|
||||
info?: PluginWebpackEnvInfo;
|
||||
package?: any;
|
||||
status?: PluginStatus;
|
||||
};
|
||||
|
||||
/** Plugins the controller hasn't loaded are found by their status key instead of the plugin name */
|
||||
function pluginPath(plugin: PluginRow) {
|
||||
return `/plugins/${encodeURIComponent(plugin.meta?.name ?? plugin.key)}/view`;
|
||||
}
|
||||
|
||||
function WebUiStatus(props: { plugin: PluginRow }) {
|
||||
const control = useContext(ControlContext);
|
||||
const { meta } = props.plugin;
|
||||
if (!meta) {
|
||||
return <Typography.Text type="secondary">Not on controller</Typography.Text>;
|
||||
}
|
||||
if (!meta.enabled) {
|
||||
return <><InfoCircleFilled style={{ color: "#1668dc" }} /> Disabled on controller</>;
|
||||
}
|
||||
if (!meta.web.main && !meta.web.error) {
|
||||
return <Typography.Text type="secondary">No web module</Typography.Text>;
|
||||
}
|
||||
if (!meta.web.error && !control.pluginInfos.has(meta.name)) {
|
||||
return <><InfoCircleFilled style={{ color: "#1668dc" }} /> Reload page to load</>;
|
||||
}
|
||||
if (!props.plugin.package) {
|
||||
return <><CloseCircleFilled style={{ color: "#dc4446" }} /> Error loading module</>;
|
||||
}
|
||||
if (props.plugin.package.version !== meta.version) {
|
||||
return <><CloseCircleFilled style={{ color: "#dc4446" }} /> Version mis-matched</>;
|
||||
}
|
||||
return <>Loaded {meta.version}</>;
|
||||
}
|
||||
|
||||
|
||||
export default function PluginsPage() {
|
||||
const control = useContext(ControlContext);
|
||||
const account = useAccount();
|
||||
let [pluginList, setPluginList] = useState<PluginWebApi[]>([]);
|
||||
const tableState = useTableQueryState<PluginRow>({
|
||||
namespace: "plugin", defaultSortKey: "name",
|
||||
});
|
||||
const nameSearch = useColumnSearch<PluginRow>(
|
||||
tableState, "name", plugin => (plugin.info ? plugin.info.title : plugin.meta.name), "Search plugins"
|
||||
tableState, "name", plugin => plugin.title, "Search plugins"
|
||||
);
|
||||
const { machines, unknown, refresh } = useMachinePlugins();
|
||||
const installer = usePluginInstaller(refresh);
|
||||
const statuses = machines ? computePluginStatuses(machines, unknown) : new Map<string, PluginStatus>();
|
||||
const latestVersions = useLatestVersions(
|
||||
[...statuses.values()].flatMap(status => (status.npmPackage ? [status.npmPackage] : []))
|
||||
);
|
||||
const rowNav = useRowNavigation();
|
||||
|
||||
@@ -47,84 +94,96 @@ export default function PluginsPage() {
|
||||
|
||||
let tableContents: PluginRow[] = [];
|
||||
for (let meta of pluginList) {
|
||||
const info = control.loadedPlugins.get(meta.name);
|
||||
if (info) {
|
||||
tableContents.push({
|
||||
meta, info,
|
||||
package: info.package,
|
||||
});
|
||||
} else {
|
||||
tableContents.push({
|
||||
meta, info: control.pluginInfos.get(meta.name),
|
||||
});
|
||||
const key = pluginStatusKey(meta);
|
||||
const loaded = control.loadedPlugins.get(meta.name);
|
||||
const info = loaded ?? control.pluginInfos.get(meta.name);
|
||||
tableContents.push({
|
||||
key,
|
||||
title: info ? info.title : meta.name,
|
||||
meta,
|
||||
info,
|
||||
package: loaded?.package,
|
||||
status: statuses.get(key),
|
||||
});
|
||||
}
|
||||
// Plugins installed on hosts, or waiting for a restart, that the controller hasn't loaded
|
||||
const listedKeys = new Set(tableContents.map(row => row.key));
|
||||
for (const status of statuses.values()) {
|
||||
if (!listedKeys.has(status.key)) {
|
||||
tableContents.push({ key: status.key, title: status.title, status });
|
||||
}
|
||||
}
|
||||
const latestOf = (plugin: PluginRow) => (
|
||||
plugin.status?.npmPackage ? latestVersions[plugin.status.npmPackage] : undefined
|
||||
);
|
||||
|
||||
return <PageLayout nav={[{ name: "Plugins" }]}>
|
||||
<PageHeader title="Plugins" />
|
||||
<PageHeader
|
||||
title="Plugins"
|
||||
extra={<Space wrap>
|
||||
<RestartPendingButton
|
||||
statuses={[...statuses.values()]} machines={machines ?? []} installer={installer}
|
||||
/>
|
||||
<UpdateAllButton
|
||||
statuses={[...statuses.values()]}
|
||||
titles={new Map(tableContents.map(row => [row.key, row.title]))}
|
||||
latestVersions={latestVersions}
|
||||
installer={installer}
|
||||
/>
|
||||
{account.hasPermission("core.plugin.search")
|
||||
&& <PluginBrowserButton statuses={machines && statuses} installer={installer} />}
|
||||
</Space>}
|
||||
/>
|
||||
<Table
|
||||
columns={[
|
||||
{
|
||||
title: "Name",
|
||||
key: "name",
|
||||
className: "table-link-cell",
|
||||
render: (_, plugin) => <Link
|
||||
to={`/plugins/${plugin.meta.name}/view`}
|
||||
style={{ color: "inherit" }}
|
||||
>
|
||||
{plugin.info ? plugin.info.title : plugin.meta.name}
|
||||
render: (_, plugin) => <Link to={pluginPath(plugin)} style={{ color: "inherit" }}>
|
||||
{plugin.title}
|
||||
</Link>,
|
||||
sorter: (a, b) => strcmp(a.info ? a.info.title : a.meta.name, b.info ? b.info.title : b.meta.name),
|
||||
sorter: (a, b) => strcmp(a.title, b.title),
|
||||
sortOrder: tableState.sortOrder("name"),
|
||||
filteredValue: tableState.filteredValue("name"),
|
||||
...nameSearch,
|
||||
},
|
||||
{
|
||||
title: "Version",
|
||||
key: "version",
|
||||
render: (_, plugin) => {
|
||||
if (!plugin.meta.enabled) {
|
||||
return <><InfoCircleFilled style={{ color: "#1668dc" }} /> Disabled on controller</>;
|
||||
}
|
||||
if (!plugin.meta.web.main && !plugin.meta.web.error) {
|
||||
return plugin.meta.version;
|
||||
}
|
||||
if (!plugin.meta.web.error && !control.pluginInfos.has(plugin.meta.name)) {
|
||||
return <><InfoCircleFilled style={{ color: "#1668dc" }} /> Reload page to load</>;
|
||||
}
|
||||
if (!plugin.package) {
|
||||
return <><CloseCircleFilled style={{ color: "#dc4446" }} /> Error loading module</>;
|
||||
}
|
||||
if (plugin.package.version !== plugin.meta.version) {
|
||||
return <><CloseCircleFilled style={{ color: "#dc4446" }} /> Version mis-matched</>;
|
||||
}
|
||||
return plugin.package.version;
|
||||
},
|
||||
sorter: (a, b) => strcmp(a.meta.version, b.meta.version),
|
||||
sortOrder: tableState.sortOrder("version"),
|
||||
title: "Versions",
|
||||
key: "versions",
|
||||
render: (_, plugin) => (plugin.status
|
||||
? <PluginVersions status={plugin.status} latest={latestOf(plugin)} />
|
||||
: plugin.meta?.version
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Loaded",
|
||||
key: "loaded",
|
||||
render: (_, plugin) => {
|
||||
if (plugin.package) {
|
||||
return "Yes";
|
||||
}
|
||||
if (!plugin.meta.web.main && !plugin.meta.web.error) {
|
||||
return "No web module";
|
||||
}
|
||||
return null;
|
||||
},
|
||||
sorter: (a, b) => Number(Boolean(a.package)) - Number(Boolean(b.package)),
|
||||
sortOrder: tableState.sortOrder("loaded"),
|
||||
title: "Web UI Status",
|
||||
key: "web",
|
||||
render: (_, plugin) => <WebUiStatus plugin={plugin} />,
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
key: "actions",
|
||||
align: "right",
|
||||
render: (_, plugin) => (plugin.status
|
||||
// Keep clicks on the update action from opening the plugin
|
||||
? <span onClick={event => { event.stopPropagation(); }}>
|
||||
<PluginUpdateButton
|
||||
status={plugin.status}
|
||||
title={plugin.title}
|
||||
latest={latestOf(plugin)}
|
||||
installer={installer}
|
||||
/>
|
||||
</span>
|
||||
: null
|
||||
),
|
||||
},
|
||||
]}
|
||||
dataSource={tableContents}
|
||||
rowKey={plugin => plugin.meta.name}
|
||||
rowKey={plugin => plugin.key}
|
||||
pagination={tableState.pagination}
|
||||
onChange={tableState.onChange}
|
||||
onRow={plugin => rowNav(`/plugins/${plugin.meta.name}/view`)}
|
||||
onRow={plugin => rowNav(pluginPath(plugin))}
|
||||
/>
|
||||
</PageLayout>;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/** Runs one task at a time, refusing tasks started while another is running */
|
||||
export class ExclusiveRunner<State> {
|
||||
current?: State;
|
||||
onChange: (state?: State) => void;
|
||||
|
||||
constructor(onChange: (state?: State) => void) {
|
||||
this.onChange = onChange;
|
||||
}
|
||||
|
||||
/** Run the task unless another is running, returns false if it was refused */
|
||||
async run(state: State, task: () => Promise<void>) {
|
||||
if (this.current !== undefined) {
|
||||
return false;
|
||||
}
|
||||
this.current = state;
|
||||
this.onChange(state);
|
||||
try {
|
||||
await task();
|
||||
} finally {
|
||||
this.current = undefined;
|
||||
this.onChange(undefined);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { ReactNode } from "react";
|
||||
import { notification } from "antd";
|
||||
import { logger } from "@clusterio/lib";
|
||||
|
||||
@@ -5,7 +6,7 @@ import { logger } from "@clusterio/lib";
|
||||
export default function notify(
|
||||
message: string | Error,
|
||||
type: "success"|"info"|"warning"|"error" = "info",
|
||||
description?: string,
|
||||
description?: ReactNode,
|
||||
) {
|
||||
notification[type]({
|
||||
message: typeof message === "string" ? message : "ERROR: See console",
|
||||
|
||||
@@ -0,0 +1,233 @@
|
||||
import semver from "semver";
|
||||
|
||||
import type * as lib from "@clusterio/lib";
|
||||
|
||||
import type { UserAccount } from "../BaseWebPlugin";
|
||||
import type { InstallTarget, PackageOperation, RemoteAccess } from "../components/PluginInstall";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
export type Machine = {
|
||||
name: string,
|
||||
isController: boolean,
|
||||
address: lib.AddressShorthand,
|
||||
/** A host that isn't connected, its plugins are the ones it had when last seen */
|
||||
offline?: boolean,
|
||||
};
|
||||
export type MachinePlugins = Machine & { plugins: lib.PluginDetails[] };
|
||||
|
||||
/** A plugin on one machine, with the version it runs now and the version it will run after a restart */
|
||||
export type MachinePlugin = Machine & {
|
||||
plugin?: lib.PluginDetails,
|
||||
running?: string,
|
||||
afterRestart?: string,
|
||||
disabled: boolean,
|
||||
/** Installed but never loaded on this machine as it has no entrypoint for it */
|
||||
noEntrypoint: boolean,
|
||||
/** The group's npm package name is a dependency here for another plugin, such as an alias */
|
||||
dependencyTaken: boolean,
|
||||
/** Another package here provides the plugin by the same name, startup skips this one */
|
||||
nameTaken: boolean,
|
||||
/** A host from before installedVersion was reported, it can't install a specific version */
|
||||
oldHost: boolean,
|
||||
};
|
||||
export type PluginStatus = {
|
||||
key: string,
|
||||
title: string,
|
||||
/** Not present for plugins that aren't published to npm */
|
||||
npmPackage?: string,
|
||||
machines: MachinePlugin[],
|
||||
/** Machines that haven't reported their plugins, such as hosts on an older version */
|
||||
unknown: Machine[],
|
||||
};
|
||||
|
||||
/** Plugins are matched across machines by npm package, falling back to the plugin name */
|
||||
export function pluginStatusKey(plugin: { name: string, npmPackage?: string }) {
|
||||
return plugin.npmPackage ?? plugin.name;
|
||||
}
|
||||
|
||||
export function compareVersions(a: string, b: string) {
|
||||
if (semver.valid(a) && semver.valid(b)) {
|
||||
return semver.compare(a, b);
|
||||
}
|
||||
return strcmp(a, b);
|
||||
}
|
||||
|
||||
/** The plugin with the given key on one machine, names are the plugin names known for that key */
|
||||
function machinePlugin(machine: MachinePlugins, key: string, names: Set<string>): MachinePlugin {
|
||||
const { plugins, ...rest } = machine;
|
||||
// A package removed and installed again is listed twice, the removed one may still be running
|
||||
const candidates = plugins.filter(candidate => pluginStatusKey(candidate) === key);
|
||||
const plugin = candidates.find(candidate => !candidate.removed) ?? candidates[0];
|
||||
const absent = !plugin || Boolean(plugin.removed);
|
||||
const dependencyTaken = absent && plugins.some(
|
||||
candidate => candidate.dependencyName === key && !candidate.removed
|
||||
);
|
||||
const nameTaken = Boolean(plugin?.nameTaken) || (absent || Boolean(plugin.pendingInstall)) && plugins.some(
|
||||
candidate => (
|
||||
pluginStatusKey(candidate) !== key && names.has(candidate.name)
|
||||
&& !candidate.removed && !candidate.pendingInstall && !candidate.nameTaken
|
||||
)
|
||||
);
|
||||
const noEntrypoint = Boolean(plugin?.noEntrypoint);
|
||||
const oldHost = !machine.isController && plugins.length > 0 && plugins.every(
|
||||
candidate => candidate.installedVersion === undefined && !candidate.removed
|
||||
);
|
||||
const entry: MachinePlugin = {
|
||||
...rest, plugin, disabled: false, noEntrypoint, dependencyTaken, nameTaken, oldHost,
|
||||
};
|
||||
// What runs now follows loaded, what runs after a restart follows enabled and what is on disk
|
||||
entry.running = candidates.find(candidate => candidate.loaded)?.version;
|
||||
if (nameTaken) {
|
||||
return entry;
|
||||
}
|
||||
if (plugin?.pendingInstall) {
|
||||
// Plugins load by default, so a new install runs after the next restart
|
||||
entry.afterRestart = plugin.installedVersion ?? plugin.version;
|
||||
} else if (plugin && !plugin.removed && !noEntrypoint) {
|
||||
if (plugin.enabled) {
|
||||
entry.afterRestart = plugin.installedVersion ?? plugin.version;
|
||||
} else {
|
||||
entry.disabled = true;
|
||||
}
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
/** Group the plugin lists of every machine by plugin */
|
||||
export function computePluginStatuses(
|
||||
machines: MachinePlugins[], unknown: Machine[] = [],
|
||||
): Map<string, PluginStatus> {
|
||||
const titles = new Map<string, string>();
|
||||
// Plugin names known from startup, pending installs are listed by package name
|
||||
const pluginNames = new Map<string, Set<string>>();
|
||||
for (const machine of machines) {
|
||||
for (const plugin of machine.plugins) {
|
||||
const key = pluginStatusKey(plugin);
|
||||
if (!titles.has(key) || plugin.loaded) {
|
||||
titles.set(key, plugin.title);
|
||||
}
|
||||
if (!plugin.pendingInstall && !plugin.nameTaken) {
|
||||
pluginNames.set(key, (pluginNames.get(key) ?? new Set()).add(plugin.name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const statuses = new Map<string, PluginStatus>();
|
||||
for (const [key, title] of titles) {
|
||||
const status: PluginStatus = { key, title, machines: [], unknown };
|
||||
for (const machine of machines) {
|
||||
const entry = machinePlugin(machine, key, pluginNames.get(key) ?? new Set());
|
||||
status.npmPackage ??= entry.plugin?.npmPackage;
|
||||
status.machines.push(entry);
|
||||
}
|
||||
statuses.set(key, status);
|
||||
}
|
||||
return statuses;
|
||||
}
|
||||
|
||||
export type SkippedOperation = { kind: PackageOperation["kind"], target: InstallTarget, reason: string };
|
||||
export type UpdatePlan = {
|
||||
operations: PackageOperation[],
|
||||
skipped: SkippedOperation[],
|
||||
};
|
||||
|
||||
/**
|
||||
* Bring every connected machine up to the given version, installing where the plugin is missing.
|
||||
* Machines already on that version or a newer one are left alone.
|
||||
*/
|
||||
export function planUpdate(
|
||||
status: PluginStatus,
|
||||
version: string,
|
||||
access: RemoteAccess,
|
||||
account: Pick<UserAccount, "hasPermission">,
|
||||
): UpdatePlan {
|
||||
const plan: UpdatePlan = { operations: [], skipped: [] };
|
||||
if (!status.npmPackage) {
|
||||
return plan;
|
||||
}
|
||||
const packageSpec = `${status.npmPackage}@${version}`;
|
||||
for (const machine of status.machines) {
|
||||
const { plugin } = machine;
|
||||
const target = { name: machine.name, address: machine.address };
|
||||
const installed = plugin && !plugin.removed;
|
||||
if (installed && compareVersions(plugin.installedVersion ?? plugin.version, version) >= 0) {
|
||||
continue;
|
||||
}
|
||||
const kind = !installed || plugin.pendingInstall ? "install" : "update";
|
||||
const skip = (reason: string, skipKind: PackageOperation["kind"] = kind) => {
|
||||
plan.skipped.push({ kind: skipKind, target, reason });
|
||||
};
|
||||
if (machine.offline) {
|
||||
skip("offline");
|
||||
} else if (machine.nameTaken) {
|
||||
skip("another package provides this plugin", "install");
|
||||
} else if (machine.dependencyTaken) {
|
||||
skip(`another package is installed as ${status.npmPackage}`, "install");
|
||||
} else if (installed && !plugin.npmPackage) {
|
||||
// An alias, private or local package, installing the group's npm package would replace it
|
||||
skip("not installed from npm");
|
||||
} else if (machine.oldHost) {
|
||||
skip("host too old to install a specific version");
|
||||
} else if (kind === "install") {
|
||||
if (!account.hasPermission("core.plugin.install")) {
|
||||
skip("no install permission");
|
||||
} else if (access.canInstall(machine.address)) {
|
||||
plan.operations.push({ kind, target, packageSpec });
|
||||
} else {
|
||||
skip("remote installs disabled");
|
||||
}
|
||||
} else if (!account.hasPermission("core.plugin.update")) {
|
||||
skip("no update permission");
|
||||
} else if (access.canUpdate(machine.address)) {
|
||||
plan.operations.push({ kind, target, packageSpec });
|
||||
} else {
|
||||
skip("remote updates disabled");
|
||||
}
|
||||
}
|
||||
if (plan.operations.length || plan.skipped.length) {
|
||||
for (const machine of status.unknown) {
|
||||
const target = { name: machine.name, address: machine.address };
|
||||
plan.skipped.push({ kind: "update", target, reason: "plugins not reported" });
|
||||
}
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
/** Plan for the given machine only */
|
||||
export function planMachineUpdate(
|
||||
status: PluginStatus,
|
||||
machine: MachinePlugin,
|
||||
version: string,
|
||||
access: RemoteAccess,
|
||||
account: Pick<UserAccount, "hasPermission">,
|
||||
): UpdatePlan {
|
||||
return planUpdate({ ...status, machines: [machine], unknown: [] }, version, access, account);
|
||||
}
|
||||
|
||||
/** Short descriptions of the state of a plugin on a machine, beyond the versions it runs */
|
||||
export function machineStatusLabels(machine: MachinePlugin) {
|
||||
const labels: string[] = [];
|
||||
if (machine.offline) {
|
||||
labels.push("Offline");
|
||||
}
|
||||
if (machine.nameTaken) {
|
||||
labels.push("Another package provides this plugin");
|
||||
} else if (!machine.plugin || machine.plugin.removed) {
|
||||
labels.push(machine.dependencyTaken ? "Another package is installed by this name" : "Not installed");
|
||||
} else {
|
||||
if (!machine.plugin.npmPackage) {
|
||||
labels.push("Not installed from npm");
|
||||
}
|
||||
if (machine.noEntrypoint) {
|
||||
labels.push("No entrypoint for this machine");
|
||||
}
|
||||
if (machine.disabled) {
|
||||
labels.push("Disabled");
|
||||
}
|
||||
}
|
||||
if (machine.running !== machine.afterRestart) {
|
||||
labels.push("Restart pending");
|
||||
}
|
||||
return labels;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Run a task now and again intervalMs after each run finishes, so runs never overlap.
|
||||
* setInterval can't be used as an async task may take longer than the interval.
|
||||
* The task handles its own errors.
|
||||
*
|
||||
* @returns Function that stops polling, a run in progress still finishes
|
||||
*/
|
||||
export function poll(task: () => Promise<void>, intervalMs: number) {
|
||||
let stopped = false;
|
||||
let timeout: ReturnType<typeof setTimeout> | undefined;
|
||||
async function run() {
|
||||
if (stopped) {
|
||||
return;
|
||||
}
|
||||
await task();
|
||||
// Stopping while the task ran must not schedule another run
|
||||
if (!stopped) {
|
||||
timeout = setTimeout(run, intervalMs);
|
||||
}
|
||||
}
|
||||
run();
|
||||
return () => {
|
||||
stopped = true;
|
||||
clearTimeout(timeout);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user