From 535dde06d8fc15a6e7c84fa76fc014a6ebbe39e3 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Mon, 17 Apr 2023 01:46:45 +0700 Subject: [PATCH] add vestasync.js --- files/vestasync.js | 105 +++++++++++++++++++++++++++++++++++++++++++++ vestasync.py | 4 ++ 2 files changed, 109 insertions(+) create mode 100644 files/vestasync.js diff --git a/files/vestasync.js b/files/vestasync.js new file mode 100644 index 0000000..cb8fe9f --- /dev/null +++ b/files/vestasync.js @@ -0,0 +1,105 @@ +defineVirtualDevice("vestasync", { + title: "Vestasync", + cells: { + "Last push": { + type: "text", + value: "Update...", + title: "Last push" + }, + "Current commit": { + type: "text", + value: "Update...", + title: "Last commit hash" + }, + autopush: { + type: "switch", + value: true, + title: "Auto push on files changed" + }, + hostname: { + type: "text", + value: "", + title: "Hostname" + } + } +}); + + +function _update_vestasync() { + runShellCommand("git -C /mnt/data/etc log -1 --format=%ct", { + captureOutput: true, + exitCallback: function (exitCode, capturedOutput) { + if (exitCode === 0) { + var last_push_time = parseInt(capturedOutput); + var now = new Date(); + var diff_in_seconds = Math.floor((now.getTime() / 1000) - last_push_time); + var diff_in_minutes = Math.floor(diff_in_seconds / 60); + var diff_in_hours = Math.floor(diff_in_minutes / 60); + var diff_in_days = Math.floor(diff_in_hours / 24); + var human_readable_time = ""; + if (diff_in_days > 0) { + human_readable_time = diff_in_days + " days ago"; + } else if (diff_in_hours > 0) { + human_readable_time = diff_in_hours + " hours ago"; + } else if (diff_in_minutes > 0) { + human_readable_time = diff_in_minutes + " minutes ago"; + } else { + human_readable_time = "just now"; + } + dev.vestasync["Last push"] = human_readable_time; + } + } + }); + runShellCommand("git -C /mnt/data/etc log -1 --format=%H", { + captureOutput: true, + exitCallback: function (exitCode, capturedOutput) { + if (exitCode === 0) { + var shortenedCommit = capturedOutput.trim().substring(0, 10); + dev.vestasync["Current commit"] = shortenedCommit; + } + } + }); + + runShellCommand("systemctl is-enabled pushgit_inotify.service", { + captureOutput: true, + exitCallback: function (exitCode, capturedOutput) { + if (exitCode === 0) { + var isEnabled = capturedOutput.trim() === "enabled"; + dev.vestasync.autopush = isEnabled; + } else { + log("Error checking autopush status:" + capturedOutput + exitCode); + } + } + }); + + runShellCommand("hostname", { + captureOutput: true, + exitCallback: function (exitCode, capturedOutput) { + if (exitCode === 0) { + var hostname = capturedOutput.trim(); + dev.vestasync.hostname = hostname; + } else { + console.error("Error checking hostname:", capturedOutput.trim()); + } + } + }); + +}; + + +defineRule("_vestasync_autopush", { + whenChanged: "vestasync/autopush", + then: function (newValue, devName, cellName) { + if (dev.vestasync.autopush) { + runShellCommand("systemctl start pushgit_inotify.service"); + } else { + runShellCommand("systemctl stop pushgit_inotify.service"); + } + } +}); + + +_update_vestasync(); +setInterval(_update_vestasync, 10000); + + diff --git a/vestasync.py b/vestasync.py index 4792913..e8096a1 100755 --- a/vestasync.py +++ b/vestasync.py @@ -97,6 +97,9 @@ def init_repo(c): c.run(f'cd /mnt/data/etc/ && git remote add origin {args.vestasync_gitea_protocol}://{gitea_user}:{args.gitea_token}@{args.vestasync_gitea_host}:{args.vestasync_gitea_port}/{gitea_user}/{hostname}.git') +def copy_wb_rule(c): + c.put("./files/vestasync.js", "/mnt/data/etc/wb-rules/vestasync.js") + def create_automac_systemd(c): apply_macs_script_path = "/usr/local/bin/apply_macs.sh" c.put("./files/apply_macs.sh", apply_macs_script_path) @@ -202,6 +205,7 @@ def device_install(): ppush_the_repo(c) save_mac_in_cfg(c) save_hostname(c) + copy_wb_rule(c) ppush_the_repo(c) create_automac_systemd(c) create_autogit_systemd(c)