dsh-ops-kit 0.3.2 → 0.3.3

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/client.js CHANGED
@@ -96,7 +96,9 @@ window.__ModuleLoader__.load({
96
96
  aiNeedTerm: '先连上终端,生成的命令才会发到服务器', aiModel: '模型',
97
97
  process: '进程', byCpu: '按 CPU', byMem: '按内存', pid: 'PID', cpuUse: 'CPU', memUse: '内存', cmd: '命令',
98
98
  end: '结束', ending: '结束中…', confirmKill: '结束这个进程?',
99
- logs: '日志', logSource: '来源(nginx.service 或 file:/var/log/x.log)', lines: '行数', load: '读取', auto: '自动刷新(5s)',
99
+ logs: '日志', logSource: '日志来源(可下拉选:服务名 / pid:12345 / file:/var/log/x.log)', lines: '行数', load: '读取', auto: '自动刷新(5s)',
100
+ logHint: '不知道填什么?点输入框会弹出下拉列表:systemd 服务(如 nginx.service)、java 进程(pid:12345)、或日志文件(file:/var/log/xxx.log)。选 java 进程时插件自己找日志——属于某个服务就用 journalctl,否则取它打开的最新 .log / .out 文件。',
101
+ logPick: '先选一个来源(点输入框有下拉列表),再点「读取」。',
100
102
  ssl: '证书', domain: '域名', checkSsl: '检查证书', daysLeft: '剩余天数', expireAt: '到期', noExpire: '没读到证书',
101
103
  files: '文件', upload: '上传', newFolder: '新建文件夹', size: '大小', modified: '修改时间', open: '进入', download: '下载', del: '删除', folderName: '文件夹名', confirmDel: '删除这个路径?',
102
104
  ask: '问一下', diagnose: '智能诊断', diagnosing: '诊断中…', askPh: '例如:网站为什么打不开?',
@@ -177,7 +179,9 @@ window.__ModuleLoader__.load({
177
179
  aiNeedTerm: 'Connect the terminal first so the command can be sent', aiModel: 'model',
178
180
  process: 'Processes', byCpu: 'By CPU', byMem: 'By memory', pid: 'PID', cpuUse: 'CPU', memUse: 'MEM', cmd: 'Command',
179
181
  end: 'End', ending: 'Ending…', confirmKill: 'Kill this process?',
180
- logs: 'Logs', logSource: 'Source (nginx.service or file:/var/log/x.log)', lines: 'Lines', load: 'Load', auto: 'Auto refresh (5s)',
182
+ logs: 'Logs', logSource: 'Log source (pick from the list: unit / pid:12345 / file:/var/log/x.log)', lines: 'Lines', load: 'Load', auto: 'Auto refresh (5s)',
183
+ logHint: 'Not sure what to type? Click the field for a list: a systemd unit (nginx.service), a java process (pid:12345), or a log file (file:/var/log/x.log). For a java process the plugin finds the log itself — journalctl when it belongs to a unit, otherwise the newest .log / .out file it has open.',
184
+ logPick: 'Pick a source first (click the field for the list), then press Load.',
181
185
  ssl: 'Certificate', domain: 'Domain', checkSsl: 'Check certificate', daysLeft: 'days left', expireAt: 'expires', noExpire: 'no certificate read',
182
186
  files: 'Files', upload: 'Upload', newFolder: 'New folder', size: 'Size', modified: 'Modified', open: 'Open', download: 'Download', del: 'Delete', folderName: 'Folder name', confirmDel: 'Delete this path?',
183
187
  ask: 'Ask', diagnose: 'Diagnose', diagnosing: 'Diagnosing…', askPh: 'e.g. why is the site down?',
@@ -516,11 +520,14 @@ window.__ModuleLoader__.load({
516
520
  } catch (e) { setError(String(e.message || e)) } finally { setBusy('') }
517
521
  }
518
522
 
519
- const loadLogs = async () => {
523
+ const loadLogs = async (src) => {
524
+ const source = String(src ?? logSource).trim()
525
+ if (!source) { setLogText(T.logPick); return }
520
526
  setBusy('logs')
521
527
  try {
522
- const data = await api(`tail?host=${encodeURIComponent(cHost || hosts[0]?.name)}&source=${encodeURIComponent(logSource)}&lines=${logLines}`, {}, timeoutFor('action'))
523
- setLogText(data.lines || data.error || '')
528
+ const data = await api(`tail?host=${encodeURIComponent(cHost || hosts[0]?.name)}&source=${encodeURIComponent(source)}&lines=${logLines}`, {}, timeoutFor('action'))
529
+ const head = data.resolved ? `# ${data.via === 'journal' ? 'journalctl -u ' + data.resolved : data.resolved}\n\n` : ''
530
+ setLogText(head + (data.lines || data.error || ''))
524
531
  } catch (e) { setLogText(String(e.message || e)) } finally { setBusy('') }
525
532
  }
526
533
 
@@ -1069,7 +1076,20 @@ window.__ModuleLoader__.load({
1069
1076
  h('span', { className: app.status ? 'ops-ok' : 'ops-dim', style: { fontSize: '12px' } }, app.status || ''),
1070
1077
  h('span', { style: { display: 'flex', gap: '6px', justifyContent: 'flex-end' } },
1071
1078
  app.config ? h('button', { className: 'ops-btn ops-btn-sm', onClick: () => { void openFile(app.config, app.name) } }, T.configFile) : null,
1072
- h('button', { className: 'ops-btn ops-btn-sm', disabled: busy === `act:${app.kind}:${app.name}:logs`, onClick: () => { void doAction(app.kind, app.name, 'logs') } }, busy === `act:${app.kind}:${app.name}:logs` ? T.reading : T.logs),
1079
+ h('button', {
1080
+ className: 'ops-btn ops-btn-sm',
1081
+ disabled: busy === `act:${app.kind}:${app.name}:logs`,
1082
+ onClick: () => {
1083
+ if (app.kind === 'java') {
1084
+ const src = `pid:${String(app.name).replace(/[^0-9]/g, '')}`
1085
+ setLogSource(src)
1086
+ setDetailTab('logs')
1087
+ void loadLogs(src)
1088
+ return
1089
+ }
1090
+ void doAction(app.kind, app.name, 'logs')
1091
+ },
1092
+ }, busy === `act:${app.kind}:${app.name}:logs` ? T.reading : T.logs),
1073
1093
  app.kind === 'service' ? h('button', { className: 'ops-btn ops-btn-sm', disabled: busy === `act:${app.kind}:${app.name}:stop`, onClick: () => { void doAction(app.kind, app.name, 'stop') } }, T.stop) : null,
1074
1094
  app.kind !== 'java' ? h('button', { className: 'ops-btn ops-btn-sm', disabled: busy === `act:${app.kind}:${app.name}:restart`, onClick: () => { void doAction(app.kind, app.name, 'restart') } }, busy === `act:${app.kind}:${app.name}:restart` ? '…' : T.restart) : null))))),
1075
1095
  output ? h('pre', { className: 'ops-pre' }, output) : null,
@@ -1137,6 +1157,15 @@ window.__ModuleLoader__.load({
1137
1157
  h('div', { className: sslResult.daysLeft !== null && sslResult.daysLeft < 15 ? 'ops-bad' : 'ops-sub', style: { fontSize: '12px' } },
1138
1158
  sslResult.daysLeft === null ? T.noExpire : `${T.expireAt} ${sslResult.notAfter} · ${T.daysLeft} ${sslResult.daysLeft}`)) : null)))
1139
1159
 
1160
+ const logChoices = (() => {
1161
+ const apps = inv?.apps || []
1162
+ const services = apps.filter(a => a.kind === 'service').map(a => a.name)
1163
+ const pids = apps.filter(a => a.kind === 'java').map(a => `pid:${String(a.name).replace(/[^0-9]/g, '')}`)
1164
+ const files = ['file:/var/log/syslog', 'file:/var/log/messages', 'file:/var/log/nginx/error.log',
1165
+ 'file:/var/log/nginx/access.log', 'file:/var/log/auth.log']
1166
+ return [...services, ...pids, ...files]
1167
+ })()
1168
+
1140
1169
  const logsView = h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } },
