Files
nexus/modules/pi_control/pages/console.php
2026-03-06 22:57:44 +01:00

338 lines
14 KiB
PHP

<?php
$pdo = module_fn('pi_control', 'pdo');
module_fn('pi_control', 'ensure_schema');
$table = fn(string $name) => module_fn('pi_control', 'table', $name);
$notice = null;
$error = null;
$terminalNotice = null;
$terminalError = null;
$terminalUrl = null;
$terminalToken = null;
$terminalHostLabel = null;
$settings = modules()->settings('pi_control');
$assets = app()->assets();
if ($assets) {
$assets->addStyle('/module/pi_control/asset?file=pi_control.css');
$assets->addScript('/module/pi_control/asset?file=console.js', 'footer', true);
}
$ttydUrl = trim((string)($settings['ttyd_url'] ?? '/ttyd'));
$defaultProvider = 'ttyd';
$defaultTimeout = (int)($settings['exec_default_timeout'] ?? (getenv('PI_CONTROL_EXEC_DEFAULT_TIMEOUT') !== false ? (int)getenv('PI_CONTROL_EXEC_DEFAULT_TIMEOUT') : 300));
$defaultTimeout = $defaultTimeout > 0 ? $defaultTimeout : 300;
$queueName = (string)($settings['redis']['queue'] ?? ($settings['redis.queue'] ?? (getenv('PI_CONTROL_REDIS_QUEUE') ?: 'pi_control:queue')));
$tokenTtl = (int)($settings['terminal_token_ttl'] ?? 10);
$tokenTtl = $tokenTtl > 0 ? $tokenTtl : 10;
$hosts = $pdo->query('SELECT * FROM ' . $table('hosts') . ' ORDER BY name ASC')->fetchAll(PDO::FETCH_ASSOC);
$commands = $pdo->query('SELECT * FROM ' . $table('commands') . ' ORDER BY label ASC')->fetchAll(PDO::FETCH_ASSOC);
$renderRuns = function (array $runs): string {
ob_start();
if (!$runs) {
echo '<tr><td colspan="7" class="muted">Noch keine Runs.</td></tr>';
return ob_get_clean();
}
foreach ($runs as $r) {
$out = (string)($r['output'] ?? '');
$err = (string)($r['error'] ?? '');
$snippet = $out !== '' ? $out : $err;
if (strlen($snippet) > 140) {
$snippet = substr($snippet, 0, 140) . '…';
}
echo '<tr>';
echo '<td>' . e((string)$r['id']) . '</td>';
echo '<td>' . e((string)($r['status'] ?? '')) . '</td>';
echo '<td>' . e((string)($r['host_name'] ?? $r['host_addr'] ?? '')) . '</td>';
echo '<td class="muted" style="max-width:360px;"><code>' . e((string)($r['command_text'] ?? '')) . '</code></td>';
echo '<td>' . e((string)($r['created_by'] ?? '')) . '</td>';
echo '<td class="muted" style="max-width:240px;"><code>' . e($snippet) . '</code></td>';
echo '<td>' . (!empty($r['timeout_sec']) ? e((string)$r['timeout_sec']) . 's' : 'default') . '</td>';
echo '</tr>';
}
return ob_get_clean();
};
if (isset($_GET['queue_json'])) {
$runs = $pdo->query(
'SELECT r.*, h.name AS host_name, h.host AS host_addr
FROM ' . $table('runs') . ' r
LEFT JOIN ' . $table('hosts') . ' h ON h.id = r.host_id
ORDER BY r.id DESC LIMIT 20'
)->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'ok' => true,
'next' => 10,
'html' => $renderRuns($runs),
]);
exit;
}
if (isset($_GET['open_console_json'])) {
$hostId = (int)($_POST['terminal_host_id'] ?? 0);
$presetId = (int)($_POST['terminal_preset_id'] ?? 0);
$rawCommand = trim((string)($_POST['terminal_command_text'] ?? ''));
$terminalError = null;
$terminalUrl = null;
$terminalToken = null;
$terminalHostLabel = null;
if ($hostId <= 0) {
$terminalError = 'Bitte einen Host wählen.';
} elseif ($ttydUrl === '') {
$terminalError = 'ttyd URL fehlt. Bitte im Setup setzen.';
} else {
$presetCommand = '';
if ($presetId > 0) {
foreach ($commands as $c) {
if ((int)$c['id'] === $presetId) {
if (!auth_is_admin() && !empty($c['admin_only'])) {
$terminalError = 'Diese Vorlage ist nur für Admins.';
} else {
$presetCommand = (string)$c['command'];
}
break;
}
}
}
if (!$terminalError) {
$driver = (string)$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
$expiresSql = $driver === 'pgsql'
? "NOW() + INTERVAL '{$tokenTtl} minutes'"
: "DATETIME('now', '+{$tokenTtl} minutes')";
$token = bin2hex(random_bytes(24));
$terminalToken = $token;
$stmt = $pdo->prepare(
'INSERT INTO ' . $table('sessions') . ' (token, host_id, provider, command_text, created_by, expires_at)
VALUES (:token, :host_id, :provider, :command_text, :created_by, ' . $expiresSql . ')'
);
$commandToRun = $presetCommand !== '' ? $presetCommand : $rawCommand;
$stmt->execute([
'token' => $token,
'host_id' => $hostId,
'provider' => 'ttyd',
'command_text' => $commandToRun !== '' ? $commandToRun : null,
'created_by' => auth_display_name() ?: null,
]);
$sep = str_contains($ttydUrl, '?') ? '&' : '?';
$terminalUrl = rtrim($ttydUrl, '/') . '/' . $sep . 'arg=' . rawurlencode($token);
foreach ($hosts as $h) {
if ((int)$h['id'] === $hostId) {
$terminalHostLabel = (string)($h['name'] ?? $h['host']);
break;
}
}
}
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'ok' => $terminalError === null,
'error' => $terminalError,
'url' => $terminalUrl,
'token' => $terminalToken,
'host' => $terminalHostLabel,
]);
exit;
}
if (isset($_GET['run_command_json'])) {
$hostId = (int)($_POST['terminal_host_id'] ?? 0);
$commandId = (int)($_POST['terminal_preset_id'] ?? 0);
$rawCommand = trim((string)($_POST['terminal_command_text'] ?? ''));
$timeoutSec = $defaultTimeout;
$error = null;
$notice = null;
if ($hostId <= 0) {
$error = 'Bitte einen Host wählen.';
} elseif ($commandId <= 0 && $rawCommand === '') {
$error = 'Bitte einen Befehl wählen oder einen Befehl eingeben.';
} else {
$selectedCommand = '';
if ($commandId > 0) {
foreach ($commands as $c) {
if ((int)$c['id'] === $commandId) {
if (!auth_is_admin() && !empty($c['admin_only'])) {
$error = 'Dieser Befehl ist nur für Admins.';
} else {
$selectedCommand = (string)$c['command'];
if (!empty($c['timeout_sec'])) {
$timeoutSec = (int)$c['timeout_sec'];
}
}
break;
}
}
}
if (!$error) {
$commandText = $selectedCommand !== '' ? $selectedCommand : $rawCommand;
$driver = (string)$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
if ($driver === 'pgsql') {
$stmt = $pdo->prepare(
'INSERT INTO ' . $table('runs') . ' (host_id, command_id, command_text, status, timeout_sec, created_by)
VALUES (:host_id, :command_id, :command_text, :status, :timeout_sec, :created_by)
RETURNING id'
);
$stmt->execute([
'host_id' => $hostId,
'command_id' => $commandId > 0 ? $commandId : null,
'command_text' => $commandText,
'status' => 'queued',
'timeout_sec' => $timeoutSec,
'created_by' => auth_display_name() ?: null,
]);
$runId = (int)$stmt->fetchColumn();
} else {
$stmt = $pdo->prepare(
'INSERT INTO ' . $table('runs') . ' (host_id, command_id, command_text, status, timeout_sec, created_by)
VALUES (:host_id, :command_id, :command_text, :status, :timeout_sec, :created_by)'
);
$stmt->execute([
'host_id' => $hostId,
'command_id' => $commandId > 0 ? $commandId : null,
'command_text' => $commandText,
'status' => 'queued',
'timeout_sec' => $timeoutSec,
'created_by' => auth_display_name() ?: null,
]);
$runId = (int)$pdo->lastInsertId();
}
try {
$redis = module_fn('pi_control', 'redis');
$payload = json_encode(['run_id' => $runId], JSON_THROW_ON_ERROR);
$redis->command(['RPUSH', $queueName, $payload]);
$notice = 'Befehl wurde in die Queue gestellt.';
} catch (\Throwable $e) {
$pdo->exec('UPDATE ' . $table('runs') . ' SET status = \'queue_error\' WHERE id = ' . (int)$runId);
$notice = 'Befehl gespeichert, aber Queue nicht erreichbar.';
}
}
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode([
'ok' => $error === null,
'error' => $error,
'notice' => $notice,
]);
exit;
}
// Form submits are handled via AJAX to avoid reloads.
$runs = $pdo->query(
'SELECT r.*, h.name AS host_name, h.host AS host_addr
FROM ' . $table('runs') . ' r
LEFT JOIN ' . $table('hosts') . ' h ON h.id = r.host_id
ORDER BY r.id DESC LIMIT 20'
)->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="card">
<div class="pill">Pi Control</div>
<h1 style="margin-top:.75rem;">Konsole</h1>
<p class="muted">Wähle einen Host und führe einen Befehl aus.</p>
<?php if ($error): ?>
<div class="card" style="margin-top:1rem; border-color:#ffb4a8; background:#fff5f3; color:#7a2114;">
<?= e($error) ?>
</div>
<?php elseif ($notice): ?>
<div class="card" style="margin-top:1rem; border-color:var(--accent-2);">
<?= e($notice) ?>
</div>
<?php endif; ?>
<div class="grid" style="margin-top:1rem;">
<div class="card form-card" style="background:var(--panel-2);">
<strong>Live-Konsole</strong>
<div class="card" style="margin-top:1rem; border-color:#ffb4a8; background:#fff5f3; color:#7a2114; display:none;" data-console-error></div>
<div class="card" style="margin-top:1rem; border-color:var(--accent-2); display:none;" data-console-notice></div>
<form method="post" class="form-grid" style="margin-top:.75rem;" data-console-form>
<label class="form-field">
<span class="muted">Host</span>
<select name="terminal_host_id" required title="Pi auswählen, zu dem die Konsole verbindet.">
<option value="">Host wählen</option>
<?php foreach ($hosts as $h): ?>
<option value="<?= e((string)$h['id']) ?>"><?= e($h['name'] ?? $h['host']) ?></option>
<?php endforeach; ?>
</select>
</label>
<label class="form-field">
<span class="muted">Vorlage</span>
<select name="terminal_preset_id" title="Optional: Vorlage direkt in der Konsole ausführen.">
<option value="">Vorlage auswählen (optional)</option>
<?php foreach ($commands as $c): ?>
<?php if (!auth_is_admin() && !empty($c['admin_only'])) { continue; } ?>
<option value="<?= e((string)$c['id']) ?>" data-command="<?= e((string)($c['command'] ?? '')) ?>">
<?= e($c['label'] ?? '') ?>
</option>
<?php endforeach; ?>
</select>
</label>
<label class="form-field">
<span class="muted">Direkter Befehl</span>
<textarea name="terminal_command_text" rows="2" placeholder="Optional: Befehl direkt ausführen"></textarea>
</label>
<div style="display:flex; gap:10px; flex-wrap:wrap;">
<button class="cta-button" type="button" data-open-console>Neue Konsole öffnen</button>
<button class="nav-link" type="button" data-run-command>Im Hintergrund ausführen</button>
<button class="nav-link" type="button" data-send-active>In neuer Konsole ausführen</button>
</div>
</form>
<div class="muted" style="margin-top:.5rem;">
Token: <code data-console-token></code>
</div>
<?php if ($terminalUrl): ?>
<div class="console-launch"
data-url="<?= e($terminalUrl) ?>"
data-host="<?= e($terminalHostLabel ?: 'Konsole') ?>"
data-token="<?= e($terminalToken ?: '') ?>"></div>
<?php endif; ?>
</div>
<div class="card form-card" style="background:var(--panel-2);">
<strong>Konsole Tabs</strong>
<div class="console-tabs" data-console-tabs>
<div class="console-tab-bar" data-console-tab-bar></div>
<div class="console-tab-panels" data-console-tab-panels></div>
</div>
<p class="muted" style="margin-top:.5rem;">Mehrere Konsolen bleiben hier parallel offen.</p>
</div>
<div class="card form-card" style="background:var(--panel-2);">
<div style="display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap;">
<strong>Queue</strong>
<div class="muted" style="display:flex; align-items:center; gap:10px;">
<span>Update in <span data-queue-countdown>10</span>s</span>
<button class="icon-button" type="button" data-queue-refresh title="Jetzt aktualisieren">↻</button>
</div>
</div>
<div style="margin-top:.75rem; overflow:auto;">
<table class="table" style="min-width:560px;">
<thead>
<tr>
<th>ID</th>
<th>Status</th>
<th>Host</th>
<th>Command</th>
<th>Von</th>
<th>Output</th>
<th>Timeout</th>
</tr>
</thead>
<tbody data-queue-body>
<?= $renderRuns($runs) ?>
</tbody>
</table>
</div>
</div>
</div>
</div>