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,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosts tab: the host table with search (debounced through listHosts),
|
|
3
|
+
* add/edit/delete/test actions, ~/.ssh/config import, and a connect action
|
|
4
|
+
* that hands the alias to the terminal tab via onConnect.
|
|
5
|
+
*/
|
|
6
|
+
import { useCallback, useEffect, useRef, useState, type ReactNode } from 'react'
|
|
7
|
+
import type { SshApi } from '../api.ts'
|
|
8
|
+
import type { ImportSkipBlock, ImportSkipReason, LocalConfigHost, SshHostSummary, TestResult } from '../../protocol.ts'
|
|
9
|
+
import type { SshKey } from '../locales.ts'
|
|
10
|
+
import { errorMessage, tt } from './helpers.ts'
|
|
11
|
+
import { HostFormDialog } from './HostFormDialog.tsx'
|
|
12
|
+
import css from './panel.module.css'
|
|
13
|
+
|
|
14
|
+
/** Hosts tab props. */
|
|
15
|
+
export interface HostsTabProps {
|
|
16
|
+
api: SshApi
|
|
17
|
+
/** Connect the given alias in the terminal tab. */
|
|
18
|
+
onConnect: (alias: string) => void
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The host-form dialog invocation. */
|
|
22
|
+
type DialogState = { mode: 'create' } | { mode: 'edit'; host: SshHostSummary }
|
|
23
|
+
|
|
24
|
+
/** Skipped-block rows rendered before the "and N more" summary. */
|
|
25
|
+
const IMPORT_SKIP_LIMIT = 8
|
|
26
|
+
|
|
27
|
+
/** Locale key per import skip reason (no dynamic key concatenation). */
|
|
28
|
+
const IMPORT_REASON_KEY: Record<ImportSkipReason, SshKey> = {
|
|
29
|
+
wildcard: 'import.reason.wildcard',
|
|
30
|
+
existing: 'import.reason.existing',
|
|
31
|
+
match: 'import.reason.match',
|
|
32
|
+
invalid: 'import.reason.invalid',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Host list grouping modes (#379). */
|
|
36
|
+
export type HostGroupBy = 'none' | 'environment' | 'tags'
|
|
37
|
+
|
|
38
|
+
/** One collapsible group section of the grouped host list. */
|
|
39
|
+
export interface HostGroup {
|
|
40
|
+
/** Group key: the environment name, one tag, or '' for the ungrouped bucket. */
|
|
41
|
+
key: string
|
|
42
|
+
hosts: SshHostSummary[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Bucket hosts into collapsible groups (#379). Grouping by tags places a
|
|
47
|
+
* multi-tag host in every one of its tag groups (folder view); hosts without
|
|
48
|
+
* the grouping key land in the '' bucket, which always sorts last. Groups
|
|
49
|
+
* sort alphabetically; host order inside a group follows the API listing.
|
|
50
|
+
*/
|
|
51
|
+
export function groupHosts(hosts: SshHostSummary[], groupBy: HostGroupBy): HostGroup[] {
|
|
52
|
+
if (groupBy === 'none') return [{ key: '', hosts }]
|
|
53
|
+
const buckets = new Map<string, SshHostSummary[]>()
|
|
54
|
+
const push = (key: string, host: SshHostSummary): void => {
|
|
55
|
+
const bucket = buckets.get(key)
|
|
56
|
+
if (bucket === undefined) buckets.set(key, [host])
|
|
57
|
+
else bucket.push(host)
|
|
58
|
+
}
|
|
59
|
+
for (const host of hosts) {
|
|
60
|
+
if (groupBy === 'environment') {
|
|
61
|
+
push(host.environment ?? '', host)
|
|
62
|
+
} else if (host.tags.length === 0) {
|
|
63
|
+
push('', host)
|
|
64
|
+
} else {
|
|
65
|
+
for (const tag of host.tags) push(tag, host)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return [...buckets.entries()]
|
|
69
|
+
.sort(([a], [b]) => (a === '' ? 1 : b === '' ? -1 : a.localeCompare(b)))
|
|
70
|
+
.map(([key, group]) => ({ key, hosts: group }))
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The hosts table plus its toolbar and dialogs. */
|
|
74
|
+
export function HostsTab({ api, onConnect }: HostsTabProps) {
|
|
75
|
+
const [hosts, setHosts] = useState<SshHostSummary[] | null>(null)
|
|
76
|
+
const [error, setError] = useState<string | null>(null)
|
|
77
|
+
const [search, setSearch] = useState('')
|
|
78
|
+
const [testingAlias, setTestingAlias] = useState<string | null>(null)
|
|
79
|
+
const [testResults, setTestResults] = useState<Record<string, TestResult>>({})
|
|
80
|
+
const [importing, setImporting] = useState(false)
|
|
81
|
+
const [notice, setNotice] = useState<string | null>(null)
|
|
82
|
+
/** Blocks the last import skipped, with the reason each one was left out. */
|
|
83
|
+
const [importSkips, setImportSkips] = useState<ImportSkipBlock[]>([])
|
|
84
|
+
/** ssh_config hosts that are not in the store yet. */
|
|
85
|
+
const [localConfig, setLocalConfig] = useState<LocalConfigHost[]>([])
|
|
86
|
+
const [dialog, setDialog] = useState<DialogState | null>(null)
|
|
87
|
+
const [groupBy, setGroupBy] = useState<HostGroupBy>('none')
|
|
88
|
+
const [collapsed, setCollapsed] = useState<Record<string, boolean>>({})
|
|
89
|
+
const [testingGroup, setTestingGroup] = useState<string | null>(null)
|
|
90
|
+
const seqRef = useRef(0)
|
|
91
|
+
// Unmount guard for the async load below: the seq check only orders
|
|
92
|
+
// overlapping loads, it does not stop a late resolution/rejection landing
|
|
93
|
+
// after the tab unmounted — a setState there races the test-environment
|
|
94
|
+
// teardown (window is not defined; observed as a main-CI flake). The
|
|
95
|
+
// sibling tabs (terminal / transfer / tunnels) already guard with a
|
|
96
|
+
// disposed flag.
|
|
97
|
+
const mountedRef = useRef(true)
|
|
98
|
+
useEffect(() => () => { mountedRef.current = false }, [])
|
|
99
|
+
|
|
100
|
+
const load = useCallback(async (query?: string): Promise<void> => {
|
|
101
|
+
const seq = ++seqRef.current
|
|
102
|
+
try {
|
|
103
|
+
const list = await api.listHosts(query)
|
|
104
|
+
if (!mountedRef.current || seq !== seqRef.current) return
|
|
105
|
+
setHosts(list)
|
|
106
|
+
setError(null)
|
|
107
|
+
} catch (cause) {
|
|
108
|
+
if (!mountedRef.current || seq !== seqRef.current) return
|
|
109
|
+
setError(errorMessage(cause))
|
|
110
|
+
}
|
|
111
|
+
}, [api])
|
|
112
|
+
|
|
113
|
+
useEffect(() => { void load() }, [load])
|
|
114
|
+
|
|
115
|
+
// What the local ssh config offers but the store lacks. Without this the
|
|
116
|
+
// panel looks like it never read ~/.ssh/config at all: the hosts are simply
|
|
117
|
+
// absent and the only clue is a button.
|
|
118
|
+
const loadLocalConfig = useCallback(async (): Promise<void> => {
|
|
119
|
+
try {
|
|
120
|
+
const list = await api.listLocalConfigHosts()
|
|
121
|
+
if (!mountedRef.current) return
|
|
122
|
+
setLocalConfig(list.filter(host => host.skippedReason === undefined))
|
|
123
|
+
} catch {
|
|
124
|
+
// A missing preview must never break the host list.
|
|
125
|
+
if (mountedRef.current) setLocalConfig([])
|
|
126
|
+
}
|
|
127
|
+
}, [api])
|
|
128
|
+
|
|
129
|
+
useEffect(() => { void loadLocalConfig() }, [loadLocalConfig])
|
|
130
|
+
|
|
131
|
+
// Debounced search: every keystroke re-filters through the API.
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
const timer = setTimeout(() => {
|
|
134
|
+
const query = search.trim()
|
|
135
|
+
void load(query === '' ? undefined : query)
|
|
136
|
+
}, 300)
|
|
137
|
+
return () => { clearTimeout(timer) }
|
|
138
|
+
}, [search, load])
|
|
139
|
+
|
|
140
|
+
// Every async setState path guards with mountedRef, not just load(): a
|
|
141
|
+
// promise settling after unmount would setState against the torn-down
|
|
142
|
+
// environment (window is not defined, main-CI flake).
|
|
143
|
+
const runTest = async (alias: string): Promise<void> => {
|
|
144
|
+
if (!mountedRef.current) return
|
|
145
|
+
setTestingAlias(alias)
|
|
146
|
+
try {
|
|
147
|
+
const result = await api.testHost(alias)
|
|
148
|
+
if (!mountedRef.current) return
|
|
149
|
+
setTestResults(prev => ({ ...prev, [alias]: result }))
|
|
150
|
+
} catch (cause) {
|
|
151
|
+
if (!mountedRef.current) return
|
|
152
|
+
setTestResults(prev => ({ ...prev, [alias]: { ok: false, error: errorMessage(cause) } }))
|
|
153
|
+
} finally {
|
|
154
|
+
if (mountedRef.current) setTestingAlias(null)
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const deleteHost = async (alias: string): Promise<void> => {
|
|
159
|
+
if (!window.confirm(tt('hosts.deleteConfirm', { alias }))) return
|
|
160
|
+
try {
|
|
161
|
+
await api.deleteHost(alias)
|
|
162
|
+
if (!mountedRef.current) return
|
|
163
|
+
void load()
|
|
164
|
+
} catch (cause) {
|
|
165
|
+
if (!mountedRef.current) return
|
|
166
|
+
setError(errorMessage(cause))
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Group-header batch action (#379): test every host in the group.
|
|
171
|
+
const testGroup = async (group: HostGroup): Promise<void> => {
|
|
172
|
+
if (!mountedRef.current) return
|
|
173
|
+
setTestingGroup(group.key)
|
|
174
|
+
try {
|
|
175
|
+
await Promise.all(group.hosts.map(host => runTest(host.alias)))
|
|
176
|
+
} finally {
|
|
177
|
+
if (mountedRef.current) setTestingGroup(null)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const importConfig = async (): Promise<void> => {
|
|
182
|
+
if (!mountedRef.current) return
|
|
183
|
+
setImporting(true)
|
|
184
|
+
try {
|
|
185
|
+
const result = await api.importSshConfig()
|
|
186
|
+
if (!mountedRef.current) return
|
|
187
|
+
setNotice(tt('hosts.imported', {
|
|
188
|
+
parsed: result.parsed,
|
|
189
|
+
added: result.added,
|
|
190
|
+
updated: result.updated,
|
|
191
|
+
skipped: result.skipped,
|
|
192
|
+
}))
|
|
193
|
+
setImportSkips(result.skippedBlocks)
|
|
194
|
+
void load()
|
|
195
|
+
void loadLocalConfig()
|
|
196
|
+
} catch (cause) {
|
|
197
|
+
if (!mountedRef.current) return
|
|
198
|
+
setError(errorMessage(cause))
|
|
199
|
+
} finally {
|
|
200
|
+
if (mountedRef.current) setImporting(false)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// One source of truth for column visibility: the header and every body cell
|
|
205
|
+
// read this, so they cannot disagree and knock the table out of alignment.
|
|
206
|
+
// A column appears only when some host in the currently rendered set has data
|
|
207
|
+
// for it; three permanently blank columns were eating ~40% of the width.
|
|
208
|
+
const visibleColumns = {
|
|
209
|
+
environment: (hosts ?? []).some(host => (host.environment ?? '') !== ''),
|
|
210
|
+
tags: (hosts ?? []).some(host => host.tags.length > 0),
|
|
211
|
+
description: (hosts ?? []).some(host => (host.description ?? '') !== ''),
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const renderHostRow = (host: SshHostSummary): ReactNode => {
|
|
215
|
+
const test = testResults[host.alias]
|
|
216
|
+
const testing = testingAlias === host.alias
|
|
217
|
+
// The rail reports what this panel last observed. A host nobody has tested
|
|
218
|
+
// stays uncoloured: "not checked" and "reachable" are different facts.
|
|
219
|
+
const state = testing ? 'testing' : test === undefined ? undefined : test.ok ? 'ok' : 'fail'
|
|
220
|
+
return (
|
|
221
|
+
<tr key={host.alias} data-state={state}>
|
|
222
|
+
<td>
|
|
223
|
+
{/* alias over user@host:port — the machine as the operator already
|
|
224
|
+
writes it in ~/.ssh/config, in one readable unit. */}
|
|
225
|
+
<div className={css.hostIdentity}>
|
|
226
|
+
<span className={css.hostAlias} title={host.alias}>{host.alias}</span>
|
|
227
|
+
<span className={css.hostEndpoint} title={`${host.user}@${host.host}:${host.port}`}>
|
|
228
|
+
<span>{host.user}@{host.host}:{host.port}</span>
|
|
229
|
+
{host.proxyCommand !== undefined && (
|
|
230
|
+
<span className={css.badge} data-kind="proxy" title={host.proxyCommand}>{tt('hosts.proxyBadge')}</span>
|
|
231
|
+
)}
|
|
232
|
+
{host.forwardAgent === true && (
|
|
233
|
+
<span className={css.badge} data-kind="forward-agent" title={tt('hosts.forwardAgentHint')}>{tt('hosts.forwardAgentBadge')}</span>
|
|
234
|
+
)}
|
|
235
|
+
</span>
|
|
236
|
+
</div>
|
|
237
|
+
</td>
|
|
238
|
+
<td><span className={css.badge} data-kind={host.auth}>{host.auth === 'key' ? tt('form.auth.key') : host.auth === 'password' ? tt('form.auth.password') : host.auth === 'none' ? tt('form.auth.none') : tt('form.auth.agent')}</span></td>
|
|
239
|
+
{visibleColumns.environment && <td className={css.cellMuted}>{host.environment ?? ''}</td>}
|
|
240
|
+
{visibleColumns.tags && (
|
|
241
|
+
<td>
|
|
242
|
+
{host.tags.length === 0 ? null : (
|
|
243
|
+
<span className={css.tagList}>
|
|
244
|
+
{host.tags.map(tag => <span key={tag} className={css.tag}>{tag}</span>)}
|
|
245
|
+
</span>
|
|
246
|
+
)}
|
|
247
|
+
</td>
|
|
248
|
+
)}
|
|
249
|
+
{visibleColumns.description && <td className={css.cellMuted}>{host.description ?? ''}</td>}
|
|
250
|
+
<td>
|
|
251
|
+
<div className={css.actions}>
|
|
252
|
+
{testing && <span className={css.spinner} aria-hidden="true" />}
|
|
253
|
+
{test !== undefined && !testing && (
|
|
254
|
+
<span
|
|
255
|
+
className={css.inlineTest}
|
|
256
|
+
data-status={test.ok ? 'ok' : 'fail'}
|
|
257
|
+
// The chip shows a summary because a full error is a paragraph;
|
|
258
|
+
// the complete text stays reachable on hover.
|
|
259
|
+
title={test.ok ? tt('hosts.testOk', { latency: test.latencyMs ?? 0 }) : test.error ?? ''}
|
|
260
|
+
>
|
|
261
|
+
{test.ok ? tt('hosts.testOkShort', { latency: test.latencyMs ?? 0 }) : tt('hosts.testFailShort')}
|
|
262
|
+
</span>
|
|
263
|
+
)}
|
|
264
|
+
<span className={css.actionLinks}>
|
|
265
|
+
<button type="button" className={css.actionLink} disabled={testing} onClick={() => { void runTest(host.alias) }}>
|
|
266
|
+
{testing ? tt('hosts.testingShort') : tt('hosts.test')}
|
|
267
|
+
</button>
|
|
268
|
+
<button type="button" className={css.actionLink} onClick={() => { setDialog({ mode: 'edit', host }) }}>{tt('hosts.edit')}</button>
|
|
269
|
+
<button type="button" className={css.actionLink} data-danger onClick={() => { void deleteHost(host.alias) }}>{tt('hosts.delete')}</button>
|
|
270
|
+
</span>
|
|
271
|
+
<button type="button" className={css.connectButton} onClick={() => { onConnect(host.alias) }}>{tt('hosts.connected')}</button>
|
|
272
|
+
</div>
|
|
273
|
+
</td>
|
|
274
|
+
</tr>
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const renderHostTable = (rows: SshHostSummary[]): ReactNode => (
|
|
279
|
+
<table className={`${css.table} ${css.hostsTable}`}>
|
|
280
|
+
<thead>
|
|
281
|
+
<tr>
|
|
282
|
+
<th>{tt('hosts.col.host')}</th>
|
|
283
|
+
<th>{tt('hosts.col.auth')}</th>
|
|
284
|
+
{visibleColumns.environment && <th>{tt('hosts.col.environment')}</th>}
|
|
285
|
+
{visibleColumns.tags && <th>{tt('hosts.col.tags')}</th>}
|
|
286
|
+
{visibleColumns.description && <th>{tt('hosts.col.description')}</th>}
|
|
287
|
+
<th>{tt('hosts.col.actions')}</th>
|
|
288
|
+
</tr>
|
|
289
|
+
</thead>
|
|
290
|
+
<tbody>
|
|
291
|
+
{rows.map(renderHostRow)}
|
|
292
|
+
</tbody>
|
|
293
|
+
</table>
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
const groups = hosts === null ? [] : groupHosts(hosts, groupBy)
|
|
297
|
+
|
|
298
|
+
return (
|
|
299
|
+
<div className={css.fillBody}>
|
|
300
|
+
<div className={css.toolbar}>
|
|
301
|
+
<input className={css.search} type="search" aria-label={tt('hosts.search')} placeholder={tt('hosts.search')} value={search} onChange={event => { setSearch(event.target.value) }} />
|
|
302
|
+
<select
|
|
303
|
+
className={css.groupBySelect}
|
|
304
|
+
aria-label={tt('hosts.groupBy.label')}
|
|
305
|
+
value={groupBy}
|
|
306
|
+
onChange={event => { setGroupBy(event.target.value as HostGroupBy) }}
|
|
307
|
+
>
|
|
308
|
+
<option value="none">{tt('hosts.groupBy.none')}</option>
|
|
309
|
+
<option value="environment">{tt('hosts.groupBy.environment')}</option>
|
|
310
|
+
<option value="tags">{tt('hosts.groupBy.tags')}</option>
|
|
311
|
+
</select>
|
|
312
|
+
<div className={css.toolbarSpacer} />
|
|
313
|
+
<button type="button" className={css.primaryButton} onClick={() => { setDialog({ mode: 'create' }) }}>{tt('hosts.add')}</button>
|
|
314
|
+
<button type="button" className={css.ghostButton} disabled={importing} onClick={() => { void importConfig() }}>{importing ? tt('common.loading') : tt('hosts.import')}</button>
|
|
315
|
+
</div>
|
|
316
|
+
{localConfig.length > 0 && (
|
|
317
|
+
<div className={css.banner} data-kind="info">
|
|
318
|
+
{tt('hosts.localConfigPending', { count: localConfig.length })}
|
|
319
|
+
{' '}
|
|
320
|
+
<span className={css.mono}>{localConfig.slice(0, IMPORT_SKIP_LIMIT).map(host => host.alias).join(', ')}</span>
|
|
321
|
+
{localConfig.length > IMPORT_SKIP_LIMIT && (
|
|
322
|
+
<span className={css.cellMuted}>{' '}{tt('import.more', { count: localConfig.length - IMPORT_SKIP_LIMIT })}</span>
|
|
323
|
+
)}
|
|
324
|
+
</div>
|
|
325
|
+
)}
|
|
326
|
+
{notice !== null && <div className={css.banner} data-kind="ok" role="status">{notice}</div>}
|
|
327
|
+
{notice !== null && importSkips.length > 0 && (
|
|
328
|
+
<ul className={css.importSkips}>
|
|
329
|
+
{importSkips.slice(0, IMPORT_SKIP_LIMIT).map(block => (
|
|
330
|
+
<li key={block.name} className={css.importSkipRow}>
|
|
331
|
+
<span className={css.mono}>{block.name}</span>
|
|
332
|
+
<span className={css.cellMuted}>{tt(IMPORT_REASON_KEY[block.reason])}</span>
|
|
333
|
+
</li>
|
|
334
|
+
))}
|
|
335
|
+
{importSkips.length > IMPORT_SKIP_LIMIT && (
|
|
336
|
+
<li className={css.importSkipRow}>
|
|
337
|
+
<span className={css.cellMuted}>{tt('import.more', { count: importSkips.length - IMPORT_SKIP_LIMIT })}</span>
|
|
338
|
+
</li>
|
|
339
|
+
)}
|
|
340
|
+
</ul>
|
|
341
|
+
)}
|
|
342
|
+
{error !== null && <div className={css.banner} data-kind="error" role="alert">{tt('common.error', { error })}</div>}
|
|
343
|
+
{hosts === null && error === null && <div className={css.loading}>{tt('common.loading')}</div>}
|
|
344
|
+
{hosts !== null && hosts.length === 0 && <div className={css.empty}>{tt('hosts.empty')}</div>}
|
|
345
|
+
{hosts !== null && hosts.length > 0 && groupBy === 'none' && (
|
|
346
|
+
<div className={css.tableWrap}>
|
|
347
|
+
{renderHostTable(hosts)}
|
|
348
|
+
</div>
|
|
349
|
+
)}
|
|
350
|
+
{hosts !== null && hosts.length > 0 && groupBy !== 'none' && (
|
|
351
|
+
<div className={css.tableWrap}>
|
|
352
|
+
{groups.map(group => {
|
|
353
|
+
const isCollapsed = collapsed[group.key] === true
|
|
354
|
+
const label = group.key === ''
|
|
355
|
+
? (groupBy === 'tags' ? tt('hosts.group.noTags') : tt('hosts.group.ungrouped'))
|
|
356
|
+
: group.key
|
|
357
|
+
return (
|
|
358
|
+
<section key={group.key} className={css.groupSection}>
|
|
359
|
+
<div className={css.groupHeader}>
|
|
360
|
+
<button
|
|
361
|
+
type="button"
|
|
362
|
+
className={css.groupToggle}
|
|
363
|
+
aria-expanded={!isCollapsed}
|
|
364
|
+
onClick={() => { setCollapsed(prev => ({ ...prev, [group.key]: !isCollapsed })) }}
|
|
365
|
+
>
|
|
366
|
+
<span className={css.groupChevron} data-collapsed={isCollapsed || undefined} aria-hidden="true" />
|
|
367
|
+
<span className={css.groupName}>{label}</span>
|
|
368
|
+
<span className={css.groupCount}>{tt('hosts.group.count', { count: group.hosts.length })}</span>
|
|
369
|
+
</button>
|
|
370
|
+
<button
|
|
371
|
+
type="button"
|
|
372
|
+
className={css.linkButton}
|
|
373
|
+
disabled={testingGroup === group.key}
|
|
374
|
+
onClick={() => { void testGroup(group) }}
|
|
375
|
+
>
|
|
376
|
+
{testingGroup === group.key ? tt('hosts.testing') : tt('hosts.group.testAll')}
|
|
377
|
+
</button>
|
|
378
|
+
</div>
|
|
379
|
+
{!isCollapsed && renderHostTable(group.hosts)}
|
|
380
|
+
</section>
|
|
381
|
+
)
|
|
382
|
+
})}
|
|
383
|
+
</div>
|
|
384
|
+
)}
|
|
385
|
+
{dialog !== null && (
|
|
386
|
+
<HostFormDialog
|
|
387
|
+
api={api}
|
|
388
|
+
editing={dialog.mode === 'edit' ? dialog.host : null}
|
|
389
|
+
onClose={() => { setDialog(null) }}
|
|
390
|
+
onSaved={() => { setDialog(null); void load() }}
|
|
391
|
+
/>
|
|
392
|
+
)}
|
|
393
|
+
</div>
|
|
394
|
+
)
|
|
395
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The logs tab: search many hosts at once and read the merged result.
|
|
3
|
+
*
|
|
4
|
+
* The shape follows the question an operator actually has during an incident:
|
|
5
|
+
* "did this happen anywhere else?" A per-host accordion keeps that answerable
|
|
6
|
+
* without losing which machine said what — a single merged stream would be
|
|
7
|
+
* easier to skim and useless for attribution.
|
|
8
|
+
*
|
|
9
|
+
* Results are grouped by search rather than flattened, because the same
|
|
10
|
+
* pattern gets re-run as an incident develops, and a flat list would interleave
|
|
11
|
+
* two runs of the same query beyond recognition.
|
|
12
|
+
*
|
|
13
|
+
* The source selector mirrors the host's own two modes rather than inventing
|
|
14
|
+
* a third: the journal (with an optional unit) or files under a directory.
|
|
15
|
+
* Offering a "smart" mode that guesses would hide which command ran.
|
|
16
|
+
*/
|
|
17
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
18
|
+
import type { SshApi } from '../api.ts'
|
|
19
|
+
import type { LogSearchRow, LogSearchPayload, SshHostSummary } from '../../protocol.ts'
|
|
20
|
+
import { errorMessage, tt } from './helpers.ts'
|
|
21
|
+
import css from './panel.module.css'
|
|
22
|
+
|
|
23
|
+
/** Logs tab props. */
|
|
24
|
+
export interface LogsTabProps {
|
|
25
|
+
/** The SSH API client. */
|
|
26
|
+
api: SshApi
|
|
27
|
+
/** Whether the panel is open, so refreshes only run while visible. */
|
|
28
|
+
active: boolean
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Format an epoch millisecond value as a short local timestamp. */
|
|
32
|
+
function stamp(ms: number | undefined): string {
|
|
33
|
+
if (ms === undefined) return '—'
|
|
34
|
+
return new Date(ms).toLocaleString(undefined, { hour12: false })
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The logs tab. */
|
|
38
|
+
export function LogsTab({ api, active }: LogsTabProps) {
|
|
39
|
+
const [hosts, setHosts] = useState<SshHostSummary[]>([])
|
|
40
|
+
const [searches, setSearches] = useState<LogSearchRow[]>([])
|
|
41
|
+
const [pattern, setPattern] = useState('')
|
|
42
|
+
const [source, setSource] = useState<'journal' | 'files'>('journal')
|
|
43
|
+
const [unit, setUnit] = useState('')
|
|
44
|
+
const [directory, setDirectory] = useState('/var/log')
|
|
45
|
+
const [sinceMinutes, setSinceMinutes] = useState('30')
|
|
46
|
+
const [maxLines, setMaxLines] = useState('50')
|
|
47
|
+
const [selected, setSelected] = useState<string[]>([])
|
|
48
|
+
const [busy, setBusy] = useState(false)
|
|
49
|
+
const [error, setError] = useState<string | null>(null)
|
|
50
|
+
const [openHost, setOpenHost] = useState<string | null>(null)
|
|
51
|
+
|
|
52
|
+
const refresh = useCallback(async (): Promise<void> => {
|
|
53
|
+
try {
|
|
54
|
+
setError(null)
|
|
55
|
+
const [nextHosts, nextSearches] = await Promise.all([api.listHosts(), api.logHistory(20)])
|
|
56
|
+
setHosts(nextHosts)
|
|
57
|
+
setSearches(nextSearches)
|
|
58
|
+
} catch (cause) {
|
|
59
|
+
setError(errorMessage(cause))
|
|
60
|
+
}
|
|
61
|
+
}, [api])
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (!active) return
|
|
65
|
+
void refresh()
|
|
66
|
+
}, [active, refresh])
|
|
67
|
+
|
|
68
|
+
const submit = async (): Promise<void> => {
|
|
69
|
+
if (busy) return
|
|
70
|
+
setError(null)
|
|
71
|
+
if (pattern.trim() === '') { setError(tt('logs.error.noPattern')); return }
|
|
72
|
+
const request: LogSearchPayload = {
|
|
73
|
+
pattern: pattern.trim(),
|
|
74
|
+
source,
|
|
75
|
+
...(source === 'journal' && unit.trim() !== '' ? { unit: unit.trim() } : {}),
|
|
76
|
+
...(source === 'files' ? { directory: directory.trim() === '' ? '/var/log' : directory.trim() } : {}),
|
|
77
|
+
...(Number(sinceMinutes) > 0 ? { sinceSeconds: Number(sinceMinutes) * 60 } : {}),
|
|
78
|
+
...(Number(maxLines) > 0 ? { maxLinesPerHost: Number(maxLines) } : {}),
|
|
79
|
+
}
|
|
80
|
+
setBusy(true)
|
|
81
|
+
try {
|
|
82
|
+
await api.searchLogs(request, selected.length > 0 ? selected : undefined)
|
|
83
|
+
setOpenHost(null)
|
|
84
|
+
await refresh()
|
|
85
|
+
} catch (cause) {
|
|
86
|
+
setError(errorMessage(cause))
|
|
87
|
+
} finally {
|
|
88
|
+
setBusy(false)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const toggleTarget = (alias: string): void => {
|
|
93
|
+
setSelected(current => (current.includes(alias) ? current.filter(item => item !== alias) : [...current, alias]))
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<div className={css.panel}>
|
|
98
|
+
{error !== null && <div className={css.banner} data-kind="error" role="alert">{error}</div>}
|
|
99
|
+
|
|
100
|
+
<section className={css.formCard}>
|
|
101
|
+
<h3 className={css.sectionTitle}>{tt('logs.form.title')}</h3>
|
|
102
|
+
<p className={css.hint}>{tt('logs.form.hint')}</p>
|
|
103
|
+
|
|
104
|
+
<label className={css.field}>
|
|
105
|
+
<span className={css.fieldLabel}>{tt('logs.form.pattern')}</span>
|
|
106
|
+
<input className={css.input} value={pattern} onChange={event => { setPattern(event.target.value) }} placeholder="OutOfMemory|panic|FAILED" />
|
|
107
|
+
</label>
|
|
108
|
+
|
|
109
|
+
<div className={css.inlineRow}>
|
|
110
|
+
<label className={css.field}>
|
|
111
|
+
<span className={css.fieldLabel}>{tt('logs.form.source')}</span>
|
|
112
|
+
<select className={css.input} value={source} onChange={event => { setSource(event.target.value === 'files' ? 'files' : 'journal') }}>
|
|
113
|
+
<option value="journal">{tt('logs.source.journal')}</option>
|
|
114
|
+
<option value="files">{tt('logs.source.files')}</option>
|
|
115
|
+
</select>
|
|
116
|
+
</label>
|
|
117
|
+
{source === 'journal' && (
|
|
118
|
+
<label className={css.field}>
|
|
119
|
+
<span className={css.fieldLabel}>{tt('logs.form.unit')} <em className={css.cellMuted}>{tt('logs.form.optional')}</em></span>
|
|
120
|
+
<input className={css.input} value={unit} onChange={event => { setUnit(event.target.value) }} placeholder="nginx" />
|
|
121
|
+
</label>
|
|
122
|
+
)}
|
|
123
|
+
{source === 'files' && (
|
|
124
|
+
<label className={css.field}>
|
|
125
|
+
<span className={css.fieldLabel}>{tt('logs.form.directory')}</span>
|
|
126
|
+
<input className={css.input} value={directory} onChange={event => { setDirectory(event.target.value) }} placeholder="/var/log" />
|
|
127
|
+
</label>
|
|
128
|
+
)}
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
<div className={css.inlineRow}>
|
|
132
|
+
<label className={css.field}>
|
|
133
|
+
<span className={css.fieldLabel}>{tt('logs.form.since')}</span>
|
|
134
|
+
<input className={css.input} value={sinceMinutes} onChange={event => { setSinceMinutes(event.target.value) }} inputMode="numeric" />
|
|
135
|
+
</label>
|
|
136
|
+
<label className={css.field}>
|
|
137
|
+
<span className={css.fieldLabel}>{tt('logs.form.maxLines')}</span>
|
|
138
|
+
<input className={css.input} value={maxLines} onChange={event => { setMaxLines(event.target.value) }} inputMode="numeric" />
|
|
139
|
+
</label>
|
|
140
|
+
</div>
|
|
141
|
+
|
|
142
|
+
<div className={css.field}>
|
|
143
|
+
<span className={css.fieldLabel}>{tt('logs.form.targets')} <em className={css.cellMuted}>{tt('logs.form.targetsHint')}</em></span>
|
|
144
|
+
<div className={css.chipRow}>
|
|
145
|
+
{hosts.length === 0 && <span className={css.cellMuted}>{tt('logs.form.noHosts')}</span>}
|
|
146
|
+
{hosts.map(host => (
|
|
147
|
+
<button
|
|
148
|
+
key={host.alias}
|
|
149
|
+
type="button"
|
|
150
|
+
className={css.chip}
|
|
151
|
+
data-selected={selected.includes(host.alias) ? 'true' : 'false'}
|
|
152
|
+
onClick={() => { toggleTarget(host.alias) }}
|
|
153
|
+
>
|
|
154
|
+
{host.alias}
|
|
155
|
+
</button>
|
|
156
|
+
))}
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
|
|
160
|
+
<button type="button" className={css.primaryButton} disabled={busy} onClick={() => { void submit() }}>
|
|
161
|
+
{busy ? tt('logs.form.running') : tt('logs.form.run')}
|
|
162
|
+
</button>
|
|
163
|
+
</section>
|
|
164
|
+
|
|
165
|
+
<section>
|
|
166
|
+
<h3 className={css.sectionTitle}>{tt('logs.results.title')}</h3>
|
|
167
|
+
{searches.length === 0 && <p className={css.hint}>{tt('logs.results.empty')}</p>}
|
|
168
|
+
{searches.map((search, index) => {
|
|
169
|
+
const matches = search.hosts.reduce((total, host) => total + host.lines.length, 0)
|
|
170
|
+
const failed = search.hosts.filter(host => !host.ok).length
|
|
171
|
+
return (
|
|
172
|
+
<div className={css.resultCard} key={`${String(search.at)}-${String(index)}`}>
|
|
173
|
+
<div className={css.resultHead}>
|
|
174
|
+
<code>{search.pattern}</code>
|
|
175
|
+
<span className={css.cellMuted}>
|
|
176
|
+
{search.source} · {search.hosts.length} {tt('logs.results.hosts')} · {matches} {tt('logs.results.lines')}
|
|
177
|
+
{failed > 0 && <> · {failed} {tt('logs.results.failed')}</>}
|
|
178
|
+
</span>
|
|
179
|
+
<span className={css.cellMuted}>{stamp(search.at)}</span>
|
|
180
|
+
</div>
|
|
181
|
+
<div className={css.hostList}>
|
|
182
|
+
{search.hosts.map(host => (
|
|
183
|
+
<div key={host.alias}>
|
|
184
|
+
<button
|
|
185
|
+
type="button"
|
|
186
|
+
className={css.hostRow}
|
|
187
|
+
onClick={() => { setOpenHost(openHost === `${String(search.at)}-${host.alias}` ? null : `${String(search.at)}-${host.alias}`) }}
|
|
188
|
+
>
|
|
189
|
+
<span className={css.badge} data-status={host.ok ? 'ok' : 'fail'}>{host.alias}</span>
|
|
190
|
+
<span className={css.cellMuted}>
|
|
191
|
+
{host.ok
|
|
192
|
+
? <>{host.lines.length}{host.truncated ? '+' : ''} {tt('logs.results.lines')}{host.total > host.lines.length ? ` / ${String(host.total)} ${tt('logs.results.matched')}` : ''}</>
|
|
193
|
+
: (host.error ?? tt('logs.results.failed'))}
|
|
194
|
+
</span>
|
|
195
|
+
</button>
|
|
196
|
+
{openHost === `${String(search.at)}-${host.alias}` && host.lines.length > 0 && (
|
|
197
|
+
<pre className={css.logBlock}>{host.lines.join('\n')}</pre>
|
|
198
|
+
)}
|
|
199
|
+
</div>
|
|
200
|
+
))}
|
|
201
|
+
</div>
|
|
202
|
+
</div>
|
|
203
|
+
)
|
|
204
|
+
})}
|
|
205
|
+
</section>
|
|
206
|
+
</div>
|
|
207
|
+
)
|
|
208
|
+
}
|