1141
1170
  h('div', { className: 'ops-chips' }, (detailHost ? null : hostPicker(cHost, setCHost))),
1142
1171
  h('div', { className: 'ops-card' },
@@ -1147,9 +1176,15 @@ window.__ModuleLoader__.load({
1147
1176
  h('button', { className: 'ops-btn ops-btn-primary ops-btn-sm', disabled: busy === 'logs', onClick: () => { void loadLogs() } }, busy === 'logs' ? T.reading : T.load))),
1148
1177
  h('div', { className: 'ops-card-b' },
1149
1178
  h('div', { className: 'ops-chips' },
1150
- h('input', { className: 'ops-input', style: { flex: '1 1 320px' }, value: logSource, placeholder: T.logSource, onChange: e => setLogSource(e.target.value), onKeyDown: e => { if (e.key === 'Enter') void loadLogs() } }),
1179
+ h('input', {
1180
+ className: 'ops-input', style: { flex: '1 1 320px' }, value: logSource, list: 'ops-log-sources',
1181
+ placeholder: T.logSource, onChange: e => setLogSource(e.target.value),
1182
+ onKeyDown: e => { if (e.key === 'Enter') void loadLogs() },
1183
+ }),
1184
+ h('datalist', { id: 'ops-log-sources' }, ...logChoices.map(o => h('option', { key: o, value: o }))),
1151
1185
  h('label', { className: 'ops-sub' }, T.lines,
1152
1186
  h('input', { className: 'ops-input', type: 'number', min: 20, max: 1000, value: logLines, style: { width: '90px' }, onChange: e => setLogLines(Number(e.target.value)) }))),
1187
+ h('p', { className: 'ops-sub', style: { margin: '0 0 8px', fontSize: '12px' } }, T.logHint),
1153
1188
  h('pre', { className: 'ops-pre', style: { maxHeight: '55vh' } }, logText || (busy === 'logs' ? T.reading : '')))))
