Files
Varaverk/Plugin/unraid/api/setup.php
T
Gmer4Lfe 1e9681a71e Add first-run wizard, host.conf template, fix GitHub URL
Plugin/varaverk.plg: URL updated to github.com/FailedProxy/Varaverk

Configurations/host.conf.template:
  Full host conf structure with HOSTN/hostn placeholders.
  All personal values blank, all sections documented.
  Covers identity, rsync, docker, fallback, media, monitors,
  transcodes, arrs, system watchdog, resource manager.

Varaverk.page: checks if HOST1 is blank before rendering tabs.
  If blank → shows setup wizard, returns early (tabs never render).

pages/setup.php: first-run wizard UI.
  Auto-populates hostname from hostname -s.
  Role selection: primary (HOST1) or partner (HOST2+).
  Partner slot selector for HOST3+.

api/setup.php: handles wizard form POST.
  Writes HOST1/HOST2 (and HOST3+) into master.conf preserving all
  other content. Creates host*.conf from template with HOSTN/hostn
  replaced and SSH key path pre-filled from hostname convention.
  Never overwrites an existing host*.conf.
2026-05-30 15:22:46 -04:00

105 lines
3.8 KiB
PHP

<?php
header('Content-Type: application/json');
require_once dirname(__DIR__) . '/include/config.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['ok' => false, 'error' => 'Method not allowed']);
exit;
}
$host1 = trim($_POST['host1'] ?? '');
$host2 = trim($_POST['host2'] ?? '');
$mySlot = trim($_POST['my_slot'] ?? 'host1'); // 'host1', 'host2', 'host3', ...
$myHostname = trim($_POST['my_hostname'] ?? '');
if (empty($host1)) {
echo json_encode(['ok' => false, 'error' => 'HOST1 hostname is required']);
exit;
}
// Validate slot — must be host\d+
if (!preg_match('/^host\d+$/', $mySlot)) {
echo json_encode(['ok' => false, 'error' => 'Invalid slot']);
exit;
}
// ── Write HOST1 / HOST2 into master.conf ─────────────────────────────────────────────────────
$master = vv_read_conf_raw('master.conf');
if ($master === '') {
echo json_encode(['ok' => false, 'error' => 'master.conf not found — check SCRIPTS_DIR in varaverk.cfg']);
exit;
}
// Replace HOST1 and HOST2 lines (preserving indentation)
$master = preg_replace(
'/^(\s*HOST1\s*=\s*).*$/m',
'${1}"' . addslashes($host1) . '"',
$master
);
$master = preg_replace(
'/^(\s*HOST2\s*=\s*).*$/m',
'${1}"' . addslashes($host2) . '"',
$master
);
// For partner slots beyond HOST2 — ensure the slot line exists in master.conf
$slotNum = (int) preg_replace('/\D/', '', $mySlot);
if ($slotNum > 2 && !empty($myHostname)) {
$hostKey = 'HOST' . $slotNum;
if (!preg_match('/^\s*' . $hostKey . '\s*=/m', $master)) {
// Append after HOST2 line
$master = preg_replace(
'/^(\s*HOST2\s*=.*$)/m',
'$1' . "\n {$hostKey}=\"" . addslashes($myHostname) . '"',
$master
);
} else {
$master = preg_replace(
'/^(\s*' . $hostKey . '\s*=\s*).*$/m',
'${1}"' . addslashes($myHostname) . '"',
$master
);
}
}
if (!vv_write_conf_raw('master.conf', $master)) {
echo json_encode(['ok' => false, 'error' => 'Failed to write master.conf']);
exit;
}
// ── Create host*.conf from template ──────────────────────────────────────────────────────────
$hostId = strtoupper($mySlot); // HOST1, HOST2, HOST3 ...
$hostIdLow = strtolower($mySlot); // host1, host2, host3 ...
$confFile = $hostIdLow . '.conf';
// Only create if it doesn't already exist — never overwrite an existing conf
if (!file_exists(CONF_DIR . '/' . $confFile)) {
$template = @file_get_contents(CONF_DIR . '/host.conf.template');
if ($template === false) {
$template = @file_get_contents(SCRIPTS_DIR . '/Configurations/host.conf.template');
}
if ($template !== false) {
// Derive SSH key path from hostname convention
// unRAID-MyServer → /root/.ssh/myserver_rsync_automation
$sshOwner = strtolower(preg_replace('/^unraid-/i', '', $myHostname));
$sshKeyPath = '/root/.ssh/' . $sshOwner . '_rsync_automation';
$conf = str_replace('HOSTN', $hostId, $template);
$conf = str_replace('hostn', $hostIdLow, $conf);
// Pre-fill the SSH key path
$conf = preg_replace(
'/^(\s*' . $hostId . '_SSH_KEY\s*=\s*)""/m',
'${1}"' . $sshKeyPath . '"',
$conf
);
if (!vv_write_conf_raw($confFile, $conf)) {
echo json_encode(['ok' => false, 'error' => "Failed to write $confFile"]);
exit;
}
}
// Template missing is non-fatal — user can create the conf manually
}
echo json_encode(['ok' => true, 'host_id' => $hostId, 'conf_file' => $confFile]);