Remove orphaned API key code — correct key name format, collapse duplicate buttons

This commit is contained in:
Gmer4Lfe
2026-06-06 18:17:30 -04:00
parent dab8d62e95
commit a24f9db2ff
2 changed files with 24 additions and 48 deletions
+4 -9
View File
@@ -96,15 +96,14 @@ if ($action === 'detect' && $_SERVER['REQUEST_METHOD'] === 'POST') {
}
// ── Unraid API key status ─────────────────────────────────────────────────────
// Keys live in host*.conf (private, not master.conf):
// host1.conf: HOST1_UNRAID_API_KEY (own) + HOST2_UNRAID_API_KEY (HOST1's access to HOST2)
// host2.conf: HOST2_UNRAID_API_KEY (own) + HOST1_UNRAID_API_KEY (HOST2's access to HOST1)
if ($action === 'api_status') {
require_once dirname(__DIR__) . '/include/unraid_api.php';
$localStatus = vv_api_get_status();
$vars = vv_conf_vars();
$myHost = vv_detect_host();
$myId = strtoupper($myHost);
$hn = trim((string)shell_exec("hostname -s 2>/dev/null | sed 's/^[Uu][Nn][Rr][Aa][Ii][Dd]-//'")) ?: 'Varaverk';
$localKeyName = 'Varaverk ' . $hn;
$hosts = [];
foreach ($vars as $k => $v) {
@@ -113,14 +112,11 @@ if ($action === 'api_status') {
$keyVar = $id . '_UNRAID_API_KEY';
$key = $vars[$keyVar] ?? '';
$isLocal = ($id === $myId);
// For local: key is Varaverk_HOST1 registered on own machine
// For remote: key is Varaverk_HOST1 registered on HOST2's machine (stored in host1.conf)
$hosts[] = [
'host_id' => $id,
'hostname' => $v,
'is_local' => $isLocal,
'key_var' => $keyVar,
'key_name' => 'Varaverk_' . ($isLocal ? $myId : $myId), // Varaverk_HOST1 on that registry
'key_present' => !empty($key),
'key_preview' => $key ? substr($key, 0, 8) . '...' . substr($key, -4) : null,
'api_ok' => $isLocal
@@ -133,6 +129,7 @@ if ($action === 'api_status') {
echo json_encode([
'ok' => true,
'my_id' => $myId,
'key_name' => $localKeyName,
'hosts' => $hosts,
'fallbacks' => $localStatus['fallbacks'],
]);
@@ -145,11 +142,9 @@ if ($action === 'setup_apikeys' && $_SERVER['REQUEST_METHOD'] === 'POST') {
if (!file_exists($script)) {
echo json_encode(['ok' => false, 'error' => 'unraid_api_key_renew.sh not found']); exit;
}
$allHosts = ($_POST['all_hosts'] ?? '0') === '1';
$flags = $allHosts ? ' --all-hosts' : '';
set_time_limit(60);
$output = []; $exit = 0;
exec('bash ' . escapeshellarg($script) . $flags . ' 2>&1', $output, $exit);
exec('bash ' . escapeshellarg($script) . ' 2>&1', $output, $exit);
echo json_encode(['ok' => $exit === 0, 'exit' => $exit, 'output' => implode("\n", $output)]);
exit;
}
+20 -39
View File
@@ -13,10 +13,6 @@ $_enableLogging = ($_vars['ENABLE_LOGGING'] ?? 'false') === 'true';
$_discordKey = $_myId . '_DISCORD_WEBHOOK';
$_discordHook = $_vars[$_discordKey] ?? '';
// API key
$_apiKeyVar = $_myId . '_UNRAID_API_KEY';
$_apiKey = $_vars[$_apiKeyVar] ?? '';
$_apiPreview = $_apiKey ? substr($_apiKey, 0, 8) . '...' . substr($_apiKey, -4) : '';
?>
<style>
.vv-set-card { background:#161616;border:1px solid #2a2a2a;border-radius:6px;padding:14px 16px;margin-bottom:14px; }
@@ -152,18 +148,15 @@ $_apiPreview = $_apiKey ? substr($_apiKey, 0, 8) . '...' . substr($_apiKey, -4)
<div id="vv-api-hosts" style="margin-bottom:12px;"></div>
<div style="font-size:11px;color:#444;margin-bottom:12px;line-height:1.6;">
Each host registers
<code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">Varaverk_HOST1</code>
in <em>every</em> machine's local Unraid registry — own machine plus all partners.
The key values are stored in the private <code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">host*.conf</code>
Each host registers its key in the local Unraid registry and pushes it to all partners over SSH.
Key values are stored in the private <code style="font-size:10px;color:#4a7a4a;background:#080808;padding:1px 4px;border-radius:2px;">host*.conf</code>
so each host can query every other host's GraphQL API directly — real-time monitoring, no SSH.
During offboard, partner keys are removed from each registry automatically.
</div>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
<button class="vv-set-btn" onclick="vvApiCheck()" id="vv-api-check-btn">↻ Check status</button>
<button class="vv-set-btn" onclick="vvApiRenew(false)" id="vv-api-local-btn">Renew local</button>
<button class="vv-set-btn primary" onclick="vvApiRenew(true)" id="vv-api-all-btn">Setup all host keys</button>
<button class="vv-set-btn primary" onclick="vvApiRenew()" id="vv-api-renew-btn">Renew</button>
<span id="vv-api-fb" style="font-size:11px;color:#444;"></span>
</div>
<pre class="vv-set-out" id="vv-api-out"></pre>
@@ -345,7 +338,7 @@ function vvApiCheck() {
.then(d => {
btn.disabled = false; btn.textContent = '↻ Check status';
if (!d.ok) return;
const allOk = d.hosts.every(h => h.api_ok);
const allOk = d.hosts.every(h => h.api_ok);
const anyMissing = d.hosts.some(h => !h.key_present);
badge.textContent = allOk ? 'all ok' : anyMissing ? 'keys missing' : 'partial';
badge.className = 'vv-set-badge ' + (allOk ? 'internal' : 'flash');
@@ -356,16 +349,12 @@ function vvApiCheck() {
: `<span style="color:#2a2a3a;font-size:9px;margin-left:4px;">${d.my_id}'s key on ${h.host_id}</span>`;
const keyBit = h.key_present
? `<code style="font-size:9px;color:#4a6a4a;">${h.key_preview}</code>`
: `<span style="color:#8b2a2a;font-size:10px;">not set — click Setup</span>`;
const dot = h.api_ok ? '#4caf50' : (h.key_present ? '#ffb74d' : '#444');
// Key name in that machine's registry: always Varaverk_<MY_ID>
const keyName = `Varaverk_${d.my_id}`;
const registry = h.is_local ? h.host_id : h.host_id;
: `<span style="color:#8b2a2a;font-size:10px;">not set — click Renew</span>`;
const dot = h.api_ok ? '#4caf50' : (h.key_present ? '#ffb74d' : '#444');
return `<div style="display:flex;align-items:center;gap:8px;padding:4px 0;border-bottom:1px solid #111;">
<span style="width:6px;height:6px;border-radius:50%;background:${dot};flex-shrink:0;display:inline-block;"></span>
<span style="font-size:11px;color:#666;flex-shrink:0;min-width:50px;">${h.host_id}</span>
<code style="font-size:9px;color:#2a3a2a;flex-shrink:0;">${keyName}</code>
<span style="font-size:9px;color:#2a2a2a;flex-shrink:0;">on ${registry}</span>
<code style="font-size:9px;color:#2a3a2a;flex-shrink:0;">${h.is_local ? d.key_name : h.key_var}</code>
${tag}
<span style="flex:1;text-align:right;">${keyBit}</span>
</div>`;
@@ -374,36 +363,28 @@ function vvApiCheck() {
.catch(() => { btn.disabled = false; btn.textContent = '↻ Check status'; });
}
function vvApiRenew(allHosts) {
const localBtn = document.getElementById('vv-api-local-btn');
const allBtn = document.getElementById('vv-api-all-btn');
const out = document.getElementById('vv-api-out');
const fb = document.getElementById('vv-api-fb');
const badge = document.getElementById('vv-api-badge');
const activeBtn = allHosts ? allBtn : localBtn;
[localBtn, allBtn].forEach(b => { if(b) b.disabled = true; });
activeBtn.textContent = allHosts ? 'Setting up…' : 'Renewing…';
function vvApiRenew() {
const btn = document.getElementById('vv-api-renew-btn');
const out = document.getElementById('vv-api-out');
const fb = document.getElementById('vv-api-fb');
const badge = document.getElementById('vv-api-badge');
btn.disabled = true; btn.textContent = 'Renewing…';
fb.textContent = ''; out.textContent = ''; out.style.display = '';
const fd = new FormData();
fd.append('action', 'setup_apikeys');
fd.append('all_hosts', allHosts ? '1' : '0');
fd.append('action', 'setup_apikeys');
fetch('/plugins/varaverk/api/storage.php', { method: 'POST', body: fd })
.then(r => r.json())
.then(d => {
localBtn.disabled = false; localBtn.textContent = 'Renew local';
allBtn.disabled = false; allBtn.textContent = 'Setup all host keys';
out.textContent = d.output || '(no output)';
out.scrollTop = out.scrollHeight;
fb.style.color = d.ok ? '#4caf50' : '#ef5350';
fb.textContent = d.ok ? '✓ Done' : 'Failed — see output';
btn.disabled = false; btn.textContent = 'Renew';
out.textContent = d.output || '(no output)';
out.scrollTop = out.scrollHeight;
fb.style.color = d.ok ? '#4caf50' : '#ef5350';
fb.textContent = d.ok ? '✓ Done' : 'Failed — see output';
if (d.ok) { badge.textContent = ''; vvApiCheck(); }
})
.catch(() => {
localBtn.disabled = false; localBtn.textContent = 'Renew local';
allBtn.disabled = false; allBtn.textContent = 'Setup all host keys';
btn.disabled = false; btn.textContent = 'Renew';
fb.style.color = '#ef5350'; fb.textContent = 'Request failed';
});
}