vestasync/files/vestasync.js

148 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-04-16 21:46:45 +03:00
defineVirtualDevice("vestasync", {
title: "Vestasync",
cells: {
2023-04-17 16:55:56 +03:00
last_push: {
2023-04-16 21:46:45 +03:00
type: "text",
value: "Update...",
title: "Last push"
},
2023-04-17 16:55:56 +03:00
last_commit: {
2023-04-16 21:46:45 +03:00
type: "text",
value: "Update...",
title: "Last commit hash"
},
2023-04-17 14:20:23 +03:00
autopush_inotify: {
2023-04-16 21:46:45 +03:00
type: "switch",
2023-04-17 14:20:23 +03:00
value: false,
2023-04-16 21:46:45 +03:00
title: "Auto push on files changed"
},
2023-04-17 14:20:23 +03:00
autopush_timer: {
type: "switch",
value: false,
title: "Auto push every day"
},
push_now: {
2023-04-17 21:46:40 +03:00
type: "pushbutton",
2023-04-17 14:20:23 +03:00
value: false,
2023-04-17 21:46:40 +03:00
title: "Commit and push now"
2023-04-17 14:20:23 +03:00
},
2023-04-16 21:46:45 +03:00
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";
}
2023-04-17 16:55:56 +03:00
dev.vestasync.last_push = human_readable_time;
2023-04-16 21:46:45 +03:00
}
}
});
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);
2023-04-17 16:55:56 +03:00
dev.vestasync.last_commit = shortenedCommit;
2023-04-16 21:46:45 +03:00
}
}
});
2023-04-17 11:22:39 +03:00
runShellCommand("systemctl is-active pushgit_inotify.service", {
2023-04-16 21:46:45 +03:00
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
2023-04-17 11:22:39 +03:00
var isEnabled = capturedOutput.trim() === "active";
2023-04-17 21:46:29 +03:00
getControl("vestasync/autopush_inotify").setValue({ value: isEnabled, notify: false })
2023-04-17 14:20:23 +03:00
}
});
runShellCommand("systemctl is-active pushgit.timer", {
captureOutput: true,
exitCallback: function (exitCode, capturedOutput) {
var isEnabled = capturedOutput.trim() === "active";
2023-04-17 21:46:29 +03:00
getControl("vestasync/autopush_inotify").setValue({ value: isEnabled, notify: false })
2023-04-16 21:46:45 +03:00
}
});
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());
}
}
});
};
2023-04-17 14:20:23 +03:00
defineRule("_vestasync_autopush_timer", {
whenChanged: "vestasync/autopush_timer",
then: function (newValue, devName, cellName) {
if (dev.vestasync.autopush_timer) {
2023-04-17 21:47:04 +03:00
runShellCommand("systemctl stop pushgit.timer ; systemctl disable pushgit.timer ; systemctl daemon-reload ; systemctl enable pushgit.timer ; systemctl start pushgit.timer");
2023-04-17 14:20:23 +03:00
_update_vestasync();
} else {
2023-04-17 21:47:04 +03:00
runShellCommand("systemctl stop pushgit.timer ; systemctl disable pushgit.timer");
2023-04-17 14:20:23 +03:00
_update_vestasync();
}
}
});
2023-04-16 21:46:45 +03:00
2023-04-17 14:20:23 +03:00
defineRule("_vestasync_autopush_inotify", {
whenChanged: "vestasync/autopush_inotify",
2023-04-16 21:46:45 +03:00
then: function (newValue, devName, cellName) {
2023-04-17 14:20:23 +03:00
if (dev.vestasync.autopush_inotify) {
2023-04-17 21:47:04 +03:00
runShellCommand("systemctl stop pushgit_inotify.service ; systemctl disable pushgit_inotify.service ; systemctl daemon-reload ; systemctl enable pushgit_inotify.service ; systemctl start pushgit_inotify.service");
2023-04-17 14:20:23 +03:00
_update_vestasync();
2023-04-16 21:46:45 +03:00
} else {
2023-04-17 21:47:04 +03:00
runShellCommand("systemctl stop pushgit_inotify.service ; systemctl disable pushgit_inotify.service");
2023-04-17 14:20:23 +03:00
_update_vestasync();
}
}
});
2023-04-17 21:47:04 +03:00
2023-04-17 14:20:23 +03:00
defineRule("_vestasync_push", {
whenChanged: "vestasync/push_now",
then: function (newValue, devName, cellName) {
if (dev.vestasync.push_now) {
2023-04-17 21:47:04 +03:00
runShellCommand("systemctl start pushgit.service");
2023-04-17 14:20:23 +03:00
dev.vestasync.push_now = false;
_update_vestasync();
2023-04-16 21:46:45 +03:00
}
}
});
_update_vestasync();
2023-04-17 21:47:04 +03:00
setInterval(_update_vestasync, 60000);
2023-04-16 21:46:45 +03:00