Initiated update-mechanic, minor fixes

This commit is contained in:
2025-06-20 18:06:57 +02:00
parent caa4142aa7
commit 2ac79b66db
6 changed files with 978 additions and 868 deletions

8
check_update.php Normal file
View File

@ -0,0 +1,8 @@
<?php
$repoPath = '/usr/thos';
$updateCount = trim(shell_exec("git -C $repoPath fetch origin && git -C $repoPath rev-list HEAD...origin/main --count"));
echo json_encode([
'updates' => (int)$updateCount,
'upToDate' => $updateCount === '0',
]);

1734
index.php

File diff suppressed because it is too large Load Diff

14
run_update.php Normal file
View File

@ -0,0 +1,14 @@
<?php
$mode = $_GET['mode'] ?? 'git'; // Future-proof!
if ($mode === 'git') {
$output = shell_exec("git -C /usr/thos pull 2>&1");
echo "✅ Git update applied:\n$output";
} elseif ($mode === 'curl') {
// Placeholder for future upgrade logic
$output = shell_exec("curl -s https://your.url/thos-update.sh | sudo bash 2>&1");
echo "🔧 Installer run:\n$output";
} else {
http_response_code(400);
echo "❌ Unknown update mode";
}

View File

@ -1,6 +1,5 @@
<?php <?php
require_once('state.php');
$input = json_decode(file_get_contents('php://input'), true); $input = json_decode(file_get_contents('php://input'), true);
if ($input) { if ($input) thos_save_state($input);
file_put_contents('/home/surillya/.thos_state.json', json_encode($input));
}
?> ?>

View File

@ -85,6 +85,19 @@
<option value="never">Never</option> <option value="never">Never</option>
</select> </select>
</div> </div>
<div class="mt-6">
<label class="block text-sm text-gray-300 mb-2">System Update</label>
<button id="update-btn" type="button" onclick="checkOrInstallUpdate()"
class="w-full bg-gray-800 text-white border border-gray-700 hover:bg-gray-700 font-medium px-4 py-2 rounded-lg transition flex items-center justify-between">
<span id="update-button-label">Check for Updates</span>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-indigo-400" fill="none"
viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M5 13l4 4L19 7" />
</svg>
</button>
<p id="update-status" class="text-xs text-gray-400 mt-2 whitespace-pre-wrap"></p>
</div>
<!-- Will eventually actually add this, but requires stuff in .xinitrc and really don't want to do it rn --> <!-- Will eventually actually add this, but requires stuff in .xinitrc and really don't want to do it rn -->
<!-- <div> <!-- <div>
<label for="powerMode" class="block text-sm text-gray-300 mb-2">Power Mode</label> <label for="powerMode" class="block text-sm text-gray-300 mb-2">Power Mode</label>
@ -176,6 +189,66 @@
location.reload(); location.reload();
} }
} }
let updatePending = false;
let pendingUpdateCount = 0;
function checkOrInstallUpdate() {
if (updatePending) {
installUpdate(); // Confirmed
return;
}
const label = document.getElementById("update-button-label");
const status = document.getElementById("update-status");
const button = document.getElementById("update-btn");
label.textContent = "Checking...";
status.textContent = "";
fetch("check_update.php")
.then(res => res.json())
.then(data => {
if (data.upToDate) {
label.textContent = "System is Up To Date";
status.textContent = "✅ You're running the latest version of THOS.";
} else {
updatePending = true;
pendingUpdateCount = data.updates;
label.textContent = `⬇️ ${data.updates} update(s) available. Click to update.`;
button.classList.replace("bg-gray-800", "bg-yellow-800");
button.classList.replace("hover:bg-gray-700", "hover:bg-yellow-700");
status.textContent = "Updates detected. Click again to confirm and install.";
}
})
.catch(err => {
label.textContent = "Check for Updates";
status.textContent = "❌ Update check failed.";
});
}
function installUpdate() {
const label = document.getElementById("update-button-label");
const status = document.getElementById("update-status");
const button = document.getElementById("update-btn");
label.textContent = "Installing...";
status.textContent = "⏳ Applying updates, please wait...";
fetch("run_update.php")
.then(res => res.text())
.then(output => {
label.textContent = "✅ Update Applied";
status.textContent = "THOS is now up to date!\n\n" + output;
button.classList.replace("bg-yellow-800", "bg-green-800");
button.classList.replace("hover:bg-yellow-700", "hover:bg-green-700");
updatePending = false;
})
.catch(() => {
label.textContent = "❌ Update Failed";
status.textContent = "Please try again or check your network.";
});
}
</script> </script>
</body> </body>

12
state.php Normal file
View File

@ -0,0 +1,12 @@
<?php
define('THOS_STATE_FILE', '/home/surillya/.thos_state.json');
function thos_load_state() {
if (!file_exists(THOS_STATE_FILE)) return null;
return json_decode(file_get_contents(THOS_STATE_FILE), true);
}
function thos_save_state($data) {
file_put_contents(THOS_STATE_FILE, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
}
?>