From 1de35c673c4f85ec05476cce1541c40f737bc19b Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Mon, 9 May 2022 17:39:23 +0000 Subject: [PATCH] check tailscale install status Signed-off-by: Xe Iaso --- main.py | 29 ++++++++++++++++++++++------- main_view.html | 11 ++++++++++- plugin.json | 2 +- 3 files changed, 33 insertions(+), 9 deletions(-) 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; }

Hello World

-
+

Status: loading

diff --git a/plugin.json b/plugin.json index b304a3d..3a81c04 100644 --- a/plugin.json +++ b/plugin.json @@ -3,5 +3,5 @@ "author": "Xe Iaso", "main_view_html": "main_view.html", "tile_view_html": "", - "flags": ["hot_reload", "root"] + "flags": ["hot_reload", "root", "debug"] }