<?php
// Triggers a synchronous re-run of pve-cluster-status.py as the `automation` user, then
// returns to the dashboard. php-fpm runs this as www-data, which typically has no SSH key
// of its own and can't reach the automation user's home directory -- so we hop to
// automation via a narrowly scoped NOPASSWD sudoers rule instead of loosening home-directory
// permissions or exposing the SSH key to the web server user. Add this to /etc/sudoers.d/:
//
//   www-data ALL=(automation) NOPASSWD: /usr/bin/python3 /home/automation/scripts/pve-cluster-status.py
//
// No user input is used in the command, so there is no command-injection surface.
$script = "/home/automation/scripts/pve-cluster-status.py";
$cmd = ["/usr/bin/sudo", "-u", "automation", "/usr/bin/python3", $script];

$descriptorspec = [1 => ["pipe", "w"], 2 => ["pipe", "w"]];
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
    fclose($pipes[1]);
    fclose($pipes[2]);
    proc_close($process);
}

header("Location: /");
exit;
