dsh-ssh-hub 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +271 -2
- package/cordis.patch.yml +16 -0
- package/icon.svg +3 -0
- package/lib/client.js +13975 -0
- package/lib/index.js +6908 -0
- package/lib/types/activity/history.d.ts +50 -0
- package/lib/types/alerting/notify.d.ts +89 -0
- package/lib/types/alerting/prompt-summary.d.ts +28 -0
- package/lib/types/alerting/rules.d.ts +135 -0
- package/lib/types/alerting/tools.d.ts +30 -0
- package/lib/types/audit/audit-log.d.ts +84 -0
- package/lib/types/audit/recorder.d.ts +50 -0
- package/lib/types/client/api.d.ts +222 -0
- package/lib/types/client/index.d.ts +45 -0
- package/lib/types/client/locales.d.ts +319 -0
- package/lib/types/client/native-panel.d.ts +70 -0
- package/lib/types/client/panel/ClusterTab.d.ts +7 -0
- package/lib/types/client/panel/DeployTab.d.ts +10 -0
- package/lib/types/client/panel/FleetTab.d.ts +10 -0
- package/lib/types/client/panel/HostFormDialog.d.ts +12 -0
- package/lib/types/client/panel/HostsTab.d.ts +25 -0
- package/lib/types/client/panel/LogsTab.d.ts +10 -0
- package/lib/types/client/panel/SshPanel.d.ts +14 -0
- package/lib/types/client/panel/TerminalTab.d.ts +27 -0
- package/lib/types/client/panel/TransferTab.d.ts +7 -0
- package/lib/types/client/panel/TunnelEditDialog.d.ts +15 -0
- package/lib/types/client/panel/TunnelsTab.d.ts +27 -0
- package/lib/types/client/panel/controller.d.ts +253 -0
- package/lib/types/client/panel/helpers.d.ts +45 -0
- package/lib/types/client/panel/tabs.d.ts +62 -0
- package/lib/types/client/panel/xterm.css.d.ts +2 -0
- package/lib/types/client/settings-binding.d.ts +71 -0
- package/lib/types/deploy/orchestrator.d.ts +158 -0
- package/lib/types/deploy/tools.d.ts +14 -0
- package/lib/types/dsh-home.d.ts +16 -0
- package/lib/types/engine/cluster.d.ts +19 -0
- package/lib/types/engine/connection-pool.d.ts +183 -0
- package/lib/types/engine/proxy-command.d.ts +36 -0
- package/lib/types/engine/pty.d.ts +32 -0
- package/lib/types/engine/sftp.d.ts +20 -0
- package/lib/types/engine/terminal-sessions.d.ts +67 -0
- package/lib/types/engine/tunnel-discovery.d.ts +89 -0
- package/lib/types/engine/tunnel-remarks.d.ts +49 -0
- package/lib/types/engine/tunnel.d.ts +76 -0
- package/lib/types/engine.d.ts +103 -0
- package/lib/types/http.d.ts +59 -0
- package/lib/types/index.d.ts +128 -0
- package/lib/types/inspect/checks.d.ts +118 -0
- package/lib/types/inspect/inspection.d.ts +111 -0
- package/lib/types/inspect/tools.d.ts +50 -0
- package/lib/types/logs/search.d.ts +131 -0
- package/lib/types/logs/tools.d.ts +6 -0
- package/lib/types/loopback.d.ts +64 -0
- package/lib/types/monitor/loop.d.ts +133 -0
- package/lib/types/monitor/metrics.d.ts +151 -0
- package/lib/types/mount-once.d.ts +55 -0
- package/lib/types/protocol.d.ts +524 -0
- package/lib/types/routes.d.ts +82 -0
- package/lib/types/security/known-hosts.d.ts +153 -0
- package/lib/types/security/master-secret.d.ts +157 -0
- package/lib/types/security/redact.d.ts +44 -0
- package/lib/types/security/secrets.d.ts +70 -0
- package/lib/types/ssh-config.d.ts +42 -0
- package/lib/types/store.d.ts +183 -0
- package/lib/types/tool-surface.d.ts +72 -0
- package/lib/types/tools.d.ts +56 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +126 -6
- package/src/activity/history.ts +117 -0
- package/src/alerting/notify.ts +212 -0
- package/src/alerting/prompt-summary.ts +71 -0
- package/src/alerting/rules.ts +281 -0
- package/src/alerting/tools.ts +263 -0
- package/src/audit/audit-log.ts +199 -0
- package/src/audit/recorder.ts +73 -0
- package/src/client/api.ts +782 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/index.ts +150 -0
- package/src/client/locales.ts +654 -0
- package/src/client/native-panel.tsx +141 -0
- package/src/client/panel/ClusterTab.tsx +141 -0
- package/src/client/panel/DeployTab.tsx +314 -0
- package/src/client/panel/FleetTab.tsx +494 -0
- package/src/client/panel/HostFormDialog.tsx +289 -0
- package/src/client/panel/HostsTab.tsx +395 -0
- package/src/client/panel/LogsTab.tsx +208 -0
- package/src/client/panel/SshPanel.tsx +105 -0
- package/src/client/panel/TerminalTab.tsx +550 -0
- package/src/client/panel/TransferTab.tsx +238 -0
- package/src/client/panel/TunnelEditDialog.tsx +174 -0
- package/src/client/panel/TunnelsTab.tsx +328 -0
- package/src/client/panel/controller.ts +507 -0
- package/src/client/panel/helpers.ts +84 -0
- package/src/client/panel/panel.module.css +1734 -0
- package/src/client/panel/tabs.ts +52 -0
- package/src/client/panel/xterm.css.ts +2 -0
- package/src/client/settings-binding.ts +184 -0
- package/src/deploy/orchestrator.ts +326 -0
- package/src/deploy/tools.ts +225 -0
- package/src/dsh-home.ts +42 -0
- package/src/engine/cluster.ts +60 -0
- package/src/engine/connection-pool.ts +721 -0
- package/src/engine/proxy-command.ts +248 -0
- package/src/engine/pty.ts +78 -0
- package/src/engine/sftp.ts +229 -0
- package/src/engine/terminal-sessions.ts +280 -0
- package/src/engine/tunnel-discovery.ts +353 -0
- package/src/engine/tunnel-remarks.ts +120 -0
- package/src/engine/tunnel.ts +300 -0
- package/src/engine.ts +195 -0
- package/src/http.ts +127 -0
- package/src/index.ts +690 -0
- package/src/inspect/checks.ts +251 -0
- package/src/inspect/inspection.ts +321 -0
- package/src/inspect/tools.ts +366 -0
- package/src/logs/search.ts +251 -0
- package/src/logs/tools.ts +160 -0
- package/src/loopback.ts +119 -0
- package/src/monitor/loop.ts +265 -0
- package/src/monitor/metrics.ts +333 -0
- package/src/mount-once.ts +142 -0
- package/src/protocol.ts +534 -0
- package/src/routes.ts +998 -0
- package/src/security/known-hosts.ts +342 -0
- package/src/security/master-secret.ts +334 -0
- package/src/security/redact.ts +119 -0
- package/src/security/secrets.ts +152 -0
- package/src/ssh-config.ts +182 -0
- package/src/store.ts +757 -0
- package/src/tool-surface.ts +96 -0
- package/src/tools.ts +550 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The panel's tab table, in one place.
|
|
3
|
+
*
|
|
4
|
+
* It lives apart from the component so the shell and the tests read the same
|
|
5
|
+
* list. The bug this prevents is specific and easy to repeat: a tab component
|
|
6
|
+
* exists, its locale keys exist, and nothing renders it — which looks like a
|
|
7
|
+
* working feature in review because every file involved is correct on its own.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { SshKey } from '../locales.ts'
|
|
11
|
+
import type { SshTab } from './controller.ts'
|
|
12
|
+
|
|
13
|
+
/** One entry in the panel's tab bar. */
|
|
14
|
+
export interface SshTabSpec {
|
|
15
|
+
/** The tab id, also the value stored in the controller. */
|
|
16
|
+
id: SshTab
|
|
17
|
+
/** The locale key for its label. */
|
|
18
|
+
labelKey: SshKey
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The tabs, in display order.
|
|
23
|
+
*
|
|
24
|
+
* `satisfies` rather than a plain annotation: it keeps the literal types while
|
|
25
|
+
* proving the array is exhaustive against {@link SshTab}, so adding a member to
|
|
26
|
+
* the union without adding a tab fails the type check instead of shipping a tab
|
|
27
|
+
* nobody can reach.
|
|
28
|
+
*/
|
|
29
|
+
export const SSH_TABS = [
|
|
30
|
+
{ id: 'hosts', labelKey: 'tab.hosts' },
|
|
31
|
+
{ id: 'terminal', labelKey: 'tab.terminal' },
|
|
32
|
+
{ id: 'transfer', labelKey: 'tab.transfer' },
|
|
33
|
+
{ id: 'tunnels', labelKey: 'tab.tunnels' },
|
|
34
|
+
{ id: 'cluster', labelKey: 'tab.cluster' },
|
|
35
|
+
{ id: 'deploy', labelKey: 'tab.deploy' },
|
|
36
|
+
{ id: 'logs', labelKey: 'tab.logs' },
|
|
37
|
+
{ id: 'fleet', labelKey: 'tab.fleet' },
|
|
38
|
+
] as const satisfies ReadonlyArray<SshTabSpec>
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Every tab id, as a set for the exhaustiveness assertion below.
|
|
42
|
+
*
|
|
43
|
+
* Written as a type-level check rather than a runtime one: a missing tab is a
|
|
44
|
+
* build error, not a test failure discovered later.
|
|
45
|
+
*/
|
|
46
|
+
type CoveredTab = (typeof SSH_TABS)[number]['id']
|
|
47
|
+
|
|
48
|
+
/** Compile-time proof that the table covers the union exactly. */
|
|
49
|
+
export type TabsCoverEveryTab = Exclude<SshTab, CoveredTab> extends never ? true : never
|
|
50
|
+
|
|
51
|
+
/** Compile-time proof that the table adds no id outside the union. */
|
|
52
|
+
export type TabsAddNoUnknownTab = Exclude<CoveredTab, SshTab> extends never ? true : never
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** xterm.css text embedded at build-authoring time (the ?raw suffix is not resolvable by the tsdown/rolldown pipeline). Generated file — see the package docs. */
|
|
2
|
+
export const XTERM_CSS: string = "/**\n * Copyright (c) 2014 The xterm.js authors. All rights reserved.\n * Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)\n * https://github.com/chjj/term.js\n * @license MIT\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in\n * all copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n * THE SOFTWARE.\n *\n * Originally forked from (with the author's permission):\n * Fabrice Bellard's javascript vt100 for jslinux:\n * http://bellard.org/jslinux/\n * Copyright (c) 2011 Fabrice Bellard\n * The original design remains. The terminal itself\n * has been extended to include xterm CSI codes, among\n * other features.\n */\n\n/**\n * Default styles for xterm.js\n */\n\n.xterm {\n cursor: text;\n position: relative;\n user-select: none;\n -ms-user-select: none;\n -webkit-user-select: none;\n}\n\n.xterm.focus,\n.xterm:focus {\n outline: none;\n}\n\n.xterm .xterm-helpers {\n position: absolute;\n top: 0;\n /**\n * The z-index of the helpers must be higher than the canvases in order for\n * IMEs to appear on top.\n */\n z-index: 5;\n}\n\n.xterm .xterm-helper-textarea {\n padding: 0;\n border: 0;\n margin: 0;\n /* Move textarea out of the screen to the far left, so that the cursor is not visible */\n position: absolute;\n opacity: 0;\n left: -9999em;\n top: 0;\n width: 0;\n height: 0;\n z-index: -5;\n /** Prevent wrapping so the IME appears against the textarea at the correct position */\n white-space: nowrap;\n overflow: hidden;\n resize: none;\n}\n\n.xterm .composition-view {\n /* TODO: Composition position got messed up somewhere */\n background: #000;\n color: #FFF;\n display: none;\n position: absolute;\n white-space: nowrap;\n z-index: 1;\n}\n\n.xterm .composition-view.active {\n display: block;\n}\n\n.xterm .xterm-viewport {\n /* On OS X this is required in order for the scroll bar to appear fully opaque */\n background-color: #000;\n overflow-y: scroll;\n cursor: default;\n position: absolute;\n right: 0;\n left: 0;\n top: 0;\n bottom: 0;\n}\n\n.xterm .xterm-screen {\n position: relative;\n}\n\n.xterm .xterm-screen canvas {\n position: absolute;\n left: 0;\n top: 0;\n}\n\n.xterm-char-measure-element {\n display: inline-block;\n visibility: hidden;\n position: absolute;\n top: 0;\n left: -9999em;\n line-height: normal;\n}\n\n.xterm.enable-mouse-events {\n /* When mouse events are enabled (eg. tmux), revert to the standard pointer cursor */\n cursor: default;\n}\n\n.xterm.xterm-cursor-pointer,\n.xterm .xterm-cursor-pointer {\n cursor: pointer;\n}\n\n.xterm.column-select.focus {\n /* Column selection mode */\n cursor: crosshair;\n}\n\n.xterm .xterm-accessibility:not(.debug),\n.xterm .xterm-message {\n position: absolute;\n left: 0;\n top: 0;\n bottom: 0;\n right: 0;\n z-index: 10;\n color: transparent;\n pointer-events: none;\n}\n\n.xterm .xterm-accessibility-tree:not(.debug) *::selection {\n color: transparent;\n}\n\n.xterm .xterm-accessibility-tree {\n font-family: monospace;\n user-select: text;\n white-space: pre;\n}\n\n.xterm .xterm-accessibility-tree > div {\n transform-origin: left;\n width: fit-content;\n}\n\n.xterm .live-region {\n position: absolute;\n left: -9999px;\n width: 1px;\n height: 1px;\n overflow: hidden;\n}\n\n.xterm-dim {\n /* Dim should not apply to background, so the opacity of the foreground color is applied\n * explicitly in the generated class and reset to 1 here */\n opacity: 1 !important;\n}\n\n.xterm-underline-1 { text-decoration: underline; }\n.xterm-underline-2 { text-decoration: double underline; }\n.xterm-underline-3 { text-decoration: wavy underline; }\n.xterm-underline-4 { text-decoration: dotted underline; }\n.xterm-underline-5 { text-decoration: dashed underline; }\n\n.xterm-overline {\n text-decoration: overline;\n}\n\n.xterm-overline.xterm-underline-1 { text-decoration: overline underline; }\n.xterm-overline.xterm-underline-2 { text-decoration: overline double underline; }\n.xterm-overline.xterm-underline-3 { text-decoration: overline wavy underline; }\n.xterm-overline.xterm-underline-4 { text-decoration: overline dotted underline; }\n.xterm-overline.xterm-underline-5 { text-decoration: overline dashed underline; }\n\n.xterm-strikethrough {\n text-decoration: line-through;\n}\n\n.xterm-screen .xterm-decoration-container .xterm-decoration {\n\tz-index: 6;\n\tposition: absolute;\n}\n\n.xterm-screen .xterm-decoration-container .xterm-decoration.xterm-decoration-top-layer {\n\tz-index: 7;\n}\n\n.xterm-decoration-overview-ruler {\n z-index: 8;\n position: absolute;\n top: 0;\n right: 0;\n pointer-events: none;\n}\n\n.xterm-decoration-top {\n z-index: 2;\n position: relative;\n}\n\n\n\n/* Derived from vs/base/browser/ui/scrollbar/media/scrollbar.css */\n\n/* xterm.js customization: Override xterm's cursor style */\n.xterm .xterm-scrollable-element > .scrollbar {\n cursor: default;\n}\n\n/* Arrows */\n.xterm .xterm-scrollable-element > .scrollbar > .scra {\n\tcursor: pointer;\n\tfont-size: 11px !important;\n}\n\n.xterm .xterm-scrollable-element > .visible {\n\topacity: 1;\n\n\t/* Background rule added for IE9 - to allow clicks on dom node */\n\tbackground:rgba(0,0,0,0);\n\n\ttransition: opacity 100ms linear;\n\t/* In front of peek view */\n\tz-index: 11;\n}\n.xterm .xterm-scrollable-element > .invisible {\n\topacity: 0;\n\tpointer-events: none;\n}\n.xterm .xterm-scrollable-element > .invisible.fade {\n\ttransition: opacity 800ms linear;\n}\n\n/* Scrollable Content Inset Shadow */\n.xterm .xterm-scrollable-element > .shadow {\n\tposition: absolute;\n\tdisplay: none;\n}\n.xterm .xterm-scrollable-element > .shadow.top {\n\tdisplay: block;\n\ttop: 0;\n\tleft: 3px;\n\theight: 3px;\n\twidth: 100%;\n\tbox-shadow: var(--vscode-scrollbar-shadow, #000) 0 6px 6px -6px inset;\n}\n.xterm .xterm-scrollable-element > .shadow.left {\n\tdisplay: block;\n\ttop: 3px;\n\tleft: 0;\n\theight: 100%;\n\twidth: 3px;\n\tbox-shadow: var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset;\n}\n.xterm .xterm-scrollable-element > .shadow.top-left-corner {\n\tdisplay: block;\n\ttop: 0;\n\tleft: 0;\n\theight: 3px;\n\twidth: 3px;\n}\n.xterm .xterm-scrollable-element > .shadow.top.left {\n\tbox-shadow: var(--vscode-scrollbar-shadow, #000) 6px 0 6px -6px inset;\n}\n"
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Settings reader of the dsh-ssh browser half.
|
|
3
|
+
*
|
|
4
|
+
* The plugin's browser half owns one preference: the xterm `fontFamily` the
|
|
5
|
+
* panel applies to its terminals. Under the 0.1.7 settings model that field
|
|
6
|
+
* belongs to the plugin's own profile entry (its Cordis Config schema), and
|
|
7
|
+
* the browser surface addresses settings as one form per profile entry id — so
|
|
8
|
+
* the entry has to be resolved before its section can be read:
|
|
9
|
+
*
|
|
10
|
+
* - the family binder (`ctx.get('webUiSettings')`, published while
|
|
11
|
+
* dsh-web-settings is loaded) resolves the dsh-ssh namespace to its entry id
|
|
12
|
+
* through the Host bridge and hands back the native shared form;
|
|
13
|
+
* - without that group the shared configuration forms serve the form directly,
|
|
14
|
+
* and the entry is recognised by the one field this plugin's own schema
|
|
15
|
+
* declares: the entry whose resolved section carries `terminalFontFamily`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { Context as ClientContext } from '@deepseek-ai/cordis'
|
|
19
|
+
import type { ConfigForm, ConfigFormSnapshot, ConfigForms } from '@deepseek-ai/dsh-client-ui-settings/client'
|
|
20
|
+
|
|
21
|
+
/** Domain-owned description of one settings namespace a family surface binds. */
|
|
22
|
+
export interface ConfigFormSpec<T> {
|
|
23
|
+
/** Settings namespace the surface edits. */
|
|
24
|
+
namespace: string
|
|
25
|
+
/**
|
|
26
|
+
* Narrow one wire section; undefined keeps the section the Host resolved.
|
|
27
|
+
* The shared form already validates against the namespace's serialized
|
|
28
|
+
* schema, so a decoder exists only to narrow beyond it.
|
|
29
|
+
*/
|
|
30
|
+
decode?: (section: unknown) => T | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
declare module '@deepseek-ai/cordis' {
|
|
34
|
+
interface Context {
|
|
35
|
+
/**
|
|
36
|
+
* Optional family settings binder provided by dsh-web-settings; absent when
|
|
37
|
+
* that group plugin is not installed, so callers fall back to the shared
|
|
38
|
+
* configuration forms service (`ctx.configForms`).
|
|
39
|
+
*/
|
|
40
|
+
webUiSettings?: { bind<S>(spec: ConfigFormSpec<S>): ConfigForm<S> }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The read face this plugin needs from its own settings entry. */
|
|
45
|
+
export interface SettingsReader<T> {
|
|
46
|
+
/** @returns the current snapshot (stable reference until the next change). */
|
|
47
|
+
getSnapshot(): ConfigFormSnapshot<T>
|
|
48
|
+
/** Observe snapshot replacements. */
|
|
49
|
+
subscribe(listener: () => void): () => void
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A reader plus the release of every subscription it installed. */
|
|
53
|
+
export interface SettingsBinding<T> extends SettingsReader<T> {
|
|
54
|
+
/** Release the reader's subscriptions; later calls are no-ops. */
|
|
55
|
+
dispose(): void
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The snapshot a reader reports before the Host has answered with this
|
|
60
|
+
* plugin's entry: nothing is readable yet, and no write is attempted.
|
|
61
|
+
*/
|
|
62
|
+
const PENDING_SNAPSHOT: ConfigFormSnapshot<never> = {
|
|
63
|
+
status: 'loading',
|
|
64
|
+
value: undefined,
|
|
65
|
+
base: undefined,
|
|
66
|
+
user: undefined,
|
|
67
|
+
revision: undefined,
|
|
68
|
+
writable: false,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Whether one Host namespace section carries a field this plugin's schema declares. */
|
|
72
|
+
function carriesField(section: unknown, field: string): boolean {
|
|
73
|
+
return typeof section === 'object' && section !== null && Object.hasOwn(section, field)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Reader over the shared configuration forms service.
|
|
78
|
+
*
|
|
79
|
+
* The 0.1.7 surface carries no package identity, so this plugin recognises its
|
|
80
|
+
* own entry by the field its Config schema declares. The entry list arrives
|
|
81
|
+
* asynchronously with the shared describe mirror, and an id the Host has not
|
|
82
|
+
* answered with yet is not evidence of absence: the reader re-resolves on every
|
|
83
|
+
* mirror change and reports `loading` until it holds a form.
|
|
84
|
+
*/
|
|
85
|
+
class SharedFormsReader<T> implements SettingsReader<T> {
|
|
86
|
+
private readonly listeners = new Set<() => void>()
|
|
87
|
+
private readonly unsubscribeMirror: () => void
|
|
88
|
+
private unsubscribeForm: (() => void) | undefined
|
|
89
|
+
private form: ConfigForm<T> | undefined
|
|
90
|
+
private snapshot: ConfigFormSnapshot<T> = PENDING_SNAPSHOT as ConfigFormSnapshot<T>
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @param forms - the shared configuration forms service (`ctx.configForms`).
|
|
94
|
+
* @param field - the field this plugin's own schema declares, which is what identifies its entry.
|
|
95
|
+
*/
|
|
96
|
+
constructor(private readonly forms: ConfigForms, private readonly field: string) {
|
|
97
|
+
this.unsubscribeMirror = forms.describe().subscribe(() => {
|
|
98
|
+
this.bind()
|
|
99
|
+
this.publish()
|
|
100
|
+
})
|
|
101
|
+
this.bind()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getSnapshot(): ConfigFormSnapshot<T> {
|
|
105
|
+
return this.snapshot
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
subscribe(listener: () => void): () => void {
|
|
109
|
+
this.listeners.add(listener)
|
|
110
|
+
return () => {
|
|
111
|
+
this.listeners.delete(listener)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Release the mirror and form subscriptions and drop every listener. */
|
|
116
|
+
dispose(): void {
|
|
117
|
+
this.unsubscribeMirror()
|
|
118
|
+
this.unsubscribeForm?.()
|
|
119
|
+
this.unsubscribeForm = undefined
|
|
120
|
+
this.listeners.clear()
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Bind the entry form once the shared mirror names this plugin's entry. */
|
|
124
|
+
private bind(): void {
|
|
125
|
+
if (this.form !== undefined) return
|
|
126
|
+
const namespaces = this.forms.describe().getSnapshot().view?.namespaces
|
|
127
|
+
const entry = namespaces?.find(candidate => carriesField(candidate.value, this.field))
|
|
128
|
+
if (entry === undefined) return
|
|
129
|
+
const form = this.forms.get<T>(entry.ns)
|
|
130
|
+
// A service that answers no form for its own served entry leaves the reader
|
|
131
|
+
// unbound; it retries on the next mirror answer rather than failing a boot.
|
|
132
|
+
if (form === undefined) return
|
|
133
|
+
this.form = form
|
|
134
|
+
this.snapshot = form.getSnapshot()
|
|
135
|
+
this.unsubscribeForm = form.subscribe(() => {
|
|
136
|
+
this.snapshot = form.getSnapshot()
|
|
137
|
+
this.publish()
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private publish(): void {
|
|
142
|
+
for (const listener of this.listeners) listener()
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Bind this plugin's settings reader.
|
|
148
|
+
*
|
|
149
|
+
* `configForms` is deliberately read with `ctx.get` rather than declared in
|
|
150
|
+
* the client `inject` list. Declaring an optional service as a dependency
|
|
151
|
+
* makes the whole client fiber wait for it forever when the family does not
|
|
152
|
+
* ship it, and the panel then never mounts at all — a missing settings
|
|
153
|
+
* service would cost the entire panel, which is far too high a price for one
|
|
154
|
+
* terminal-font preference. Both paths below are optional by design: with
|
|
155
|
+
* neither present the reader reports `undefined`, and the panel uses its CSS
|
|
156
|
+
* font chain.
|
|
157
|
+
*
|
|
158
|
+
* @param ctx - client context carrying the family binder and/or the shared forms service.
|
|
159
|
+
* @param namespace - the family settings namespace this plugin owns.
|
|
160
|
+
* @param field - the field this plugin's own schema declares, used to recognise its entry when the family binder is absent.
|
|
161
|
+
* @returns the reader and the release of its subscriptions.
|
|
162
|
+
*/
|
|
163
|
+
export function bindSettingsReader<T>(ctx: ClientContext, namespace: string, field: string): SettingsBinding<T> {
|
|
164
|
+
const family = ctx.get('webUiSettings')
|
|
165
|
+
if (family !== undefined) {
|
|
166
|
+
const form = family.bind<T>({ namespace })
|
|
167
|
+
return {
|
|
168
|
+
getSnapshot: () => form.getSnapshot(),
|
|
169
|
+
subscribe: listener => form.subscribe(listener),
|
|
170
|
+
dispose: () => {},
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const forms = ctx.get('configForms')
|
|
174
|
+
// No forms surface at all: report "nothing to read" rather than throwing or
|
|
175
|
+
// waiting. The preference is cosmetic; its absence must not gate the panel.
|
|
176
|
+
if (forms === undefined) {
|
|
177
|
+
return {
|
|
178
|
+
getSnapshot: () => PENDING_SNAPSHOT as ConfigFormSnapshot<T>,
|
|
179
|
+
subscribe: () => () => {},
|
|
180
|
+
dispose: () => {},
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return new SharedFormsReader<T>(forms, field)
|
|
184
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deployment orchestration: roll a change out to a fleet in batches, check
|
|
3
|
+
* each batch, and roll back the batch that failed.
|
|
4
|
+
*
|
|
5
|
+
* The design constraint that shapes everything here: a rollout that cannot
|
|
6
|
+
* be undone is a deployment that only works on the happy path. So the
|
|
7
|
+
* orchestrator refuses to call a deployment successful unless it can also
|
|
8
|
+
* describe how to undo it, and it reports "cannot roll back" honestly when
|
|
9
|
+
* there is no previous version to roll back to — rather than pretending the
|
|
10
|
+
* batch is fine.
|
|
11
|
+
*
|
|
12
|
+
* A plan is:
|
|
13
|
+
* - a set of target hosts (selected like every other fleet operation)
|
|
14
|
+
* - the steps to run on each host (upload / exec)
|
|
15
|
+
* - a health probe run after the steps
|
|
16
|
+
* - a rollback command, run when the health probe fails
|
|
17
|
+
*
|
|
18
|
+
* Rollback is per BATCH, not per host: a batch that came up unhealthy is
|
|
19
|
+
* rolled back as a unit, because half a batch rolled back and half not is
|
|
20
|
+
* the worst possible state to hand an operator.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { SshEngine } from '../engine.ts'
|
|
24
|
+
import type { ExecResult } from '../protocol.ts'
|
|
25
|
+
|
|
26
|
+
/** One step of a deployment. */
|
|
27
|
+
export interface DeployStep {
|
|
28
|
+
/** A short name for the step (shown in the report). */
|
|
29
|
+
name: string
|
|
30
|
+
/** The command to run on the remote host. */
|
|
31
|
+
command: string
|
|
32
|
+
/**
|
|
33
|
+
* The command that undoes this step.
|
|
34
|
+
*
|
|
35
|
+
* Without it the step cannot be rolled back, and the orchestrator says so
|
|
36
|
+
* instead of silently leaving the host in the broken state.
|
|
37
|
+
*/
|
|
38
|
+
rollbackCommand?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A health probe run after the steps. */
|
|
42
|
+
export interface HealthCheck {
|
|
43
|
+
/** The command to run. */
|
|
44
|
+
command: string
|
|
45
|
+
/** The expected exit code (default 0). */
|
|
46
|
+
expectExitCode?: number
|
|
47
|
+
/** How long to wait for the service to come up before probing (ms). */
|
|
48
|
+
settleMs?: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A deployment plan. */
|
|
52
|
+
export interface DeployPlan {
|
|
53
|
+
/** A stable name for the plan. */
|
|
54
|
+
id: string
|
|
55
|
+
/** A one-line description. */
|
|
56
|
+
description?: string
|
|
57
|
+
/** The steps, in order. */
|
|
58
|
+
steps: DeployStep[]
|
|
59
|
+
/** The probe run after the steps. */
|
|
60
|
+
healthCheck?: HealthCheck
|
|
61
|
+
/** How many hosts to deploy to at once. */
|
|
62
|
+
batchSize?: number
|
|
63
|
+
/** Per-command timeout (ms). */
|
|
64
|
+
timeoutMs?: number
|
|
65
|
+
/** Stop the rollout at the first failing batch. */
|
|
66
|
+
stopOnFailure?: boolean
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The outcome for one host. */
|
|
70
|
+
export interface DeployHostResult {
|
|
71
|
+
alias: string
|
|
72
|
+
ok: boolean
|
|
73
|
+
/** The exit code of the last step. */
|
|
74
|
+
exitCode?: number | null
|
|
75
|
+
/** Whether the health probe passed. */
|
|
76
|
+
healthy?: boolean
|
|
77
|
+
/** Whether a rollback was attempted. */
|
|
78
|
+
rolledBack?: boolean
|
|
79
|
+
/** Whether the rollback succeeded. */
|
|
80
|
+
rollbackOk?: boolean
|
|
81
|
+
/** The error message, when the host failed. */
|
|
82
|
+
error?: string
|
|
83
|
+
/** The health probe output, for the operator to read. */
|
|
84
|
+
healthOutput?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The outcome for one batch. */
|
|
88
|
+
export interface DeployBatchResult {
|
|
89
|
+
/** The batch index, starting at 1. */
|
|
90
|
+
index: number
|
|
91
|
+
/** The hosts in this batch. */
|
|
92
|
+
aliases: string[]
|
|
93
|
+
/** The per-host results. */
|
|
94
|
+
results: DeployHostResult[]
|
|
95
|
+
/** Whether every host in the batch came up healthy. */
|
|
96
|
+
ok: boolean
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The outcome of a whole deployment. */
|
|
100
|
+
export interface DeployResult {
|
|
101
|
+
planId: string
|
|
102
|
+
ok: boolean
|
|
103
|
+
batches: DeployBatchResult[]
|
|
104
|
+
/** The hosts that were deployed to. */
|
|
105
|
+
targets: string[]
|
|
106
|
+
/** The hosts that were skipped because an earlier batch failed. */
|
|
107
|
+
skipped: string[]
|
|
108
|
+
/** How long the whole rollout took. */
|
|
109
|
+
durationMs: number
|
|
110
|
+
/** Why the deployment failed, when it did. */
|
|
111
|
+
error?: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The default batch size when a plan does not set one. */
|
|
115
|
+
export const DEFAULT_BATCH_SIZE = 5
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Split a list into batches.
|
|
119
|
+
*
|
|
120
|
+
* @param items - the items to split.
|
|
121
|
+
* @param size - the batch size.
|
|
122
|
+
* @returns the batches.
|
|
123
|
+
*/
|
|
124
|
+
export function chunk<T>(items: T[], size: number): T[][] {
|
|
125
|
+
const width = Math.max(1, size)
|
|
126
|
+
const batches: T[][] = []
|
|
127
|
+
for (let index = 0; index < items.length; index += width) {
|
|
128
|
+
batches.push(items.slice(index, index + width))
|
|
129
|
+
}
|
|
130
|
+
return batches
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Run one deployment plan against the given hosts.
|
|
135
|
+
*
|
|
136
|
+
* @param engine - the SSH engine.
|
|
137
|
+
* @param plan - the plan.
|
|
138
|
+
* @param targets - the host aliases, in rollout order.
|
|
139
|
+
* @returns the deployment outcome.
|
|
140
|
+
*/
|
|
141
|
+
export async function deploy(
|
|
142
|
+
engine: SshEngine,
|
|
143
|
+
plan: DeployPlan,
|
|
144
|
+
targets: string[],
|
|
145
|
+
): Promise<DeployResult> {
|
|
146
|
+
const startedAt = Date.now()
|
|
147
|
+
const batchSize = plan.batchSize ?? DEFAULT_BATCH_SIZE
|
|
148
|
+
const timeoutMs = plan.timeoutMs ?? 120_000
|
|
149
|
+
const batches = chunk(targets, batchSize)
|
|
150
|
+
const results: DeployBatchResult[] = []
|
|
151
|
+
const skipped: string[] = []
|
|
152
|
+
let failure: string | undefined
|
|
153
|
+
|
|
154
|
+
for (let index = 0; index < batches.length; index += 1) {
|
|
155
|
+
const aliases = batches[index] as string[]
|
|
156
|
+
if (failure !== undefined && plan.stopOnFailure !== false) {
|
|
157
|
+
// The rollout was stopped: the remaining hosts are reported as
|
|
158
|
+
// skipped, never as deployed.
|
|
159
|
+
skipped.push(...aliases)
|
|
160
|
+
continue
|
|
161
|
+
}
|
|
162
|
+
const hostResults = await Promise.all(
|
|
163
|
+
aliases.map(alias => deployHost(engine, plan, alias, timeoutMs)),
|
|
164
|
+
)
|
|
165
|
+
const ok = hostResults.every(result => result.ok)
|
|
166
|
+
results.push({ index: index + 1, aliases, results: hostResults, ok })
|
|
167
|
+
if (!ok) {
|
|
168
|
+
failure = `batch ${index + 1} failed: ${hostResults.filter(result => !result.ok).map(result => result.alias).join(', ')}`
|
|
169
|
+
if (plan.stopOnFailure === false) {
|
|
170
|
+
// The operator asked to continue past a failing batch: the next
|
|
171
|
+
// batch still runs, and the deployment as a whole still reports
|
|
172
|
+
// failure.
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
planId: plan.id,
|
|
180
|
+
ok: failure === undefined,
|
|
181
|
+
batches: results,
|
|
182
|
+
targets: [...targets],
|
|
183
|
+
skipped,
|
|
184
|
+
durationMs: Date.now() - startedAt,
|
|
185
|
+
...(failure !== undefined ? { error: failure } : {}),
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Deploy to one host: run the steps, then the health probe, then roll back
|
|
191
|
+
* if the probe failed.
|
|
192
|
+
*
|
|
193
|
+
* @param engine - the SSH engine.
|
|
194
|
+
* @param plan - the plan.
|
|
195
|
+
* @param alias - the host alias.
|
|
196
|
+
* @param timeoutMs - the per-command timeout.
|
|
197
|
+
* @returns the per-host outcome.
|
|
198
|
+
*/
|
|
199
|
+
export async function deployHost(
|
|
200
|
+
engine: SshEngine,
|
|
201
|
+
plan: DeployPlan,
|
|
202
|
+
alias: string,
|
|
203
|
+
timeoutMs: number,
|
|
204
|
+
): Promise<DeployHostResult> {
|
|
205
|
+
let exitCode: number | null = null
|
|
206
|
+
for (const step of plan.steps) {
|
|
207
|
+
const result: ExecResult = await engine.exec(alias, step.command, timeoutMs)
|
|
208
|
+
exitCode = result.exitCode
|
|
209
|
+
if (!result.success) {
|
|
210
|
+
// A step failed: try to roll this host back before giving up.
|
|
211
|
+
const rollback = await rollbackHost(engine, plan, alias, timeoutMs)
|
|
212
|
+
return {
|
|
213
|
+
alias,
|
|
214
|
+
ok: false,
|
|
215
|
+
exitCode,
|
|
216
|
+
rolledBack: rollback.attempted,
|
|
217
|
+
rollbackOk: rollback.ok,
|
|
218
|
+
error: `step '${step.name}' failed: ${result.error ?? `exit code ${result.exitCode ?? 'null'}`}`,
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (plan.healthCheck === undefined) {
|
|
224
|
+
// No probe: the steps succeeded, so the host is considered deployed.
|
|
225
|
+
return { alias, ok: true, exitCode, healthy: true }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const probe = plan.healthCheck
|
|
229
|
+
if (probe.settleMs !== undefined && probe.settleMs > 0) {
|
|
230
|
+
await sleep(probe.settleMs)
|
|
231
|
+
}
|
|
232
|
+
const health = await engine.exec(alias, probe.command, timeoutMs)
|
|
233
|
+
const expected = probe.expectExitCode ?? 0
|
|
234
|
+
if (health.success && health.exitCode === expected) {
|
|
235
|
+
return { alias, ok: true, exitCode, healthy: true, healthOutput: health.stdout.trim().slice(0, 2000) }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// The health probe failed: roll the host back.
|
|
239
|
+
const rollback = await rollbackHost(engine, plan, alias, timeoutMs)
|
|
240
|
+
return {
|
|
241
|
+
alias,
|
|
242
|
+
ok: false,
|
|
243
|
+
exitCode,
|
|
244
|
+
healthy: false,
|
|
245
|
+
rolledBack: rollback.attempted,
|
|
246
|
+
rollbackOk: rollback.ok,
|
|
247
|
+
error: `health check failed: ${health.error ?? `exit code ${health.exitCode ?? 'null'}`}`,
|
|
248
|
+
healthOutput: health.stdout.trim().slice(0, 2000),
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Roll one host back to the previous version.
|
|
254
|
+
*
|
|
255
|
+
* A rollback is only possible when the plan declares a rollback command for
|
|
256
|
+
* every step. When it does not, the orchestrator says so plainly: the
|
|
257
|
+
* host is left in the failed state and the operator has to intervene.
|
|
258
|
+
*
|
|
259
|
+
* @param engine - the SSH engine.
|
|
260
|
+
* @param plan - the plan.
|
|
261
|
+
* @param alias - the host alias.
|
|
262
|
+
* @param timeoutMs - the per-command timeout.
|
|
263
|
+
* @returns whether a rollback was attempted and whether it worked.
|
|
264
|
+
*/
|
|
265
|
+
export async function rollbackHost(
|
|
266
|
+
engine: SshEngine,
|
|
267
|
+
plan: DeployPlan,
|
|
268
|
+
alias: string,
|
|
269
|
+
timeoutMs: number,
|
|
270
|
+
): Promise<{ attempted: boolean; ok: boolean }> {
|
|
271
|
+
const rollbackSteps = plan.steps
|
|
272
|
+
.map(step => step.rollbackCommand)
|
|
273
|
+
.filter((command): command is string => command !== undefined && command !== '')
|
|
274
|
+
if (rollbackSteps.length === 0) {
|
|
275
|
+
// Nothing to undo — and saying so is the honest outcome.
|
|
276
|
+
return { attempted: false, ok: false }
|
|
277
|
+
}
|
|
278
|
+
if (rollbackSteps.length !== plan.steps.length) {
|
|
279
|
+
// Some steps have no undo: a partial rollback would leave the host in
|
|
280
|
+
// a state nobody designed. Do not attempt it.
|
|
281
|
+
return { attempted: false, ok: false }
|
|
282
|
+
}
|
|
283
|
+
// Undo in reverse order: the last step is undone first.
|
|
284
|
+
for (const command of [...rollbackSteps].reverse()) {
|
|
285
|
+
const result = await engine.exec(alias, command, timeoutMs)
|
|
286
|
+
if (!result.success) {
|
|
287
|
+
return { attempted: true, ok: false }
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return { attempted: true, ok: true }
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** A promise that resolves after the given delay. */
|
|
294
|
+
function sleep(ms: number): Promise<void> {
|
|
295
|
+
return new Promise(resolve => { setTimeout(resolve, ms) })
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* A human-readable rendering of a deployment outcome.
|
|
300
|
+
*
|
|
301
|
+
* @param result - the deployment outcome.
|
|
302
|
+
* @returns the report text.
|
|
303
|
+
*/
|
|
304
|
+
export function renderDeploy(result: DeployResult): string {
|
|
305
|
+
const lines: string[] = [
|
|
306
|
+
`部署 ${result.planId}:${result.ok ? '成功' : '失败'}(${result.targets.length} 台,耗时 ${result.durationMs}ms)`,
|
|
307
|
+
]
|
|
308
|
+
if (result.error !== undefined) lines.push(`失败原因:${result.error}`)
|
|
309
|
+
for (const batch of result.batches) {
|
|
310
|
+
lines.push(`批次 ${batch.index}:${batch.aliases.join(', ')} — ${batch.ok ? '正常' : '异常'}`)
|
|
311
|
+
for (const host of batch.results) {
|
|
312
|
+
if (host.ok) {
|
|
313
|
+
lines.push(` ${host.alias}:部署成功`)
|
|
314
|
+
} else {
|
|
315
|
+
const rollback = host.rolledBack === true
|
|
316
|
+
? (host.rollbackOk === true ? '已回滚' : '回滚失败')
|
|
317
|
+
: '无法回滚(计划未提供回滚命令)'
|
|
318
|
+
lines.push(` ${host.alias}:${host.error ?? '失败'} — ${rollback}`)
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (result.skipped.length > 0) {
|
|
323
|
+
lines.push(`未部署(因前序批次失败而跳过):${result.skipped.join(', ')}`)
|
|
324
|
+
}
|
|
325
|
+
return lines.join('\n')
|
|
326
|
+
}
|