adad
This commit is contained in:
@@ -13,6 +13,9 @@ $terminalToken = null;
|
||||
$settings = modules()->settings('pi_control');
|
||||
$ttydUrl = trim((string)($settings['ttyd_url'] ?? '/ttyd'));
|
||||
$defaultProvider = 'ttyd';
|
||||
$defaultTimeout = (int)($settings['exec_default_timeout'] ?? 300);
|
||||
$defaultTimeout = $defaultTimeout > 0 ? $defaultTimeout : 300;
|
||||
$queueName = (string)($settings['redis']['queue'] ?? 'pi_control:queue');
|
||||
$tokenTtl = (int)($settings['terminal_token_ttl'] ?? 10);
|
||||
$tokenTtl = $tokenTtl > 0 ? $tokenTtl : 10;
|
||||
|
||||
@@ -56,6 +59,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$hostId = (int)($_POST['host_id'] ?? 0);
|
||||
$commandId = (int)($_POST['command_id'] ?? 0);
|
||||
$rawCommand = trim((string)($_POST['command_text'] ?? ''));
|
||||
$timeoutSec = $defaultTimeout;
|
||||
|
||||
if ($hostId <= 0) {
|
||||
$error = 'Bitte einen Host wählen.';
|
||||
@@ -70,6 +74,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$error = 'Dieser Befehl ist nur für Admins.';
|
||||
} else {
|
||||
$selectedCommand = (string)$c['command'];
|
||||
if (!empty($c['timeout_sec'])) {
|
||||
$timeoutSec = (int)$c['timeout_sec'];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -78,24 +85,57 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
if (!$error) {
|
||||
$commandText = $selectedCommand !== '' ? $selectedCommand : $rawCommand;
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO ' . $table('runs') . ' (host_id, command_id, command_text, status, created_by) VALUES (:host_id, :command_id, :command_text, :status, :created_by)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'host_id' => $hostId,
|
||||
'command_id' => $commandId > 0 ? $commandId : null,
|
||||
'command_text' => $commandText,
|
||||
'status' => 'pending',
|
||||
'created_by' => auth_display_name() ?: null,
|
||||
]);
|
||||
|
||||
$notice = 'Befehl wurde erfasst. (Execution-Backend folgt)';
|
||||
$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.';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$runs = $pdo->query('SELECT * FROM ' . $table('runs') . ' ORDER BY id DESC LIMIT 20')->fetchAll(PDO::FETCH_ASSOC);
|
||||
$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>
|
||||
@@ -189,7 +229,7 @@ $runs = $pdo->query('SELECT * FROM ' . $table('runs') . ' ORDER BY id DESC LIMIT
|
||||
</label>
|
||||
<button class="cta-button" type="submit">Befehl senden</button>
|
||||
</form>
|
||||
<p class="muted" style="margin-top:.5rem;">Hinweis: Execution-Backend wird im nächsten Schritt ergänzt.</p>
|
||||
<p class="muted" style="margin-top:.5rem;">Befehle werden über die Queue ausgeführt.</p>
|
||||
</div>
|
||||
|
||||
<div class="card form-card" style="background:var(--panel-2);">
|
||||
@@ -200,20 +240,34 @@ $runs = $pdo->query('SELECT * FROM ' . $table('runs') . ' ORDER BY id DESC LIMIT
|
||||
<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>
|
||||
<?php if (!$runs): ?>
|
||||
<tr><td colspan="4" class="muted">Noch keine Runs.</td></tr>
|
||||
<tr><td colspan="7" class="muted">Noch keine Runs.</td></tr>
|
||||
<?php endif; ?>
|
||||
<?php foreach ($runs as $r): ?>
|
||||
<?php
|
||||
$out = (string)($r['output'] ?? '');
|
||||
$err = (string)($r['error'] ?? '');
|
||||
$snippet = $out !== '' ? $out : $err;
|
||||
if (strlen($snippet) > 140) {
|
||||
$snippet = substr($snippet, 0, 140) . '…';
|
||||
}
|
||||
?>
|
||||
<tr>
|
||||
<td><?= e((string)$r['id']) ?></td>
|
||||
<td><?= e($r['status'] ?? '') ?></td>
|
||||
<td><?= e($r['host_name'] ?? $r['host_addr'] ?? '') ?></td>
|
||||
<td class="muted" style="max-width:360px;"><code><?= e($r['command_text'] ?? '') ?></code></td>
|
||||
<td><?= e($r['created_by'] ?? '') ?></td>
|
||||
<td class="muted" style="max-width:240px;"><code><?= e($snippet) ?></code></td>
|
||||
<td><?= !empty($r['timeout_sec']) ? e((string)$r['timeout_sec']) . 's' : 'default' ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
|
||||
Reference in New Issue
Block a user