1154
1189
 
1155
1190
  const filesView = h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } },
package/docs/USAGE.md CHANGED
@@ -99,6 +99,8 @@ Click a host from the overview; the left side has ten sub-pages:
99
99
  | **Tasks** | visual crontab editing |
100
100
  | **Deploy** | one-click job: git pull → build → restart → health check |
101
101
 
102
+ **You do not have to remember log sources**: clicking the source field opens a list — a systemd unit (`nginx.service`), a java process (`pid:3154754`), or a log file (`file:/var/log/x.log`). For a java process the plugin finds the log itself: `journalctl -u` when the process belongs to a unit, otherwise the newest `.log` / `.out` file it has open. The resolved source is shown above the output.
103
+
102
104
  **Restart, stop, restore and rollback all ask for confirmation first.**
103
105
 
104
106
  ---
@@ -86,6 +86,8 @@ ops_check_all
86
86
  | **计划任务** | crontab 可视化增删 |
87
87
  | **发布** | 一键发布任务:git pull → 构建 → 重启 → 健康检查 |
88
88
 
89
+ **日志来源不用记**:点来源输入框会弹出列表 —— systemd 服务名(`nginx.service`)、java 进程(`pid:3154754`)、或日志文件(`file:/var/log/xxx.log`)。选 java 进程时插件自己找日志:该进程属于某个服务就用 `journalctl -u`,否则取它打开的最新的 `.log` / `.out` 文件。读取结果顶部会标出实际用的来源。
90
+
89
91
  **重启 / 停止 / 回滚 / 恢复都会先弹确认**,不会点了就执行。
90
92
 
91
93
  ---
