cron und module
This commit is contained in:
@@ -32,6 +32,78 @@ if (isset($_GET['status_json'])) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if (isset($_GET['update_json'])) {
|
||||
require_admin();
|
||||
$id = (int)($_GET['id'] ?? 0);
|
||||
$stmt = $pdo->prepare('SELECT * FROM ' . $table('hosts') . ' WHERE id = :id LIMIT 1');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$host = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$host) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode(['ok' => false, 'error' => 'not_found']);
|
||||
exit;
|
||||
}
|
||||
$settings = modules()->settings('pi_control');
|
||||
$strictHostKey = !empty($settings['terminal_strict_hostkey']);
|
||||
|
||||
$updateCmd = "apt-get -s upgrade | grep '^Inst'";
|
||||
$upgradeCmd = 'current="$(. /etc/os-release && echo "$VERSION_CODENAME")"; latest="$(curl -fsSL https://deb.debian.org/debian/dists/stable/Release | awk -F\': \' \'/^Codename:/{print $2}\')"; echo "Installed: $current"; echo "Latest stable: $latest"; [ "$current" != "$latest" ] && echo "OS UPGRADE AVAILABLE" || echo "NO OS UPGRADE AVAILABLE"';
|
||||
|
||||
[$updExit, $updOut, $updErr] = runSshCommandCapture($host, $updateCmd, $strictHostKey, 20);
|
||||
$lines = array_values(array_filter(preg_split('/\r?\n/', (string)$updOut)));
|
||||
$updateCount = $updExit === 0 ? count($lines) : null;
|
||||
$updatePreview = $updateCount ? implode("\n", array_slice($lines, 0, 6)) : '';
|
||||
|
||||
[$upgExit, $upgOut, $upgErr] = runSshCommandCapture($host, $upgradeCmd, $strictHostKey, 25);
|
||||
$upgradeAvailable = null;
|
||||
if ($upgExit === 0) {
|
||||
$upgradeAvailable = str_contains($upgOut, 'OS UPGRADE AVAILABLE');
|
||||
}
|
||||
|
||||
$driver = (string)$pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
|
||||
$nowExpr = $driver === 'pgsql' ? 'NOW()' : "DATETIME('now')";
|
||||
$updCountVal = $updateCount;
|
||||
$updErrVal = $updExit === 0 ? null : trim($updErr ?: $updOut);
|
||||
$upgAvailVal = $upgradeAvailable;
|
||||
$upgErrVal = $upgExit === 0 ? null : trim($upgErr ?: $upgOut);
|
||||
$stmt = $pdo->prepare(
|
||||
'UPDATE ' . $table('hosts') . ' SET update_checked_at = ' . $nowExpr . ',
|
||||
update_count = :update_count,
|
||||
update_preview = :update_preview,
|
||||
update_error = :update_error,
|
||||
upgrade_available = :upgrade_available,
|
||||
upgrade_raw = :upgrade_raw,
|
||||
upgrade_error = :upgrade_error
|
||||
WHERE id = :id'
|
||||
);
|
||||
$stmt->execute([
|
||||
'update_count' => $updCountVal,
|
||||
'update_preview' => $updatePreview !== '' ? $updatePreview : null,
|
||||
'update_error' => $updErrVal,
|
||||
'upgrade_available' => $upgAvailVal === null ? null : ($upgAvailVal ? 1 : 0),
|
||||
'upgrade_raw' => $upgExit === 0 ? trim($upgOut) : null,
|
||||
'upgrade_error' => $upgErrVal,
|
||||
'id' => $id,
|
||||
]);
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'ok' => true,
|
||||
'updates' => [
|
||||
'count' => $updateCount,
|
||||
'preview' => $updatePreview,
|
||||
'error' => $updExit === 0 ? '' : trim($updErr ?: $updOut),
|
||||
],
|
||||
'os' => [
|
||||
'available' => $upgradeAvailable,
|
||||
'raw' => $upgExit === 0 ? trim($upgOut) : '',
|
||||
'error' => $upgExit === 0 ? '' : trim($upgErr ?: $upgOut),
|
||||
],
|
||||
'checked_at' => date('c'),
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
require_admin();
|
||||
$deleteId = (int)($_POST['delete_id'] ?? 0);
|
||||
@@ -138,6 +210,64 @@ function runSshCommand(string $cmd, int $timeoutSec): int
|
||||
}
|
||||
}
|
||||
|
||||
function runSshCommandCapture(array $host, string $command, bool $strictHostKey, int $timeoutSec): array
|
||||
{
|
||||
$hostAddr = (string)($host['host'] ?? '');
|
||||
$user = (string)($host['username'] ?? '');
|
||||
$port = (int)($host['port'] ?? 22);
|
||||
$authType = (string)($host['auth_type'] ?? 'key');
|
||||
$keyPath = (string)($host['key_path'] ?? '');
|
||||
$password = (string)($host['password'] ?? '');
|
||||
|
||||
$opts = $strictHostKey
|
||||
? '-o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=/root/.ssh/known_hosts'
|
||||
: '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';
|
||||
$opts .= ' -o ConnectTimeout=6 -o NumberOfPasswordPrompts=1';
|
||||
|
||||
$target = escapeshellarg($user . '@' . $hostAddr);
|
||||
$remote = '/bin/bash -lc ' . escapeshellarg($command);
|
||||
$cmd = 'ssh ' . $opts . ' -p ' . (int)$port . ' ';
|
||||
if ($authType === 'key' && $keyPath !== '') {
|
||||
$cmd .= '-i ' . escapeshellarg($keyPath) . ' -o BatchMode=yes ';
|
||||
} elseif ($authType === 'key') {
|
||||
$cmd .= '-o BatchMode=yes ';
|
||||
}
|
||||
$cmd .= $target . ' -- ' . $remote;
|
||||
if ($authType === 'pass' && $password !== '') {
|
||||
$cmd = 'sshpass -p ' . escapeshellarg($password) . ' ' . $cmd;
|
||||
}
|
||||
|
||||
$descriptors = [
|
||||
1 => ['pipe', 'w'],
|
||||
2 => ['pipe', 'w'],
|
||||
];
|
||||
$process = proc_open($cmd, $descriptors, $pipes);
|
||||
if (!is_resource($process)) {
|
||||
return [255, '', 'proc_open failed'];
|
||||
}
|
||||
stream_set_blocking($pipes[1], false);
|
||||
stream_set_blocking($pipes[2], false);
|
||||
$out = '';
|
||||
$err = '';
|
||||
$start = time();
|
||||
while (true) {
|
||||
$status = proc_get_status($process);
|
||||
$out .= stream_get_contents($pipes[1]);
|
||||
$err .= stream_get_contents($pipes[2]);
|
||||
if (!$status['running']) {
|
||||
$exit = (int)$status['exitcode'];
|
||||
proc_close($process);
|
||||
return [$exit, $out, $err];
|
||||
}
|
||||
if (time() - $start > $timeoutSec) {
|
||||
proc_terminate($process, 9);
|
||||
proc_close($process);
|
||||
return [124, $out, $err];
|
||||
}
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
|
||||
function hostAuthOk(array $host, bool $strictHostKey): bool
|
||||
{
|
||||
$hostAddr = (string)($host['host'] ?? '');
|
||||
@@ -179,7 +309,10 @@ function hostAuthOk(array $host, bool $strictHostKey): bool
|
||||
<div class="pill">Pi Control</div>
|
||||
<div style="display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap; margin-top:.75rem;">
|
||||
<h1 style="margin:0;">Hosts</h1>
|
||||
<button class="cta-button" type="button" data-host-new>+ Neuer Host</button>
|
||||
<div style="display:flex; gap:10px; flex-wrap:wrap;">
|
||||
<button class="nav-link" type="button" data-host-check-all>Alle Hosts prüfen</button>
|
||||
<button class="cta-button" type="button" data-host-new>+ Neuer Host</button>
|
||||
</div>
|
||||
</div>
|
||||
<p class="muted">Verwalte die Raspberry Pis, die du steuern möchtest.</p>
|
||||
|
||||
@@ -209,7 +342,13 @@ function hostAuthOk(array $host, bool $strictHostKey): bool
|
||||
data-username="<?= e((string)($h['username'] ?? '')) ?>"
|
||||
data-auth="<?= e((string)($h['auth_type'] ?? 'key')) ?>"
|
||||
data-key-path="<?= e((string)($h['key_path'] ?? '')) ?>"
|
||||
data-image-url="<?= e((string)($h['image_url'] ?? '')) ?>">
|
||||
data-image-url="<?= e((string)($h['image_url'] ?? '')) ?>"
|
||||
data-update-checked="<?= e((string)($h['update_checked_at'] ?? '')) ?>"
|
||||
data-update-count="<?= e((string)($h['update_count'] ?? '')) ?>"
|
||||
data-update-error="<?= e((string)($h['update_error'] ?? '')) ?>"
|
||||
data-upgrade-available="<?= e((string)($h['upgrade_available'] ?? '')) ?>"
|
||||
data-upgrade-raw="<?= e((string)($h['upgrade_raw'] ?? '')) ?>"
|
||||
data-upgrade-error="<?= e((string)($h['upgrade_error'] ?? '')) ?>">
|
||||
<div class="host-card-image"<?= !empty($h['image_url']) ? ' style="background-image:url(' . e((string)$h['image_url']) . ')"' : '' ?>>
|
||||
<div class="host-card-overlay"></div>
|
||||
</div>
|
||||
@@ -232,6 +371,14 @@ function hostAuthOk(array $host, bool $strictHostKey): bool
|
||||
</div>
|
||||
<div class="muted"><?= e((string)($h['host'] ?? '')) ?>:<?= e((string)($h['port'] ?? 22)) ?></div>
|
||||
<div class="muted"><?= e((string)($h['username'] ?? '')) ?> · <?= e((string)($h['auth_type'] ?? '')) ?></div>
|
||||
<div class="host-update-row">
|
||||
<span class="update-badge" data-update-badge>Updates: –</span>
|
||||
<span class="update-badge" data-upgrade-badge>OS: –</span>
|
||||
<span class="muted" data-update-time>Nie geprüft</span>
|
||||
</div>
|
||||
<div style="display:flex; gap:8px; flex-wrap:wrap;">
|
||||
<button class="nav-link" type="button" data-host-check>Updates prüfen</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
|
||||
Reference in New Issue
Block a user