diff --git a/main.py b/main.py index b7c6083..c02f488 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,31 @@ +import json import os +import pathlib +import subprocess class Plugin: async def whoami(self, *args): return os.getuid() - - # A normal method. It can be called from JavaScript using call_plugin_function("method_1", argument1, argument2) - async def method_1(self, *args): - pass - # A normal method. It can be called from JavaScript using call_plugin_function("method_2", argument1, argument2) - async def method_2(self, *args): - pass + def _systemd_status(self, service = "tailscaled.service"): + result = subprocess.run(["/bin/sh", "-c", "systemctl status tailscaled.service | grep active"]) + return result.stdout.parse().split(": ")[1] + + async def install_state(self, *args): + sysext = subprocess.run(["/usr/bin/systemd-sysext", "list", "--json=shot"]) + sysexts = json.load(sysext.stdout) + for hier in sysexts: + if hier.hierarchy != "/usr": + continue + + if hier.extensions == "none": + if pathlib.Path("/etc/extensions/tailscale").exists(): + return "installed" + + if "tailscale" in hier.extensions: + return self._systemd_status() + + return "impossible state" # Asyncio-compatible long-running code, executed in a task when the plugin is loaded async def _main(self): diff --git a/main_view.html b/main_view.html index 958c9e6..09b56f2 100644 --- a/main_view.html +++ b/main_view.html @@ -8,17 +8,26 @@ async function getUID() { return call_plugin_method("whoami", {}); } + + async function getInstallState() { + return call_plugin_method("install_state", {}); + } async function onReady() { let heading = document.getElementById("heading"); let id = await getUID(); heading.innerText = `Hello ${id}`; + + let status = document.getElementById("status"); + let install_status = await getInstallState(); + + status.inner_text = install_status; }