package/index.js CHANGED
@@ -592,10 +592,16 @@ function createApiHandler({ connection } = {}) {
592
592
  if (route === 'app' && method === 'POST') {
593
593
  const body = (await readBody(req)) || {}
594
594
  const kind = String(body.kind ?? '')
595
- if (!['service', 'docker'].includes(kind)) { sendJson(res, 400, { error: 'unsupported app kind' }); return }
595
+ if (!['service', 'docker', 'java'].includes(kind)) { sendJson(res, 400, { error: 'unsupported app kind' }); return }
596
596
  const action = String(body.action ?? 'logs')
597
597
  if (action !== 'logs' && body.confirm !== true) { sendJson(res, 400, { error: 'changing an app needs confirm=true' }); return }
598
598
  const cfg = pickHost(await loadHosts(), body.host)
599
+ if (kind === 'java') {
600
+ if (action !== 'logs') { sendJson(res, 400, { error: 'a java process only supports logs' }); return }
601
+ const pid = Number(String(body.name ?? '').replace(/[^0-9]/g, ''))
602
+ sendJson(res, 200, await tailProcessLog(cfg, pid, Number(body.lines) || 200))
603
+ return
604
+ }
599
605
  sendJson(res, 200, await hostAction(kind, cfg, String(body.name ?? ''), action))
600
606
  return
601
607
  }
@@ -1663,6 +1669,7 @@ async function killProcess(cfg, pid, signal = 'TERM') {
1663
1669
  async function tailLog(cfg, source, lines = 200) {
1664
1670
  const n = Math.min(Math.max(Number(lines) || 200, 20), 1000)
1665
1671
  const raw = String(source || '')
1672
+ if (raw.startsWith('pid:')) return await tailProcessLog(cfg, Number(raw.slice(4)), n)
1666
1673
  let command
1667
1674
  if (raw.startsWith('file:')) {
1668
1675
  const path = raw.slice(5)
@@ -1676,6 +1683,51 @@ async function tailLog(cfg, source, lines = 200) {
1676
1683
  return { host: cfg.name, source: raw, ok: result.ok, lines: String(result.stdout || '').slice(0, 60_000), error: result.ok ? '' : cleanStderr(result.stderr) }
1677
1684
  }
1678
1685
 
1686
+ /**
1687
+ * Log for a bare PID (java processes have no unit name).
1688
+ * Uses the owning systemd unit when there is one, otherwise tails the newest
1689
+ * .log/.out file the process actually has open.
1690
+ */
1691
+ async function tailProcessLog(cfg, pid, lines = 200) {
1692
+ const n = Math.min(Math.max(Number(lines) || 200, 20), 1000)
1693
+ if (!Number.isInteger(pid) || pid <= 0) throw new Error('a valid pid is required')
1694
+
1695
+ const unitRes = await ssh(cfg, `ps -o unit= -p ${pid} 2>/dev/null | tr -d ' \n'; true`, 20_000)
1696
+ const unit = String(unitRes.stdout || '').trim()
1697
+ if (unit && unit !== '-' && !unit.startsWith('session-') && !unit.includes(' ')) {
1698
+ const res = await ssh(cfg, `journalctl -u ${unit} -n ${n} --no-pager 2>&1`, 25_000)
1699
+ return {
1700
+ host: cfg.name, source: `pid:${pid}`, resolved: unit, via: 'journal',
1701
+ ok: res.ok, lines: String(res.stdout || '').slice(0, 60_000),
1702
+ error: res.ok ? '' : cleanStderr(res.stderr),
1703
+ }
1704
+ }
1705
+
1706
+ const scan = await ssh(
1707
+ cfg,
1708
+ `for f in /proc/${pid}/fd/*; do t=$(readlink "$f" 2>/dev/null); case "$t" in *.log|*.out) [ -f "$t" ] && printf '%s\\n' "$t";; esac; done 2>/dev/null | sort -u | head -20`,
1709
+ 20_000,
1710
+ )
1711
+ const files = String(scan.stdout || '').split('\n').map(s => s.trim()).filter(f => f.startsWith('/'))
1712
+ if (files.length === 0) {
1713
+ return {
1714
+ host: cfg.name, source: `pid:${pid}`, resolved: '', via: 'none',
1715
+ ok: false, lines: '',
1716
+ error: `PID ${pid} 既不是 systemd 服务,也没有打开 .log / .out 文件。请到「文件」页找到日志路径,再在来源里填 file:/var/log/xxx.log`,
1717
+ }
1718
+ }
1719
+
1720
+ const quoted = files.map(f => JSON.stringify(f)).join(' ')
1721
+ const newest = await ssh(cfg, `ls -1t ${quoted} 2>/dev/null | head -1`, 20_000)
1722
+ const picked = String(newest.stdout || '').split('\n')[0].trim() || files[0]
1723
+ const res = await ssh(cfg, `tail -n ${n} ${JSON.stringify(picked)} 2>&1`, 25_000)
1724
+ return {
1725
+ host: cfg.name, source: `pid:${pid}`, resolved: picked, via: 'file',
1726
+ ok: res.ok, lines: String(res.stdout || '').slice(0, 60_000),
1727
+ error: res.ok ? '' : cleanStderr(res.stderr),
1728
+ }
1729
+ }
1730
+
1679
1731
  /** TLS certificate expiry for a domain (checked from the host itself). */
1680
1732
  async function sslCheck(cfg, domain) {
1681
1733
  const host = String(domain || '').replace(/[^A-Za-z0-9.-]/g, '')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ops-kit",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "Server operations kit for DeepSeek Harness: server cards, health charts, config browser, app manager and plain-language AI diagnosis — over SSH, no agent install.",
5
5
  "license": "MIT",
6
6
  "type": "module",